OM The Eternity
OM The Eternity

Reputation: 16204

How to display the <div> as per data count displayed below?

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?

Upvotes: 0

Views: 377

Answers (3)

CristiC
CristiC

Reputation: 22698

   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".

Upvotes: 1

Eric Fortin
Eric Fortin

Reputation: 7603

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();
}

Upvotes: 0

PIM
PIM

Reputation: 314

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

Upvotes: 2

Related Questions