Jump to content

Is this right


Recommended Posts

I have this foreach statement that is supposed to take data from an array and process it to put it an e-mail, but I've noticed that the script does it as many times as your user id. Can anyone figure out why?

Array Structure:
Array
(
[1] => Array
(
[name] => John Doe
[email] => john@doe.com
[6] => Array
(
[start] => 1141124400
[end] => 1141131600
[name] => Training
[desc] => We'll have training today
)

[7] => Array
(
[start] => 1141207200
[end] => 1141221600
[name] => Party
[desc] => Party on high street
)

)
)

PHP code:
[code]
foreach($events as $m => $id){ //per player

        
foreach($id as $j => $k){
if(($j != "name")&&($j != "email")){
$text.=$k['name']." is on ".date("l dS of F \f\\r\o\m H:i",$k['start'])." till ".date("H:i",$k['end']).".
".$k['desc'].".

";
}
}
}    [/code]

Does anyone know how to fix this?
Link to comment
Share on other sites

Exactly what output are you expecting from the above? I get

[code]Training is on Tuesday 28th of February from 11:00 till 13:00
We'll have training today

Party is on Wednesday 1st of March from 10:00 till 14:00
Party on high street[/code]

BTW, you will find a TAB key on the left end of your keyboard, 4 rows up. It does help others to read your code.

[code]$events = array
(
    '1' => array
        (
            'name' => 'John Doe',
            'email' => 'john@doe.com',
            '6' => array
                (
                'start' => 1141124400,
                'end' => 1141131600,
                'name' => 'Training',
                'desc' => "We'll have training today"
                ),

            '7' => array
                (
                'start' => 1141207200,
                'end' => 1141221600,
                'name' => 'Party',
                'desc' => 'Party on high street'
                )

        )
);

foreach($events as $m => $id){ //per player
    foreach($id as $j => $k){
        if(($j != "name")&&($j != "email")){
            $text.= "{$k['name']} is on ".date("l jS of F \f\\r\o\m H:i",$k['start'])." till ".date("H:i",$k['end']). "<br>{$k['desc']}<br><br>";
        }
    }
}
echo $text;[/code]

Link to comment
Share on other sites

Hey,

the outcome is right and sorry for the tab. But php repeats this as many times as the user's id. So if my user id is 3 I'll get:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Training is on Tuesday 28th of February from 11:00 till 13:00
We'll have training today

Party is on Wednesday 1st of March from 10:00 till 14:00
Party on high street

Training is on Tuesday 28th of February from 11:00 till 13:00
We'll have training today

Party is on Wednesday 1st of March from 10:00 till 14:00
Party on high street

Training is on Tuesday 28th of February from 11:00 till 13:00
We'll have training today

Party is on Wednesday 1st of March from 10:00 till 14:00
Party on high street[/quote]

Is it because I have a foreach in another foreach or is some variable in the wrong place?
Link to comment
Share on other sites

[!--quoteo(post=350439:date=Mar 1 2006, 12:02 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Mar 1 2006, 12:02 AM) [snapback]350439[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Is the code you posted inside another loop?it'
[/quote]

No, it's inside the two foreach loops:
[code]foreach($events as $m => $id){ //per player

        
foreach($id as $j => $k){
   if(($j != "name")&&($j != "email")){
      $text.=$k['name']." is on ".date("l dS of F \f\\r\o\m H:i",$k['start'])." till ".date("H:i",$k['end']).".
      ".$k['desc'].".

      ";
   }
}
}  [/code]
Link to comment
Share on other sites

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.