Slowie Posted February 7, 2012 Share Posted February 7, 2012 hey guys just a quick one i want a drop down box which is populated from a mysql query which is eay enough but also at the same time i also want the dropdown box to look at the current record and have the value in the drop down box e.g. the drop down has values 1, 2, 3, 4, 5 in it which are pulled from a table called values the current record is from a table called staff and it has an id of 45 and a user level of 3 i want the drop down to have all the values selectable to update the record but on load it will have 3 as the current, i want to be able to chage this 3 to a 5 and update the record how can i do this.? just a brief code example would be awsome and ill adopt it to fit Quote Link to comment https://forums.phpfreaks.com/topic/256601-drop-down-box-from-mysql/ Share on other sites More sharing options...
spiderwell Posted February 7, 2012 Share Posted February 7, 2012 <select id="user_groupid" name="user_groupid"> <option value="0">Choose one</option> <? foreach ($arrUserGroups as $arrGroup) { echo '<option value="' . $arrGroup['group_id'] . '"'; if($arrUser['user_groupid'] == $arrGroup['group_id']) echo ' selected '; echo '>'; echo $arrGroup['group_name']; echo '</option>'; } ?> </select> this should point you in the right direction, i have in this case already filled arrUserGroup with values from my database Quote Link to comment https://forums.phpfreaks.com/topic/256601-drop-down-box-from-mysql/#findComment-1315450 Share on other sites More sharing options...
Slowie Posted February 7, 2012 Author Share Posted February 7, 2012 im still having trouble could someone help please my code so far looks like this but its not working as i want $sql="SELECT stafflist.id, stafflist.full_name, stafflist.branch, branch.branchid, branch.branch as branchname FROM stafflist left Join branch on stafflist.company=branch.companybranch WHERE id = '".$staffid."'"; $result=mysql_query($sql); $sql1="SELECT * FROM branch WHERE companybranch = '".$company."'"; $result1=mysql_query($sql1); <select id="userlevel" name="userlevel"> <option value="0">Choose one</option> <? $userbranch = mysql_fetch_array($result); $branchid = mysql_fetch_array($result1); echo $userbranch; echo $branchid; foreach ($userbranch as $branchid) { echo '<option value="' . $branchid['branchid'] . '"'; if($userbranch['branch'] == $branchid['branchid']) echo ' selected '; echo '>'; echo $branchid['branch']; echo '</option>'; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/256601-drop-down-box-from-mysql/#findComment-1315480 Share on other sites More sharing options...
spiderwell Posted February 7, 2012 Share Posted February 7, 2012 Ok first of all theres no looping of recordset to fill an array to make the dropdown, so i have added that and fills an array with that info, i also moved things around a bit to make more sense for you, also i havent checked for typos or syntax, so you might need to do that $sql="SELECT stafflist.id, stafflist.full_name, stafflist.branch, branch.branchid, branch.branch as branchname FROM stafflist left Join branch on stafflist.company=branch.companybranch WHERE id = '".$staffid."'"; $result=mysql_query($sql); $userbranch = mysql_fetch_array($result); $sql1="SELECT * FROM branch WHERE companybranch = '".$company."'"; $result1=mysql_query($sql1); while ($row = mysql_fetch_array($result1)) { $arrBranches = $row //this is putting each row from database into array to use later } ?> <select id="userlevel" name="userlevel"> <option value="0">Choose one</option> <? foreach ($arrBranches as $branch)//for each array in the array of branches { echo '<option value="' . $branch['branchid'] . '"'; if($userbranch['branch'] == $branch['branchid']) echo ' selected '; echo '>'; echo $branch['branch']; echo '</option>'; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/256601-drop-down-box-from-mysql/#findComment-1315492 Share on other sites More sharing options...
Slowie Posted February 7, 2012 Author Share Posted February 7, 2012 thank you so much i will try this now Quote Link to comment https://forums.phpfreaks.com/topic/256601-drop-down-box-from-mysql/#findComment-1315498 Share on other sites More sharing options...
Slowie Posted February 7, 2012 Author Share Posted February 7, 2012 its not working but it is at the same time. its getting some random fields in this order is 2,2,P,P,2,2 when it should be getting the info from the branch table. my table looks like this branchid branch companybranch 1 test 1 2 test2 1 1 test3 2 and heres the code to my page now <?php $path = $_SERVER['DOCUMENT_ROOT']; $path .= "/Apollo/dbc.php"; include_once($path); page_protect(); company(); @extract($_GET); $staffid=$id; $companyid=$company; $sql="SELECT stafflist.id, stafflist.full_name, stafflist.branch, branch.branchid, branch.branch as branchname FROM stafflist left Join branch on stafflist.company=branch.companybranch WHERE id = '".$staffid."'"; $result=mysql_query($sql); $userbranch = mysql_fetch_array($result); $sql1="SELECT * FROM branch WHERE companybranch = '".$company."'"; $result1=mysql_query($sql1); while ($row = mysql_fetch_array($result1)){ $arrBranches = $row; //this is putting each row from database into array to use later } ?> <html> <head> <title>Book Off Holiday</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script src="/Holidays/php_calendar/scripts.js" type="text/javascript"></script> <?php Header1(); ?> </head> <body> <div class="content1"> <form name "searchform" action="/Admin/EditUsers.php" method="post"> <table width="950px" border="0" align="center" cellpadding="2" cellspacing="0"> <tr class="mytables"> <td width="4%" class="title_cell"><strong>ID</strong></td> <td width="4%" class="title_cell"> <strong>Access</strong></td> <td width="10%" class="title_cell"><strong>User Name</strong></div></td> <td width="24%" class="title_cell"><strong>Email</strong></td> <td width="10%" class="title_cell"><strong>Approval</strong></td> <td width="10%" class="title_cell"> <strong>Banned</strong></td> <td width="15%" class="title_cell"> </td> </tr> <tr> <td> </td> <td width="10%"> </td> <td width="17%"><div align="center"></div></td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <?php while ($rrows = mysql_fetch_array($result)) {?> <tr> <td><input name="u[]" type="checkbox" value="<?php echo $rrows['id']; ?>" id="u[]"></td> <td><select id="userlevel" name="userlevel"> <option value="0">Choose one</option> <?php foreach ($arrBranches as $branch)//for each array in the array of branches { echo '<option value="' . $branch['branchid'] . '"'; if($userbranch['branch'] == $branch['branchid']) echo ' selected '; echo '>'; echo $branch['branchid']; echo '</option>'; } ?> </select></td> <td> <span id="accesslevel<?php echo $rrows['id']; ?>"> <?php if($rrows['user_level'] == 0) echo "Guest"; if($rrows['user_level'] == 1) echo "User"; if($rrows['user_level'] == 2) echo "Manager"; if($rrows['user_level'] == 3) echo "Area Manager"; if($rrows['user_level'] == 4) echo "Branch Owner"; if($rrows['user_level'] == 5) echo "Admin"; if($rrows['user_level'] == 10) echo "Master Admin"; ?> </span> </td> <td><?php echo $rrows['user_name'];?></div></td> <td><?php echo $rrows['user_email']; ?></td> <td> <span id="approve<?php echo $rrows['id']; ?>"> <?php if(!$rrows['approved']) { echo "Pending"; } else {echo "Active"; }?> </span> </td> <td><span id="ban<?php echo $rrows['id']; ?>"> <?php if(!$rrows['banned']) { echo "no"; } else {echo "yes"; }?> </span> </td> </tr> <?php } ?> </table> </form> </div> </body> Quote Link to comment https://forums.phpfreaks.com/topic/256601-drop-down-box-from-mysql/#findComment-1315514 Share on other sites More sharing options...
spiderwell Posted February 7, 2012 Share Posted February 7, 2012 ah i think its my mistake for missing out one tiny bit but important bit. change this $arrBranches to $arrBranches[] when looping through the database the difference is tiny but crucual Quote Link to comment https://forums.phpfreaks.com/topic/256601-drop-down-box-from-mysql/#findComment-1315562 Share on other sites More sharing options...
Slowie Posted February 8, 2012 Author Share Posted February 8, 2012 thank you so much it worked like a dream. Quote Link to comment https://forums.phpfreaks.com/topic/256601-drop-down-box-from-mysql/#findComment-1315859 Share on other sites More sharing options...
Slowie Posted February 8, 2012 Author Share Posted February 8, 2012 ive just noticed that if i add another branch then it dupicates my data onto however many tables e.g. table1 table 2 table 3 if i add 2 more branches what would cause this? Quote Link to comment https://forums.phpfreaks.com/topic/256601-drop-down-box-from-mysql/#findComment-1315891 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.