Welcome Guest, Not a member yet? Register   Sign In
foreach error
#1

[eluser]georgerobbo[/eluser]
Are you unable to have more than one foreach loop in php? When the first foreach loop is active the second one doesn't function. However as soon as I comment the first loop the second loop functions.


Code:
<div id="panel">
        
        &lt;?php foreach ($item['random_single'] as $item): ?&gt;
        <div id="preview">
    
            <div id="preview_item">
                <img src="&lt;?php echo base_url(); ?&gt;public/uploads/&lt;?php echo $item['filename'];?&gt;.jpg" alt="" />
            </div>
            
            <div id="gradient">
            </div>
            
        </div>
        
        <div id="meta">
            <h2><a href="&lt;?php echo site_url(); ?&gt;/archive/&lt;?php echo $item['permalink']; ?&gt;">&lt;?php echo $item['title']; ?&gt;</a></h2>
            <h5><small>Author:</small> <a href="&lt;?php echo site_url(); ?&gt;/profile/&lt;?php echo $item['username']; ?&gt;">&lt;?php echo $item['author']; ?&gt;</a></h5>
            <h5><small>Uploaded:</small> &lt;?php echo $item['time']; ?&gt; &sim; &lt;?php echo $item['date']; ?&gt;</h5>
            <h5><small>Tags:</small></h5>
            <p><small>&lt;?php echo $item['description']; ?&gt;</small></p>
        </div>
           &lt;?php endforeach; ?&gt;
    
    </div>
    
    <div id="dual">
            
          <div class="dualpanel">
            <h2 class="title">Whats New</h2>
         </div>
            
         <div class="dualpanel">
            <h2 class="title">Categories</h2>
            <ul class="category">
                &lt;?php foreach ($item['list_category'] as $item): ?&gt;
                <li><a href="#">&lt;?php echo $item['category']; ?&gt;</a></li>
                &lt;?php endforeach; ?&gt;
            </ul>
           </div>
        
    </div>
            
    </div>
#2

[eluser]rogierb[/eluser]
ehh, using $item, $item, $item and $item...

Change them into recognizable vars and overwriting $item will be no more....
#3

[eluser]BrianDHall[/eluser]
To elucidate:

Code:
&lt;?php foreach ($item['random_single'] as $item): ?&gt;
&lt;?php foreach ($item['list_category'] as $item): ?&gt;

You are over-writing your $item[] array with a scalar $item, thus the unexpected behavior. If the first foreach runs then the second can't because $item['list_category'] doesn't exist - at that point $item is not an array and has no such element.




Theme © iAndrew 2016 - Forum software by © MyBB