views:

38

answers:

3

Hi

I have to display below snippet according to data available in DB

<div id="testimonial-row">
            <div id="testimonial">
                <ul>

                    <li>"Hello World"</li>
                </ul>
            </div>

            <div id="testimonial">
                <ul>

                    <li>"Hello World"</li>
                </ul>
            </div>

            <div id="testimonial">
                <ul>

                    <li>"Hello World"</li>
                </ul>
            </div>
        </div>

That is, the <div id="testimonial-row"> should be created everytime the count of data (here, hello world) becomes greater than 3, hence in this way if the data count is "16" the the <div id="testimonial-row"> should be created 6 times with all the data displayed in created 6 <div> tags

So could some one tell me how to implement the for loop to make this happen in PHP?

+2  A: 

If I understand your question correctly:

for($z=0;$z<16;$z++)
 {
        if($z==0) print '<div class="testimonial-row">';
        else if($z%3==0) print '</div><div class="testimonial-row">';
        print '<div class="testimonial"><ul><li>"Hello World"</li></ul></div>';
 }
 print '</div>';
PIM
Thanks PIM!!!!!
OM The Eternity
A: 

Here's some code

$count = db_select_count...
$lastGroupCount = $count % 3;
$fullGroupCount = $count - lastGroupCount;
for ($i=0; $i<$fullGroupCount; $i++)
{
    output_testimonial_row_html(maybe_pass_an_id);
    for ($j=0; $j<3; $j++)
    {
        output_testimonial_html($count[$i*3+$j]);
    }
    close_div();
}

if ($lastGroupCount > 0)
{
    output_testimonial_row_html(maybe_pass_an_id)
    for ($j=0; $j<$lastGroupCount; $j++)
    {
        output_testimonial_html($count[$fullGroupCount*3+$j]);
    }
    close_div();
}
Eric Fortin
+1  A: 
   echo '<div class="testimonial-row">';
    for ($i=1;$i<=$mysql_num_rows($res);$i++)
     {
        $row = mysql_fetch_array($res);
                echo '<div class="testimonial">';
                echo '  <ul>';
                echo '    <li>'.$row['field'].'</li>';
                echo '  </ul>';
                echo '</div>';

        if ($i%3 == 0) echo '</div><div class="testimonial-row">';
     }  
   echo '</div>';

You should not use id="testimonial-row" or id="testimonial". Instead use class="testimonial-row".

Parkyprg
Thanks Parky.. it worked for me..... :)
OM The Eternity
I'm glad I could help you...
Parkyprg