user3311898
user3311898

Reputation: 113

How to display multiple result from MySql query using PHP

I have my current code working perfectly but I needed help on when I want the code to display as many queries exist in the table called wine... for example if there are more than wine type in category as spain then it should display all but it only displays one.

Here is my PHP:

<?php  

include"db_connection.php";

$sql = mysql_query("SELECT * FROM WINE WHERE country='Chile'");
    while($row = mysql_fetch_array($sql)){ 
         $description = $row["description"];
         $wine_type = $row["wine_type"];
         $country = $row["country"];
         $bottle_price = $row["bottle_price"];
         $indicator = $row["indicator"];
         $colour = $row["colour"];
         $case_price = $row["case_price"];
         $case_size = $row["case_size"];         
         $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));    
     }
?>

Here is my HTML:

<?php include('header.php'); ?>

<div id="content">
<table width="100%" border="0" cellspacing="0" cellpadding="15">
    <tr>
        <td width="19%" valign="top"><img src="inventory_images/<?php echo $id; ?>.jpg" width="142" height="188" alt="<?php echo $wine_type; ?>" /><br />
        <a href="inventory_images/<?php echo $id; ?>.jpg">View Full Size Image</a></td>
            <td width="81%" valign="top"><h3><?php echo $wine_type; ?></h3>
            <p><?php echo "$".$bottle_price; ?><br /><br />
                <?php echo "$country $indicator"; ?> <br /><br />
                <?php echo $description; ?> <br />
            </p>

              <form id="form1" name="form1" method="post" action="cart.php">
                <input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
                <input type="submit" name="button" id="button" value="Add to Shopping Cart" />
              </form>
            </td>
    </tr>
</table>
</div>

<?php include('footer.php'); ?>

Upvotes: 1

Views: 2069

Answers (1)

kero
kero

Reputation: 10658

The problem is currently that the while() loop will keep overriding the variables. This can be solved in a couple of ways, one would be that you save the entire fetched column in an array and use that array later for iteration.

$allRows = array();
while($row = mysql_fetch_array($sql)) {
    $allRows[] = $row;
}

Now, as mentioned above, iterate over $allRows in your template

<?php include('header.php'); ?>
<div id="content">
<table width="100%" border="0" cellspacing="0" cellpadding="15">
<?php
foreach ($allRows as $row) {
?>
    <tr>
      <td width="19%" valign="top"><img src="inventory_images/<?php echo $row['id']; ?>.jpg" width="142" height="188" alt="<?php echo $row['wine_type']; ?>" /><br />
      etc.
    </tr>
<?php
}
?>
</table>
</div>
<?php include('footer.php'); ?>

Here I addressed the variables as $row['...'] - if you don't want to change that part of the code, simply do the assigning at the beginning of the loop.

etc.
<?php
foreach ($allRows as $row) {
     $description = $row["description"];
     $wine_type = $row["wine_type"];
     //etc.
?>

A much cleaner solution (not mixing that much HTML and PHP which usually creates lots of confusion) would be using a template engine. Also don't use mysql_* functions in new code - they are deprecated. See this answer for more information.

Upvotes: 2

Related Questions