Jump to content

[SOLVED] Show images using mysql data.


marksie1988

Recommended Posts

ok i have the following database (also included one line of data for you)

 

CREATE TABLE `IPCountry` (
  `IP_START` varchar(50) default NULL,
  `IP_END` varchar(50) default NULL,
  `IP_FROM` double default NULL,
  `IP_TO` double default NULL,
  `COUNTRY_CADE2` char(2) default NULL,
  `COUNTRY_NAME` varchar(50) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

-- 
-- Dumping data for table `IPCountry`
-- 

INSERT INTO `IPCountry` VALUES ('2.6.190.56', '2.6.190.63', 33996344, 33996351, 'GB', 'United Kingdom');

 

And here is the php all is does is uses data in the database to find out what country an ip address is from what i want to be able to do is when the country is shown a small image of that countries flag is shown.

 

but the database doesnt have the image locations in it and with over 60000 rows in the database its not possible to do so.

 

<?php
$DatabaseServer = "dbserver"; 
$Username = "dbuser"; 
$Password = "dbpass"; 
$DatabaseName = "dbname"; 
$link = mysql_connect($DatabaseServer, $Username, $Password) or die('Could not connect: ' . mysql_error()); 
mysql_select_db($DatabaseName) or die('Could not select database'); 
$IP = $_SERVER["REMOTE_ADDR"]; //Get the IP address 
$res = mysql_query("SELECT country_cade2,country_name FROM IPCountry WHERE IP_FROM<=inet_aton('$IP') AND IP_TO>=inet_aton('$IP')");//look up IP address 
$Codes = mysql_fetch_array($res); //get result 
$CountryCode = $Codes['country_cade2']; //two-letter country code 
$CountryName = $Codes['country_name']; //full country name 
echo "$CountryCode - $CountryName"; //print it out, just as an example 
mysql_close($link); //clean up
?>

Make all image names uppercase and use this

 

$CountryCode = strtoupper($CountryCode);
echo "<img src='image/$CountryCode.PNG' alt='$CountryName' />";

 

EDIT:

images are case specific ON *nix servers not windows, the above code will sort this

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.