我正在创建一个酒店和食品网站。该页面仅供注册会员使用。因此,对于未注册的用户,我创建了我的网站的演示。在该演示中,页面将在第一页中以表格的形式显示酒店名称(存储在DB中)。
当他们点击酒店名称旁边的提交按钮时,下一页应该只显示该酒店名称下的食物。
现在,我有一个问题。如何将特定的酒店id传递给下一个功能。这是我的代码,我尝试过全球、公共的一切。。什么都不起作用。我也试着返回这个变量,但我的函数中有一些回声。所以,当我调用函数时,表格会再次打印出来。
我需要将$data['id']传递给下一个函数来过滤食物表。
有人能帮我吗。。。
第一个功能:
function print_all_hotels(){
$result = mysql_query("SELECT * FROM hotels "); // selecting data through mysql_query()
if (!$result) {
die("Database query failed: " . mysql_error());
}
echo '<table border=1px >'; // opening table tag
echo'<th><div style="width: 100px" ></div>No.</th>
<th><div style="width: 200px" ></div>Name</th>
<th><div style="width: 300px" ></div>Details</th>'; //table headers
while($data = mysql_fetch_array($result))
{
// we are running a while loop to print all the rows in a table
echo'<tr>'; // printing table row
echo '<td>'.$data['id'].'</td><td>'.$data['h_name'].'</td><td><form action="details.php" method="POST" style="margin-left:-30%; margin-bottom:-5%"><input style=" border:1px solid #000" class="login_submit" type="submit" name="submit" value="View Details!"></form></td>'; // we are looping all data to be printed till last row in the table
echo'</tr>'; // closing table row
}
echo '</table>'; //closing table tag
}
第二个功能:
function print_all_foods(){
$counter =1;
$result = mysql_query("SELECT * FROM foods where /*Here is the problem*/"); // selecting data through mysql_query()
if (!$result) {
die("Database query failed: " . mysql_error());
}
echo '<table border=1px >'; // opening table tag
echo'<th><div style="width: 100px" ></div>No.</th>
<th><div style="width: 200px" ></div>Foods</th>'; //table headers
while($data = mysql_fetch_array($result))
{
// we are running a while loop to print all the rows in a table
echo'<tr>'; // printing table row
echo '<td>'.$counter++.'</td><td>'.$data['name'].'</td>'; // we are looping all data to be printed till last row in the table
echo'</tr>'; // closing table row
}
echo '</table>'; //closing table tag
}
将此行更改为.
echo '<td>'.$data['id'].'</td><td>'.$data['h_name'].'</td><td><form action="details.php" method="POST" style="margin-left:-30%; margin-bottom:-5%"><input type="hidden" value="' . $data['id'] . '" name="hotel_id"><input style=" border:1px solid #000" class="login_submit" type="submit" name="submit" value="View Details!"></form></td>'
我所做的是在表单中放入一个hidden
字段,然后像一样在那里添加酒店id
<input type="hidden" value="' . $data['id'] . '" name="hotel_id">
然后在你的php
中像一样获取
$hotelId = $_POST["hotel_id"];
-
在
print_all_hotels
功能中,当输出带有提交按钮的表单时,添加一个id为酒店的隐藏字段。假设,你称之为"id"。 -
在print_all_foods中,在SQL查询中使用$_REQUEST['id']来获取带有此id的酒店的食物。
类似这样的东西:
<form action="details.php" method="POST" style="margin-left:-30%; margin-bottom:-5%">
<input type="hidden" name="id" value="<?php print $data['id'] ?>">
<input style=" border:1px solid #000" class="login_submit" type="submit" name="submit" value="View Details!">
</form>
假设您的第一个函数位于名为hotels.php的第一个页面中。在表单html标记中,您需要通过POST或GET 通过id
<form method = "GET" action = "foods.php?id={HOTEL_ID}"></form>
然后在第二个页面(foods.php)中,您需要使用GET检索酒店ID。
例如
$hotel_id = $_GET["id"];
$sql = "SELECT * FROM FOODS WHERE id = '$hotel_id'";
然后您可以在提交页面上获得酒店id。