Jump to content

[SOLVED] looping a loop


ibinod

Recommended Posts

greetings,

 

            <?php $colors = array('blue', 'red', 'green', 'purple', 'orange', 'brown', 'gray'); ?>
            <?php foreach ($author_quotes as $quote): ?>
            <blockquote class="">...</blockquote>
            <?php enforeach; ?>

 

the $author_quotes is an array from database and it has more than 100 entry so what i am trying to do here is i want to repeat the colors from blue to gray and again blue to gray from the $colors array

in

<blockquote class="(here)">..</blockqutoe>

 

i have been trying different looping method but i can't make it work, so i came over here, pls post me a quick n short soln

regards

Link to comment
Share on other sites

acutally it's abit like this

 

<?php $colors = array('blue', 'red', 'green', 'purple', 'orange', 'brown', 'gray'); ?>

<?php foreach($author_quotes as $quote) : ?>
<blockquote class=""><?php $quote['quote'] ?></blockquote> <?php // so what i want her is i want to use the colors in the above array in place of blockquote class ?>
<?php endforeach; ?>

Link to comment
Share on other sites

You mean like this -

<?php $colors = array('blue', 'red', 'green', 'purple', 'orange', 'brown', 'gray'); ?>

<?php
foreach($author_quotes as $quote) :
     foreach ($colors as $key => $color) :
?>
          <blockquote style='color: <?php echo $color; ?>;'><?php $quote['quote'] ?></blockquote> <?php // so what i want her is i want to use the colors in the above array in place of blockquote class ?>
<?php
    endforeach;
endforeach;
?>

Link to comment
Share on other sites

Not sure I quote understand. There's an array $author_quotes, you want to display all the quotes once in a different color class? Or what?

 

 

Let's assume i have 100 different quotes to get from my database

 

and i have seven different styles

 

so i want to display those 0 to 100 quotes from (0- blue to 6- gray again 7-blue to 13-gray) like this

 

 

You mean like this -

<?php $colors = array('blue', 'red', 'green', 'purple', 'orange', 'brown', 'gray'); ?>

<?php
foreach($author_quotes as $quote) :
     foreach ($colors as $key => $color) :
?>
          <blockquote style='color: <?php echo $color; ?>;'><?php $quote['quote'] ?></blockquote> <?php // so what i want her is i want to use the colors in the above array in place of blockquote class ?>
<?php
    endforeach;
endforeach;
?>

 

Yes, somewhat like that, i tried that previously also but what happens there is it repeats each quote 7 time so i don't want it to repeat the quotes, just the colors from blue to gray

Link to comment
Share on other sites

You can try something different like this:

 

<?php
$colors = array('blue', 'red', 'green', 'purple', 'orange', 'brown', 'gray');
for($i = 0;$i < count($author_quotes);$i++)
{
$color = $colors[$i%7];
echo '<blockquote class="' . $color . '">' . $author_quotes[$i] . '</blockquote>';
}
?>

Link to comment
Share on other sites

You can try something different like this:

 

<?php
$colors = array('blue', 'red', 'green', 'purple', 'orange', 'brown', 'gray');
for($i = 0;$i < count($author_quotes);$i++)
{
$color = $colors[$i%7];
echo '<blockquote class="' . $color . '">' . $author_quotes[$i] . '</blockquote>';
}
?>

 

Thanx alot mate,

that worked as i wanted.

 

Regards

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.