dtpn60029 2014-04-24 17:20
浏览 30
已采纳

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

Hey guys pls i'm trying to retrieve values from my database using foreach but one of the fields/row in the table is an array which i serialized into the database. i have no problem retrieving this value but when i try obtaining the array values from the db. they add up to the next value. i.e if first array count is 5 and second array count is 3, when displaying the second row it gives 8 results instead of 3 here's my code below.

       $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++;
            }
        }

and the image array is

 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
    )
)

when i echo the imageList variable the images add up such that second row images = first row images + second rowimages and so on searched a lot but most results are for inputing values into the database. Thanks in advance

  • 写回答

1条回答 默认 最新

  • dtz55359 2014-04-25 02:00
    关注

    The variable $imageset is not reset. Note that in the second foreach it receives itself, plus the previous one. Try it now:

    $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++;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示