PHP while 循环时间

发布于 2024-10-09 14:53:57 字数 687 浏览 0 评论 0原文

这是我第一次尝试使用面向对象编程。我试图重复数据库中的时间段,但只得到一个结果。

时间段表包含:

10:00:00  
10:15:00
10:30:00

在 PHP 类中:

 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();
     }
 }

在网页中显示循环次数:

 $booking = new booking($mysqli);

 <?php echo $booking->get_timeslot(); ?>

结果:

10:00:00

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:

 $booking = new booking($mysqli);

 <?php echo $booking->get_timeslot(); ?>

result:

10:00:00

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦回旧景 2024-10-16 14:53:57

return 正在返回函数的值,因此您只会收到 1 个返回值。尝试

echo $r['times'].'<br/>';

 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;
 }

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;
 }
司马昭之心 2024-10-16 14:53:57

return 语句停止 while 循环继续进行,并退出该函数。您需要修改函数以返回时隙数组,或者修改逻辑以仅期望来自数据库的第一个结果。

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.

守望孤独 2024-10-16 14:53:57

return 语句将在调用函数后立即退出该函数。相反,您应该将项目添加到数组中而不是返回整个数组:

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; 
 }
}

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; 
 }
}
怀里藏娇 2024-10-16 14:53:57
 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";
 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";
月牙弯弯 2024-10-16 14:53:57

因为您正在使用 return $r['times'];循环内。

这应该可以解决您的问题:

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
}

另一方面,不建议在类方法内或任何地方使用 global 关键字,因为全局作用域允许任何进程访问和更改全局变量。
我建议您尝试使用其他方式访问数据库对象(对象注册表、受保护的属性...)

另外,使用 while 循环的替代语法 (while(): ... endwhile;) 可读性不是很好,但可以对此进行辩论。

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.

许久 2024-10-16 14:53:57

试试这个:

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;
}}

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;
}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文