从选择选项下拉列表中计算多个值
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="<<" />
<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;
}
我认为您在此处命名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;
使用上面的代码来获得所需的结果。
- 计算用户从多个文件中选择的选项数量
- Javascript-如何使用bootstrap日期时间选择器自动计算两个时间输入之间的差异
- 如何根据HTML下拉列表中的数量选择来计算价格
- 嗨,我正试图将我的movieprice数组从复选框值传递给另一个计算选择总数的函数,但我的代码没有;t运行
- 如何使用下拉菜单修复jquery日期选择器日期计算功能
- 从选择选项下拉列表中计算多个值
- 如何使用选择框输入进行计算?HTML和JS
- 计算日期选择器+时间选择器是否在东部标准时区的过去
- 更改选择框时计算价格变化
- 计算在多个下拉框中选择某个对象的次数
- Jquery 使用日期选择器计算天数
- Jquery 根据选择值计算折扣
- 根据 jquery 中 2 个选择列表的选定值动态计算的最佳方法
- 使用 each() 选择并计算未选中复选框的长度
- 如何计算两个日期选择器之间的差异
- 如何让用户使用 javascript 在其本地计算机上选择一个文件夹(而不是文件)
- 根据表单内的选择菜单值计算值
- Shopify (液体) 计算选择和更改样式中的选项大小
- 当更改页面大小时,动态地重新计算选择框的鼠标位置
- 计算选择框值