Jump to content

How to populate combo box (that is already fetched on a database field) on a selected row in table.


slenderman

Recommended Posts

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 RECORDS

do{
 
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 :)

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.

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?


<?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>

 

hey guys i already resolved it however,  it is static
heres 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?

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>

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.