pquery Posted December 23, 2007 Share Posted December 23, 2007 Hi, I'm having problem with a conditional while loop that looks at each of the checkbox options and then echo's out a statement. The problem I'm having is with the last item when the counter gets down to 1. I first counted all the items checked and then ran the routine the number of times the counter was set for, subtracting 1 from the counter each time. I trying to get it to do something different on the last array item where $flavor[0] In this function I have 4 items in the list to check and checks against if there is only 1 item selected (this part works fine). If you select more then one item, it was kicking out the last selection twice or it was starting to run into a fatal error (Fatal error: Maximum execution time of 30 seconds exceeded in on line 158) before I commented out a loop you'll see in my code below. Now it's just not displaying anything if there's more then one checkbox selected I need this routine for a few other functions in my program so your help is greatly appreciated. here is the PHP function flavors () { global $flavor; $i = 3; // counter for the number of checkboxes minus 1 for the array $c = 0; // counter foreach($flavor as $f) { $c++; } if ($c !=1) { //while ($c > 1) //{ foreach($flavor as $f) { if ($flavor[$i] != NULL) { echo ("$f, "); $c= $c-1; $i = $i-1; } } //} if ($i <= 0) { echo ("or $f "); return; } } elseif ($c == 1) { echo ("$f only please"); //echo ("<br> i= $i"); //echo ("<br> c= $c"); } } thanks again Quote Link to comment https://forums.phpfreaks.com/topic/82870-solved-conditional-while-loop-on-an-array/ Share on other sites More sharing options...
MadTechie Posted December 23, 2007 Share Posted December 23, 2007 i am not sure what your asking for exactly.. i think you mean this <?php function flavors () { global $flavor; $i = 3; // counter for the number of checkboxes minus 1 for the array $c = 0; // counter /*foreach($flavor as $f) { $c++; }*/ // replaced $c = count($flavor); if ($c !=1) { foreach($flavor as $f) { if ($i > 0) { if ($flavor[$i] != NULL) { echo ("$f, "); $c= $c-1; $i = $i-1; } }else{ echo ("or $f "); } } }elseif ($c == 1){ echo ("$f only please"); //echo ("<br> i= $i"); //echo ("<br> c= $c"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/82870-solved-conditional-while-loop-on-an-array/#findComment-421474 Share on other sites More sharing options...
pquery Posted December 23, 2007 Author Share Posted December 23, 2007 Thanks for your quick reply, and for showing me some simplification of my routine. I've implemented your solution, but I'm still getting the same result for the flavor function of no echo's, and the single choice routine stopped working. So I had to change the order a little so that foreach was at the top so the $f variable was available on that last elseif statement. I also took out the extra unused code. I'll try and explain it better: if the flavors of the array are: flavors("cheese","bbq","ketchup","mustard"), and you checked all four flavors then I was trying to get the out put to echo - mustard, ketchup, bbq, or cheese (since it was counting down) The last time I was getting output I was getting mustard, ketchup, bbq, cheese or cheese if all four were selected. Hence the last item selected was being repeated twice. Once in the function flavors () { global $flavor; $i = 3; // counter for the number of checkboxes minus 1 for the array $c = count($flavor);// counter foreach($flavor as $f) { if ($c !=1) { if ($i > 0) // I think this is where some of the problem is { if ($flavor[$i] != NULL) { echo ("$f, "); $c= $c-1; $i = $i-1; } }else{ echo ("or $f "); } }elseif ($c == 1){ echo ("$f only please"); //echo ("<br> i= $i"); //echo ("<br> c= $c"); } } } Quote Link to comment https://forums.phpfreaks.com/topic/82870-solved-conditional-while-loop-on-an-array/#findComment-421499 Share on other sites More sharing options...
MadTechie Posted December 23, 2007 Share Posted December 23, 2007 Okay heres my test script.. <?php $flavor[] = "mustard"; $flavor[] = "bbq"; $flavor[] = "ketchup"; $flavor[] = null; //debug testing $flavor[] = "super mustard"; flavors(); function flavors () { global $flavor; //not sure why you have a checkbox counter! //$i = 3; // counter for the number of checkboxes minus 1 for the array $c = count($flavor);// counter $counter = $c; foreach($flavor as $f) { if ($c > 1) //updated { $counter--; if ($counter > 0) // I think this is where some of the problem is { if (!is_null($f)) { echo ("$f, "); } }else{ echo ("or $f "); } }elseif ($c == 1){ echo ("$f only please"); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/82870-solved-conditional-while-loop-on-an-array/#findComment-421503 Share on other sites More sharing options...
pquery Posted December 23, 2007 Author Share Posted December 23, 2007 Thanks, that totally fixed my issue Quote Link to comment https://forums.phpfreaks.com/topic/82870-solved-conditional-while-loop-on-an-array/#findComment-421508 Share on other sites More sharing options...
MadTechie Posted December 23, 2007 Share Posted December 23, 2007 Revised edition: cleans up commas etc <?php $flavor[] = "mustard"; $flavor[] = "bbq"; $flavor[] = "ketchup"; $flavor[] = null; //debug testing $flavor[] = "super mustard"; flavors(); function flavors () { global $flavor; //not sure why you have a checkbox counter! //$i = 3; // counter for the number of checkboxes minus 1 for the array //rebuild clean array $cflavor = array(); foreach($flavor as $f) { if (!is_null($f)) $cflavor[] = $f; } $flavor = $cflavor; unset($cflavor); $c = count($flavor);// counter $counter = 0; foreach($flavor as $f) { if ($c > 1) //updated { $counter++; if ($counter < $c) { echo $f; echo ($counter == ($c-1)?" ":", "); }else{ echo "or $f "; } }elseif ($c == 1){ echo ("$f only please"); } } } ?> Also if this is solved please click topic solved bottom left Quote Link to comment https://forums.phpfreaks.com/topic/82870-solved-conditional-while-loop-on-an-array/#findComment-421510 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.