xcoderx Posted July 8, 2010 Share Posted July 8, 2010 guys this is my table -- -- Table structure for table `tbl_country` -- CREATE TABLE IF NOT EXISTS `tbl_country` ( `id` int(11) NOT NULL, `country_name` varchar(50) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; -- -- Dumping data for table `tbl_country` -- INSERT INTO `tbl_country` (`id`, `country_name`) VALUES (1, 'Afghanistan'), (2, 'Albania'), (3, 'Algeria'), (4, 'American Samoa'), (5, 'Andorra'), (6, 'Angola'), (7, 'Anguilla'); i am to fetch them with loop help please <div id="logo_ads_from">Display Classifieds From: <select name="country"> <?php //use php loop to print all county name in selectbox ?> </select> </div> Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/ Share on other sites More sharing options...
KevinM1 Posted July 8, 2010 Share Posted July 8, 2010 What have you tried so far? Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1082967 Share on other sites More sharing options...
xcoderx Posted July 8, 2010 Author Share Posted July 8, 2010 not getting the idea thats why i asked Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1082980 Share on other sites More sharing options...
KevinM1 Posted July 8, 2010 Share Posted July 8, 2010 Do you know how to retrieve data from a database? Do you know how to write a while-loop? Do you know how to echo variables? Combine them. Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1082990 Share on other sites More sharing options...
xcoderx Posted July 8, 2010 Author Share Posted July 8, 2010 see i done like this but then i get blank page <?php //use php loop to print all county name in selectbox $result = mysql_query("SELECT * FROM tbl_country") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { echo "<select name='country'>"; echo "<option value='$row['id'];'>$row['country_name'];</option>"; } echo "</select>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1083007 Share on other sites More sharing options...
kenrbnsn Posted July 8, 2010 Share Posted July 8, 2010 When echoing an array reference within double quotes, this will give an error: <?php echo "<option>$row['country_name'];</option>"; ?> You can do it one of these ways: <?php echo "<option>{$row['country_name']}</option>"; // // or // echo "<option>" . $row['country_name'] . "</option>"; // // or // echo "<option>$row[country_name]</option>"; ?> But those still won't work correctly since you're not setting the value of each option. This will: <?php echo "<option value="{$row['country_name'])">{$row['country_name']}</option>"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1083009 Share on other sites More sharing options...
xcoderx Posted July 8, 2010 Author Share Posted July 8, 2010 sorry yes i set value to id just made correction. Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1083011 Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2010 Share Posted July 8, 2010 And you need to move <select name="country"> outside of the loop . . . Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1083012 Share on other sites More sharing options...
KevinM1 Posted July 8, 2010 Share Posted July 8, 2010 Okay, try: echo "<select name=\"country\">"; $result = mysql_query("SELECT * FROM tbl_country"); while($row = mysql_fetch_assoc($result)) { echo "<option value=\"{$row['country_name']}\">{$row['country_name']}</option>"; } echo "</select>"; Since you're trying to output an array value within a string (within quotes), you need to use {} around them. Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1083014 Share on other sites More sharing options...
xcoderx Posted July 8, 2010 Author Share Posted July 8, 2010 done this still page turns blank <?php //use php loop to print all county name in selectbox echo "<select name='country'>"; $result = mysql_query("SELECT * FROM tbl_country") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { echo "<option value="{$row['id'])">{$row['country_name']}</option>"; } echo "</select>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1083016 Share on other sites More sharing options...
xcoderx Posted July 8, 2010 Author Share Posted July 8, 2010 Okay, try: echo "<select name=\"country\">"; $result = mysql_query("SELECT * FROM tbl_country"); while($row = mysql_fetch_assoc($result)) { echo "<option value=\"{$row['country_name']}\">{$row['country_name']}</option>"; } echo "</select>"; Since you're trying to output an array value within a string (within quotes), you need to use {} around them. works bro thanks soo much Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1083018 Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2010 Share Posted July 8, 2010 Quotes were unescaped, and the closing curly brace was a parenthese . . . echo "<option value=\"{$row['id']}\">{$row['country_name']}</option>"; Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1083019 Share on other sites More sharing options...
xcoderx Posted July 8, 2010 Author Share Posted July 8, 2010 Quotes were unescaped, and the closing curly brace was a parenthese . . . echo "<option value=\"{$row['id']}\">{$row['country_name']}</option>"; true but then my teacher tells me not to use back slashes he insist me to use single quotes instead inside of double quotes Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1083022 Share on other sites More sharing options...
kenrbnsn Posted July 8, 2010 Share Posted July 8, 2010 Sorry about the unescaped double quotes -- I'm not fully awake yet... . Ken Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1083023 Share on other sites More sharing options...
xcoderx Posted July 8, 2010 Author Share Posted July 8, 2010 kewl bro atleast my problem sloved thanks all Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1083026 Share on other sites More sharing options...
xcoderx Posted July 8, 2010 Author Share Posted July 8, 2010 Okay, try: echo "<select name=\"country\">"; $result = mysql_query("SELECT * FROM tbl_country"); while($row = mysql_fetch_assoc($result)) { echo "<option value=\"{$row['country_name']}\">{$row['country_name']}</option>"; } echo "</select>"; Since you're trying to output an array value within a string (within quotes), you need to use {} around them. bro can i not use while($row = mysql_fetch_array($result)) insteat of while($row = mysql_fetch_assoc($result))? whats the difference between assoc and array? why did we have to use assoc but not array? Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1083192 Share on other sites More sharing options...
kenrbnsn Posted July 8, 2010 Share Posted July 8, 2010 Read the manual entries for mysql_fetch_array and mysql_fetch_assoc. Everything is explained in them. Ken Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1083194 Share on other sites More sharing options...
xcoderx Posted July 9, 2010 Author Share Posted July 9, 2010 thanks got it :-0 $result = mysql_query("SELECT * FROM tbl_country",$conn); while($row = mysql_fetch_array($result)) { echo "<option value='{$row['0']}'>{$row['1']}</option>"; } outputs same results :-) Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1083669 Share on other sites More sharing options...
Pikachu2000 Posted July 9, 2010 Share Posted July 9, 2010 Yes, it does output the same results. The issue is efficiency. In your code above, you should be using mysql_fetch_row(), or possibly mysql_fetch_array( 'query string', MYSQL_NUM) to limit the results to an enumerated array, since that is how you're accessing it. mysql_fetch_row(); returns an enumerated array. mysql_fetch_assoc(); returns an associative array. mysql_fetch_array(), when used without modifying parameters, returns both an associative array and an enumerated array, which has higher overhead, and is slower. You should explicitly list the fields you need in the query string (SELECT `field1`, `field2` FROM . . . ) instead of wildcard selecting them (SELECT * FROM . . . ), unless, of course, you actually need all of the fields in the table. Quote Link to comment https://forums.phpfreaks.com/topic/207126-how-do-i-fetch-country-list-with-forloop-in-drop-down-list/#findComment-1083696 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.