Hello. I have two tables, the itinerary table and location table.
The itinerary table consists of all the itinerary:
- A - B
- B - A
- B - C
- C - B
- C - A
- A - C
The user will select from the itinerary table then it will be saved in the location table:
- A - B
- B - A
Now, I want to display all the data in the itinerary table in an array of checkbox using MySQL query. The query will check if the itinerary data exist in the location table. If it does exist, it will echo the checkbox with the attribute checked = 'yes'
. If it doesn't, then it'll simply echo the checkbox without the checked attribute.
I can do this but with some minor error, with this code:
$result=mysql_query("select * from itinerary where aircraft = '$_SESSION[rtyp]' group by location");
while($row=mysql_fetch_array($result))
{
$pr=$row['icode'];
$n=$row['location'];
$l=$row['btime'];
$res=mysql_query("select * from location where reservno = '$_SESSION[rno]'") or die(mysql_error());
while($row1=mysql_fetch_array($res))
{
$loc=$row1['location'];
if($n==$loc)
{
echo"<table border=1 align=center width=250>
<tr><td width=15>
<input type='checkbox' value='$n -> $l' checked='yes'>
</td><td>$n</td><td width=5>$l</td></tr>
</table>";
$t=$_POST['prod'];
}
elseif($n!=$loc)
{
echo"<table border=1 align=center width=250>
<tr><td width=15>
<input type='checkbox' value='$n -> $l'>
</td><td>$n</td><td width=5>$l</td></tr>
</table>";
$t=$_POST['prod'];
}
}
}
But it echoes each itineraries twice as is it loops around the result of the second query.
*** A - B**
* A - B
* B - A
*** B - A**
* B - C
* B - C
* C - B
* C - B
* C - A
* C - A
* A - C
* A - C
(the bold list identifies that the checkbox is checked)
How can I optimize this? I don't know if I'm doing something wrong in either my MySQL or PHP code but I'll really appreciate some help with MySQL and PHP code. I'm at my wit's end now. Please help me and point me to how I can achieve the desired result. Thanks a lot!