php minimum the result 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";
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论