www.webdeveloper.com
Results 1 to 2 of 2

Thread: Codeigniter group by array

  1. #1
    Join Date
    Jul 2013
    Posts
    1

    Codeigniter group by array

    I am trying to generate a "past events" page for my CI website, using a MySQL database table called "events". There are 100+ rows of data, some duplicate, that range across 5 major countries - England, Ireland, Norther Ireland, Scotland and Wales - many different major cities - again, including duplicate entries - and venues.

    I have struggled for a couple of weeks now with how to query and return the data in the following format:

    HTML Code:
    <h1>** grouped country **</h1>
    <h2> ** grouped city **</h2>
    <p>** grouped venue **</p>
    Such as:

    HTML Code:
    <h1>England</h1>
    <h2>London</h2>
    <p>Tower of London</p>
    <h2>Manchester</h2>
    <p>Canal Street</p>
    <p>Old Trafford</p>
    <h1>Ireland</h1>
    <h2>Dublin</h2>
    <p>St. James' Gate</p>
    <h1>Scotland</h1>
    <h2>Glasgow</h2>
    <p>Buchanan Street</p>
    <p>Tron Theatre</p>
    I am DELIBERATELY not including any of my code for model, view and controller, as what I have thus far seems to confuse people as to what I'm trying to achieve when they see it. And I'm open to the fact I may be attacking this from the wrong angle altogether... I just need to result the above!

    It feels as if this SHOULD be a fairly simple thing to achieve, but I NEED HELP!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,152
    Code:
    SELECT DISTINCT
      country,
      city,
      venue
    FROM
      events
    ORDER BY country, city, venue
    PHP Code:
    $data = array();
    while(
    $row whateverAssociativeDbFetchYouUse()) {
      
    $data[$row['country']][$row['city']] = $row['venue'];
    }
    foreach(
    $row as $country => $cities) {
      echo 
    "<h1>$country</h1>\n";
      foreach(
    $cities as $city => $venues) {
        echo 
    "<h2>$city</h2>\n";
        foreach(
    $venues as $venue) {
          echo 
    "<p>$venue</p>\n"
        
    }
      }

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles