Rita_Ruah Posted April 1, 2013 Share Posted April 1, 2013 (edited) Hello, first of all thanks for letting me register in this forum, I've been reading some of the topics and they really helped me so far! I am trying to make a loop for my school project but unfortunately I couldn't solve it alone... These are my variables: <?php $_POST["1st_phase_aL"] = "Paradise"; $_POST["1st_phase_aL_act_a"] = "Sonya and me"; $_POST["1st_phase_aL_act_b"] = "Katie and me"; $phases = array("1st_phase_"); $letters = range('A', 'Z'); $firsts = array(); for($i = 'a';$i <= 'z';$i++){ for($j = 'a'; $j <= 'z';$j++){ foreach ($phases as $phase) { foreach ($letters as $letter) { $firsts[] = $phase . $i . $letter; $firsts[] = $phase . $i . $letter . "_act_" . $j; } } } } foreach($firsts as $first) { if(isset($_POST[$first]) && $_POST[$first]!="") echo "*" . $_POST[$first] . "<br/>"; } ?> This results in a double "paradise" plus Katie and Sonya. Why does paradise appears 2 times? I have 3 cycles like this for all the championship stages (1st stage, 2nd stage, 3rd stage, semi-final and great-final), every single one of them have letters from A to Z in the structure that I presented in this example... Can somebody help me with a great loop to solve this problem? And tell me one thing, even with a loop with all the phases, if I get them all in the array first[], how would I display them on the HTML? I want to separate them by phase, that's why I am doing a loop for each phase... Short version: - Loop is giving me duplicate result (major problem) - Would like to have a loop for all the competition stages - If I have an array with all the competition stages, how would I echo them into the html (I cannot refer to the phase name, right? I want to group them into phases) THANKS! Edited April 1, 2013 by Rita_Ruah Quote Link to comment https://forums.phpfreaks.com/topic/276391-first-timer-php-loop-is-making-me-dizzy/ Share on other sites More sharing options...
Rita_Ruah Posted April 1, 2013 Author Share Posted April 1, 2013 I tried using => without luck I am desperate, I am sure that I will fail my test tomorrow, it's a loop like this. Quote Link to comment https://forums.phpfreaks.com/topic/276391-first-timer-php-loop-is-making-me-dizzy/#findComment-1422323 Share on other sites More sharing options...
jcbones Posted April 1, 2013 Share Posted April 1, 2013 I usually don't give answers to homework, but I will give a few pointers. 1. figure out what the output is suppose to be (look like). 2. use print_r or var_dump to find out what the arrays look like. (wrap them in <pre></pre> tags first). 3. => in foreach is only used if you need the index of the array as well as the value. *note* if I were going to sort them by phases, I would store the phase as a key, and make $firsts into a multi-dim array. Quote Link to comment https://forums.phpfreaks.com/topic/276391-first-timer-php-loop-is-making-me-dizzy/#findComment-1422326 Share on other sites More sharing options...
Rita_Ruah Posted April 1, 2013 Author Share Posted April 1, 2013 (edited) The output is supposed to be all the options that are SET and different than !=, but no repeats... I don't know how to do the two options that you considered (2 and 3) Can you help me with this loop please? I am really confused and have no idea where to start... I am sorry... I want to create a loop that goes like this: name_of_phase(maybe-a-loop-for-the-phases?)_(a-to-z)(A-TO-Z)_act_(a-to-z) If I have to create multiple loops I won't mind I just want to solve this matter =( I have no ideia what to do... And thanks for your help Edited April 1, 2013 by Rita_Ruah Quote Link to comment https://forums.phpfreaks.com/topic/276391-first-timer-php-loop-is-making-me-dizzy/#findComment-1422328 Share on other sites More sharing options...
jcbones Posted April 1, 2013 Share Posted April 1, 2013 You should probably be talking to the professor about your confusion. OK. Lets break down the problem. Are you listing EVERY single phase, or only the ones appearing in the $_POST global array? If you are making an array of every possible phase and act, and aren't planing on using them, you are creating way to much overhead. These two loops don't behave like you would expect them to: for($i = 'a'; $i <= 'z'; $i++) for($j = 'a'; $j <= 'z'; $j++) it would be better to set a range, and then use foreach: $alpha = range('a','z'); $alpha2 = $alpha; This alone will cut your $firsts array from 450K values to 18K values. To check your $firsts array, put this line at the end of your script: echo '<pre>' . print_r($firsts,true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/276391-first-timer-php-loop-is-making-me-dizzy/#findComment-1422337 Share on other sites More sharing options...
Rita_Ruah Posted April 1, 2013 Author Share Posted April 1, 2013 (edited) Once again, thanks for your help, I feel that you are pointing me in the right direction! So I need: $alpha = range('A', 'Z'); $alpha2 = range('a', 'z'); Capitals and non capitals, right? And what should I do in the foreaches? I would love to solve all the possible options (all phase and all letters) with one loop but I don't mind having more than one. My professor doesn't answer to questions, he says that we have to fight our way out of the problems Edited April 1, 2013 by Rita_Ruah Quote Link to comment https://forums.phpfreaks.com/topic/276391-first-timer-php-loop-is-making-me-dizzy/#findComment-1422339 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.