PHP while loop times
This is my first time try to use object oriented programming. I'm trying to repeat the timeslot from the database but I'm only getting one result.
timeslot table contains:
10:00:00
10:15:00
10:30:00
in PHP class:
class booking {
function __construct($mysqli) {
}
function get_timeslot() {
global $mysqli;
$q = $mysqli->query("SELECT id_timeslot, times FROM timeslot");
while($r = $q->fetch_array(MYSQLI_ASSOC)) :
return $r['times'];
endwhile;
$mysqli->close();
}
}
in the webpage to showing the looping times:
$boo开发者_Go百科king = new booking($mysqli);
<?php echo $booking->get_timeslot(); ?>
result:
10:00:00
return is returning the value for your function, thus you'll only receive 1 value returned. Try
echo $r['times'].'<br/>';
or
function get_timeslot(){
global $mysqli;
$q = $mysqli->query("SELECT id_timeslot, times FROM timeslot");
$timeSlots = array();
while($r = $q->fetch_array(MYSQLI_ASSOC))
{
$timeSlots[] = $r['times'];
}
$mysqli->close();
return $timeSlots;
}
the return statement stops the while loop from continuing any further, and exits the function. You'll either need to modify the function to return an array of time slots, or modify your logic to expect only the first result from the database.
The return
statement will exit the function immediately it's called. Instead return you should add the item to array than return the entire array:
class booking {
function __construct($mysqli){}
function get_timeslot(){
global $mysqli;
$q = $mysqli->query("SELECT id_timeslot, times FROM timeslot");
while($r = $q->fetch_array(MYSQLI_ASSOC)) :
$res[] = $r['times'];
endwhile;
$mysqli->close();
return $res;
}
}
function get_timeslot() {
global $mysqli;
$q = $mysqli->query("SELECT id_timeslot, times FROM timeslot");
while($r = $q->fetch_array(MYSQLI_ASSOC)) :
// Cast to array
$t[] = $r['times'];
endwhile;
$mysqli->close();
// Return Array
return $t;
}
// Calling the function
$booking = new booking($mysqli);
// Add print_r() as the return is now in an array format
echo "<pre>".print_r($booking->get_timeslot(),true)."</pre><br />\n";
Because you are using return $r['times']; inside the loop.
This should solve your problem:
function get_timeslot(){
global $mysqli;
$returnArray = array();
$q = $mysqli->query("SELECT id_timeslot, times FROM timeslot");
while($r = $q->fetch_array(MYSQLI_ASSOC)) :
$returnArray[] = $r['times'];
endwhile;
$mysqli->close();
return $returnArray; // now that all results are fetched from DB, return array containing them
}
On other note, using global keyword inside a class method or anywhere for that matter i not advisable, since global scope enables any process to access and change global variable. I would advise you to try using other means of accessing your DB object (object registry, protected property...)
Also using alternative syntax for while loop (while(): ... endwhile;) is not very readable, but one can debate about that.
Try this:
class booking {
function __construct($mysqli){}
function get_timeslot()
{
global $mysqli;
$q = $mysqli->query("SELECT id_timeslot, times FROM timeslot");
$return = '';
while ($r = $q->fetch_array(MYSQLI_ASSOC)) :
$return.= $r['times'] . '<br/>';
endwhile;
$mysqli->close();
return $return;
}}
精彩评论