Hi all,

3 tables :
one events table (each event can have differents types)
one types table (each type can have differents events)
and
one liaison table (link between events and types tables)

I have some problems when I show screen.

<select name="type[]" size="10" multiple="multiple">
<?php
$req_type   = "SELECT * FROM type ORDER BY type ASC";
$res_type   = mysql_query($req_type);
				
$req_liaison   = "SELECT * FROM liaison WHERE id_ev = $id";
$res_liaison   = mysql_query($req_liaison);
				
$ligne_liaison = mysql_fetch_array($res_liaison);
 
while ($ligne_type = mysql_fetch_array($res_type))
{
?>
<option value="<?php echo $ligne_type['id'] ?>" <?php if (isset($ligne_liaison) && in_array($ligne_type['id'], $ligne_liaison)) { echo 'selected="selected"'; } ?>><?php echo $ligne_type['type'] ?></option>
 <?php
}
?>
</select>

The selected don't run and don't show correct value.

Can you help me ?

Thanks a lots

please show structure of tables and add in 17 line

print_r( $ligne_liaison );

if values not empty, or write more about truoble

Just replace $columnName with the actual column name you want in the select tag. The actual query doesnt have to be in the select tag. The loop is what has to be inside the select tag. Hope this helps.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<?php
//This is the datbase connection
$con = mysql_connect("localhost","UserName","Password") or die("Check connection");

mysql_select_db("DB_Name",$con);

$query = "SELECT * FROM TableName ORDER BY columnName";
$result=mysql_query($query);

?>
</head>
<body>
<table>
<tr>
<td>
<select>
<?php

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

extract($row); // This is what you were missing

echo "<option value='$columnName'>".$columnName."</option>";

$row++; // this is what you were missing
//This will output every column name
}

?>
</select>

</td>
</tr>
</table>
</body>
</html>

@ CHI-TOWN
I must recover selected option

@ZZZubec

Yes, when I put this code (print_r)
I have one record only and in my test there are 3 records.
my request is not really wrong but incomplete for my problem

$ligne_liaison = mysql_fetch_array($res_liaison);

Hi,

I resolve my problem. :)
I made a array with my last request in a while instruction.

After I put :

<?php if (in_array($line_type['id'], my_array)) { echo 'selected="seleted"'; } ?>

Thanks for ur help (print_r, ...)

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.