简体   繁体   中英

How to display multiple result from MySql query using PHP error

I have a query for category of items, in this example I have 2 WINEs from Chile. The code i have below works fine but only the foreach loop or whatever it is, it overlaps the first output with the second one.

I am new to for-each loop

Here is MY PHP:

<?php  

include"db_connection.php";

$sql = mysql_query("SELECT * FROM WINE WHERE country='Chile'");

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


foreach ($allRows as $row) {
            $id = $row ["id"];          
            $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 the HTML:

<?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 />
      <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>
<?php
}
?>
</table>
</div>
<?php include('footer.php'); ?>

What happens is that you're overwriting your $id etc. variables on every loop, thus only the results of the last row are saved there.

However, you do not need these variables at all, as you have all information saved in $allRows . Just like you access the id via <?php echo $row['id']; ?> <?php echo $row['id']; ?> in your HTML, you should do with all other variables. Eg: $row["bottle_price"] instead of $bottle_price .

Your PHP code would look like this:

<?php 
    include"db_connection.php";

    $sql = mysql_query("SELECT * FROM WINE WHERE country='Chile'");
    $allRows = array();

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

Your HTML like this:

<?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 />
            <a href="inventory_images/<?php echo $row['id']; ?>.jpg">View Full Size Image</a>
        </td>
        <td width="81%" valign="top">
            <h3><?php echo $row['wine_type']; ?></h3>
            <p><?php echo "$".$row['bottle_price']; ?><br /><br />
                <?php echo $row['country']." ".$row['indicator']; ?> <br /><br />
                <?php echo $row['description']']; ?> <br />
            </p>

            <form id="form1" name="form1" method="post" action="cart.php">
                <input type="hidden" name="pid" id="pid" value="<?php echo $row['id']; ?>" />
                <input type="submit" name="button" id="button" value="Add to Shopping Cart" />
            </form>
        </td>
    </tr>
<?php
    }
?>
</table>
</div>
<?php include('footer.php'); ?>

Well by the look of it you loop through the array twice. Once to set the variables, then once to display it. The problem here is you are over writing the variables before you use them.

foreach ($allRows as $row) {
        $id = $row ["id"];          
        $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"]));

?>

<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 />
  <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>
?><?php
}
?>

You need to set the variables and use them within the same foreach().

Also it may be wise to move towards PDO as mysql_query is unsafe and is deprecated as of PHP 5.5.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM