Jump to content

How to display a certain image, depending on a variable.


Recommended Posts



I have a database with the following information in. User ID, Username, Country, City.


I have created a page which lists all of the users and their cities and would like to add the flag of the user's country next to their name. How would I do this?


I understand I could go with if statements (e.g. if $country = eng display englishflag.gif elseif $country = scot display scotishflag.gif) however with so many countries this would take forever and far too much code. Is there any easier way of doing this?



Link to comment
Share on other sites

Yes, there is a way. First, name all of your image files the same way (ex: LANGflag.gif) and they should all be in the same folder. Then when you display it, just use


$lang = $_GET['lang'] // (or however you get your user's language)
echo '<img src="/images/' . $lang . 'flag.gif">';

Link to comment
Share on other sites

I'm going to make your day. :)


I was looking for this last month.

PM me your email address then ill send you the script, and images.


It basically detects your country code by ip and displays the correct image by getting the variable out of the database.

Link to comment
Share on other sites

Sorry guys, I think your getting the wrong idea.


I have a list already in the database. It's just like the member's page on this forum but to the left of the username I want to put the username's country's flag.


E.g. if "!!BUBBLES!!" is from the US there will be a US flag to the left of his/her name, same for the rest of the list.


This is basically for a member system I'm making for my website. Users sign up, include where they are from in the form and then I have a users page which displays everyone's username and city.


Sorry if I confused you.

Link to comment
Share on other sites

Ok, then. So first, get the data from the database. Then, using the code I've posted above...


// I'm assuming you've already made the db connection and have executed the query...
// Substitute the row name

while($row = mysql_fetch_array($result)) {
echo '<img src="/images/' . $row['lang'] . 'flag.gif">';
// blah...

Link to comment
Share on other sites

Then I have the right Idea.


The script will determine the Country automatically by using the IP Address of the client.

It has Text(eg:United States) aswell, so you can just call it.

You can simply put one part int othe register page and then just call the saved data when You need it.


Example: Ever noticed Google has a little flag in the top right corner of the screen, your countries?

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.

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.