PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Feb 23, 2020 2:19 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Sat Mar 31, 2012 5:29 pm 
Offline
Forum Commoner

Joined: Thu Sep 29, 2011 8:53 am
Posts: 46


Top
 Profile  
 
PostPosted: Sun Apr 01, 2012 9:05 am 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
Depending on how the data is stored within the database you can retrieve it and place it inside a variable and pass this along to the mail() function.
Syntax: [ Download ] [ Hide ]
<?php
 // connect to the database and retrieve the information
 // from the db and place it inside mysql_query()
 while ($row = mysql_fetch_array($sql)) {
    $msg = $row['products'];
 }

 mail($to, $subj, $msg, $headers);
?>

Greatly simplified but this should serve as basis on how to achieve what you have in mind;

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Sun Apr 01, 2012 2:22 pm 
Offline
Forum Commoner

Joined: Thu Sep 29, 2011 8:53 am
Posts: 46


Top
 Profile  
 
PostPosted: Sun Apr 01, 2012 4:31 pm 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
If the information is retrieved from the database there is no need to pass it along with the form; before sending the email you can retrieve it, place it inside the variable and send the message. The only value you might have to pass along will be the session id (or a unique value to the specific user) which will allow you to retrieve the products from the database.

As for unpacking the items; i forgot to add the concatenation operator the my code snippet; if used in conjunction with the equals symbol (.=) it will append the new data to any existing data; you could place the data inside an array and loop through it. I think this will be an easier option
Syntax: [ Download ] [ Hide ]
<?php
 // connect to the database and retrieve the information
 // from the db and place it inside mysql_query()
 while ($row = mysql_fetch_array($sql)) {
    $msg[] = $row['products'];
 }

 foreach ($msg as $product) {
      // note the use of the concatenation operator (.)
      $mailContent .= $product . "\r\n";
 }

 // to add this to your existing message

 $message = 'Name' . $_POST['name'];
 $message .= $mailContent;
 
 mail($to, $subj, $message, $headers);
?>
 

You still don't mention how you store the data; each product in it's own row, as a group separated by commas, etc

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Wed Apr 04, 2012 1:59 pm 
Offline
Forum Commoner

Joined: Thu Sep 29, 2011 8:53 am
Posts: 46


Top
 Profile  
 
PostPosted: Wed Apr 04, 2012 4:49 pm 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Wed Apr 04, 2012 5:07 pm 
Offline
Forum Commoner

Joined: Thu Sep 29, 2011 8:53 am
Posts: 46


Top
 Profile  
 
PostPosted: Thu Apr 05, 2012 1:47 am 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
Does the undefined error refer to $mailContent? If so you can try the code below
Syntax: [ Download ] [ Hide ]
<?php
// try using this
if (isset($mailContent)) {
    $mailContent .= $product . "\r\n";
}
else {
    $mailContent = $product . "\r\n";
}
?>

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Thu Apr 05, 2012 10:53 am 
Offline
Forum Commoner

Joined: Thu Sep 29, 2011 8:53 am
Posts: 46
hi,

the undefined error message refers to $product


Top
 Profile  
 
PostPosted: Thu Apr 05, 2012 5:50 pm 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Fri Apr 06, 2012 10:34 am 
Offline
Forum Commoner

Joined: Thu Sep 29, 2011 8:53 am
Posts: 46
hello there,

Just to let you know that I appreciate your help. I tried your second code, by itself it works. When I add it into mine, the error comes.

heres a screenshot.
error.PNG
error.PNG [ 59.55 KiB | Viewed 3547 times ]


Top
 Profile  
 
PostPosted: Fri Apr 06, 2012 10:38 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
$msg is empty, so the foreach fails. Since $mailContent is only defined inside the foreach (which fails) it also doesn't exist. Hence the three errors.

EDIT: I also noticed you're trying to iterate over the same result set twice, which isn't going to work.

Try replacing this:
Syntax: [ Download ] [ Hide ]
<?php

$query = "SELECT OrderID, Product FROM orderbox_tbl where UserSessionID = '{$userSid}'";
$result = mysql_query($query);
$num = mysql_num_rows($result);
// comment start
// this section displays cart contents - for test purposes only
while ($row = mysql_fetch_assoc($result))
{
    echo "<tr>
                        <td>"
. $row['Product'] . "</td>
                </tr>"
;
}
//this is where your suggestions start
while ($row = mysql_fetch_array($result))
{
    $msg[] = $row['Product'];
}
foreach ($msg as $product)
{
    // note the use of the concatenation operator (.)
    $mailContent .= $product . "\r\n";
}

// comment end
// this displays the session ID above the table
echo $_SESSION['sessID'];

?>


With this:
Syntax: [ Download ] [ Hide ]
<?php

$query = "SELECT OrderID, Product FROM orderbox_tbl where UserSessionID = '{$userSid}'";
$result = mysql_query($query);

// Initialize variables
$mailContent = "";
// $msg doesn't seem to serve any purpose, so let's cut out the middleman
// $msg = array();

while ($row = mysql_fetch_array($result))
{
    echo "<tr><td>{$row['Product']}</td></tr>";
    // $msg[] = $row['Product'];
    $mailContent .= "{$product}\r\n";
}
echo $_SESSION['sessID'];

?>

_________________


Top
 Profile  
 
PostPosted: Fri Apr 06, 2012 11:33 am 
Offline
Forum Commoner

Joined: Thu Sep 29, 2011 8:53 am
Posts: 46


Top
 Profile  
 
PostPosted: Fri Apr 06, 2012 11:39 am 
Offline
Forum Commoner

Joined: Thu Sep 29, 2011 8:53 am
Posts: 46


Top
 Profile  
 
PostPosted: Fri Apr 06, 2012 12:29 pm 
Offline
Forum Commoner

Joined: Thu Sep 29, 2011 8:53 am
Posts: 46


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group