我是PHP编码的新手。我想根据用户从复选框中选择的首选项的相应计数,以升序显示数据库中酒店的名称。我的代码根据用户输入的首选项显示酒店的名称。逻辑是显示酒店名称,如果存在" 1"在针对该酒店的数据库中的首选项名称下。例如,如果用户从7中选择了两个复选框,那么" pool"和"健身房"所以,我的代码显示的是" 1"在数据库中的健身房和游泳池的偏好。我的数据库看起来像:
Hotel_id| Hotel_name| Pool| pool_count | Gym | gym_count| spa | spa_count| 0
1 Abc hotel 1 1.4 1 1.5 1 1.9
2 xyz hotel 1 1.2 0 0 0 0
3 xmk hotel 1 1.0 1 0.5 0 0
4 New hotel 1 1.99 0 0 0 0
5 old hotel 1 0.98 0 0 0 0
6 street hotel 1 0.78 0 0 0 0
. . . .
.
.
我正在使用SQLite3来建立数据库。如果用户选中游泳池,健身房和水疗中心的复选框,那么所有那些拥有" 1"在游泳池的栏目下,必须检索健身房和水疗中心(我的代码会这样做)然后它应该计算各自计数的总和。例如,它应该使用gym_count和spa_count添加pool_count,然后根据它们各自的总和值以升序显示酒店的名称。我试图通过首先创建一个包含计数的所有列名称的数组,然后计算总和的查询来实现它,但我不知道如何传递已经使用的首选项的特定列名称由用户选择。
php_checkbox.php
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>PHP: Get Values of Multiple Checked Checkboxes</title>
<link rel="stylesheet" href="css/php_checkbox.css"/>
</head>
<body>
<div class="container">
<div class="main">
<form action="checkbox_value.php" method="post">
<h2>PHP: Get Values of Multiple Checked Checkboxes</h2>
<select id="mySelect" name="list">
<option selected>Select a place</option>
<option value="1">Lahore</option>
<option value="2">Dubai</option>
<option value="3">New York</option>
<option value="4">Canberra</option>
<option value="5">London</option>
</select>
<p class="heading">Select Your Preferences:</p>
<input type="checkbox" name="check_list[]" value="pool" id="checkbox_1">
<label for="checkbox_1">Pool</label>
<input type="checkbox" name="check_list[]" value="gym" id="checkbox_2">
<label for="checkbox_2">Gym</label>
<input type="checkbox" name="check_list[]" value="spa" id="checkbox_3">
<label for="checkbox_3">Spa</label>
<input type="checkbox" name="check_list[]" value="is_beach" id="checkbox_4">
<label for="checkbox_1">Beach</label>
<input type="checkbox" name="check_list[]" value="is_wifi" id="checkbox_5">
<label for="checkbox_2">Wifi</label>
<input type="checkbox" name="check_list[]" value="is_familyoriented" id="checkbox_6">
<label for="checkbox_3">Family oriented</label>
<input type="checkbox" name="check_list[]" value="is_economical" id="checkbox_7">
<label for="checkbox_3">Value for money</label>
<div>
<input type="submit" name="submit" Value="Submit"/>
</div>
<?php include 'checkbox_value.php';?>
</form>
</div>
</div>
</body>
</html>
checkbox_value.php :
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('mytrip.db');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
}
$hotelOptions = array('pool', 'gym', 'spa', 'is_wifi', 'is_beach', 'is_familyoriented', 'is_economical');
$countOptions = array('pool_count', 'gym_count', 'spa_count', 'wifi_count', 'beach_count','family_count','econo_count');
if (isset($_POST['submit'])) {
if (!empty($_POST['check_list']) && is_array($_POST['check_list'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
$where = '';
foreach($_POST['check_list'] as $selected) {
echo "<p>".$selected ."</p>";
if (array_search($selected, $hotelOptions) !== false) {
$where .= " AND {$selected} = 1";
}
}
$where = substr($where, 5, strlen($where));
// $sql = "SELECT hotel_name FROM Dubai WHERE ".$where.";";
$query= "SELECT SUM( ) FROM Dubai WHERE ".$where.";";
$sql = "SELECT hotel_name FROM Dubai WHERE ".$where.";";
echo "<p>".$where ."</p>";
$ret = $db->query($sql);
while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
echo "<p> <br /></p>\n";
echo "\n". $row['hotel_name'] . "\n";
}
$db->close();
} else {
echo "<b>Please Select Atleast One Option.</b>";
}
我想要实现的第二个任务是根据用户在&#34; php_checkbox.php&#34;下拉列表中选择的目的地显示数据。码。我已为列表中的每个选项提供了唯一的ID,但我不知道如何在查询中动态发送城市名称以打开该目的地的表格。我当前的查询如下:
SELECT hotel_name FROM London WHERE
如何打开用户从下拉列表中选择的城市表?
在这方面的帮助将受到高度赞赏。
答案 0 :(得分:2)
您应该按照与$where
完全相同的选定字段进行排序。我会将选项名称映射到各自的列数:'pool' => 'pool_count'
。从用户选择的表中选择更加简单,只需将包含输入值的数组作为索引,将表名作为值,并选择将所选索引连接到查询中。
我使用了代码的简化版本(只有一个城市,更少的列),但逻辑可以处理您的数据以及调整它。剩下一件事,你应该处理$_POST['list']
不包含正确值的情况。
这是代码:
$hotelOptions = array('swimming_pool', 'roof_top', 'sea_side');
$countOptions = array(
'swimming_pool' => 'swimming_pool_count',
'roof_top' => 'roof_top_count',
'sea_side' => 'sea_side_count',
);
$cities = array(1 => 'Dubai');
if (isset($_POST['submit'])) {
if (!empty($_POST['check_list']) && is_array($_POST['check_list'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
$where = '';
$order = '';
foreach($_POST['check_list'] as $selected) {
echo "<p>".$selected ."</p>";
if (array_search($selected, $hotelOptions) !== false) {
$where .= " AND {$selected} = 1";
$order .= " {$countOptions[$selected]} DESC,";
}
}
$where = substr($where, 5, strlen($where));
$order = substr($order, 0, strlen($order) - 1);
if (isset($cities[$_POST['list']])) {
$sql = "SELECT hotel_name FROM ".$cities[$_POST['list']]." WHERE ".$where." ORDER BY ".$order.";";
$ret = $db->query($sql);
while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
echo "<p> <br /></p>\n";
echo "\n". $row['hotel_name'] . "\n";
}
$db->close();
echo "<br/><b>Note :</b> <span>Similarily, You Can Also Perform CRUD Operations using These Selected Values.</span>";
}
} else {
echo "<b>Please Select Atleast One Option.</b>";
}
}
答案 1 :(得分:0)
它应该使用gym_count和spa_count添加pool_count然后显示 酒店的名称按照他们的升序排列 各自的总和值
为此你可以多种方式完成,
获取提交的复选框,现在当你让它们循环遍历它们并在检查时创建字符串:
$query_string = '';
foreach($selected_checkboxes as $name=>$value)
{
if($value==1)
$query_string .= "`$name` + ";
}
$query_string = rtrim($query_string, " + ");
查询:
SELECT *, ($query_string) AS AMENITIES_COUNT
FROM xyz
ORDER BY AMENITIES_COUNT DESC
PHP:
只需对usort
功能中的设施进行总结,然后根据该功能对其进行排序。
在查询中动态发送城市名称以打开该表格 目的地
使查询动态化非常简单,您只需要了解哪些内容会发生变化,哪些内容不会发生变化。
因此,当用户选择城市并点击提交按钮时,您的查询变量将变为:
$selected_city = $_POST['city'];
$query = "SELECT hotel_name FROM `$selected_city` WHERE ...";
您可能还需要在过滤器查询中使用该城市。 (如果我错过了某些内容,我会编辑答案)
<强> === EDIT === 强>
代码中的一些优化:
$hotel_count_options = array ( ['pool'] => 'pool_count', ['gym'] => 'gym_count', ['spa'] => 'spa_count', ['is_wifi'] => 'wifi_count', ['is_beach'] => 'beach_count', ['is_familyoriented'] => 'family_count', ['is_economical'] => 'econo_count' );
$where = '';
$query_select_string = '';
foreach($_POST['check_list'] as $selected) {
echo "<p>".$selected ."</p>";
if (array_search($selected, array_keys($hotel_count_options)) !== false) {
$where .= " AND {$selected} = 1";
$query_select_string .= "{$hotel_count_options['$selected']} + ";
}
}
$query_select_string = rtrim($query_string, " + ");
现在只需更改您的选择查询字符串
$ sql =&#34; SELECT hotel_name,$ query_select_string as ame_count FROM Dubai WHERE&#34;。$ where。&#34; ORDER BY ame_count desc;&#34 ;;