Home  >  Q&A  >  body text

Group MySQL results by ID for looping over

I have a table with flight data in mysql. I'm writing a php code that will group and display data using codeigniter 3

journey_id     air_id  FlightDuration  out_or_in   flightduration2
    1           1           20hr 5min   outbound    1205
    2           1           20hr 5min   outbound    1300
    3           1           17hr 55min  inbound     2258
    4           1           17hr 55min  inbound     1075
    5           2           31hr 40min  outbound    1970
    6           2           31hr 40min  outbound    1900
    7           2           17hr 55min  inbound     2223
    8           2           17hr 55min  inbound     1987
    9           3           10hr 45min  outbound    645
    10          3           11hr 25min  inbound     685

I use $this->db->get() to retrieve the data and I can loop easily. But since each row is in an array, I'm finding it difficult to group them. I can't use mysql groups because I need each row.

For example, I want to display the following items

air_id - 1
20hr 5min   outbound    1205
20hr 5min   outbound    1300
17hr 55min  inbound     2258
17hr 55min  inbound     1075

air_id - 2
31hr 40min  outbound    1970
31hr 40min  outbound    1900
17hr 55min  inbound     2223
17hr 55min  inbound     1987

air_id - 3
10hr 45min  outbound    645
11hr 25min  inbound     685

What is the best way to group the results by air_id so that I can iterate over

P粉638343995P粉638343995194 days ago481

reply all(1)I'll reply

  • P粉043295337

    P粉0432953372024-04-07 16:23:46

    1. Get data from database:

      $this->db->select('journey_id, air_id, FlightDuration, out_or_in, flightduration2');
       $this->db->from('your_table_name'); // Replace 'your_table_name' with the actual table name
       $query = $this->db->get();
       $data = $query->result_array();
    2. Create an empty array to save grouped data:

      $grouped_data = array();
    3. Iterate over the acquired data and group it by air_id:

      foreach ($data as $row) {
      
          $air_id = $row['air_id'];
      
          // Check if the air_id already exists in the grouped_data array
          if (!isset($grouped_data[$air_id])) {
              // If not, initialize an empty array for this air_id
              $grouped_data[$air_id] = array();
          }
      
          // Add the current row to the group for this air_id
          $grouped_data[$air_id][] = $row;
      }
    4. Now you have the data grouped by air_id in the $grouped_data array. You can iterate through this array to display the data you specify:

      foreach ($grouped_data as $air_id => $group) {
          echo "air_id - $air_id
      "; foreach ($group as $row) { echo $row['FlightDuration'] . ' ' . $row['out_or_in'] . ' ' . $row['flightduration2'] . '
      '; } echo "
      "; }

    This code will loop through the grouped data and display it as you describe, each group of flight data is under the corresponding air_id.

    reply
    0
  • Cancelreply