从选择选项下拉列表中计算多个值

时间:2016-03-30 15:57:09

标签: javascript php jquery html pdo

我想问一下如何从下拉列表中的值计算多个选项。

我将在这里添加来自PDO函数的简单html代码和函数。我想要的是计算<div id="RightBox">中从<div id="LeftBox">移动到javascript按钮的值,然后在用$value = explode('|', $_GET['service'])爆炸后用计算计算它们的值当我爆炸这些值时,我想要从函数Typ()中取. $vysledek["Service_price"] .此值,并按此$calculation = (2 * $height * ($lenght + $width) + ($lenght+ $width)) * $value[1];模式计算所有这些值。我只为一个选定的(最后一个)工作但是我想让它适用于所有人,以便$calculation模式将用于每种类型的服务,然后所有的计算将被编号,然后到回声最终价格。我真的不知道怎么做也许你可以帮助我。谢谢。

HTML:

<div id="Calkulator">
  <form>
    <h2>Calculator</h2>
    <table cellspacing="5" cellpadding="5">
      <tbody>
        <tr>
          <td><label for="lenght">Lenght of room in m2: </label></td>
          <td><input type="number" class="text_field small" id="lenght_of_room" min="1" name="lenght"></td>
        </tr>
        <tr>
          <td><label for="width">Width of room in m2: </label></td>
          <td><input type="number" class="text_field small" id="width_of_room" min="1" name="width"></td>
        </tr>
        <tr>
          <td><label for="height">Height of room in m2: </label></td>
          <td><input type="number" class="text_field small" id="height_of_room" min="1" name="height"></td>
        </tr>
        <tr>
          <td><label for="typ">Service type: </label></td>
            <td>            
              <div id="LeftBox">
                <select id="leftValues" size="8" multiple>
                <?php
                  require_once 'funkce.php';
                  $database = new Database();
                  $database->Typ();  
               ?>
               </select>
             </div>
             <div id="Buttons">
               <input type="button" id="btnLeft" value="&lt;&lt;" />
               <input type="button" id="btnRight" value="&gt;&gt;" />
             </div>
             <div id="RightBox">
               <select id="rightValues" name="service" size="8" multiple>
               </select>
             </div>
           </td>
        </tr>
        <tr>
          <td colspan="2">
            <input type="submit" class="submit" value="Count">
            <div class="clear"></div>
          </td>
        </tr>
        <tr>
            <td colspan="2"></td>
        </tr>
      </tbody>
    </table>

    <?php
      $height = $_GET[height];
      $lenght = $_GET[lenght];
      $width = $_GET[width];
      $value = explode('|', $_GET['service']);
      var_dump($value);
      $calculation = (2 * $height * ($lenght + $width) + ($lenght+ $width)) * $value[1];  
      echo "<p>Price: $calculation Kč</p>";
    ?>       

    </form>
  </div>      
</div> 

PDO PHP:功能类型

public function Typ() {
  try {      
    $stmt = $this->conn->prepare("SELECT Service_name , Service_price FROM Service_type");
    $stmt->execute();
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

    foreach ($result as $vysledek ){
      echo '<option value="'  . $vysledek["Service_name"] . "|"  . $vysledek["Service_price"] . '">' . $vysledek["Service_name"] . '</option>';
    }  
  } catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
  }
  $this->conn = null; 
}

1 个答案:

答案 0 :(得分:0)

我认为你在这里命名你的选择元素时出错了。

<select id="rightValues" name="service" size="8" multiple></select>更改为<select id="rightValues" name="service[]" size="8" multiple></select>

name="service"name="service[]"

您可以使用array_sum()service选择框下的所选值进行求和。

$servicePriceSum = array_sum($_GET["service"]);

了解array_sum()http://php.net/manual/en/function.array-sum.php

的更多信息
  

注意:只要您选择多个选项,只需加[]   在name属性。

更新:仅从serviceName.'|'.servicePrice

获取价格
$getSelectServices  = $_GET["service"];
$servChargeArr      = array();
foreach($getSelectServices as $serviceDet) {
    $expServiceDet  = explode("|", $serviceDet);
    $servChargeArr[]= &$expServiceDet[1];
}
$servTotal          = array_sum($servChargeArr);
echo $servTotal;

使用上面的代码来获得所需的结果。

相关问题