Jump to content


Photo

Codeigniter group by array

codeigniter

  • Please log in to reply
2 replies to this topic

#1 epseix1

epseix1

    Member

  • Members
  • PipPip
  • 10 posts

Posted 01 July 2013 - 06:27 AM

I would have simply edited my previous topic had it not been locked, however I am asking the same TYPE of question - looking at it from a different angle (hope that's OK).

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:
 
<h1>** grouped country **</h1>
<h2> ** grouped city **</h2>
<p>** grouped venue **</p>
Such as:
 
<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 TrinityAW

TrinityAW

    Newbie

  • New Members
  • Pip
  • 1 posts

Posted 03 July 2013 - 05:08 AM

$events = array();
 
foreach($db->result() as $result)
{
     $events[$result->country][$result->city][$result->venue];
}
 
A bit confused but this should create a multidimensional array with a city for each country and an event for each city. 


#3 sKunKbad

sKunKbad

    Advanced Member

  • Members
  • PipPipPip
  • 1,748 posts
  • LocationTemecula, CA, USA

Posted 06 July 2013 - 06:59 PM

Unless you are specifically querying for a country, city, or venue, then I assume you are pulling in the entire set from the DB. Because of that, you can specify you want the results as an array:

$arr = $query->result_array();

Now that all of the rows are in an array, you would simply use php's sorting functions to order the array. I'd recommend looking at uasort. Also, with this type of nested parent => child relationship, you can take a look at what I did with Community Auth, and the example that shows a category menu. Community Auth is at http://community-auth.com, and is a CodeIgniter project. Look at how the database is organized, and how the results are displayed. I think you will find that it will work well for your project.
Brian's Web Design - Temecula

Freedom is only available through death.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com