user6843125
user6843125

Reputation:

How to loop through rows for data to display multiple results?

I have searched many forums including Stack Overflow and not been able to find a solution for this that works. Please do not mark this as duplicate.

I have a database of users with multiple entries for certain items. The users are saved in one table ("TABLE 1") and the items are saved in another ("TABLE 2"). What I need the code to do is search TABLE 2 for user IDs that match the selected user from TABLE 1 and then for each matching entry display the item data for that row. The code I currently have is below but it is only displaying one result and then stopping. Any help is greatly appreciated.

<?php

$queryItems = mysql_query("SELECT * FROM items WHERE user_id='$results[id]'");

$itemmatch_result = mysql_fetch_array($queryitems) or die($itemmatch."<br/><br/>".mysql_error());

{
    ?>

<div class="column6">

<div class="item-container">

<p class="itemname"><?php echo $itemmatch_result['item_make'] . " BRAND " . $itemmatch_result['item_type']; ?> item</p>

<p class="itemsize"><strong>Item Size:</strong> <span><?php echo $itemmatch_result['item_size']; ?></span></p>

<p class="tiresize"><strong>Item Model:</strong> <span><?php echo $itemmatch_result['item_model']; ?></span></p>

<p class="tiresize"><strong>Registered:</strong> <span><?php echo date('F jS, Y', $itemmatch_result['item_registered']); ?> AT STORE</span></p>

<span class="purchase-data">BOUGHT <?php echo date('F jS, Y', $itemmatch_result['item_bought']); ?></span>

</div>

<?php

}

unset($itemmatch_result);

?>

Upvotes: 0

Views: 680

Answers (1)

Andrew Larsen
Andrew Larsen

Reputation: 1267

You need to loop through all records as long as the query is giving you the records you expect.

while ($data = mysql_fetch_array($queryItems)) {
     echo '
          <p class.........>' . $data['item_make'] . ' BRAND .....
     ';
}

Btw, you shouldn't be using mysql anymore as it's deprecated, I would suggest starting to use mysqli/pdo instead.

Upvotes: 1

Related Questions