0

Following is the table i created for displaying the Restaurant Name, Location and Menu for table owners. Now each of the row for the column Menu have Button as values. My table is ready with perfect values.

NOW MY PROBLEM IS HOW TO DO:-

Upon clicking the button corresponding to the each Restaurant, a new File(openmenu.php) will open and will echo the Restaurant Name, Mobile Number of that Restaurant and the menu. But so far, on clicking every Button ,I can only display above entries of the Last row of the table. Help Me Out. I am new to php.

table.php

<?php


include 'nav.php';
$sql = 'SELECT * FROM owners';

$query = mysqli_query($con, $sql);

if (!$query) {
die ('SQL Error: ' . mysqli_error($con));
}


?>

<html>
<head>
<link rel = "stylesheet" type = "text/css" href = "css/style.css">

<style> 
    .data-table{
 width: 1024px;
    margin-left: 150px;
    text-align:center;
     border: 1px solid firebrick;
    background-color: white;

}
td,th{
    border: 1px solid firebrick; padding: 3px 2px 1px 1px;
}


</style>
</head>

<body>

<div class="container">

<article>

<table class="data-table">

    <thead>
        <tr>

            <th>Restuarant Name</th>
            <th>Location</th>
            <th>Menu</th>
        </tr>
        <tr>

        </tr>
    </thead>
    <tbody>
    <?php

    while ($row = mysqli_fetch_array($query)){

   $_SESSION['resphone'] = $row['resphone'];
      $_SESSION['restaur'] = $row['restaur'];
        echo '<tr>


                <td>'.$row['restaur'].'</td>
                <td>'.$row['loc'].'</td>

                <td style="background-color:firebrick;"><form method="post" action="openmenu.php?id=$row[restaur]"><input value="<?php echo $restaur;?>" type="hidden">
 <input type="submit"  value="View"></form></td>
            </tr>';


    }


        ?>

            </tbody>

</table>


</form>







</article>



</div>

</body>
</html>

openmenu.php

<?php 


include('nav.php');


?>

<html>
<head>

 <link rel="stylesheet" href="css/style.css">
 <style>
table, td {
border: none;
 text-align: center;
    text-align-last: center;
 }

</style>
</head>
<body>

<div class="container">




<article>

<form  method="get" align="center"  action="" class="formwrap" enctype='multipart/form-data'>
<h1><?php $restaur = $_SESSION['restaur'];
echo $restaur ;?></h1>
<h1>Call to Order:</h1>
<?php $resphone = $_SESSION['resphone'];

echo $resphone;

?>

<br>
<br>
 <?php


 $sql = "select img from owners where restaur ='$restaur'";
 $result = mysqli_query($con,$sql);
 $row = mysqli_fetch_array($result);

 $image_src2 = "upload/".$row['img'];


?>
<img src='<?php echo $image_src2; ?>' >   

    </form>

</article>


 </div>

  </body>
</html>
Saumay Paul
  • 405
  • 1
  • 7
  • 21

2 Answers2

0
first you getting data from database  & then use view button for openmenu.php but why u use this way 
<form method="post" action="openmenu.php?id=$row[restaur]"><input value="<?php echo $restaur;?>" type="hidden"><input type="submit"  value="View"></form>
Mohit Kumar
  • 952
  • 2
  • 7
  • 18
0

Issue 1

In this snippet you are setting the session variables resphone and restaur to the values of the store you are currently iterating over. Over and over again. That's why you're only ever getting the last store's information - it's the last things you set those variables to.

while ($row = mysqli_fetch_array($query)){

  $_SESSION['resphone'] = $row['resphone'];
  $_SESSION['restaur'] = $row['restaur'];

Issue 2

You should probably change the form method to get and discard the unused hidden input like so:

<form method="post" action="openmenu.php?id=<?=$row['restaur']?>">
  <input type="submit"  value="View">
</form>

Or more likely just change it a plain old a link:

<a href="openmenu.php?id=<?=$row['restaur']?>">View</a>

Issue 3

You're completely ignoring store id requested in openmenu.php. You are using $_SESSION where you should be using $_REQUEST or $_GET. I'm not going to give an example of how you should do that. Instead, please refer to this answer before moving any further.

Will
  • 2,163
  • 1
  • 22
  • 22