Woodburn2006 Posted May 10, 2008 Share Posted May 10, 2008 i have a database with fields named img1, img2, img3 up to img8. some of these fields hold paths to images and some of them have a value of 'x' just to show there is no image present. i am making a page that shows these images but i need it to only show the fields that have an image in it, i have put together a simple bit of script but i was wondering if there is an easier way of doing it using a for loop or omething similar. heres what i have got: if($img1 != "x"){echo "<img src='$img1' width='150' border='0'><br /><br />";} if($img2 != "x"){echo "<img src='$img2' width='150' border='0'><br /><br />";} if($img3 != "x"){echo "<img src='$img3' width='150' border='0'><br /><br />";} if($img4 != "x"){echo "<img src='$img4' width='150' border='0'><br /><br />";} if($img5 != "x"){echo "<img src='$img5' width='150' border='0'><br /><br />";} if($img6 != "x"){echo "<img src='$img6' width='150' border='0'><br /><br />";} if($img7 != "x"){echo "<img src='$img7' width='150' border='0'><br /><br />";} if($img8 != "x"){echo "<img src='$img8' width='150' border='0'><br /><br />";} any help would be much appreciated Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted May 10, 2008 Share Posted May 10, 2008 for ($i=1;$i<9;$i++) { $image = ${"img$i"}; if($image != "x"){ echo "<img src='$image' width='150' border='0'><br /><br />"; } } Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 10, 2008 Share Posted May 10, 2008 You ought to consider using a separate table in your database for your images, rather than having all these different fields in the same table. Try reading up on database normalization. Maybe this post will help you start: http://www.phpfreaks.com/forums/index.php/topic,126097.0.html 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.