sptrsn Posted March 23, 2011 Share Posted March 23, 2011 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] Quote Link to comment Share on other sites More sharing options...
RussellReal Posted March 23, 2011 Share Posted March 23, 2011 $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: Quote Link to comment Share on other sites More sharing options...
sptrsn Posted March 23, 2011 Author Share Posted March 23, 2011 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. Quote Link to comment Share on other sites More sharing options...
RussellReal Posted March 23, 2011 Share Posted March 23, 2011 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!* Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.