Welcome Guest, Not a member yet? Register   Sign In
codeigniter duplicate FOREACH
#1

I'm trying to take from mysql some data like a forum name comments number.

My code looks like that

PHP Code:
<?php foreach(fetch('forum_threads''limit=5') as $forum_thread) : ?>
  <?php foreach(fetch('forums''limit=5') as $forum) : ?>
    <?php foreach(fetch('forum_sections''limit=5') as $forum_section) : ?>

Everythings looks working, but i got 5 same forum threads like that:
HELLO
HELLO
HELLO
HELLO
HELOO
HELLO2
HELLo2
HELLO2
HELLO2
HELLO2

What i wanna to do looks like that:

HELLO
HELLO2.

Sorry for my bad english and thanks for answering! Cheers
Reply
#2

I'm tryed to do like that:

PHP Code:
<?php 
foreach(array_slice(fetch('forum_threads'), 05) as $forum_thread ):
foreach(
array_slice(fetch('forums'), 05) as $forum ):
foreach(
array_slice(fetch('forum_sections'), 05) as $forum_section):
  
?>

but i got the same problem...
Reply
#3

There's a lot of missing information, and I don't know what you're doing with the fetch() function, but I would have to assume it should be in the controller instead of the view. With this in mind, I've put together an example of what I think you're trying to do:

PHP Code:
<h1>Forum</h1>
<?
php 
foreach ($forum_threads as $forum_thread) : 
    
$forums $forum_thread->forums;
?>
<h2><?php echo $forum_thread->title?></h2>
    <?php 
    
foreach ($forums as $forum) : 
        
$forum_sections $forum->sections;
    
?>
<h3><?php echo $forum->title?></h3>
        <?php foreach ($forum_sections as $forum_section) : ?>
<h4><?php echo $forum_section->title?></h4>
<p>More stuff from $forum_section goes here.</p>
        <?php endforeach; ?>
<p>A footer for the current $forum could go here.</p>
    <?php endforeach; ?>
<p>A footer for the current $forum_thread could go here.</p>
<?php 
endforeach; 

Basically, the issue is that you appear to be echoing content in your inner-most loop which belongs at a higher level. Of course, there's not enough information in your question to determine if any of the code I've included is remotely correct, but I'm hoping that it clarifies the situation.
Reply
#4

None of that is codeigniter code. What does your fetch() do? You're not showing us any relevant code to be able to help you. A foreach() operating on some function we can't see doesn't help us.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB