donglao6169 2017-10-25 19:20
浏览 155

在PHP中使用foreach进行组合

I have been working for a while to combine a "foreach loop" and a "while loop". I would like my foreach to run 30 times. This is indicated by $counter. Now the problem is that my "while" again starts the "foreach" every time. But it hears 30 times turning the "while" each time again.

If I get the while part away and use static code, it will work completely. But I need to link my DB for the information. Perhaps an option is to make a "foreach" a "for" or a whole different option. But I have no idea how to set him up.

$counter = days in a month

$index = foreach time for running. Max $counter

$NUM = unique number of 1 people

The $ index together with $ cut_startday together indicate when a box is to be red or green.

Here's the code for the none working foreach:

 foreach(range(1,$counter) as $index) {/*open foreach*/
                       $get_data = "
                            SELECT * 
                            FROM core";



    $result1 = $conn->query($get_data) or die($conn ->error);


    //Start query 1
    if($result1) {
                    while($row = $result1->fetch_assoc()) {


            // Get NUM, START DATE and END DATE
            $NUM = ($row['c_m_num']);
            $startdate = ($row['e_date_s']);
            $enddate = ($row['e_date_e']);

            // Cut strings for date
            $sort_year = substr($startdate, 6, 4); // START YEAR -> 2017
            $sort_month = substr($startdate, 0, 2); // START MONTH -> 09
            $cut_startday = substr($startdate, 3, 2); // START DAY -> 17
            $cut_endday = substr($enddate, 3, 2); // END DAY -> 19



                         if($load_m_NUM == "$NUM" and $index >= $cut_startday && $index <= $cut_endday ){
                            echo"<td style='background-color:red;'>x</td>";
                            }


                            else{
                    echo"<td style='background-color:green;'></td>";
                    }


        }

        }           



/*end foreach*/ }   

What the idea is of the code. I want a calendar with a top row de days of a month and on the left side al the people. If the core find a match between days and 1 men, color the td red if not color green.

Any help greatly appreciated!

  • 写回答

1条回答 默认 最新

  • dpq755012465 2017-10-25 19:27
    关注

    You could change the foreach statement to for ($index = 0; $index < 30; $index++). To improve your code I suggest putting the for loop inside the while loop instead. That way you only query the database once instead of 30 times.

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用