• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array Looping

#1
[eluser]georgerobbo[/eluser]
Code:
<div id="sidebar">
        
        &lt;?php foreach($sideitem['0'] as $sidetitle):?&gt;
        <div class="sideitem">
            <div class="sidetitle">
                <h3>&lt;?php echo $sidetitle; ?&gt;</h3>
            </div>
            &lt;?php foreach($sideitem['1'] as $sidecont):?&gt;
                <p>&lt;?php echo $sidecont; ?&gt;</p>
            &lt;?php endforeach; ?&gt;
        </div>
        &lt;?php endforeach; ?&gt;
Code:
$data['sideitem']['0'] = array('Lorem Ipsum', 'Vivamus Lobortis', 'Donec Pharetra');
        $data['sideitem']['1'] = array('Donec pharetra ante consequat elit gravida et posuere erat convallis. Cras lorem dui, vestibulum a venenatis ac, eleifend ac odio. Ut non nibh turpis.', 'Fusce sagittis ante at lectus lacinia suscipit. Praesent sed vehicula urna. Aenean sed urna nunc. Phasellus at ante orci, sit amet pretium ipsum. Fusce rutrum orci et est tempor eu consequat quam rhoncus. Nam non ultricies eros.', 'Fusce in metus purus. Aenean quis leo ac lectus accumsan placerat vel et lacus. Pellentesque sed nunc et eros mattis dignissim et et sem. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.');

I currently have a loop going where each new value from an array should start a new <div class="sideitem">. That works, however the values from the array $data['sideitem']['1'] are not being divided into a seperate container, but instead all of them are being placed in every container.

What is the work around?
A live example http://georgerobbo.dyndns.org

#2
[eluser]kgill[/eluser]
They're in every container because you put them there. Look at your logic and your data - you're looping through the first array and each pass you make through it you do a complete loop of the second array. Lose the 2nd for loop and alter your 1st loop to get both the key and the value. Use the key to fetch the corresponding text $sideitem['1'][$key], either that or alter your array structure so that each title & text is together like this:

Code:
$sideitem[0] = array('title' => 'lorem ipsum', 'text'=>'blah blah blah');
$sideitem[1] = array('title' => 'Vivamus Lobortis', 'text'=>'foo bar baz');
$sideitem[2] = array('title' => 'Donec Pharetra', 'text'=>'foo2 bar2 baz2');
etc.

#3
[eluser]georgerobbo[/eluser]
Great! Thanks for the help, works perfectly. Now my next mountain is to get the information from those arrays to originated from a database.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.