Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,175 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,207 people online right now. Registration is fast and FREE... Join Now!




Using a Dropdown box with values from mysql database

 
Reply to this topicStart new topic

Using a Dropdown box with values from mysql database

scottyadam
23 Aug, 2008 - 02:21 PM
Post #1

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 49


My Contributions
Here is the problem...I have a page called add_user.php and from there I naturally add different users (with info) to a database called smw_members. What I want to have on the edit_user.php page is a dropdown box and like I have shown below it will contain all the access values that you could make the user. When I am editing a specific user I want THEIR level to be the default selected pull down.

$query = "SELECT member_id, usr_lvl ".
"FROM smw_members ".
"WHERE member_id = '{$_GET['member_id']}'";
$result = mysql_query($query) or die('Error : ' . mysql_error());
list($member_id, $usr_lvl) = mysql_fetch_array($result, MYSQL_NUM);

<select name="usr_lvl">
<option value="" selected="selected">Please Select Level</option>
<option if ($usr_lvl==l") selected="selected" value="1" >Level 1 [Limited User]</option>
<option if ($usr_lvl==2") selected="selected" value="2" >Level 2 [Normal User]</option>
<option if ($usr_lvl==3") selected="selected" value="3" >Level 3 [Company Admin]</option>
<option if ($usr_lvl==4") selected="selected" value="4" >Level 4 [Limited SU]</option>
<option if ($usr_lvl==5") selected="selected" value="5" >Level 5 [Super User]</option>
</select>

See anything wrong with the little code that I provided.
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: Using A Dropdown Box With Values From Mysql Database
23 Aug, 2008 - 03:00 PM
Post #2

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 481



Thanked: 32 times
My Contributions
I can't test this right now, but it should be close to what you're trying to do:
CODE
<?php
$level = array ("none", "Limited User","Normal User", "Company Admin","Limited SU", "Super User");
$result = mysql_query($query) or die('Error : ' . mysql_error());
list($member_id, $usr_lvl) = mysql_fetch_array($result, MYSQL_NUM);
?>
<form action="<?php echo $php_self ?>" method="post">
<select name="member" onchange="this.form.submit();">
<?php
for ( $i = 1; $i <= $5; $i += 1) {
unset($ch1);
if ($usr_lvl== $i) {
$ch1="selected=\"selected\"";
}
?>
<option name="member" value="<?php print $i ?>" "<?php print $ch1 ?>" ><?php echo $level[$i]; ?>
<?php
}
?>
</select>
</option>


This post has been edited by CTphpnwb: 23 Aug, 2008 - 03:03 PM
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: Using A Dropdown Box With Values From Mysql Database
23 Aug, 2008 - 04:04 PM
Post #3

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
QUOTE
$query = "SELECT member_id, usr_lvl ".
"FROM smw_members ".
"WHERE member_id = '{$_GET['member_id']}'";
$result = mysql_query($query) or die('Error : ' . mysql_error());
list($member_id, $usr_lvl) = mysql_fetch_array($result, MYSQL_NUM);

<select name="usr_lvl">
<option value="" selected="selected">Please Select Level</option>
<option if ($usr_lvl==l") selected="selected" value="1" >Level 1 [Limited User]</option>
<option if ($usr_lvl==2") selected="selected" value="2" >Level 2 [Normal User]</option>
<option if ($usr_lvl==3") selected="selected" value="3" >Level 3 [Company Admin]</option>
<option if ($usr_lvl==4") selected="selected" value="4" >Level 4 [Limited SU]</option>
<option if ($usr_lvl==5") selected="selected" value="5" >Level 5 [Super User]</option>
</select>

See anything wrong with the little code that I provided.


there's a lot wrong =/
1. never use $_GET directly in a query, someone can easily delete your database...
2. For the if statements, you're not opening and closing php....
3. PHp tags NEED to end before you start putting HTML, otherwise you'll get a white page...


also, CTphpnwb, you should be right, but you shopuld use $i++ instead of +=1.... just shorter =p
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Using A Dropdown Box With Values From Mysql Database
23 Aug, 2008 - 04:40 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Scotty, both CTphpnwb and JBrace have the right ideas. Create a loop which tests against the $user_lvl and if they are equal, have it insert the selected="selected" right into the option. That way when it is fully printed to the browser one of the choices will have the selected clause and select that option as the default.

Also don't forget to actually close the option tags and provide option text.

CTphpnwb, don't forget your semicolons in your print statements and the double quotes around the second <?php ?> tags are not needed...

CODE

<option name="member" value="<?php print $i ?>" "<?php print $ch1 ?>" ><?php echo $level[$i]; ?>

// Should be (notice closing of option tag too with the closing bracket)
<option name="member" value="<?php print $i; ?>" <?php print $ch1; ?> ><?php echo $level[$i]; ?>>


Just some friendly tips for you.

Good call CT and JBrace. icon_up.gif smile.gif

This post has been edited by Martyr2: 23 Aug, 2008 - 04:43 PM
User is offlineProfile CardPM
+Quote Post

scottyadam
RE: Using A Dropdown Box With Values From Mysql Database
25 Aug, 2008 - 06:59 AM
Post #5

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 49


My Contributions
Ok...I don't know how to integrate this into my code that I have.

CODE
        <form name="reg_company" method="post" action="edit_user.php">
         <table width="100%" border="0" align="left" cellpadding="2" cellspacing="0">
          <tr>
           <td colspan="2" align="left">
            <b>Contact Information</b><br><hr align="left" width="175">
           </td>
          </tr>
          <tr>
           <td width="30%">First Name</td>
           <td width="70%"><input name="firstname" type="text" value="<?=$firstname;?>"></td>
          </tr>
          <tr>
           <td width="30%">Last Name</font> </td>
           <td width="70%"><input name="lastname" type="text" value="<?=$lastname;?>"></td>
          </tr>
          <tr>
           <td width="30%">Email</font></td>
           <td width="70%"><input name="email" type="text" value="<?=$email;?>"></td>
          </tr>
          <tr>
           <td colspan="2" align="left">
            <br><b>Technical Information</b><hr align="left" width="175">
           </td>
          </tr>
          <tr>
           <td width="30%">Company Name</font> </td>
           <td width="70%"><input name="company" type="text" value="<?=$company;?>"></td>
          </tr>
          <tr>
           <td width="30%">Control Panel</td>
           <td width="70%"><input name="cpanel" type="text" value="<?=$cpanel;?>"></td>
          </tr>
          <tr>
           <td width="30%">OverRide Member ID</td>
           <td width="70%"><input name="member_id" type="text" value="<?=$member_id;?>"></td>
          </tr>
          <tr>
           <td colspan="2" align="left">
CODE

            <br><b>Security Information</b><hr align="left" width="175">
           </td>
          </tr>
          <tr>
           <td width="30%">Username</font></td>
           <td width="70%"><input name="usrname" type="text" value="<?=$usrname;?>"></td>
          </tr>
          <tr>
           <td width="30%">Access Level</font> </td>
           <td width="70%">
<select name="usr_lvl">
<option value="" selected="selected">Please Select Level</option>
<option <?php if ($usr_lvl==1)?>selected="selected" value="1" >Level 1 [Limited User]</option>
<option <?php if ($usr_lvl==2)?>selected="selected" value="2" >Level 2 [Normal User]</option>
<option <?php if ($usr_lvl==3)?>selected="selected" value="3" >Level 3 [Company Admin]</option>
<option <?php if ($usr_lvl==4)?>selected="selected" value="4" >Level 4 [Limited SU]</option>
<option <?php if ($usr_lvl==5)?>selected="selected" value="5" >Level 5 [Super User]</option>
</select>
           </td>
          </tr>
          <tr>
           <td width="170">&nbsp;</td>
           <td width="70%"><input type="submit" name="Submit" value="Modify User"></td>
          </tr>
         </table>
        </form>


That is was my original code and now trying to integrate this new code it confuses the heck out of me...so...here is my attempt how did I do:

CODE

        <form name="reg_company" method="post" action="edit_user.php">
         <table width="100%" border="0" align="left" cellpadding="2" cellspacing="0">
          <tr>
           <td colspan="2" align="left">
            <b>Contact Information</b><br><hr align="left" width="175">
           </td>
          </tr>
          <tr>
           <td width="30%">First Name</td>
           <td width="70%"><input name="firstname" type="text" value="<?=$firstname;?>"></td>
          </tr>
          <tr>
           <td width="30%">Last Name</font> </td>
           <td width="70%"><input name="lastname" type="text" value="<?=$lastname;?>"></td>
          </tr>
          <tr>
           <td width="30%">Email</font></td>
           <td width="70%"><input name="email" type="text" value="<?=$email;?>"></td>
          </tr>
          <tr>
           <td colspan="2" align="left">
            <br><b>Technical Information</b><hr align="left" width="175">
           </td>
          </tr>
          <tr>
           <td width="30%">Company Name</font> </td>
           <td width="70%"><input name="company" type="text" value="<?=$company;?>"></td>
          </tr>
          <tr>
           <td width="30%">Control Panel</td>
           <td width="70%"><input name="cpanel" type="text" value="<?=$cpanel;?>"></td>
          </tr>
          <tr>
           <td width="30%">OverRide Member ID</td>
           <td width="70%"><input name="member_id" type="text" value="<?=$member_id;?>"></td>
          </tr>
          <tr>
           <td colspan="2" align="left">
            <br><b>Security Information</b><hr align="left" width="175">
           </td>
          </tr>
          <tr>
           <td width="30%">Username</font></td>
           <td width="70%"><input name="usrname" type="text" value="<?=$usrname;?>"></td>
          </tr>
          <tr>
           <td width="30%">Access Level</font> </td>
           <td width="70%">
            <!--<input name="usr_lvl" type="text" value="<?//=$usr_lvl;?>">-->
<?php
$level = array ("none", "Limited User","Normal User", "Company Admin","Limited SU", "Super User");
list($member_id, $usr_lvl) = mysql_fetch_array($result, MYSQL_NUM);
?>
<select name="member" onchange="this.form.submit();">
<?php
for ( $i = 1; $i <= $5; $i += 1) {
unset($ch1);
if ($usr_lvl== $i) {
$ch1="selected=\"selected\"";
}
?>
<option name="member" value="<?php print $i; ?>" <?php print $ch1; ?> ><?php echo $level[$i]; ?>
<?php
}
?>
</select>
</option>
           </td>
          </tr>
          <tr>
           <td width="170">&nbsp;</td>
           <td width="70%"><input type="submit" name="Submit" value="Modify User"></td>
          </tr>
         </table>
        </form>


I basically copied it in there...yes I know but I don't really understand it which is missing the point of copying this code that you guys made for me...so first did my copy job come close because it is giving me errors and second could you give me a small explanation =)
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: Using A Dropdown Box With Values From Mysql Database
25 Aug, 2008 - 07:27 AM
Post #6

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 481



Thanked: 32 times
My Contributions
You're confusing php code with html formatting.

Look closer at my first response. Notice that there are blocks of code with html in between them:

<?php
.
. Code here
.
?>

<some html....
<some html....

<?php
.
. Code here
.
?>



The important part is the php code, because it generates the rest of the html that the browser will see.





User is offlineProfile CardPM
+Quote Post

scottyadam
RE: Using A Dropdown Box With Values From Mysql Database
25 Aug, 2008 - 07:55 AM
Post #7

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 49


My Contributions
I understand that fact of the matter but I keep getting this error with:

CODE

<?php
    require_once('auth.php');
    include 'config.php';

if(isset($_GET['member_id']))
{
    $query = "SELECT member_id, usr_lvl ".
             "FROM smw_members ".
             "WHERE member_id = '{$_GET['member_id']}'";
    $level = array ("none", "Limited User","Normal User", "Company Admin","Limited SU", "Super User");
    $result = mysql_query($query) or die('Error : ' . mysql_error());
    list($member_id, $usr_lvl) = mysql_fetch_array($result, MYSQL_NUM);
}
?>
<html>
<body>
<form action="<?php echo $php_self ?>" method="post">

<select name="member" onchange="this.form.submit();">
<?php
for ( $i = 1; $i <= $5; $i += 1) {
unset($ch1);
if ($usr_lvl== $i) {
$ch1="selected=\"selected\"";
}
?>
<option name="member" value="<?php print $i; ?>" <?php print $ch1; ?>><?php echo $level[$i]; ?>
<?php
}
?>
</select>
</option>
</body>
</html>


I took all the formating out of it like tables and fonts so that I could see it better...when I run this I get the error:
CODE
Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /home/scottyme/public_html/panel/test_edit.php on line 21


For the following line of code:

CODE
for ( $i = 1; $i <= $5; $i += 1) {

User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: Using A Dropdown Box With Values From Mysql Database
25 Aug, 2008 - 08:20 AM
Post #8

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 481



Thanked: 32 times
My Contributions
Oops!

Sorry, it shouldn't have a $ next to the 5:

for ( $i = 1; $i <= 5; $i += 1) {


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:33AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month