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


Calculating more than one value from select option dropdown

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

我将在这里添加简单的html代码和PDO函数中的函数。我想要的是计算<div id="RightBox">中的值,这些值是通过javascript按钮从<div id="LeftBox">移动到那里的,然后在用$value = explode('|', $_GET['service'])分解它们之后用计算来计算它们的值。当我分解这些值时,我想从函数Type()中取. $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; 
}

我认为您在此处命名select元素时出错了。

<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;

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