0

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
}
3
  • 1
    have you tried to use session? Commented Dec 9, 2013 at 3:25
  • No, because the id is from the table. When a submit button on a row is clicked,the particular row's id should be passed to the next function. That is the problem :/ Commented Dec 9, 2013 at 3:27
  • ohh, i see the form. check my answer. Commented Dec 9, 2013 at 3:32

3 Answers 3

1

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"];
Sign up to request clarification or add additional context in comments.

Comments

0
  1. 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".

  2. 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>

1 Comment

He could probably also just persist the hotel id in the url instead of setting a hidden field. Either way works for me
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.