猿问

foreach 循环循环遍历要显示在单独表中的日期数组

我正在尝试构建我的 foreach 循环来循环遍历时间戳数组中的所有日期,以便我可以在每个日期的 5 个单独的表中显示结果


这是我从数据库获取信息的功能


$users_site = "London";


$timeStampFrom = ( [0] => 1596655740 [1] => 1596745740 [2] => 1596828540 [3] => 1597087740 [4] => 1597177800 );


$timeStampTo = ( [0] => 1596659340 [1] => 1596749340 [2] => 1596832140 [3] => 1597095000 [4] => 1597181400 );


function show_available_equipment($users_site, $timeStampFrom, $timeStampTo){

// defines db from instialize page as a global variable

foreach ($timeStampFrom as $timeStampStart=>$timeStampTo) {


global $db;

//SQL statement to query the database for available gear

$sql= "SELECT av_inventory.ID AS equipID, av_equipment_type.equipment, "

        . "av_inventory.product, av_inventory.serial, av_inventory.current_location, "

        . "av_inventory.site, av_inventory.status, av_products.product, av_buildings.building FROM av_inventory JOIN "

        . "av_equipment_type ON av_inventory.equipment =av_equipment_type.ID "

        . "JOIN av_products ON av_inventory.product = av_products.ID JOIN "

        . "av_buildings ON av_inventory.current_location = av_buildings.ID WHERE "

        . "av_inventory.site =?  AND av_inventory.ID NOT IN (SELECT av_bookings.av_inventory_id FROM "

        . "av_bookings WHERE date_time_from <= ? and date_time_to >= ?)  GROUP BY av_inventory.ID;";



        // prepares our sql statement

        $stmt = mysqli_stmt_init($db);

        //if there is a SQL error exit

        if (!mysqli_stmt_prepare($stmt, $sql)) {

        header("Location: book_gear.php?error=sqlerror123");

         exit();

         }

}}}


这里我尝试根据给定的 5 个日期显示信息。每天一张桌子。目前我刚刚获得第一天的信息,这让我认为我的函数循环不正确。


开心每一天1111
浏览 98回答 1
1回答

UYOU

在获取SQL数据的函数中,第一次循环后返回结果:function show_available_equipment($users_site, $timeStampFrom, $timeStampTo){&nbsp; &nbsp; &nbsp; // defines db from instialize page as a global variable&nbsp; &nbsp; &nbsp; foreach ($timeStampFrom as $timeStampStart=>$timeStampTo) { // <----- Only runs once&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ........................&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if there is a SQL error exit&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!mysqli_stmt_prepare($stmt, $sql)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header("Location: book_gear.php?error=sqlerror123");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // otherwise go ahead&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...........................&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result = mysqli_stmt_get_result($stmt);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $result; //&nbsp; <---------- Returns after first loop&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }}从数据库检索每个结果后,您需要将其添加到某种$all_results变量中。循环完成后,返回$all_results。
随时随地看视频慕课网APP
我要回答