当前位置: 动力学知识库 > 问答 > 编程问答 >

php - show only three records and then move to next line

问题描述:

This is a very basic problem but I am very new to PHP.

I need to show results in such scenario that only three records in same line then add <br /> and then on next line, same thing should happend. I am unable to make its logic and in a great trouble :(

Right now, I am just using the simple way i.e.

while($data = mysql_fetch_array($res_set)) {

?>

<div><?php echo $data['name']?><img src="images/<?php echo $data['image']?>" /></div>

<?php

}

this is my code

$cnt = 0;

while ($prd = mysql_fetch_array($res)) {

?>

<div class="imageRow">

<?php

$cat_id = $prd['cat_id'];

$sql = "select * from tbl_category where id = $cat_id";

$cat_res = mysql_query($sql);

$cat_data = mysql_fetch_array($cat_res);

$cat_name = $cat_data['name'];

?>

<div class="set">

<div class="single first">

<a href="<?php echo $ru ?>admin/product_images/<?php echo $cat_name ?>/thumb/<?php echo $prd['thumb_img'] ?>" rel="lightbox[plants]"><img src="<?php echo $ru ?>admin/product_images/<?php echo $cat_name ?>/large/<?php echo $prd['thumb_img'] ?>" style="height: 100px; width: 100px;" /></a><br />

<a href="<?php echo $ru ?>contact_form.php?id=<?php echo $prd['id'] ?>">Choose</a>

</div>

</div>

</div>

<?php

$cnt = $cnt++;

if($cnt%3 == 0) {echo "<br />";}

}

//echo $cnt;

?>

any help will be appreciated.

Thanks

网友答案:
<div class="imageRow">
<?php
$i = 0;
while($data = mysql_fetch_array($res_set)) { ?>
    <div>
        <?php echo $data['name']; ?><img src="images/<?php echo $data['image']; ?>" />
    </div>
    <?php
    $i++;
    if($i % 3 == 0) {
        echo '</div><div class="imageRow"><br />';
    }
}
?>
</div>

Explanation:

Set the variable $i to a number, which will then be used to keep track of how many items you've written.

Then, within the while loop, increment $i ($i++), which is the same as $i = $i + 1; By doing this, you always know which item you're on - whatever the value of $i is. Some people choose to set it to 1 initially then increment at the very end - other like to set it to 0, and set it near the beginning - either is completely fine - whatever you need it to do / whichever way you like better.

Lastly, check if $i is evenly divisible by 3 (kind of like remainder - it's called "mod" and is represented by the percent symbol). If it is, then echo a line break.

网友答案:

Try this

<?php
echo "<div>";

while($data = mysql_fetch_attay($res_set)) {
?>
 <div style="width:30%; float:left"><?php echo $data['name]?><img src="images/<?php echo $data['image']?>" /></div>
<?php
}

echo "</div>";

?>

Output

<div>
    <div>1</div> <div>2</div> <div>3</div>
    <div>1</div> <div>2</div> <div>3</div>
    <div>1</div> <div>2</div> <div>3</div>
</div>
网友答案:

That's some super ugly code you have there. This will do what you need, and is cleaner.

<?php
$i=1;
while($data = mysql_fetch_attay($res_set)) {
    $frame = '<div><img src="%s" /></div>';
    printf($frame, $data['image']);
    if($i % 3 == 0) { echo '<br />'; }
    $i++;
}

Also, <img> elements are already block elements to begin with, you really don't need to wrap another <div> around them.

分享给朋友:
您可能感兴趣的文章:
随机阅读: