Foreach循环没有正确地从数据库中检索值

时间:2014-04-24 17:20:08

标签: php arrays foreach

嘿伙计们,我试图使用foreach从我的数据库中检索值,但表中的一个字段/行是我序列化到数据库中的数组。我没有问题检索此值,但当我尝试从数据库中获取数组值时。他们加起来是下一个值。即如果第一个数组计数为5且第二个数组计数为3,则在显示第二行时,它会给出8个结果,而不是3个,这是我的代码,如下所示。

       $imageDisplay->getall('images',array('id', 'ASC' ));
        if (!$imageDisplay->count()) {
            # code...
            $imagelist = "<h2>No Images Yet</h2>";
        }
        else {
            $x = 0;
            foreach ($imageDisplay->results() as $imageDisplay) {
                $event = $imageDisplay->event; 
                $cover = $imageDisplay->image_cover;
                $image = unserialize($imageDisplay->image);
                $date = $imageDisplay->date;
                foreach ($image as $key => $image) {
                    # code...
                    $imageset .= "<img src='../images/".$event."/".$image."' />";
                    $x++;
                }
                $imagelist .= "<div class='servicelist'><br /><h2>". $event ."</h2><br><img src='../images/". $cover ."' />
                <div class='clear'></div>".$imageset."
                <span> <a href='index.php?navsection=gallery&editgallerySection=".$imageDisplay->id."'>Edit</a> <a href='index.php?navsection=gallery&deleteID=".$imageDisplay->id."'>Delete</a></span></div><div class='clear high'></div>";
                #$x++;
            }
        }

并且图像数组是

 Array
 (
 [0] => Array
    (
        [0] => event1.jpg
        [1] => event2.jpg
        [2] => event3.jpg
        [3] => event4.jpg
        [4] => event5.jpg
    )

[1] => Array
    (
        [0] => event1.jpg
        [1] => event2.jpg
        [2] => event3.jpg
        [3] => event4.jpg
        [4] => event5.jpg
        [5] => event6.jpg
        [6] => event7.jpg
        [7] => event8.jpg
        [8] => event9.jpg
    )

[2] => Array
    (
        [0] => bkg-03.png
        [1] => oil.png
        [2] => yamaha.jpg
        [3] => yamaha2.jpg
    )
)

当我回显imageList变量时,图像相加,使得第二行图像=第一行图像+第二行图像等等 搜索了很多,但大多数结果是用于将值输入数据库。提前致谢

1 个答案:

答案 0 :(得分:1)

变量$imageset未重置。请注意,在第二个foreach中它接收自己,加上前一个。立即尝试:

$imageDisplay->getall('images',array('id', 'ASC' ));
if (!$imageDisplay->count()) {
    # code...
    $imagelist = "<h2>No Images Yet</h2>";
}
else {
    $x = 0;
    foreach ($imageDisplay->results() as $imageDisplay) {
        $event = $imageDisplay->event; 
        $cover = $imageDisplay->image_cover;
        $image = unserialize($imageDisplay->image);
        $date = $imageDisplay->date;
        $imageset = '';
        foreach ($image as $key => $image) {
            # code...
            $imageset .= "<img src='../images/".$event."/".$image."' />";
            $x++;
        }
        $imagelist .= "<div class='servicelist'><br /><h2>". $event ."</h2>
        <br>
        <img src='../images/". $cover ."' />
        <div class='clear'></div>".$imageset."
        <span> 
            <a href='index.php?navsection=gallery&editgallerySection=".$imageDisplay->id."'>
                Edit
            </a> 
            <a href='index.php?navsection=gallery&deleteID=".$imageDisplay->id."'>
                Delete
            </a>
        </span></div><div class='clear high'></div>";
        # $x++;
    }
}