LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2004, 02:02 PM   #1
Gerardoj
Member
 
Registered: May 2003
Location: Somewhere over the Rainbow
Distribution: Slackware 9.x, Knoppix, Damn Small Linux, RedHat.
Posts: 465

Rep: Reputation: 30
PHP Mail and Foreach function


Hi Guys. I recently finished my session cart but I have a couple of questions:

When I want to display the cart items I do:

PHP Code:
 $items $cart->get_contents();

  foreach(
$items as $item) {
    echo 
"Code/ID :".$item['id']."<br>";
    echo 
"Quantity:".$item['qty']."<br>";
    echo 
"Price   :".$item['price']."<br>";
    echo 
"Info    :".$item['info']."<br>";
    echo 
"Subtotal".$item['subtotal']."<br>";
  } 
But now I would like to send all the items of the cart by email, but I dont know how can I assign all the above function to a variable and send it as:

PHP Code:
mail($email'Subject' $alltheitems,
     
"From: $email\r\n" .
     
"Reply-To: $email\r\n" .
     
"X-Mailer: PHP/" phpversion()); 
Or how can you suggest me to send all the items via email?

I will appreciate some help.

Thanks a lot.
 
Old 10-06-2004, 02:27 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: PHP Mail and Foreach function

Something like this worked for me:

PHP Code:
$items $cart->get_contents();

foreach(
$items as $item) {
    
$alltheitems  "Code/ID : ".$item['id'] ."\n";
    
$alltheitems .= "Quantity: ".$item['qty'] ."\n";
    
$alltheitems .= "Price   : ".$item['price'] ."\n";
    
$alltheitems .= "Info    : ".$item['info'] ."\n";
    
$alltheitems .= "Subtotal: ".$item['subtotal'] ."\n";
}

$email "you@localhost";   // dummy example address
$from "shop@localhost";    // dummy example address
    
mail$email,
      
"Your order",
      
$alltheitems,
      
"From: $from\r\n".
      
"X-Mailer: PHP/" phpversion() ); 

Last edited by Hko; 10-06-2004 at 02:31 PM.
 
Old 10-06-2004, 02:59 PM   #3
Gerardoj
Member
 
Registered: May 2003
Location: Somewhere over the Rainbow
Distribution: Slackware 9.x, Knoppix, Damn Small Linux, RedHat.
Posts: 465

Original Poster
Rep: Reputation: 30
Thanks Hko. I have another question. Do I need to decare $alltheitems inside the loop or outside?, cause I tried and it works fine but just send the last request item on the cart.

PD: what does .= do?

Thanks in advance..
 
Old 10-06-2004, 03:06 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by Gerardoj
Do I need to decare $alltheitems inside the loop or outside?, cause I tried and it works fine but just send the last request item on the cart.
Hmm, I did try the script and for me it sent all the lines.
In PHP you don't need to declare a variable, just assign it a value.

Quote:
PD: what does .= do?
I appends a string to a string variable.
PHP Code:
$strvar .= "some text"
does the same thing as:
PHP Code:
$strvar $strvar "some text"
Note that in the loop I posted, the first time $alltheitems is assigned a string it does not have the dot ( . ).

Did you actually use the dots as in ".=" when you tried my script?
If not, that would explain why you get only the last item...

Last edited by Hko; 10-06-2004 at 03:11 PM.
 
Old 10-06-2004, 05:46 PM   #5
Gerardoj
Member
 
Registered: May 2003
Location: Somewhere over the Rainbow
Distribution: Slackware 9.x, Knoppix, Damn Small Linux, RedHat.
Posts: 465

Original Poster
Rep: Reputation: 30
Thanks lot Hko. now Its working fine.

I just changed it for:

PHP Code:
$alltheitems="";
foreach(
$items as $item) {
    
$alltheitems .= "Code/ID : ".$item['id'] ."\n";
    
$alltheitems .= "Quantity: ".$item['qty'] ."\n";
    
$alltheitems .= "Price   : ".$item['price'] ."\n";
    
$alltheitems .= "Info    : ".$item['info'] ."\n";
    
$alltheitems .= "Subtotal: ".$item['subtotal'] ."\n";

Thanks again
 
Old 10-07-2004, 05:50 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by Gerardoj
I just changed it for:

PHP Code:
$alltheitems="";
foreach(
$items as $item) {
// [..snip..] 
That looks OK.
Didn't it work without initializing before the loop? ($alltheitems="";)
(just wondering)

Last edited by Hko; 10-07-2004 at 06:09 AM.
 
Old 10-08-2004, 05:24 PM   #7
Gerardoj
Member
 
Registered: May 2003
Location: Somewhere over the Rainbow
Distribution: Slackware 9.x, Knoppix, Damn Small Linux, RedHat.
Posts: 465

Original Poster
Rep: Reputation: 30
I tried but It didnt worked me.. Maybe I did it wrong...

But thanks again...

Regards....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP !!! php mail function akamaru607 Programming 22 08-26-2005 02:44 PM
PHP Mail() function doesn't work Messiadbunny Linux - Software 4 06-11-2005 04:40 AM
sendmail and PHP mail() function tudekoen Debian 1 05-09-2004 03:53 AM
PHP - mail function saravanan1979 Programming 2 08-06-2003 01:46 AM
Using the PHP mail() Function !?!? Hdata Programming 0 06-22-2003 06:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration