session arrays


#1

This may be a simple question for you folks. I have studied books up and down but still no solution.
I have collected some inputs from a form, name and email. Then i have some items from a session input. I can show the result on the screen but I want to mail the session arrays to my mail adr.
The $line could be several lines. Echoing this shows each lines with product id, name and price and all the lines.
Sending the $line in a mail shows the last line, so i think the other lines has been wtitten over. Can someone give me a hint how to make a text string for mail purposes with the rest of the lines ?Yes I have tried for some nights now :)

[php]
session_start();
$session=session_id();
header(“Cache-control: private”); //IE 6 Fix

if (isset($_SESSION[“item_count”]) && $_SESSION[“item_count”] > 0) {
$total = 0;
for($i=0; $i<$_SESSION[“item_count”]; $i++) {
$ident=$_SESSION[‘items’][$i][0];
$name=$_SESSION[‘items’][$i][1];
$pri=$_SESSION[“items”][$i][2];
$pieces=$_SESSION[“items”][$i][3];
$line="$ident -,$name, Price:$pri,-,Pieces:$piecesn";

echo “$lines”;
}

The mail functin is ok, I am receiving without problem but onely one line.
[/php]


#2

If I am understanding this correctly you would like to make an array into a string. If this is the case then I would suggest the implode() function.

http://www.php.net/implode


#3

Yes I want a text string:

[php]
$line="$ident -,$name, Price:$pri,-,Pieces:$piecesn";

echo “$lines”;
}

[/php]
As long as this code is within the loop,- the lines are echoed correctly line by line, so maybe I ned to generate a multi dim array. Moving the echo"lines" out of the loop results in only one (the last) in $line.


#4

try the concatination symbol (.) with the variables.

$line=$ident. ‘-, ‘.$name.’, Price:’.$pri.’,-,Pieces: '.$pieces.‘n’;


#5

Thank you for the answer.
Tested it out but $line still contains the last line. You can test it here
http://ozonart.com/testshop/


#6

Quick question… Where are these variables coming from. If the original code is at the very top of the page, where are the variables being loaded? If it from a form then you will need to retrieve them with $_GET or $_POST depending on what the form action is (requiered with register_globals = off).

Ex:
$ident = $_POST[‘ident’];

Reference:
http://www.php.net/variables.predefined


#7

The variables are coming from the saved session as the customer has filled a basket with product1, product2…and retrived by opening this session.
If you have visited my test shop, this script is retrieving the session (coockie), (page2 in the shop). My problem is that I do not know how to colect the different products (lines) in one array, say $lines[]…for the purpose of sending it as a text string later.
Echo to the screen works fine with an
tag at the end of the $line and as long as echo is inside the loop.


#8

have you looked at array_push?

Reference:
http://www.php.net/manual/en/function.array-push.php


#9

Thanks again. I will look inti it but sadly, array_push() does not create an array if the array doesn’t exist. So if you’re pushing the first element onto an array, you need to check and create it manually but I am looking into it. Did some test but couldn’t get it working so far.

Array_merge() merges the elements of one or more arrays ($line) together so that the values of one $line are appended to the end of the previous one. It returns the resulting array. I will do my best testing this too. Something like:

[php]
$line[]=$line2[$i]($ident. ‘-, ‘.$name.’, Price:’.$pri.’,-,Pieces: '.$pieces.‘n’);
[/php]

but the index will be the same and probably cause errors…


#10

$line = array();
array_push($line, $whatever_to_go_in_there);


#11

Jeep, that seems to do the job, thank you again.