Jump to content

Help with depreciated 'each' function replacement


Recommended Posts

Have some code I loop through results and now moving to php 8 the '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>";
							 }
						

 

+1 to the bad DB design.

Instead of
 

+-------+-----------------+------------------------+------------------------+
|  id   | image_url       |   img_cap_1            |   img_cap_2            |
+-------+-----------------+------------------------+------------------------+
|  1    | imgA;imgB;imgC  | capA_1; capB_1;capC_1  | capA_1; capB_1;capC_1  |
+-------+-----------------+------------------------+------------------------+

you should have
 

+-------+-------------+---------------+--------------+
|  id   | image_url   |   img_cap_1   |   img_cap_2  |
+-------+-------------+---------------+--------------+
|  1    |     imgA    |     capA_1    |    capA_2    |
|  1    |     imgB    |     capB_1    |    capB_2    |
|  1    |     imgC    |     capC_1    |    capC_2    |
+-------+-------------+---------------+--------------+

 

I knew when I was writing that there would be some purist raising an objection. However I decided the returns in this case weren't worth the extra effort and took a pragmatic approach.

35 minutes ago, Barand said:

I knew when I was writing that there would be some purist raising an objection. However I decided the returns in this case weren't worth the extra effort and took a pragmatic approach.

It's hardly purist. It is basic Database Normalization. What are you going to do if there are more image caps, keep adding columns? +2 to the ADDITIONAL bad DB design. How is the OP supposed to learn when a forum expert shows them the wrong way.

Edited by benanamen

I've seen lots of images with captions. I've also seen lots of images with a caption and a sub-caption. What I haven't seen are images with a caption, sub-caption, sub-sub-caption et al.

Perhaps naming the columns "Caption" and "Subcaption" (without the number suffices) will allow you to sleep tonight :)

Edited by Barand
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.