sks2416 Posted September 27, 2021 Share Posted September 27, 2021 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/313825-help-with-depreciated-each-function-replacement/ Share on other sites More sharing options...
Barand Posted September 27, 2021 Share Posted September 27, 2021 Use foreach() Quote Link to comment https://forums.phpfreaks.com/topic/313825-help-with-depreciated-each-function-replacement/#findComment-1590422 Share on other sites More sharing options...
ginerjm Posted September 27, 2021 Share Posted September 27, 2021 It does appear that you have a bad database design in use. Never combine items into a single column of a table. You have 3 columns that are doing that. Quote Link to comment https://forums.phpfreaks.com/topic/313825-help-with-depreciated-each-function-replacement/#findComment-1590423 Share on other sites More sharing options...
Barand Posted September 27, 2021 Share Posted September 27, 2021 +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 | +-------+-------------+---------------+--------------+ Quote Link to comment https://forums.phpfreaks.com/topic/313825-help-with-depreciated-each-function-replacement/#findComment-1590424 Share on other sites More sharing options...
benanamen Posted September 27, 2021 Share Posted September 27, 2021 Surprised at you @Barand. There should be a separate table for the image caps keyed to the image url id not consecutive numbered columns. I know you know that. Quote Link to comment https://forums.phpfreaks.com/topic/313825-help-with-depreciated-each-function-replacement/#findComment-1590425 Share on other sites More sharing options...
Barand Posted September 27, 2021 Share Posted September 27, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313825-help-with-depreciated-each-function-replacement/#findComment-1590426 Share on other sites More sharing options...
benanamen Posted September 27, 2021 Share Posted September 27, 2021 (edited) 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 September 27, 2021 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/313825-help-with-depreciated-each-function-replacement/#findComment-1590427 Share on other sites More sharing options...
ginerjm Posted September 27, 2021 Share Posted September 27, 2021 C'mon. Give him a break. He was more concerned about the 'each' question and didn't notice the design. I doubt that the OP has the desire to re-design his database, maybe not even the skills. Quote Link to comment https://forums.phpfreaks.com/topic/313825-help-with-depreciated-each-function-replacement/#findComment-1590430 Share on other sites More sharing options...
Barand Posted September 28, 2021 Share Posted September 28, 2021 (edited) 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 September 28, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/313825-help-with-depreciated-each-function-replacement/#findComment-1590431 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.