Reputation: 576
I'm creating a website for hotels and foods. The page is for registered members only. So, for unregistered users I created a demo of my site. In that demo, the page will show the hotels name(stored in the DB) as a table in the first page.
When they click the submit button next to the hotel name the next page should show the foods under that hotel name only.
Now, I have a problem. How can I pass the specific hotel id to the next function. Here are my codes, I've tried global, public everything.. nothing is working. I've tried to return that variable also, but there are some echos in my function. So, when I call the function the table is printed again.
I need to pass the $data['id'] to the next function to filter the food table.
Can anybody help me on this please...
First Function:
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
}
Second Function:
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
}
Upvotes: 0
Views: 2824
Reputation: 1573
Change this line to.
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>'
what is did is i put a hidden
field inside the form then add the hotel id there like
<input type="hidden" value="' . $data['id'] . '" name="hotel_id">
then in your php
get it like
$hotelId = $_POST["hotel_id"];
Upvotes: 1
Reputation: 524
let's assume your first function is in the first page called hotels.php. in your form html tag you need to past the id through POST or GET
<form method = "GET" action = "foods.php?id={HOTEL_ID}"></form>
then in your second page(foods.php) you need to retrieve the hotel ID using GET.
e.g.
$hotel_id = $_GET["id"];
$sql = "SELECT * FROM FOODS WHERE id = '$hotel_id'";
Then you can get the hotel id on page submit.
Upvotes: 0
Reputation: 23749
When outputting the form with submit button in print_all_hotels
function, add a hidden field with the id of hotel. Suppose, you called it "id".
In print_all_foods use $_REQUEST['id'] in SQL query to get the foods for the hotel with this id.
Something like this:
<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>
Upvotes: 0