Jump to content

Trying to change inline image conditionally in an array...help


sptrsn

Recommended Posts

Currently, I insert a small thumbnail image at the begging of each row in a set of records from a mysql query. But that image is the same image for each record in that array.

 

Is there a way to conditionally change which image is used, based on a value either in that same table, or a related table.

See attached screenshot... and here's my current code....

<?php
$query = "select address from nvc";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$details='';
$details.='<li class="menu"><a href=detail.php?address='.(urlencode($row['address'])).'><img src="thumbs/house.png" /><span class="name">'.$row['city'].'</a>';
$details.='</li>';	
echo($details);
}
?>

 

Thank you for any input.

 

[attachment deleted by admin]

$query = "select address, IFNULL(icons.icon,'defaultIcon.gif') As icon from nvc LEFT JOIN icons ON (icons.addy = nvc.address)";

 

sorry just realized I misnamed the function IFNULL <3

 

SORRY AGAIN I used inner join instead of LEFT JOIN D:

Very cool. I have some studying to do. I've never seen some of what you're used here. Thanks. I'll get to work.

 

np bro, I'll explain it :)

 

IFNULL simply checks if parameter 1 is null, if it is, return parameter 2, if it isn't null, it returns parameter 1 :)

(to make it make sense! IFNULL(param1,param2) )

 

LEFT JOIN is an OUTER JOIN, INNER JOIN is self explanatory :)

 

The difference between outer joins and inner joins, outer joins will not fail the selection if the ON parameter doesn't match, it will simply join a NULL ROW

 

so basically if there is NO ROW in `icons` for the address 'playground' then it will instead of not pull a row for 'playground' it will pull playground from `nvc` and a null row from `icons`..

 

so back to IFNULL, because IFNULL tests param1 against NULL, if `icons` doesn't have a row for the current `nvc` row, IFNULL(icons.icon,'defaultPicture.gif') will naturally return parameter2 which is the default picture for rows without a thumbnail :)

 

I gotta run out :)! Thanks for choosing PHPFreaks for your PHP help <33 *got paid $5 to say that.. cough cough* *not really, I just love this forum!*

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.