slenderman Posted October 4, 2014 Share Posted October 4, 2014 Hi Good day Can someone pls help me regarding with this matter.I have a page for inserting records. And I have a combobox that is fetched through the specific field in database. I also have a table of records, for each row i have an href edit which throws the values to its respective texbox and combobox. THE PROBLEM is that when i click the href edit, the value from a selected row was not displayed from the combobox but with the textboxes it is working except from the combobox which is already fetched from the database.------> HERE IS THE CODE FOR COMBOBOX <select name="cbocourse" style="height:35px; width:280px; background-color:#923227; box-shadow:1px 1px #FFF;color:#C90;"> <?php $sql = 'SELECT * from tblcourse'; $query = mysql_query($sql); while($row = mysql_fetch_array($query)) { echo '<option value= "' . $row['id']. '">' . $row['course'] . '</option>'; } ?> <?php if(@$courseid ==5){ echo "<option value = '5'>Associate in Computer Technology/option>"; } ?> ------> CODE FOR TABLE OF RECORDSdo{ echo '<tr>'; echo "<td><a href = 'fastormain.php?id=".$result['id']."'onclick=\" disableupdatebtn();\"> ". '<center>' . "edit" . '<center>' ."</a></td>"; echo "<td><a href = 'fastormain.php?id=".$result['id']."&action='delete' onClick=\"return confirm('Do you want to delete this record?')\">". '<center>'."delete" . '</center>'."</a></td>"; echo '<td>' . $result['id'] .'</td>'; echo '<td>' . $result['Name'] .'</td>'; echo '<td>' . $result['gender'] .'</td>'; echo '<td>' . $result['birthday'] .'</td>'; echo '<td>' . $result['address'] .'</td>'; echo '<td>' . $result['contactnumber'] .'</td>'; echo '<td>' . $result['course'] .'</td>'; echo '<td>' . $result['branch'] .'</td>'; echo '<td>' . $result['YEAR'] .'</td>'; echo '<td>' . $result['imagename'] .'</td>'; echo '</tr>'; $result= mysql_fetch_assoc($query); } while($result);{ ----------------> AND THE CODE FOR GETTING THE ID IN HREF ?php if(isset($_GET['id'])) { $id = $_GET['id']; $sql = "select * from tblcontestants where id = {$id} limit 1"; $query = @mysql_query($sql,$connection) or die("DATABASE QUERY FAILED"); $result = mysql_fetch_assoc($query); $id=$result['id']; $fname=$result['fname']; $mname=$result['mname']; $lname=$result['lname']; $age=$result['age']; $genderid=$result['genderid']; $birthday=$result['birthday']; $address=$result['address']; $contactnumber=$result['contactnumber']; $courseid=$result['courseid']; $branchid=$result['branchid']; $yearid=$result['yearid']; $imagename=$result['imagename']; }; ?>I KNOW THAT SOMEONE CAN HELP ME! SO PLEASEEEEEEE! IT WILL BE HIGHLY APPRECIATED Quote Link to comment https://forums.phpfreaks.com/topic/291432-how-to-populate-combo-box-that-is-already-fetched-on-a-database-field-on-a-selected-row-in-table/ Share on other sites More sharing options...
Ch0cu3r Posted October 4, 2014 Share Posted October 4, 2014 (edited) You need to apply the selected attribute to the current option if the value stored in the database matches it. Example test code <select name="option"> <?php // values in the drop down $options = array('apple', 'banana', 'orange', 'peach'); // the value stored in the database $valueInDB = 'orange'; foreach($options as $option) { // if the current option matches the value from the database apply the selected attribute $selected = $option == $valueInDB ? ' selected="selected"' : ''; echo '<option value="'.$option.'"'.$selected.'>'.$option.'</option>'; } ?> </select> NOTE when you are posting code please wrap it within tags. Also do not use CAPS this. Edited October 4, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/291432-how-to-populate-combo-box-that-is-already-fetched-on-a-database-field-on-a-selected-row-in-table/#findComment-1492691 Share on other sites More sharing options...
slenderman Posted October 4, 2014 Author Share Posted October 4, 2014 do i need to replaced this code <?php $sql = 'SELECT * from tblcourse'; $query = mysql_query($sql); while($row = mysql_fetch_array($query)) { echo '<option value= "' . $row['id']. '">' . $row['course'] . '</option>'; } ?> <?php if(@$courseid ==5){ echo "<option value = '5'>Associate in Computer Technology/option>"; } ?> to that code? Quote Link to comment https://forums.phpfreaks.com/topic/291432-how-to-populate-combo-box-that-is-already-fetched-on-a-database-field-on-a-selected-row-in-table/#findComment-1492694 Share on other sites More sharing options...
Ch0cu3r Posted October 4, 2014 Share Posted October 4, 2014 No I have only posted example code which shows how to set a matching value as selected in the dropdown menu. Do you understand what I posted? Quote Link to comment https://forums.phpfreaks.com/topic/291432-how-to-populate-combo-box-that-is-already-fetched-on-a-database-field-on-a-selected-row-in-table/#findComment-1492695 Share on other sites More sharing options...
slenderman Posted October 4, 2014 Author Share Posted October 4, 2014 (edited) What if i want to have a dynamic item fetched in the database, how to do it? without setting apple, banana etc Edited October 4, 2014 by slenderman Quote Link to comment https://forums.phpfreaks.com/topic/291432-how-to-populate-combo-box-that-is-already-fetched-on-a-database-field-on-a-selected-row-in-table/#findComment-1492697 Share on other sites More sharing options...
Frank_b Posted October 4, 2014 Share Posted October 4, 2014 (edited) <?php function buildOptions($tablename, $columnname, $selectedId = 0) { $sql = 'SELECT id, '.$columnnmame.' FROM '.$tablename; $html = ''; $query = mysql_query($sql); while($row = mysql_fetch_assoc($query)) // use mysql_fetch_assoc! { $selected = ''; if($row['id'] == $selectedId) $selected = ' selected="selected"'; $html .= '<option'.$selected.' value= "' . $row['id']. '">' . $row[$columnnmame] . '</option>' . "\n"; } return $html; } $userId = 0; if($_SERVER['REQUEST_METHOD'] == 'POST') { $userId = $_POST['user']; } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Example</title> </head> <body> <form action="" method="post"> <select name="user"> <?php echo buildOptions('users', 'name', $userId); ?> </select> </form> </body> </html> Edited October 4, 2014 by Frank_b Quote Link to comment https://forums.phpfreaks.com/topic/291432-how-to-populate-combo-box-that-is-already-fetched-on-a-database-field-on-a-selected-row-in-table/#findComment-1492698 Share on other sites More sharing options...
slenderman Posted October 4, 2014 Author Share Posted October 4, 2014 i tried to apply the algo but it says undefined $columname where and how can i define it? Quote Link to comment https://forums.phpfreaks.com/topic/291432-how-to-populate-combo-box-that-is-already-fetched-on-a-database-field-on-a-selected-row-in-table/#findComment-1492699 Share on other sites More sharing options...
slenderman Posted October 4, 2014 Author Share Posted October 4, 2014 hey guys i already resolved it however, it is staticheres the code <?php $CATEGORY = 5; //from DB table, consider 3 as category id for sample $sql="SELECT tblcourse.id as id, tblcourse.course as course FROM tblcourse"; $result=mysql_query($sql) or die(mysql_error()); $options=""; while ($row=mysql_fetch_assoc($result)) { $id=$row["id"]; $thing=$row["course"]; $isSel = ($CATEGORY == $id)?"selected":''; $options.= " <OPTION VALUE='$id' $isSel>$thing</option>"; } ?>the $CATEGORY is set into 5.. How to set it dynamically so that whatever i selected it populate the combobox with its desired id? Quote Link to comment https://forums.phpfreaks.com/topic/291432-how-to-populate-combo-box-that-is-already-fetched-on-a-database-field-on-a-selected-row-in-table/#findComment-1492701 Share on other sites More sharing options...
Frank_b Posted October 4, 2014 Share Posted October 4, 2014 appologizes there was a typo :-) <?php function buildOptions($tablename, $columnname, $selectedId = 0) { $sql = 'SELECT id, '.$columnname.' FROM '.$tablename; $html = ''; $query = mysql_query($sql); while($row = mysql_fetch_assoc($query)) // use mysql_fetch_assoc! { $selected = ''; if($row['id'] == $selectedId) $selected = ' selected="selected"'; $html .= '<option'.$selected.' value= "' . $row['id']. '">' . $row[$columnname] . '</option>' . "\n"; } return $html; } $userId = 0; if($_SERVER['REQUEST_METHOD'] == 'POST') { $userId = $_POST['user']; } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Example</title> </head> <body> <form action="" method="post"> <select name="user"> <?php echo buildOptions('users', 'name', $userId); ?> </select> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/291432-how-to-populate-combo-box-that-is-already-fetched-on-a-database-field-on-a-selected-row-in-table/#findComment-1492702 Share on other sites More sharing options...
slenderman Posted October 4, 2014 Author Share Posted October 4, 2014 how can i put the $CATEGORY dynamically so that whatever i click on the table it will retrieved in the combo box? (without settng its id to any number like 5 ) <?php $CATEGORY = 5; //from DB table, consider 3 as category id for sample $sql="SELECT tblcourse.id as id, tblcourse.course as course FROM tblcourse"; $result=mysql_query($sql) or die(mysql_error()); $options=""; while ($row=mysql_fetch_assoc($result)) { $id=$row["id"]; $thing=$row["course"]; $isSel = ($CATEGORY == $id)?"selected":''; $options.= " <OPTION VALUE='$id' $isSel>$thing</option>"; } ?> My Combobox form code <select name="cbocourse" style="height:35px; width:280px; background-color:#923227; box-shadow:1px 1px #FFF;color:#C90;" onClick="submitCATEGORY();"> <option value="<?php echo $CATEGORY; ?>"> <?php echo $options;?></option></select> Quote Link to comment https://forums.phpfreaks.com/topic/291432-how-to-populate-combo-box-that-is-already-fetched-on-a-database-field-on-a-selected-row-in-table/#findComment-1492705 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.