Jump to content

Recommended Posts

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 by Rita_Ruah
Link to comment
https://forums.phpfreaks.com/topic/276391-first-timer-php-loop-is-making-me-dizzy/
Share on other sites

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.

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)  :sweat:

 

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 by Rita_Ruah

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>';

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 by Rita_Ruah
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.