jason360 Posted December 16, 2014 Share Posted December 16, 2014 Hey guys, I am using a function that echos out 10 images from my database. I need to have a minimum of 4 echoed images. Some categories have less than 4 images, so I would like to have a default image(s) echoed out if the database has 4 or less images. Does anyone know how I can do this? Below is the code I am using now to echo out the 10 images. Thanks in advance: <?php $query = mysql_query('SELECT image_id FROM images ORDER BY RAND() LIMIT 10'); $i = 0; class myCounter3 implements Countable { public function count() { static $count = 0; return ++$count; } } $counter = new myCounter3; while ($row = mysql_fetch_array($query)) { if($i % 10 === 0) { } echo '<img src="http://www.mysite.com/'.$row['image_id'].'.jpg" width="300" height="auto"/>'; $i++; } ?> Default Image: <img src="http://www.mysite.com/default.jpg" width="300" height="auto"/> Quote Link to comment https://forums.phpfreaks.com/topic/293120-echo-array-question/ Share on other sites More sharing options...
Solution requinix Posted December 16, 2014 Solution Share Posted December 16, 2014 After the while loop use a for loop: for (; $i and output your default image inside that. Quote Link to comment https://forums.phpfreaks.com/topic/293120-echo-array-question/#findComment-1499708 Share on other sites More sharing options...
jason360 Posted December 16, 2014 Author Share Posted December 16, 2014 Thanks requinix! Worked like a charm. Quote Link to comment https://forums.phpfreaks.com/topic/293120-echo-array-question/#findComment-1499711 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.