Forum Moderators: coopster
$ordersArray = array();
array_push($ordersArray, $field_qty1, $field_partID1, $field_unit1, $field_unitprice1, $field_total, $field_description1);
$arraySize = count($ordersArray);
$itr=0;
while($arraySize > $itr);
{
//Simplified Code
echo $ordersArray[$itr];
echo $ordersArray[$itr+1];
echo $ordersArray[$itr+2];
echo $ordersArray[$itr+3];
echo $ordersArray[$itr+4];
echo $ordersArray[$itr+5];
$itr = $itr+6;
}
$field_qty1 = $_POST['qty1'];
then on the previous php page as well as this one i'd have:
<?php
$field_qty1 = "";
if (isset($_SESSION['qty1'])) $field_qty1 = $_SESSION['qty1'];
elseif ($_POST['qty1']) {
$_SESSION['qty1'] = $_POST['qty1'];
$field_qty1 = $_SESSION['qty1'];
}
?>
<input type="text" name="qty1" size="5">
How could I incorporate an $ordersArray array variable for example? Any help is much appreciated...hehe, i just picked up php after doing java for a bit but my transition is not as smooth as i'd like it to be :)
So if I have this:
$ordersArray = array();
if (isset($_SESSION['orders'])) {
$ordersArray = $_SESSION['orders'];
}
That should check to see if the session has passed along the array? If so $ordersArray receives the array?
THen I'll push the vars i want added:
array_push($ordersArray, $field_qty1, $field_partID1, $field_unit1, $field_unitprice1, $field_total, $field_description1);
and finally save the new larger array to the session for next time?
$_SESSION['orders'] = $ordersArray;
But this does not work.
$ordersArray[] = array($field_qty1, $field_partID1, $field_unit1, $field_unitprice1, $field_total, $field_description1);
or $_SESSION['orders[]'] = array($field_qty1, $field_partID1, $field_unit1, $field_unitprice1, $field_total, $field_description1);
moreover stupidScript, you don't have to write for($i, ... ,$i++) and then $i = $i+5, but can do it straight:
for($itr = 0; $itr < $arraySize; $itr + 6){...}
Best regards!
Michal Cibor
As for the problem, the array does not appear to be passed on, a new array is created containing only the most recent form field values, yet I need to maintain an array each time adding the new values at the end of it. I think it's gotta be something with the way I'm saving it to the session?
The
$ordersArray[] = array($field_qty1, $field_partID1, $field_unit1, $field_unitprice1, $field_total, $field_description1); does the trick. Such code adds and array to the end of $ordersArray. if you have in
$order = array("a", "b", "c");//And you write
$order[] = "d"; // Then you have "a", "b", "c", "d". now if you write
$order[] = array("e", "f");// Then you get "a","b","c", "d", "e", "f". Without using push
To really check your code see what's in $ordersArray before you push new values and after you push values. There may be some problem in there.
Best regards
Michal Cibor
<?php $thisarr=array(); $thisarr[]="0"; $thisarr[]="1"; $thisarr[]="2"; $thisarr[]=array("3","4","5"); $i=0; foreach($thisarr as $val) { echo $i.": ".$val."<br />\n"; $i++; } ?> In fact,
$thisarray[3] is an array. While it may be comma-separated, it would still need to be dealt with as an array, wouldn't it? Here's the difference:
<?php $thisarr=array(); $thisarr[]="0"; $thisarr[]="1"; $thisarr[]="2"; $thisarr[]=array("3","4","5"); $i=0; foreach($thisarr as $val) { if (is_array($val)) { foreach($val as $subval) { echo $i.": ".$subval."<br />\n"; $i++; } } else { echo $i.": ".$val."<br />\n"; $i++; } } ?>