hi,i thought i'd fixed this prb,but i have'nt.i have a edit form with a drop down.i want the drop down to show the value that is saved in the databse along with the other values.ie,when i click the edit form,the value in the drop down that is visible should be the 1 that the databse stores.but it should also hold the other options.any ides how i can go about with it.i've dne the following

<?php
  $query=caseQuery("select designation from dsg");
echo "<select name='dsgn' >";
while($noticia = mysql_fetch_assoc($query)) { 
echo  "<option value='$noticia[designation]'>$noticia[designation]</option>";
}
echo "</select>";
?>

Recommended Answers

All 12 Replies

hi,i thought i'd fixed this prb,but i have'nt.i have a edit form with a drop down.i want the drop down to show the value that is saved in the databse along with the other values.ie,when i click the edit form,the value in the drop down that is visible should be the 1 that the databse stores.but it should also hold the other options.any ides how i can go about with it.i've dne the following

<?php
  $query=caseQuery("select designation from dsg");
echo "<select name='dsgn' >";
while($noticia = mysql_fetch_assoc($query)) { 
echo  "<option value='$noticia[designation]'>$noticia[designation]</option>";
}
echo "</select>";
?>

You need something to differentiate the selected value. In this example you are pulling all values from a specified table and displaying them all in a drop down, but how do you know which one is going to be the selected value?

Also, I see people doing this all the time. is "designation" a constant? if not, that should be in quotes. This is one of those things where php will let you do it but it is not a good habit to get into.

Thanx alot.i'l keep that in mind.but any idea how i can do the drop down thing?

Thanx alot.i'l keep that in mind.but any idea how i can do the drop down thing?

if "select designation from dsg" returns multiple rows, how do you know which is suppose to be selected. Is there some type of boolean field in the database to define which row is suppose to be the selected row or are you looping through another array to create the drop down and using one single row from the database to define the selected value? For example:

<?php
$query=caseQuery("select designation from dsg");
$noticia = mysql_fetch_assoc($query); //fetches just the top row
$arrDD = array("one", "two", "three", "four");

echo "<select name='dsgn' >";
foreach($arrDD as $value)
{
    echo '<option' . ($noticia["designation"] == $value?' selected="selected"':"") . ' value=' . $value . '>' . $value . '</otion>';
}
echo "</select>";
?>

here you will see that I am using an array to construct the select element and using the value from the database to define the selected value.

i've tried it with an array and it works fine.but i have alot of values,about 20.so i was wondering if there is any other way of doing it besides hardcoding the values in an array??

actually i just realised there wont be just 20 values,there'l be atleast 100 values.so hardcoding all these values will be difficult

i have 2 table.1 with the designations and their ids called 'dsg' and another table that holds the designations and the rest of the details.is it ppossible to use 'dsg' instead of the array?

i've tried it with an array and it works fine.but i have alot of values,about 20.so i was wondering if there is any other way of doing it besides hardcoding the values in an array??

Some where you have to define the value that you want selected whether it is a boolean field in the database that flags the row that should be selected by default or you hard code it in the script.

<?php
$query=caseQuery("select designation from dsg");
$strSelectValue = "this is the value that I want selected";
echo "<select name='dsgn' >";
    while($noticia = mysql_fetch_assoc($query)) {
        echo  "<option" . ($noticia["designation"] == $strSelectValue?' selected="selected"':"") . " value='$noticia[designation]</option>";
    }
echo "</select>";
?>

Here you see that I included a variable in the script to define the value that I want selected by default. This is a viable solution if the default selected value never changes. Otherwise you are really talking about a multiple to one table relationship in the database and a join query.

i dont want a value to be selected by default.the value has to be selected based on the row i want to edit.basically i have a edit form wherein i retireve some rows from the database.i of the field in the row is 'designation'.there are different designations.now say i want to change the designation of 1 row then i click the edit button corresponding to that row and i'm given a edit form where 1 field is a drop down with the designation.but the 1st value that i should see in the designation is the 1 that is stored in the databse(ie the designation value in the database of the row i wanna edit).hope u understand what im tryin to say

is it possible to retrive the designation values from the dsg table and put all of them in an array and than compare the array as done before?

i have 2 table.1 with the designations and their ids called 'dsg' and another table that holds the designations and the rest of the details.is it ppossible to use 'dsg' instead of the array?

I think this is what you are getting at and here is what I would do.

You have two tables, dsg which holds the designation name and id, then you have another table that holds designation details. Just a comment here, if these two tables are a one to one relationship then they really should be merged. You should have another table that defines the selected value per user. I assume that you have users so your users table will have user details and a primary key field and this other table will store two required fields being the designation id and the users primary key and it may also have a third field being the primary key for that table. So ultimately you are talking about two separate queries here, the first to get all the designations from the dsg table to fill your dropdown and the second to pull the selected id for that specific user or page or whatever scheme you have that defines the selected value. That second query will look something like this

"select id from dsg_users where userpk = '" . $_SESSION['userpk'] . "'"

. This will define the id that defines the default selected value from the dropdown.

is it possible to retrive the designation values from the dsg table and put all of them in an array and than compare the array as done before?

Yes, but you are still missing the element that defines the selected value.

i tried this

$quer=caseQuery("select designation from dsg");
	  while(($row = mysql_fetch_assoc($quer)))
	    {   $info[] = $row;  }
	  //$info=mysql_fetch_array($quer);
	  //$info = array('Minister','Secretary','Director of Panchayats', 'Addl Director of Panchayats(North)'); 

$query = caseQuery("SELECT designation from designation where sr_no=$id") or die(mysql_error()); 

while ($row = mysql_fetch_assoc($query)) 
{ 

echo '<select name="dsgn">'; 

foreach ($info as $data) 
{ 
echo '<option'.($row['designation']==$data? ' selected' : '').'>'.$data.'</option>'; 
} 

echo '</select>'; 

}

but the drop down just show a list of 'Array'.any idea how i could make this work?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.