Have some code I loop through results and now moving to php 8 each function is no longer working. Need some help with a replacement.

   			
    while ($row1 = mysqli_fetch_array($result1) )
	{ 
$pics_array = explode(';', $row1['image_url']);
$cap1_array = explode(';', $row1['img_cap_1']);
$cap2_array = explode(';', $row1['img_cap_2']);
	}

      $count_total = count($pics_array);
        for ($counter=0; $counter < $count_total; $counter++)
		{
	$line = each ($pics_array);
		print "<div class=\"slide\"><div class=\"image\"><a href=\"#\"><img src=\"slider/$line[value]\" /></a></div>";

	$line1 = each ($cap1_array);
		print"<span class=\"caption1\">$line1[value]</span>";

	$line2 = each ($cap2_array);
		print"<span class=\"caption2\">$line2[value]</span>";
                print"</div>";
		}
		?>

    While you should store the data as one row per image, not as a delimited list within one row, your goal for the current method would be to loop over the array keys and access the corresponding values in the three $pics, $cap1, and $cap2 arrays -

    // loop over the array keys (from one array) and access the corresponding values in each array
    foreach(array_keys($pics_array) as $key)
    {
    	print "<div class='slide'><div class='image'><a href='#'><img src='slider/$pics_array[$key]' /></a></div>";
    	print "<span class='caption1'>$cap1_array[$key]</span>";
    	print "<span class='caption2'>$cap2_array[$key]</span>";
    	print "</div>";
    }
    

    pbismad

    Thanks. That worked. I was thinking I needed to break the arrays out separately

      Write a Reply...