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

 

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.

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

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>

Archived

This topic is now archived and is closed to further replies.

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