Welcome Guest, Not a member yet? Register   Sign In
[SOLVED]Newbie question: Create a Multi-Dimensional Array
#1

[eluser]Juan Velandia[/eluser]
Hello everyone, I've been dealing with this with little progress, I hope you can help:

I get data with the following model

Code:
function get_section_articles()
    {
        $this->db->select('cms_article.name AS article_name');
        $this->db->select('cms_section.name AS section_name');
        $this->db->from('cms_article');
        $this->db->join('cms_section','cms_article.id_section = cms_section.id_section');
        $this->db->order_by('section_name');
        $query = $this->db->get()->result_array();
        return $query;
    }

and I would like to arrange this data in a multidimensional Array something like this:

Code:
$section = array(
  'Clients'=>array(
    'pepsi',
    'cocacola',
    'sevenup'
  ),
  'Services'=>array(
    'Web Design',
    'Graphic Design',
    'Data Migration'
  ),
  'Techonologies'=>array(
    'codeigniter',
    'postgresql',
    'ubuntu'
  )
);

I have a database where, 'Clients', 'Services','Techonologies' are in the section Table each one with it's id_section, and, 'pepsi', 'cocacola', 'sevenup', Web Design','Graphic Design', 'Data Migration','codeigniter','postgresql', 'ubuntu' are in the article table wich have a id_section column.

I hope I've been clear, thanks in advance!
#2

[eluser]smilie[/eluser]
It's late here, can't think properly.
But wanted to give you one advise regarding DB structure and naming convention.

Make every single field name in all of your tables and databases unique.
What I see now is that you have:

Table cms_section, field name;
Table cms_article, field name;

So, when you do SQL queries, you must specify table name and field name.

In this case:

Table cms_section, field section_name;
Table cms_article, field article_name;

MySQL recognizes that both field names are unique and does not require you to specify table, so:

$this->db->select('article_name,section_name');

will be sufficiant for MySQL to perform query :-)

Good luck!

Cheers,
Smilie
#3

[eluser]Juan Velandia[/eluser]
Thanks mate, I will have this in mind!
#4

[eluser]Juan Velandia[/eluser]
After some hours dealing with this and with some previous threads I came up with he solution

Model

Code:
function get_section_articles()
    {
        $this->db->select('cms_article.name AS article_name');
        $this->db->select('cms_section.name AS section_name');
        $this->db->select('cms_section.url_section');
        $this->db->from('cms_article');
        $this->db->join('cms_section','cms_article.url_section = cms_section.url_section', 'left');
        $this->db->group_by('section_name');
        $this->db->group_by('article_name');
        $this->db->group_by('cms_section.url_section');
        $this->db->order_by('section_name');
        $query = $this->db->get();
        
        $previous_section = null;
            foreach($query->result() as $row) {
            
                if ($row->url_section != $previous_section)
                {
                    if ($previous_section != null)
                    {
                        $results[] = $section;
                    }
                    $section = array();
                    $section['url_section'] = $row->url_section;
                    $section['section_name'] = $row->section_name;
                    $section['articles'] = array();
                    $previous_section = $row->url_section;
                }
                $article = array();
                $article['article_name'] = $row->article_name;
                $section['articles'][] = $article;
            }
            if ($previous_section != 0)
            {
                $results[] = $section;
            }
        return $results;

    }

controller
Code:
$data['section_articles'] = $this->section_content_model->get_section_articles();

view:

Code:
foreach ($section_articles as $section): ?>

    <p><strong>&lt;?php echo $section['section_name'] ?&gt;</strong></p>
        &lt;?php foreach ($section['articles'] as $article): ?&gt;
        <p>&lt;?php echo $article['article_name'] ?&gt;</p>
    &lt;?php endforeach; ?&gt;
    
&lt;?php endforeach; ?&gt;
#5

[eluser]VolkS[/eluser]
Thanks Juan Velandia !!!




Theme © iAndrew 2016 - Forum software by © MyBB