【问题标题】:Calculating more than one value from select option dropdown从选择选项下拉列表中计算多个值
【发布时间】:2016-07-18 18:16:22
【问题描述】:

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

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

【问题讨论】:

  • 你的意思是,你想对选择框下选择的值求和?
  • 我想对添加到 RightBox 中的值求和,以便右框中的所有值。这些类型的服务添加在右侧框中,然后当用户选择它们时,它们的价格会有所不同。它在由数据库中的.$vysledek["Service_price"] . 表示的函数 Typ() 中。然后对每种服务类型使用计算模式,然后将所有价格计算为一个最终价格。
  • 是的,然后在下面检查我的答案,非常重要的是,您需要使用JS 来选择每个右侧选项,这意味着带有属性selected

标签: javascript php jquery html pdo


【解决方案1】:

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

&lt;select id="rightValues" name="service" size="8" multiple&gt;&lt;/select&gt; 更改为&lt;select id="rightValues" name="service[]" size="8" multiple&gt;&lt;/select&gt;

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

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

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

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

注意:如果您有多个选项,只需添加[] 在名称属性中。

更新:仅获取来自serviceName.'|'.servicePrice的价格

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

使用上面的代码得到你需要的结果。

【讨论】:

  • 好吧,我尝试了这个,但当我的选项收集服务名称和服务价格时,仍然如何使用这个 array_sum。这就是我使用爆炸的原因
  • 你可以查看这个[链接]mape.8u.cz/…
  • @Lukas.K 您没有在名称属性中添加[],这就是我的回答。添加它,它肯定会起作用。
  • 添加了,但它仍然只返回 int(0) .. 我认为你不能那样使用它,因为 echo '&lt;option value="' . $vysledek["Service_name"] . "|" . $vysledek["Service_price"] . '"&gt;' . $vysledek["Service_name"] . '&lt;/option&gt;'; 我想你可能只是不明白我,因为我想在类似 for 语句的语句中进行计算,我在右框中遍历所有选定的服务,然后使用计算模式对每个服务进行计算,然后对每个服务的所有计算价格进行编号。
  • 先告诉我,你的 PHP 文件中是否获得了所有选中的记录?
猜你喜欢
  • 1970-01-01
  • 2018-12-16
  • 1970-01-01
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多