Forum Moderators: coopster

Message Too Old, No Replies

Populating Select box,

but using a record element to default

         

Esqulax

12:34 pm on Feb 26, 2008 (gmt 0)

10+ Year Member



hi there...
more of a PHP/HTML issue i got here..
I have a record that i want to update.

one of the elemnts of the record, has a drop down box to populate it (its populated from another database).
getting it to display is coo, but i ant get it to have the default value to be whats already in the database

(types is in car database, type is in car_types(this is where i put Small/Med/Large)database) When a car is added, its assigned one of the values in the list (selected by the user)

now when it comes to EDIT these, thats when it goes a bit squiffy


$sql = "SELECT * FROM cars WHERE car_id = '$upid'";
$rs1=(mysql_query($sql,$conn));

while($row = mysql_fetch_array($rs1)){

echo ("<br>You chose the following record:<br><br>");
echo ("<input type=\"hidden\" name=\"updateid[]\" value=\"{$upid}\"> ");
echo ('Reg No:<input type="text" name="reg" size="8" value="'.$row["reg_no"].'"> ');
echo ('Make:<input type="text" name="make" size="8" value="'.$row["make"].'"> ');
echo ('Model:<input type="text" name="model" size="8" value="'.$row["model"].'">');
echo ("type:" .$row['type']);
$typevar=$row['type'];
$sql1 ="SELECT types FROM car_types";
$query = mysql_query($sql1,$conn);

echo '<select name="type1">';
while
($row = mysql_fetch_array($query))
{
if($typevar = $row['types']){
echo '<option value="',$row['types'],', selected="selected">',$row['types'],'</option>';
}else
echo '<option value="',$row['types'],'">',$row['types'],'</option>';

}
echo '</select>';

}

echo('</form>')
?>

any ideas?

jatar_k

2:12 pm on Feb 26, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if($typevar == $row['types']){

Esqulax

4:23 pm on Feb 26, 2008 (gmt 0)

10+ Year Member



Had a go at that, but to no avail...
Logically:
echo ("type:" .$row['type']);
$typevar=$row['type'];

$typevar contains what i see displayed on screen when calling $row['type'], which is a text string.


if($typevar == $row['types']){
echo '<option value="',$row['types'],', selected="selected">',$row['types'],'</option>';
}else
echo '<option value="',$row['types'],'">',$row['types'],'</option>';

If my new $typevar is == to $row['types'], then display the dropdown box with that option selected, if not display a normal element.

Thats my logic for it, i cant see whats not happening

jatar_k

4:52 pm on Feb 26, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you had a single = sign in your code up top so it was assigning and wouldn't work

if you made the change and it still doesn't work then you should echo the 2 values you are comparing to be sure they actually match

Esqulax

4:58 pm on Feb 26, 2008 (gmt 0)

10+ Year Member



i tried echoing the 2nd value $row['types], but its not appearing... i rekcon it may have something to do with the while loop being inside the <select> tags

Esqulax

5:04 pm on Feb 26, 2008 (gmt 0)

10+ Year Member



<epleteive deleted> it, ill just hard code it for now.. no-one will know :p

jatar_k

5:05 pm on Feb 26, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you should be able to echo

echo '<select name="type1">';
while ($row = mysql_fetch_array($query)) {
echo '<br>typevar: ',$typevar,' - row[types]: ',$row['types'];
if($typevar == $row['types']) {
echo '<option value="',$row['types'],'" selected="selected">',$row['types'],'</option>';
} else {
echo '<option value="',$row['types'],'">',$row['types'],'</option>';
}
echo '</select>';
}

I also made a change to the option display, did you check the page source to check the html? I am guessing that you didn't. Before changing this code, look at your page source and see if the 'selected' is getting echoed but just not working due to syntax issues

this original line is wrong
echo '<option value="',$row['types'],', selected="selected">',$row['types'],'</option>';

the one in the code I put above is correct

eelixduppy

5:15 pm on Feb 26, 2008 (gmt 0)



You're reassigning $row for the second query within the while loop which may cause a problem. Also, since you have the same query being sent to the database server for each iteration, you are really causing a lot to happen for no particular reason. To quicken the pace, you should take this out of the loop. Try the following code with some corrections:

$types = array();
$sql1 ="SELECT types FROM car_types";
$query = mysql_query($sql1,$conn);
while($row = mysql_fetch_assoc($query))
$types[] = $row['types'];
#
$sql = "SELECT * FROM cars WHERE car_id = '".mysql_real_escape_string($upid)."'";
$rs1=(mysql_query($sql,$conn));
#
while($row = mysql_fetch_array($rs1)){
#
echo ("<br>You chose the following record:<br><br>");
echo ("<input type=\"hidden\" name=\"updateid[]\" value=\"{$upid}\"> ");
echo ('Reg No:<input type="text" name="reg" size="8" value="'.$row["reg_no"].'"> ');
echo ('Make:<input type="text" name="make" size="8" value="'.$row["make"].'"> ');
echo ('Model:<input type="text" name="model" size="8" value="'.$row["model"].'">');
echo ("type:" .$row['type']);
#
$typevar=$row['type'];
#
foreach($types as $type) {
echo '<select name="type1">';
#
if($type == $typevar) {
echo '<option value="',$type],', selected="selected">',$type,'</option>';
} else {
echo '<option value="',$type,'">',$type,'</option>';
}
}
echo '</select>';
}
echo('</form>')
?>

Try that and see where that gets you. Note that this is untested.

[edit]
wow, took me awhile to post this :-P

Esqulax

5:34 pm on Feb 26, 2008 (gmt 0)

10+ Year Member



SWEEEET ....working!
Thanks guys...This place is a godsend!