PHP: How to carry over data to another page after mysql read using while loop


I have a slight dilemma on my hands. This is the situation. On a webpage, I am using a php while loop to read data into a page from a database. That works fine but I need each of this items when clicked, go to another webpage with more info on them. This 'more info' is just data that i will have on another column in database so its no biggie. So my dilemma is how do I know which item is clicked. Is there a way to track which unique index was clicked? (Keep in mind that I might me listing thousands of items on the page).

This is my while loop code

while($row = mysql_fetch_array($result)){
 echo '<div class="thumb">';
 echo '<a href="template.php"><img src='.$row['dresses_pic'].'/></a>';
 echo '<p>'.$row['price_'].'</p>';
 echo '</div>';
}

I have tried putting a SESSION inside the while loop but the next page always reads the last listing as the item.
Is there a better way to achieve what am trying to do?
Thanks

Upvotes: 1

Views: 2122

Answers (4)

brimat
brimat

Reputation: 166

Use a query string to pass the item ID to your template.php page.

while($row = mysql_fetch_array($result)){
 echo '<div class="thumb">';
 echo '<a href="template.php?id='.$row['item_id'].'"><img src='.$row['dresses_pic'].'/></a>';
 echo '<p>'.$row['price_'].'</p>';
 echo '</div>';
}

See altered line above...

Upvotes: 1

xdazz
xdazz

Reputation: 160843

Pass the id of the record to the next page(Use url parameter is most simple, ?id=xxx), and query the db use the id (You could get it by $_GET['id']) to get the info.

Upvotes: 1

John Conde
John Conde

Reputation: 219814

Just append the ID of the item to your link and then you can get it from the next page as a $_GET variable:

while($row = mysql_fetch_array($result)){
 echo '<div class="thumb">';
 echo '<a href="template.php?id='.$row['id'].'"><img src='.$row['dresses_pic'].'/></a>';
 echo '<p>'.$row['price_'].'</p>';
 echo '</div>';
}

template.php

$id = (int) $_GET['id']; // Use this to get the item from the DB

Upvotes: 2

Jay Sidri
Jay Sidri

Reputation: 6406

Try passing in the ID of the row item and THEN retrieve the data for that ID on the new page.

 echo '<a href="template.php?id='.$row['id'].'"><img src='.$row['dresses_pic'].'/></a>';

Upvotes: 1

Related Questions