【发布时间】: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="<<" />
<input type="button" id="btnRight" value=">>" />
</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