d.bayo
d.bayo

Reputation: 164

Foreach loop not properly retrieving values from the database

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

Upvotes: 0

Views: 211

Answers (1)

Lucas Henrique
Lucas Henrique

Reputation: 1364

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

Upvotes: 1

Related Questions