Jump to content

Displaying images from a folder using php


liomon41

Recommended Posts

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...

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 />';
}	
}

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.