Jump to content


Photo

Is this right


  • Please log in to reply
4 replies to this topic

#1 running_out_of_imagination

running_out_of_imagination
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationSouthampton, UK

Posted 27 February 2006 - 06:45 PM

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:
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'].".

";
}
}
}    

Does anyone know how to fix this?

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 27 February 2006 - 07:45 PM

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

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

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.

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


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 running_out_of_imagination

running_out_of_imagination
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationSouthampton, UK

Posted 28 February 2006 - 11:48 PM

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?

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 01 March 2006 - 12:02 AM

Is the code you posted inside another loop?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 running_out_of_imagination

running_out_of_imagination
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationSouthampton, UK

Posted 01 March 2006 - 12:20 AM

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

No, it's inside the two foreach loops:
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'].".

      ";
   }
}
}  





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users