开发者

php minimum the result timeslot

开发者 https://www.devze.com 2023-02-16 13:11 出处:网络
Timeslot table: idTimes ------------ 110:00 210:30 311:00 411:30 512:00 612:30 713:00 813:30 914:00 1014:30 $q = $db->query(\"SEL开发者_C百科ECT * FROM timeslot\");

Timeslot table:

id   Times
------------
1    10:00
2    10:30
3    11:00
4    11:30
5    12:00
6    12:30
7    13:00
8    13:30
9    14:00
10   14:30

$q = $db->query("SEL开发者_C百科ECT * FROM timeslot");
while($r = $q->fetch_array(MYSQLI_ASSOC)) :
  echo '<tr>';
  echo '<td>'.$r['times'].'</td>';
  echo '</tr>';
endwhile;

how do i showing only 10:00 to 12:00 (id: 1-5) timeslot. what about using forloop?

while($r = $q->fetch_array(MYSQLI_ASSOC)) :
  for($i; $i<=5; $i++) {
    echo '<tr>';
    echo '<td>'.$r['times'].'</td>';
    echo '</tr>';
  }
endwhile;

its showing five timeslot but the times showing incorrect time (all 10:00)? please help. any other solution?

result

times:
10:00
10:00
10:00
10:00
10:00


Possibly the best way to print only 5 is to SELECT only 5

$q = $db->query("SELECT * FROM timeslot ORDER BY times LIMIT 5");
while($r = $q->fetch_array(MYSQLI_ASSOC)) :
  echo '<tr>';
  echo '<td>'.$r['times'].'</td>';
  echo '</tr>';
endwhile;

The problem with your WHILE/FOR is that the WHILE loop is only getting one record, then the FOR is running 5 times on the same record.

while($r = $q->fetch_array(MYSQLI_ASSOC)) :
   for($i; $i<=5; $i++) {   // each one uses the same $r

Generally though, in PHP alone, you can use a counter variable to exit a while loop

$i = 0;
while(($r = $q->fetch_array(MYSQLI_ASSOC)) && ($i < 5)) :
  echo '<tr>';
  echo '<td>'.$r['times'].'</td>';
  echo '</tr>';
  $i++;
endwhile;


$q = $db->query("SELECT * FROM timeslot WHERE id BETWEEN 1 AND 5");


Depending on the data type of Times, try the BETWEEN … AND … operator:

SELECT *
FROM timeslot
WHERE Times BETWEEN "10:00" AND "12:00";
0

精彩评论

暂无评论...
验证码 换一张
取 消