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
https://forums.phpfreaks.com/topic/163587-solved-looping-a-loop/
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; ?>

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;
?>

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

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>';
}
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.