liomon41 Posted June 11, 2012 Share Posted June 11, 2012 I have a form that has a dropdown menu and a button and the list values for the dropmenu are five countries (france, england, germany, spain, norway) and i also i have in a folder in my local server the flags for these five countries (france.jpg, england.jpg, germany.jpg, spain.jpg, norway.jpg) . The idea is for everytime a user select a country from the dropdown menu and clicks the button, the countries flag is displayed from the folder unto the page right next to the button... Any ideas how to go about this... really appreciate you guys helping out.... Quote Link to comment https://forums.phpfreaks.com/topic/263976-retrieving-images-from-a-folder-using-php/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 11, 2012 Share Posted June 11, 2012 Where exactly are you stuck at doing this? If your form is submitting the country name, all you would basically need to do is make the correct path/filename.ext and put that into an <img src='path/filename.ext' alt=''> tag. Quote Link to comment https://forums.phpfreaks.com/topic/263976-retrieving-images-from-a-folder-using-php/#findComment-1352843 Share on other sites More sharing options...
liomon41 Posted June 11, 2012 Author Share Posted June 11, 2012 This is first time working with php and files, would love it if you explained in details how to really go about doing this from scratch... thanks Quote Link to comment https://forums.phpfreaks.com/topic/263976-retrieving-images-from-a-folder-using-php/#findComment-1352844 Share on other sites More sharing options...
PFMaBiSmAd Posted June 11, 2012 Share Posted June 11, 2012 A picture is worth a thousand words. Code to take a submitted country name (france, england, germany, spain, norway) and display the corresponding .jpg flag image - <?php $folder = 'flags'; // define folder with .jpg flag images $flags = glob("$folder/*.jpg"); // get a list of the flags (for building the option menu and validating the submitted value) $selected_flag = isset($_POST['country']) ? $folder.'/'.strtolower($_POST['country']).'.jpg' : ''; // build selected flag from the submitted country name $selected_flag = in_array($selected_flag,$flags) ? $selected_flag : ''; // test if submitted flag is valid (insure that only expected image files in the correct folder can be selected.) echo "<img src='$selected_flag' alt=''>"; // display selected flag If there's anything you don't understand after reading the code and comments and trying the code, just ask. Quote Link to comment https://forums.phpfreaks.com/topic/263976-retrieving-images-from-a-folder-using-php/#findComment-1352845 Share on other sites More sharing options...
liomon41 Posted June 11, 2012 Author Share Posted June 11, 2012 wow..... that worked perfectly well.... Thanks a bunch.... Quote Link to comment https://forums.phpfreaks.com/topic/263976-retrieving-images-from-a-folder-using-php/#findComment-1352846 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.