Welcome Guest, Not a member yet? Register   Sign In
Understanding the Loop
#1

[eluser]georgerobbo[/eluser]
In the view section of the user documentation there is a brief explanation of looping information from an array.

Code:
<?php
class Blog extends Controller {

    function index()
    {
        $data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');

        $data['title'] = "My Real Title";
        $data['heading'] = "My Real Heading";
        
        $this->load->view('blogview', $data);
    }
}
?>

Code:
<ul>
&lt;?php foreach($todo_list as $item):?&gt;

<li>&lt;?php echo $item;?&gt;</li>

&lt;?php endforeach;?&gt;
</ul>

That is all good but I need to code a loop where it doesn't just repeat the li tag for each object in an array, but it repeats the parent tag and another loop. Just so I'm not 100% confusing here is my HTML, it is effectively a sidebar object.

Code:
<div class="sideitem">
    <div class="sidetitle">
        <h3>&lt;?php echo $sidetitle;?&gt;</h3>
    </div>
    <p>&lt;?php echo $sidetext; ?&gt;</p>
    </div>

I wish to have multiple values for $data['sidetitle'] and $data['sidetext'] in an array, where an second value in the array will trigger a new <div class="sideitem"> etc.
I can't seem to find advanced looping in the documentation. Basically WordPress style looping.
#2

[eluser]Phil Sturgeon[/eluser]
You'll need to learn about multi-dimensional arrays in this article PHP Arrays.
#3

[eluser]georgerobbo[/eluser]
I've read through the article about arrays. I don't quite know how to insert the content from the database into a multidimensional or associative array. I know this code is completely wrong, hopefully aid and the documentation will see me through.

Code:
$query = $this->db->query('SELECT' title, content FROM ros_sidecont');
        foreach ($query->result_array() as $data)
        {
                }

And then these are my current arrays where I wish the content to be inserted into.

Code:
$data['sidetitle']['1'] = "Lorem Ipsum";
        $data['sidecont']['1'] = "Donec pharetra ante consequat elit gravida et posuere erat convallis. Cras lorem dui, vestibulum a venenatis ac, eleifend ac odio. Ut non nibh turpis.";
        $data['sidetitle']['2'] = "Dolor Sit";
        $data['sidecont']['2'] = "Aenean pellentesque lacinia nisi ac dignissim. Curabitur auctor vulputate malesuada. Curabitur id augue libero. Etiam urna enim, placerat pharetra consectetur ac, consequat eget justo. Ut cursus, erat vel porta consectetur, ipsum magna blandit lacus, eu scelerisque metus leo at tortor. Aliquam sit amet tellus in erat pellentesque semper.";
#4

[eluser]Phil Sturgeon[/eluser]
Well that does work but its less logical to work with. I prefer:

Code:
$query = $this->db->query('SELECT title, content FROM ros_sidecont');
$data = array();
foreach ($query->result_array() as $row)
{
    $item['sidetitle'] = $row['title]';
    $item['sidecont'] = $row['content'];

    $data[] = $item;
}

return $data;

This of course is pointless as it is basically renaming the result from $query->result_array(). You could just as easily do:

Code:
$query = $this->db->query('SELECT title AS sidetitle, content AS sidecont FROM ros_sidecont');
return $query->result_array();

Then you just do:

Code:
&lt;?php foreach($todo_list as $item):?&gt;

<div class="sideitem">
    <div class="sidetitle">
        <h3>&lt;?php echo $item['sidetitle'];?&gt;</h3>
    </div>
    <p>&lt;?php echo $item['sidetext']; ?&gt;</p>
</div>

&lt;?php endforeach;?&gt;

Let me know if this doesn't make sense. Arrays can get pretty confusing to begin with but bloody handy when you get the hang of them.




Theme © iAndrew 2016 - Forum software by © MyBB