liomon41 Posted June 7, 2012 Share Posted June 7, 2012 Hey you guys i have a little project im working on right now, really need your help. I created a database called countries and it contains list of 10 countries e.g (england, us, germany.... etc) and in a folder i have the flags(images) for each country e.g (england.jpg, us.jpg, germany.jpg...... etc). My problem is how do you write a code in php to display the countries from the database into a list form at the same time displaying the right flag for each country right next to them.... Thanks for your help... Quote Link to comment https://forums.phpfreaks.com/topic/263792-displaying-images-from-a-folder-using-php/ Share on other sites More sharing options...
Skewled Posted June 7, 2012 Share Posted June 7, 2012 How is your database structured? What is the current PHP code your using to query and then output the results? --- Quick & Dirty --- Add a field in the database to store the link to the image for each country. When retrieving the results from the database simply apply the value into <img> </img> tags and output the picture. Quote Link to comment https://forums.phpfreaks.com/topic/263792-displaying-images-from-a-folder-using-php/#findComment-1351803 Share on other sites More sharing options...
scootstah Posted June 7, 2012 Share Posted June 7, 2012 You'll need to match the name of the image in the database to the physical image on the webserver. Something like... $result = mysql_query("SELECT flag FROM countries"); while($row = mysql_fetch_assoc($result)) { if (file_exists($image = 'images/flags/' . $row['flag'])) { echo '<img src="http://yoursite.com/' . $image . '" /><br />'; } } Quote Link to comment https://forums.phpfreaks.com/topic/263792-displaying-images-from-a-folder-using-php/#findComment-1351897 Share on other sites More sharing options...
liomon41 Posted June 7, 2012 Author Share Posted June 7, 2012 Thanks for your reply... this is what i have so far but nothing is showing on my page ... <table width="400" border="0"> <?php $query = "select * from fixtures"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $first = $row['firstTeam']; $second = $row['secondTeam']; if (file_exists($image = 'images/' . $first)) { ?> <tr> <td><?php echo $first; ?></td><td><?php echo '<img src="images/' . $image . '" />'; ?> </td><td>vs</td><td><?php echo $second; ?></td> </tr> <?php } } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/263792-displaying-images-from-a-folder-using-php/#findComment-1351995 Share on other sites More sharing options...
scootstah Posted June 7, 2012 Share Posted June 7, 2012 Since you assigned $image to "images/$first", you don't need to do "images/$image" in the img tag. Just put src="$image". You might want to throw the domain in front of it though. Quote Link to comment https://forums.phpfreaks.com/topic/263792-displaying-images-from-a-folder-using-php/#findComment-1351997 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.