Welcome Guest, Not a member yet? Register   Sign In
foreach in mail
#1

[eluser]E303[/eluser]
I have created a small order form that once completed gets emailed. The problem I have is that when I try to run a foreach loop to display the items I get an unexpected T_FOREACH error.

This is my model
Code:
function mailOrder()
    {
        /* GRABS INFO FROM ORDERED ITEMS */
        $sessionid = $this->session->userdata['session_id'];
        
        $query = array('session_id' => $sessionid);
        
        $this->db->select('id, product_id, session_id');
        $this->db->from('ordered_items');
        $this->db->where($query);
        $item_data = $this->db->get();
                
        if ($item_data->num_rows() > 0)
        {
                $row = $item_data->row();
                /* USERS ORDERED ITEMS PRODUCT_ID's TO GET PRODUCT DATA */
            
                $itemid = $row->product_id;
                
                $query = array('id' => $itemid);
                
                $this->db->select('title, price');
                $this->db->from('products');
                $this->db->where($query);
                $product_data = $this->db->get();
                
                $row1 = $product_data->row();
                
                /*USE ORDERED ITEMS SESSION_ID's TO GET USER DATA */
                $Osessionid = $row->session_id;
                
                $query2 = array('id' => $Osessionid);
                
                $this->db->select('username');
                $this->db->from('orders');
                $this->db->where($query2);
                $user_data = $this->db->get();
                
                $row2 = $user_data->row();
                
                $uname = $row2->username;
                $query3 = array('username' => $uname);
                $this->db->select('fname, lname, phone, email, address, address2, postcode, state');
                $this->db->from('users');
                $this->db->where($query3);
                $postal_data = $this->db->get();
                
                $row3 = $postal_data->row();
        
                $emailMessage = "<h1>Order</h1>
                                        <h3>Postal Address</h3>
                                        <p>To:". $row3->fname ." ". $row3->lname ."<br />
                                        Address:". $row3->address ."<br />
                                        Address 2:". $row3->address2 ."<br />
                                        Postcode:". $row3->postcode ."<br />
                                        State:". $row3->state ."<br />
                                        Phone:". $row3->phone ."<br />
                                        Email:". $row3->email ."
                                        
                                        </p>    
                                        <h3>Ordered Items</h3>                
                                        <p>
                                        ".
                                         foreach($row1 as $item_row)
                                         {
                                             $item_row['title'] $item_row['price']
                                            
                                         }
                                        ."
                                        </p>";
        
                $this->email->from('[email protected]', 'Online Ordering');
                $this->email->to('[email protected]');
                $this->email->subject('An online order');
                $this->email->message($emailMessage);    
        
                $this->email->send();

            
        }
                
    }
Any help would be greatly appreciated.
#2

[eluser]Pascal Kriete[/eluser]
You're not printing anything from the foreach. Why don't you just do the foreach outside the e-mail.
Code:
$items;
foreach($row1 as $item_row) {
    $items = $item_row['title'].$item_row['price'];
}

$emailmessage = 'some stuff' . $items . 'more stuff';
#3

[eluser]E303[/eluser]
Thanks, it seems to have worked.

Now the script runs however no email is sent. I don't get any errors or anything like that. Any ideas
#4

[eluser]E303[/eluser]
the emails came through finally. Thanks
#5

[eluser]E303[/eluser]
Just quickly,

the email code is now like this:
Code:
foreach($row1 as $item_row)
                    {
                         $items = $item_row['title'].$item_row['price'];
                                        
                     }
        
                $emailMessage = "<h1>Order</h1>
                                        <h3>Postal Address</h3>
                                        <p>To:". $row3->fname ." ". $row3->lname ."<br />
                                        Address:". $row3->address ."<br />
                                        Address 2:". $row3->address2 ."<br />
                                        Postcode:". $row3->postcode ."<br />
                                        State:". $row3->state ."<br />
                                        Phone:". $row3->phone ."<br />
                                        Email:". $row3->email ."
                                        
                                        </p>    
                                        <h3>Ordered Items</h3>                
                                        <p>" . $items . " </p>";

However in the email the $items prints out the number 44

I have been able to work out that the ordered_items.id is 4 and the cost of the item is 42.50 but they are the only two 4's I can find. Any idea what is going wrong?
#6

[eluser]Seppo[/eluser]
row1 is actually a row, not an array of rows, so in the foreach $item_row have the row data, not an array....

Try replacing this
Code:
$row1 = $product_data->row();
// with
$row1 = $product_data->result_array(); //also casting as an array, not an object
#7

[eluser]E303[/eluser]
thanks for the reply.

I have done that at that is how i get this 44?
#8

[eluser]Seppo[/eluser]
$row1 contains array('id' => 4, 'col1' => 'something', 'col2' => 'something', /* ... */ 'price' => 42.50);
Then, on the foreach, $item_row has 4 the first time, 'something' on the second... and 42.50 on the last one.
Then, $items only cares about the last one (because it is being fully rewritten on each loop), and the last one should be 42.50, you are casting it as string and taking the first character when you try to use it as an array...

It's an odd PHP behaviour
#9

[eluser]E303[/eluser]
so what is the solution?
#10

[eluser]tonanbarbarian[/eluser]
try this
Code:
$items = '';
foreach($row1 as $item_row)
                    {
                         $items .= $item_row['title'].' '.$item_row['price'].'<br />';
                                        
                     }

the problem was very simple.
you are setting item to the item_row data each time through the loop. Instead you have to concatenate to the end of the string
this is simple php stuff




Theme © iAndrew 2016 - Forum software by © MyBB