1

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
    • workout 1 content
    • workout 2 title
    • workout 2 content
    • workout 3......
  • week 2
    • workout 1 title
    • workout 1 content
    • workout 2 title
    • workout 2 content
    • workout 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

1
  • have you initialized $workout_data Commented Feb 3, 2012 at 21:28

2 Answers 2

4

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. 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; 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. You can fix this by setting/resetting $workout_data at the top of the inner loop:


$workout_data = array(); 
if($workouts->num_rows > 0) {
   foreach($workouts as $workout) {
      $workout_data[] = $workout;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0
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.

1 Comment

How could you go about doing that? I agree one query would be ideal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.