Kemik Posted July 4, 2007 Share Posted July 4, 2007 Hello, 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? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/58477-how-to-display-a-certain-image-depending-on-a-variable/ Share on other sites More sharing options...
john010117 Posted July 4, 2007 Share Posted July 4, 2007 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 <?php $lang = $_GET['lang'] // (or however you get your user's language) echo '<img src="/images/' . $lang . 'flag.gif">'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/58477-how-to-display-a-certain-image-depending-on-a-variable/#findComment-289979 Share on other sites More sharing options...
DeathStar Posted July 4, 2007 Share Posted July 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/58477-how-to-display-a-certain-image-depending-on-a-variable/#findComment-289980 Share on other sites More sharing options...
Kemik Posted July 4, 2007 Author Share Posted July 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/58477-how-to-display-a-certain-image-depending-on-a-variable/#findComment-289985 Share on other sites More sharing options...
john010117 Posted July 4, 2007 Share Posted July 4, 2007 Ok, then. So first, get the data from the database. Then, using the code I've posted above... <?php // 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... ?> Quote Link to comment https://forums.phpfreaks.com/topic/58477-how-to-display-a-certain-image-depending-on-a-variable/#findComment-289986 Share on other sites More sharing options...
Kemik Posted July 4, 2007 Author Share Posted July 4, 2007 Oh yeah, good idea. So name the country flags the same as the info in the database and just add .gif to the end. Never thought of that Quote Link to comment https://forums.phpfreaks.com/topic/58477-how-to-display-a-certain-image-depending-on-a-variable/#findComment-289987 Share on other sites More sharing options...
DeathStar Posted July 4, 2007 Share Posted July 4, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/58477-how-to-display-a-certain-image-depending-on-a-variable/#findComment-289988 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.