Than Htut Oo Than Htut Oo - 4 months ago 6
PHP Question

How to pas array in loop in php?

I am facing the problem is that I want to display all image by using array and loop,it is not showing all image ,it does show only one image.Here is my code

if($objResult['Image_Type'] == '01') {
$img11 = array($objResult['Image_Name']);
for ($i=0; $i < count($img11[$i]); $i++) {
$im_mouths = array($img11[$i]);
}
}


here is in the database to extract out all image
in database

the out put is showing only one image by using this code

<? echo $im_mouths[0].'11111'.'<br>';?>
<? echo $im_mouths[1].'222222'.'<br>';?>
<? echo $im_mouths[2].'333333'.'<br>';?>


output

I am not sure I'm doing correctly or not ,help me out this problem.
Thanks :)

Answer

It's only showing one image because you're re-writing $im_mouths each time you loop. You should change your loop to do the following:

$im_mouths = array();
if($objResult['Image_Type'] == '01') {
    $img11 = array($objResult['Image_Name']);
    for ($i=0; $i < count($img11[$i]); $i++) {
        $im_mouths[] = array($img11[$i]);
    }
}

We initialize $im_mouths as an array, then add each element to the array as an element: $im_mouths[] = ....