Problem with links within a mysql_fetch_array - any help appreciated

Hello there. I’m working on a basic ad listings website. I am displaying all the titles of each listing using the mysql_fetch_array command and then echoing the output and creating a link to the full ad page. I then want the script to pull out the full database entry for which ever row/link is clicked on. I tried specifying the id of the row into a session variable but when any of the listings are clicked on it only pulls out info for the last id in the array, not the id of the row that was clicked. This is the code below:

[php]

$query = “SELECT * FROM rooms_wanted”;

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
echo “

”;
echo “”;
echo $row[‘title’]. " ". $row[‘description’];
echo “
”;
echo “

”;
}

[/php]

So what I can’t figure out how to do is how to get ‘full_ad.php’ to display the contents of whichever row of the array was clicked on, i.e. if the first row was clicked on I want to bring up all the columns from that particular row.

Any help very much appreciated, I have spent a long time trying to figure this out to no avail, which is why I have posted this here. Thank you.

ok, so if i understand what your doing, you have a page that gives a basic listing and when they click on the url, you want the full stuff to come up? easy enough to. first, change the url to
[php]$query = “SELECT * FROM rooms_wanted”;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
?>

<a href=‘full_ad.php?id=<?=$row['id']?>’ ><?=$row['title']. " ". $row['description']?>

<?php } ?>[/php]

Then on ad_full.php, do
[php]
if(isset($_GET[‘id’])) {
$id = $_GET[‘id’] // the id inside the get matches what’s after the ? in the url
$find_ad = mysql_query(do query here WHERE id = $id);
if(mysql_num_rows($find_ad) == 0) {
// displays if information can’t be found
$oops = “Sorry, no ad could be displayed for this URL”;
} else {
$ad = mysql_fetch_assoc($find_ad);
}
} else {
// prevent direct linking and white pages
header(‘Location: room_listing.php’);
}

// after this, you’re free to use the info in $ad however you want.
[/php]

That should be enough to get you started :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service