Hafidh
Hafidh

Reputation: 130

How to looping correctly while inside while

A little problem with my query, which my query is :

  SELECT * FROM tkursi WHERE tkursi.no_kursi NOT IN 
(SELECT no_kursi FROM tpesanantiket LEFT join tpenjadwalantiket ON 
tpenjadwalantiket.id_perjalanan=tpesanantiket.id_perjalanan LEFT JOIN tbus ON 
tbus.id_bus=tpenjadwalantiket.id_bus WHERE tbus.id_bus='$bus' AND 
tpesanantiket.id_perjalanan='$trip') ORDER BY no_kursi ASC

this query showing no_kursi where some number does not display which status ordered.

I want to change in list no_kursi there no_kursi which one status ordered still display but has comment ordered, other color background or can't click.

I try to use while inside while, but it not work properly.

<table class=" table table-bordered table-striped"> 
<?php
    $s = mysqli_query($koneksidb, "SELECT * FROM tkursi ORDER BY no_kursi ASC"); 
    while ($kolomData = mysqli_fetch_array($s)) { 
    $x = mysqli_query($koneksidb, "SELECT no_kursi FROM tpesanantiket 
    LEFT join tpenjadwalantiket ON tpenjadwalantiket.id_perjalanan=tpesanantiket.id_perjalanan
    LEFT JOIN tbus ON tbus.id_bus=tpenjadwalantiket.id_bus 
    WHERE tbus.id_bus='$Bus' AND tpesanantiket.id_perjalanan='$Trip'
GROUP BY no_kursi");
     while ($kolomx = mysqli_fetch_array($x)) { 
?>   
    <tr>   
<td width="200"> <input type="radio" name="tambah3" value="<?php echo $kolomData['no_kursi']; ?>"  
<?php echo ($data7==$kolomData['no_kursi']) ?  "checked" : "" ;  ?>/>
<label  class="inline" for="<?php echo $kolomData['no_kursi']; ?>">
<?php echo $kolomData['no_kursi']; ?> &nbsp; 
<?php if ($kolomData['no_kursi']==$kolomx['no_kursi']) {
?> ordered 
<?php } ?>
</label> </td> 
    </tr>   
<?php  
} 
    }?>

the result has ordered in some no_kursi, but it loop too many.

Edited database show :
$s : no_kursi
001
002
003
004
005
006
007
...
043
$x : no_kursi
005
006

the result :
no_kursi
001
001
002
002
003
003
004
004
005 ordered
005
006 ordered
006
007
007
...
043
043

the result loop more as much as $x result.

Upvotes: 1

Views: 55

Answers (1)

you can change your query..

SELECT tkursi.*,CASE WHEN q.no_tkursi IS NULL THEN 0 ELSE 1 END AS is_orderer FROM tkursi LEFT JOIN (select no_tkursi FROM tpesanantiket LEFT join tpenjadwalantiket ON tpenjadwalantiket.id_perjalanan=tpesanantiket.id_perjalanan LEFT JOIN tbus ON tbus.id_bus=tpenjadwalantiket.id_bus WHERE tbus.id_bus='$Bus' AND tpesanantiket.id_perjalanan='$Trip') AS q ON q.no_tkursi = tkursi.no_tkursi ORDER BY tkursi.no_tkursi

and you've a unique query.

this query could better. without subquerys. but i dont know your db structure.

your result should be...

no_kursi | .... | is_orderer

1 | .... | 0

....

5 | .... | 1

6 | .... | 1

7 | .... | 0

Upvotes: 1

Related Questions