graham23s Posted June 1, 2007 Share Posted June 1, 2007 Hi Guys, when users on my site look at they're profile details some of the data in like this: <select name="somename"> <option value="Option 1">Option 1</option> <option value="Option 2">Option 2</option> </select> once a user has selected an option and it's stored in mysql i was wanting the specific information to be displayed in the drop down box, if i selected option 2 next time i log in option 2 is displayed and so on. what would be the best way to achieve this? thanks guys Graham Quote Link to comment https://forums.phpfreaks.com/topic/53892-echoing-users-data-from-mysql/ Share on other sites More sharing options...
MadTechie Posted June 1, 2007 Share Posted June 1, 2007 just say the call from the database is row and the field is gender i would do something like this $theList = array("Male", "Female", "CrossGender"); <select name="somename"> foreach ($theList as $Item) { $default = ($Item = $row['gender'])?" selected ":""; echo "<option $default value='Option 1'>Option 1</option>"; } </select> untested but you should get the idea Quote Link to comment https://forums.phpfreaks.com/topic/53892-echoing-users-data-from-mysql/#findComment-266485 Share on other sites More sharing options...
graham23s Posted June 1, 2007 Author Share Posted June 1, 2007 Hi MT, i'm testing the code on users countries (to get to grips with the code) then apply it to other text boxes my countries are in a 2d array like so: $countries_list = array( 1 => 'Afganistan', 2 => 'Albania', 3 => 'Algeria', 4 => 'Andorra', and so on, i changed the code slightly to this: echo '<select name="country">'; foreach ($countries_list as $Item) { $default = ($Item = $row['country'])?" selected ":""; echo "<option $default value='Option 1'>Option 1</option>"; } echo '</select>'; but i get a whole load of "Option 1" 's in the select boxes is it because the arrays are 2d at all? cheers mate Graham Quote Link to comment https://forums.phpfreaks.com/topic/53892-echoing-users-data-from-mysql/#findComment-266535 Share on other sites More sharing options...
per1os Posted June 1, 2007 Share Posted June 1, 2007 echo '<select name="country">'; foreach ($countries_list as $id => $Item) { $default = ($Item = $row['country'])?" selected ":""; echo "<option $default value='$id'>$Item</option>"; } echo '</select>'; That should do it. Quote Link to comment https://forums.phpfreaks.com/topic/53892-echoing-users-data-from-mysql/#findComment-266537 Share on other sites More sharing options...
MadTechie Posted June 1, 2007 Share Posted June 1, 2007 I agree with frost.. His too quick on the reply button (gets me every time) LOL Quote Link to comment https://forums.phpfreaks.com/topic/53892-echoing-users-data-from-mysql/#findComment-266547 Share on other sites More sharing options...
graham23s Posted June 1, 2007 Author Share Posted June 1, 2007 LOL forgive me if this post is to long i'm going to post, but is shows the main page code: frost1100 that code works (it has the NUMBER of the country in the drop down box but the same number the whole way down like the option 1) <?php // A 2 dimensional array with the countries...////////////////////////////////////// $countries_list = array( 54 => 'Afganistan', 65 => 'Albania', 35 => 'Algeria', 68 => 'Andorra', 36 => 'Angola', 35 => 'Algeria', 89 => 'Antigua Barbuda', 20 => 'Australia', 37 => 'Austria', 82 => 'Bahamas', 86 => 'Bangladesh', 85 => 'Barbados', 16 => 'Belgium', 34 => 'Belize', 67 => 'Bosnia Herzegovina', 18 => 'Brazil', 104 => 'Bulgaria', 60 => 'Burkina Faso', 82 => 'Bahamas', 5 => 'Canada', 51 => 'Chile', 8 => 'China', 99 => 'Colombia', 102 => 'Costa Rica', 53 => 'Congo', 97 => 'Croatia', 52 => 'Cuba', 46 => 'Czech Republic', 10 => 'Denmark', 41 => 'Dominican Republic', 103 => 'Egypt', 98 => 'Estonia', 14 => 'Finland', 6 => 'France', 7 => 'Germany', 42 => 'Greece', 43 => 'Guatemala', 33 => 'Hong Kong', 79 => 'Honduras', 74 => 'Hungary', 62 => 'Iceland', 70 => 'India', 44 => 'Israel', 9 => 'Italy', 13 => 'Ireland', 31 => 'Jamaica', 17 => 'Japan', 58 => 'Kiribati', 87 => 'Laos', 101 => 'Latvia', 100 => 'Lebanon', 69 => 'Lithuania', 32 => 'Luxembourg', 40 => 'Malaysia', 25 => 'Mexico', 63 => 'Nauru', 15 => 'Netherlands', 71 => 'Netherlands Antilles', 21 => 'New Zealand', 61 => 'Nigeria', 96 => 'North Korea', 11 => 'Norway', 45 => 'Pakistan', 90 => 'Paraguay', 83 => 'Peru', 59 => 'Philippines', 14 => 'Poland', 24 => 'Portugal', 50 => 'Puerto Rico', 75 => 'Romania', 3 => 'Russia', 47 => 'Serbia', 48 => 'Seychelles', 200 => 'Scotland', 94 => 'Senegal', 26 => 'Singapore', 64 => 'Slovenia', 29 => 'South Africa', 30 => 'South Korea', 23 => 'Spain', 1 => 'Sweden', 57 => 'Switzerland', 49 => 'Taiwan', 93 => 'Thailand', 50 => 'Togo', 78 => 'Trinidad & Tobago', 55 => 'Turkey', 55 => 'Ukraine', 12 => 'United Kingdom', 2 => 'United States of America', 88 => 'Uruguay', 56 => 'Uzbekistan', 73 => 'Venezuela', 77 => 'Vietnam', 39 => 'Western Samoa', 38 => 'Yugoslavia', ); ?> <?php // Get the users details from mysql...////////////////////////////////////////////// $query1 = "SELECT * FROM `membership` WHERE `username`='$member'"; $result1 = mysql_query($query1) or die (mysql_error()); $row = mysql_fetch_array($result1); $id = $row['id']; $username = $row['username']; $email = $row['email']; $country = $row['country']; $avatar = $row['avatar']; $user_class = $row['user_class']; $ban = $row['ban']; // if the user is banned no further...////////////////////////////////////////////// if($ban == "Y") { echo "<br /><b><h4>This Account Has Been Disabled</h4></b>"; echo "<meta http-equiv=\"refresh\" content=\"1;URL=login.php\">"; include("includes/footer.php"); exit; } // check if the user has messages in there inbox...///////////////////////////////// $query_msg = "SELECT * FROM `pms` WHERE `reciever_id`='$id'"; $result_msg = mysql_query($query_msg) or die (mysql_error()); if (mysql_num_rows($result_msg) > 0) { echo '<br /> <table border="1" bordercolor="#000000" cellspacing="0" cellpadding="10" bgcolor="red"> <tr> <td style="padding: 10px; background: red"> <strong><a class="msg_inbox" href="inbox.php">You Have Messages In Your Inbox</a> </td> </tr> </table>'; } // the POST data...///////////////////////////////////////////////////////////////// $email_updated = $_POST['email']; $country_updated = $_POST['country']; // isset...///////////////////////////////////////////////////////////////////////// if(isset($_POST['submit'])) { // a query to update mysql.../////////////////////////////////////////////////////// $query2 = "UPDATE `membership` SET `email`='$email_updated',`country`='$country_updated' WHERE `username`='$member'"; $result2 = mysql_query($query2) or die (mysql_error()); if($result2) { echo "<br /><b>Profile Updated Successfully!</b><br /><br />"; } } else { // get the id...//////////////////////////////////////////////////////////////////// echo "<br /><h3><p align=\"center\"> Hi, <a href=\"user_details.php?id=$id\">$username</p></a></h3>"; echo '<br /> <table width="400" border="1" bordercolor="#000000" cellspacing="0" cellpadding="3" /> <form action="my_account.php" method="POST" /> <th colspan="2" bgcolor="#004E98"><font color="#ffffff">Edit Your Profile</font></th> <tr> <td align="right"><b>Your E-Mail:</b></td><td align="left"><input type="text" name="email" size="30" value="'.$email.'" /></td> </tr> <tr> <td align="right"><b>Select Your Country:</b></td><td align="left">'; $select = '<select name="country" id="country">'."\r\n"; foreach($countries_list as $key => $value) { $select .= "\t".'<option value="'.$key.'">' . $value.'</option>'."\r\n"; } $select .= '</select>'; echo $select; ################################################################## echo '<select name="country">'; foreach ($countries_list as $id => $Item) { $default = ($Item = $row['country'])?" selected ":""; echo "<option $default value='$id'>$Item</option>"; } echo '</select>'; ################################################################## echo '</td> <tr> <td align="right"><b>Your Selected Country:</b></td><td align="center">'; ## get the flag...//////////////////////////////////////////////////////////// $query3 = "SELECT * FROM `countries` WHERE `id`='$country'"; $result3 = mysql_query($query3); $row = mysql_fetch_array($result3); $flag_pic = $row['flagpic']; echo '<img src="flags/'.$flag_pic.'">'; ## get the flag...//////////////////////////////////////////////////////////// echo ' </td> </tr> <tr> <td align="right"><b>Current Avatar:</b></td><td align="center">'; // an if incase theres no avatar.../////////////////////////////////////////// if(empty($avatar)) { echo "<img src=\"images/default_avatar.gif\" />"; } else { echo "<img src=\"avatars/$avatar\" />"; } echo ' </td> </tr> <tr> <td align="right"><b>Upload An Avatar:</b></td> <td align="left"><a href="upload_avatar.php">Upload Avatar</a></td> </tr> <tr> <td align="right"><b>Change Your Password:</b></td> <td align="left"><a href="change_password.php">Change Password</a></td> </tr> <tr> <td align="right"><b>Write About You:</b></td> <td align="left"><a href="about_me.php">About Me</a></td> </tr> <tr> <td align="right"><b>Your Friends List:</b></td> <td align="left"><a href="friends_list.php">Friends</a></td> </tr> <tr> <td align="right"><b>Your Uploaded Files:</b></td> <td align="left"><a href="my_files.php">My Files</a></td> </tr> <tr> <td align="right"><b>Your Requests:</b></td> <td align="left"><a href="my_requests.php">My Requests</a></td> </tr> <tr> <td colspan="2" bgcolor="#004E98" align="right"><input type="submit" name="submit" value="Update Details >>>"> </td> </tr> </table></form><br />'; // if the user is a site admin echo a new table out........................... if($user_class == "Site Administrator") { echo '<br /> <table width="400" border="0" bordercolor="#000000" cellspacing="0" cellpadding="3" /> <th colspan="2" bgcolor="#004E98"><font color="#ffffff">>>>>>Admin Options<<<<<</font></th> <tr> <td align="center"><a href="mass_pm.php">Mass PM All Users</a></td> </tr> <tr> <td bgcolor="#004E98" colspan="2"><font color="#ffffff"><b>>>>>>Admin Options<<<<<</b></font></td> </tr> </table><br />'; } } // end the isset...//////////////////////////////////////////////////////////////////// ?> so the drop down box displays: 73 73 73 73 etc <-- the number corresponding to the country but not the country name as such. thanks for your help guys Graham Quote Link to comment https://forums.phpfreaks.com/topic/53892-echoing-users-data-from-mysql/#findComment-266551 Share on other sites More sharing options...
MadTechie Posted June 1, 2007 Share Posted June 1, 2007 change $default = ($Item = $row['country'])?" selected ":""; to $default = ($Item == $row['country'])?" selected ":""; Quote Link to comment https://forums.phpfreaks.com/topic/53892-echoing-users-data-from-mysql/#findComment-266555 Share on other sites More sharing options...
graham23s Posted June 1, 2007 Author Share Posted June 1, 2007 Hi MT, that works good, it updates the country like it should, the only thing is if i login/logout or goto another page then go back again, the drop down box doesn't remember the last country i selected it automatically goes to the very first one "Afganistan" is there anyway i can fix this? once i get the code down i can appy it to other boxes in the future. thanks for your time guys Graham Quote Link to comment https://forums.phpfreaks.com/topic/53892-echoing-users-data-from-mysql/#findComment-266560 Share on other sites More sharing options...
MadTechie Posted June 1, 2007 Share Posted June 1, 2007 Humm, it should be ok but try this revised script <?php // A 2 dimensional array with the countries...////////////////////////////////////// $countries_list = array( 54 => 'Afganistan', 65 => 'Albania', 35 => 'Algeria', 68 => 'Andorra', 36 => 'Angola', 35 => 'Algeria', 89 => 'Antigua Barbuda', 20 => 'Australia', 37 => 'Austria', 82 => 'Bahamas', 86 => 'Bangladesh', 85 => 'Barbados', 16 => 'Belgium', 34 => 'Belize', 67 => 'Bosnia Herzegovina', 18 => 'Brazil', 104 => 'Bulgaria', 60 => 'Burkina Faso', 82 => 'Bahamas', 5 => 'Canada', 51 => 'Chile', 8 => 'China', 99 => 'Colombia', 102 => 'Costa Rica', 53 => 'Congo', 97 => 'Croatia', 52 => 'Cuba', 46 => 'Czech Republic', 10 => 'Denmark', 41 => 'Dominican Republic', 103 => 'Egypt', 98 => 'Estonia', 14 => 'Finland', 6 => 'France', 7 => 'Germany', 42 => 'Greece', 43 => 'Guatemala', 33 => 'Hong Kong', 79 => 'Honduras', 74 => 'Hungary', 62 => 'Iceland', 70 => 'India', 44 => 'Israel', 9 => 'Italy', 13 => 'Ireland', 31 => 'Jamaica', 17 => 'Japan', 58 => 'Kiribati', 87 => 'Laos', 101 => 'Latvia', 100 => 'Lebanon', 69 => 'Lithuania', 32 => 'Luxembourg', 40 => 'Malaysia', 25 => 'Mexico', 63 => 'Nauru', 15 => 'Netherlands', 71 => 'Netherlands Antilles', 21 => 'New Zealand', 61 => 'Nigeria', 96 => 'North Korea', 11 => 'Norway', 45 => 'Pakistan', 90 => 'Paraguay', 83 => 'Peru', 59 => 'Philippines', 14 => 'Poland', 24 => 'Portugal', 50 => 'Puerto Rico', 75 => 'Romania', 3 => 'Russia', 47 => 'Serbia', 48 => 'Seychelles', 200 => 'Scotland', 94 => 'Senegal', 26 => 'Singapore', 64 => 'Slovenia', 29 => 'South Africa', 30 => 'South Korea', 23 => 'Spain', 1 => 'Sweden', 57 => 'Switzerland', 49 => 'Taiwan', 93 => 'Thailand', 50 => 'Togo', 78 => 'Trinidad & Tobago', 55 => 'Turkey', 55 => 'Ukraine', 12 => 'United Kingdom', 2 => 'United States of America', 88 => 'Uruguay', 56 => 'Uzbekistan', 73 => 'Venezuela', 77 => 'Vietnam', 39 => 'Western Samoa', 38 => 'Yugoslavia', ); ?> <?php // Get the users details from mysql...////////////////////////////////////////////// $query1 = "SELECT * FROM `membership` WHERE `username`='$member'"; $result1 = mysql_query($query1) or die (mysql_error()); $row = mysql_fetch_array($result1); $id = $row['id']; $username = $row['username']; $email = $row['email']; $country = $row['country']; $avatar = $row['avatar']; $user_class = $row['user_class']; $ban = $row['ban']; // if the user is banned no further...////////////////////////////////////////////// if($ban == "Y") { echo "<br /><b><h4>This Account Has Been Disabled</h4></b>"; echo "<meta http-equiv=\"refresh\" content=\"1;URL=login.php\">"; include("includes/footer.php"); exit; } // check if the user has messages in there inbox...///////////////////////////////// $query_msg = "SELECT * FROM `pms` WHERE `reciever_id`='$id'"; $result_msg = mysql_query($query_msg) or die (mysql_error()); if (mysql_num_rows($result_msg) > 0) { echo '<br /> <table border="1" bordercolor="#000000" cellspacing="0" cellpadding="10" bgcolor="red"> <tr> <td style="padding: 10px; background: red"> <strong><a class="msg_inbox" href="inbox.php">You Have Messages In Your Inbox</a> </td> </tr> </table>'; } // the POST data...///////////////////////////////////////////////////////////////// $email_updated = $_POST['email']; $country_updated = $_POST['country']; $country = $country_updated; // isset...///////////////////////////////////////////////////////////////////////// if(isset($_POST['submit'])) { // a query to update mysql.../////////////////////////////////////////////////////// $query2 = "UPDATE `membership` SET `email`='$email_updated',`country`='$country_updated' WHERE `username`='$member'"; $result2 = mysql_query($query2) or die (mysql_error()); if($result2) { echo "<br /><b>Profile Updated Successfully!</b><br /><br />"; } } else { // get the id...//////////////////////////////////////////////////////////////////// echo "<br /><h3><p align=\"center\"> Hi, <a href=\"user_details.php?id=$id\">$username</p></a></h3>"; echo '<br /> <table width="400" border="1" bordercolor="#000000" cellspacing="0" cellpadding="3" /> <form action="my_account.php" method="POST" /> <th colspan="2" bgcolor="#004E98"><font color="#ffffff">Edit Your Profile</font></th> <tr> <td align="right"><b>Your E-Mail:</b></td><td align="left"><input type="text" name="email" size="30" value="'.$email.'" /></td> </tr> <tr> <td align="right"><b>Select Your Country:</b></td><td align="left">'; $select = '<select name="country" id="country">'."\r\n"; foreach($countries_list as $key => $value) { $select .= "\t".'<option value="'.$key.'">' . $value.'</option>'."\r\n"; } $select .= '</select>'; echo $select; ################################################################## echo '<select name="country">'; foreach ($countries_list as $id => $Item) { $default = ($Item == $country)?" selected ":""; echo "<option $default value='$id'>$Item</option>"; } echo '</select>'; ################################################################## echo '</td> <tr> <td align="right"><b>Your Selected Country:</b></td><td align="center">'; ## get the flag...//////////////////////////////////////////////////////////// $query3 = "SELECT * FROM `countries` WHERE `id`='$country'"; $result3 = mysql_query($query3); $row = mysql_fetch_array($result3); $flag_pic = $row['flagpic']; echo '<img src="flags/'.$flag_pic.'">'; ## get the flag...//////////////////////////////////////////////////////////// echo ' </td> </tr> <tr> <td align="right"><b>Current Avatar:</b></td><td align="center">'; // an if incase theres no avatar.../////////////////////////////////////////// if(empty($avatar)) { echo "<img src=\"images/default_avatar.gif\" />"; } else { echo "<img src=\"avatars/$avatar\" />"; } echo ' </td> </tr> <tr> <td align="right"><b>Upload An Avatar:</b></td> <td align="left"><a href="upload_avatar.php">Upload Avatar</a></td> </tr> <tr> <td align="right"><b>Change Your Password:</b></td> <td align="left"><a href="change_password.php">Change Password</a></td> </tr> <tr> <td align="right"><b>Write About You:</b></td> <td align="left"><a href="about_me.php">About Me</a></td> </tr> <tr> <td align="right"><b>Your Friends List:</b></td> <td align="left"><a href="friends_list.php">Friends</a></td> </tr> <tr> <td align="right"><b>Your Uploaded Files:</b></td> <td align="left"><a href="my_files.php">My Files</a></td> </tr> <tr> <td align="right"><b>Your Requests:</b></td> <td align="left"><a href="my_requests.php">My Requests</a></td> </tr> <tr> <td colspan="2" bgcolor="#004E98" align="right"><input type="submit" name="submit" value="Update Details >>>"> </td> </tr> </table></form><br />'; // if the user is a site admin echo a new table out........................... if($user_class == "Site Administrator") { echo '<br /> <table width="400" border="0" bordercolor="#000000" cellspacing="0" cellpadding="3" /> <th colspan="2" bgcolor="#004E98"><font color="#ffffff">>>>>>Admin Options<<<<<</font></th> <tr> <td align="center"><a href="mass_pm.php">Mass PM All Users</a></td> </tr> <tr> <td bgcolor="#004E98" colspan="2"><font color="#ffffff"><b>>>>>>Admin Options<<<<<</b></font></td> </tr> </table><br />'; } } // end the isset...//////////////////////////////////////////////////////////////////// ?> Quote Link to comment https://forums.phpfreaks.com/topic/53892-echoing-users-data-from-mysql/#findComment-266574 Share on other sites More sharing options...
graham23s Posted June 1, 2007 Author Share Posted June 1, 2007 Hi MT, it doesn't seem to be updating mysql with the new country, it's weird it looks ok i can't see where the error lies. cheers Graham Quote Link to comment https://forums.phpfreaks.com/topic/53892-echoing-users-data-from-mysql/#findComment-266578 Share on other sites More sharing options...
MadTechie Posted June 2, 2007 Share Posted June 2, 2007 try this $query2 = "UPDATE `membership` SET `email`='$email_updated',`country`='$country_updated' WHERE `username`='$username'"; $result2 = mysql_query($query2) or die (mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/53892-echoing-users-data-from-mysql/#findComment-266631 Share on other sites More sharing options...
graham23s Posted June 2, 2007 Author Share Posted June 2, 2007 Thanks MT works perfect:) Graham Quote Link to comment https://forums.phpfreaks.com/topic/53892-echoing-users-data-from-mysql/#findComment-267010 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.