1

I am kinda lost with what I am trying to achieve here, really need some help with the following.

While fetching DB using script provided below, I need to add <div class="clear"></div> after every third occurrence and if it is less than 3, lets say 2 or even one and there is no more after that after that last one.

Here is my script

<?
    $template_query = 'SELECT * FROM Files WHERE parentpageID = :id and show_in_category = "1" ORDER BY ID asc';
    $res = $db->prepare($template_query);
    $res->execute(array(':id' => $current));


$add_rowNum = 0;
while ($info = $res -> fetch()){
    $add_rowNum++;
    $templateTitle = $info['templateTitle'];
    $add_refering_url = $info['referring_url'];
    $templ_link = $category_folder.$add_refering_url;
    $teaserText = $info['teaserText'];
    $path_to_add_images = $image_path.$info['ImagePath'].DS;
    $add_img_info = $path_to_add_images.$info['templateImage'];
    $add_img_alt && $add_img_title && $templateTitle = $info['templateTitle'];
    list($width, $height, $type, $attr) = getimagesize($add_img_info);
    $last_class = ($add_rowNum == $res->rowCount()) ? 'frame' : 'frame frame_margin';

print<<<END
<div class="$last_class">
<div class="prod">
<div class="title"><a href="$templ_link">$templateTitle
<img src="$add_img_info" alt="$add_img_alt" $attr title="$add_img_title"></a>
</div>
</div>
<div class="prd">
$teaserText
</div>
</div>

END;
}
?>

your help is highly appreciated

AlexB
  • 2,164
  • 6
  • 27
  • 61
  • possible duplicate of: http://stackoverflow.com/questions/9008522/insert-tr-after-every-third-loop[link] – Niket Malik Aug 05 '13 at 13:22
  • what about 5 records. It will having 2 div clear or just 1 after first three. – Deepak Biswal Aug 05 '13 at 13:22
  • @DeepakBiswal, it should have 2 div clear after 5 records, or if there is 7 records than it will have to be 3 div clear – AlexB Aug 05 '13 at 13:24
  • You could make a variable to hold a count, increment that count each run. then use an if statement that checks if it is equal to 3, if it is print the div and reset count. then after the while check if the count != 0, if not print another div after the while loop. –  Aug 05 '13 at 13:25

3 Answers3

1

Use below code:

If(($add_rowNum % 3) == 0) {
   echo "<div class='clear'></div>";
}

Add this code after the END syntax.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
0

Using with %

if ($add_rowNum % 3 == 1)
   echo '<div class="clear"></div>';
Bora
  • 10,529
  • 5
  • 43
  • 73
0

Try this. I think this logic will solve this problem!

$total_records = 10;
for($i=1;$i<=$total_records;$i++) {
    echo "Hi";
    if($i%3 == 0) {
        echo "<br/>==================<br/>";
    }

    if(($i == $total_records) && ($total_records%3 != 0)) {
        echo "<br/>==================<br/>";
    }
}
Deepak Biswal
  • 4,280
  • 2
  • 20
  • 37