简体   繁体   English

PHP循环内循环

[英]PHP Loop within a loop

In my database I have a table of "weeks" and a table of "workouts". 在我的数据库中,我有一个“星期”表和一个“锻炼”表。 I would like to achieve something like the following: 我想实现以下目标:

  • Week 1 第一周
    • workout 1 title 锻炼1标题
    • workout 1 content 锻炼1内容
    • workout 2 title 锻炼2标题
    • workout 2 content 锻炼2内容
    • workout 3...... 锻炼3 ......
  • week 2 第二周
    • workout 1 title 锻炼1标题
    • workout 1 content 锻炼1内容
    • workout 2 title 锻炼2标题
    • workout 2 content 锻炼2内容
    • workout 3...... 锻炼3 ......
  • week 3..... 第三周.....

I've trying something like: 我正在尝试类似的东西:

function get_weekly_content() {

        $user_id = $this->session->userdata('id');

        $weeks = $this->db->select('week_no')->where('user_id', $user_id)->get('weeks');

        if($weeks->num_rows > 0) {
            foreach($weeks->result() as $row) {
                $week_no = $row->week_no;

                $workouts = $this->db->where('user_id', $user_id)->where('week_no', $week_no)->get('workouts');

                if($workouts->num_rows > 0) {
                    foreach($workouts as $workout) {
                        $workout_data[] = $workout;
                    }
                }


                $weekly_data[] = $workout_data;

            }
            return $weekly_data;
        }
    }

but maybe I have the wrong idea. 但也许我有一个错误的主意。 I would then also need to display the data. 然后,我还需要显示数据。

EDIT my question is, what would be the best way to achieve the above and get and array/object to loop through on a view page? 编辑我的问题是,什么将是最好的方法,以实现上述并获取和数组/对象在视图页面上循环?

Thanks 谢谢

Looping with in a loop is just fine, and your approach is appropirate. 循环循环就可以了,您的方法是适当的。 However! 然而!

  1. Generally speaking you shouldn't run mysql queries inside of loops if there is any way to avoid it because queries are quite slow this will really degrade the performance of your script. 一般来说,如果有任何避免方法,则不应在循环内运行mysql查询,因为查询速度很慢,这实际上会降低脚本的性能。 Otherwise, the idea is correct. 否则,这个想法是正确的。 Try to write a query outside of the loops that puts all the relevant user data into a keyed array and then pull the information from the array while you're looping. 尝试在循环外编写一个查询,该查询将所有相关的用户数据放入键控数组中,然后在循环时从数组中提取信息。

  2. You're going to see problems with this line: $weekly_data[] = $workout_data; 您将看到此行的问题: $weekly_data[] = $workout_data; if $workouts->num_rows > 0 ... if it's the first loop, it will throw an error because $workout_data won't be set, and if it's subsequent loops then on any week that doesn't have results, it will display the previous week's data. 如果$workouts->num_rows > 0 ...如果是第一个循环,则将抛出错误,因为将不会设置$workout_data ,如果是随后的循环,则在没有结果的任何一周,它将显示前一周的数据。 You can fix this by setting/resetting $workout_data at the top of the inner loop: 您可以通过在内部循环顶部设置/重置$workout_data来解决此问题:


$workout_data = array(); 
if($workouts->num_rows > 0) {
   foreach($workouts as $workout) {
      $workout_data[] = $workout;
   }
}
function get_weekly_content() {

        $user_id = $this->session->userdata('id');

        $weeks = $this->db->select('week_no')->where('user_id', $user_id)->get('weeks');
        $weekly_data = array();
        if($weeks->num_rows > 0) {
            foreach($weeks->result() as $row) {
                $workout_data = array();
                $week_no = $row->week_no;

                $workouts = $this->db->where('user_id', $user_id)->where('week_no', $week_no)->get('workouts');

                if($workouts->num_rows > 0) {
                    foreach($workouts as $workout) {
                        $workout_data[] = $workout;
                    }
                }


                $weekly_data[$week_no] = $workout_data;
            }
        }
        return $weekly_data;
    }

Then access the data: 然后访问数据:

$weekly_data = get_weekly_content();

foreach( $weekly_data as $week_no => $workout_data){
  // do something with it.
}

That being said, I agree with @Ben D, you should think about your query and create one that can return all the results at once. 话虽如此,我同意@Ben D,您应该考虑一下您的查询并创建一个可以立即返回所有结果的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM