0

我将如何在下面获取我的代码以输出.choice一个元素中的 5 个.row元素

        <?php
        $count = 0;

        foreach($imp_photos['data'] as $photo)
        {
            $count = ($count + 1)%5;
            if ($count == 0) {

        ?>
                <div class="choice">
                    <input type="checkbox" name="photos[]" value="<?php echo $photo->id; ?>" class="fb_checkbox"><img src="<?php echo $photo->picture; ?>" class="clear" />
                </div>

        <?php
        }
        else
        {
        ?>
            <div class="choice">
                <input type="checkbox" name="photos[]" value="<?php echo $photo->id; ?>" class="fb_checkbox"><img src="<?php echo $photo->picture; ?>" class="clear" />
            </div>

        <?php
        }
        }

        ?>

我知道我必须做一些事情,%但我无法让它发挥作用。

4

2 回答 2

2

你计算计数的方式是错误的。它变成 1 -> .20 -> 1.20 -> .24... 这是一种更直接的方法。

$count++;
if($count%5 == 0) { /* Do your div stuff here */ }
于 2012-09-20T19:28:10.753 回答
0

未经测试,但应该做的伎俩

<?php
    //Prepare the data
    $rows = array();
    $row = array();
    $rowSize = 5;
    foreach($imp_photos["data"] AS $photo) {
        $choice = array(
            "id" => $photo->id,
            "picture" => $photo->picture;
        );
        $row[] = $choice;

        if(sizeof($row) == $rowSize) {
            $rows[] = $row;
            $row = array();
        }
    }
?>

<!-- display the data -->
<?php foreach($rows AS $row) { ?>
    <div class="row">
        <?php foreach($row AS $choice) { ?>
            <input type="checkbox" name="photos[]" value="<?php echo $choice["id"]; ?>" class="fb_checkbox"><img src="<?php echo $choice["picture"]; ?>" class="clear" />
        <?php } //foreach row ?>
    </div>
<?php } //foreach $rows ?>
于 2012-09-20T19:34:30.337 回答