problem: Involving recalling variables in a for function
Posted: Wed Jan 19, 2011 4:36 pm
Greetings and thank you for allowing me to post in your forum! I am stumped as to what I can use and how to phrase the following answer to this in context:
I have a shopping cart with a for statement for each item in the cart. The chunk of code of this for is:
as you can see the $i is 0 for the first item, 2 for the next item, 3 for the next and so on. Each item can have multiple packages as seen in this chunk of code:
I want to compile the extracted values for the first package's dimensions and weight as well as the second, if applicable, into a way that the session can transfer the values to a second page. The second page needs the shipping dimensions lined up one-by-one, with no regard to what items they belong to (it's not important). I already tried
$_SESSION['shiplength']=$itemshippinginfo[0]['length'];
but it only counts the packages of the last item on the page. I've tried adding $i and $j inside brackets to no avail. How can I make it so no matter the amount of items, all the shipping dimensions are arranged so they can be equivalent to:
$shippingitem1[length]
$shippingitem1[width]
$shippingitem1[height]
$shippingitem1[weight]
$shippingitem2[length]
$shippingitem2[width]
$shippingitem2[height]
$shippingitem2[weight]
and so on for the second page, counting every package in every item in the cart? I already tried adding $i (for amount of items) and $j (amount of packages per item) in brackets but it doesn't give any output.
I know it involves using Sessions for carrying arrays but I don't know how to make each package for each item count with an individual number when the second chunk of code I posted are entirely inside a for statement. Any solutions?
I have a shopping cart with a for statement for each item in the cart. The chunk of code of this for is:
Code: Select all
for($i=0;$i<sizeof($_SESSION['cart']);$i++){
$val=$_SESSION['cart'][$i];
$items= split("\|\|",$val);
if($items[0]!=""){
$query = "SELECT *,category from products,categories where categories.id=products.pid and products.id=" . $items[0];
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$line = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_free_result($result); Code: Select all
//if shipping weight is present then extract details
$itemshippinginfo = array();
$dims = $line['dims'];
$j=NULL;
$itemdims = substr($line['dims'],strpos($line['dims'],'shipdims'.$j)+strlen('shipdims'.$j)+1,strpos($line['dims'],'shipweight'.$j)-strpos($line['dims'],'shipdims'.$j)-strlen('shipweight'.$j));
if(strpos($line['dims'],"shipdims".($j+2)))
{
$itemweight = substr($line['dims'],strpos($line['dims'],'shipweight'.$j)+strlen('shipweight'.$j)+1,strpos($line['dims'],' ',strpos($line['dims'],'shipweight'.$j))-(strpos($line['dims'],'shipweight'.$j)+strlen('shipweight'.$j))-1);
}
else
{
$itemweight = substr($line['dims'],strpos($line['dims'],'shipweight'.$j)+strlen('shipweight'.$j)+1);
}
$itemshippinginfo[0]['length'] = substr($itemdims,0,strpos(strtolower($itemdims),'x'));
$itemshippinginfo[0]['width'] = substr($itemdims,strpos(strtolower($itemdims),'x')+1,strpos(strtolower($itemdims),'x',strpos(strtolower($itemdims),'x')+1)-strpos(strtolower($itemdims),'x')-1);
$itemshippinginfo[0]['height'] = substr($itemdims,strpos(strtolower($itemdims),'x',strpos(strtolower($itemdims),'x')+1)+1);
$itemshippinginfo[0]['weight'] = $itemweight;
$j=2;
while(strpos($line['dims'],"shipdims".$j))
{
$itemdims = substr($line['dims'],strpos($line['dims'],'shipdims'.$j)+strlen('shipdims'.$j)+1,strpos($line['dims'],'shipweight'.$j)-strpos($line['dims'],'shipdims'.$j)-strlen('shipweight'.$j));
if(strpos($line['dims'],"shipdims".($j+1)))
{
$itemweight = substr($line['dims'],strpos($line['dims'],'shipweight'.$j)+strlen('shipweight'.$j)+1,strpos($line['dims'],' ',strpos($line['dims'],'shipweight'.$j))-(strpos($line['dims'],'shipweight'.$j)+strlen('shipweight'.$j))-1);
}
else
{
$itemweight = substr($line['dims'],strpos($line['dims'],'shipweight'.$j)+strlen('shipweight'.$j)+1);
}
$itemshippinginfo[$j]['length'] = substr($itemdims,0,strpos(strtolower($itemdims),'x'));
$itemshippinginfo[$j]['width'] = substr($itemdims,strpos(strtolower($itemdims),'x')+1,strpos(strtolower($itemdims),'x',strpos(strtolower($itemdims),'x')+1)-strpos(strtolower($itemdims),'x')-1);
$itemshippinginfo[$j]['height'] = substr($itemdims,strpos(strtolower($itemdims),'x',strpos(strtolower($itemdims),'x')+1)+1);
$itemshippinginfo[$j]['weight'] = $itemweight;
$j++;
}
}
if(is_numeric($itemshippinginfo[0]['length'])) {
echo "<br /><span class='style1'>Shipping Dimensions: </span>".$itemshippinginfo[0]['length']."" Long, ".$itemshippinginfo[0]['width']."" Wide, ".$itemshippinginfo[0]['height']."" High. Weight of package, ".$itemshippinginfo[0]['weight']." pounds.";
if($itemshippinginfo[2]['length']!=''){
echo "<br /><span class='style1'>Shipping Dimensions of Second Box: </span>".$itemshippinginfo[2]['length']."" Long, ".$itemshippinginfo[2]['width']."" Wide, ".$itemshippinginfo[2]['height']."" High. Weight of package, ".$itemshippinginfo[2]['weight']." pounds.";
}
;}
elseif(is_null($itemshippinginfo[0]['length'])) {echo "Shipping dimensions not yet provided." ;}
else {echo "Shipping dimensions not yet provided." ;}
//if shipcost present extract it
$shipcostKV = strstr($dims,'shipcost=');
$shipcost = substr($shipcostKV,9); // returns all but the keyword=
$n = strpos($shipcost,' ');
//$shipcost = substr($shipcost,0,$n); // elimindated remainder of string
if ($shipcost!=''){
echo "<br /><span class='style1'>Shipping Cost:</span> $" .$shipcost;
}
echo "<br /><br />";
$numbcheck = substr($itemdims, 0, 1);
if(is_numeric($numbcheck)){
echo $itemdims."<br /><br />";
var_export($itemshippinginfo);}
else{echo $shipcost;} $_SESSION['shiplength']=$itemshippinginfo[0]['length'];
but it only counts the packages of the last item on the page. I've tried adding $i and $j inside brackets to no avail. How can I make it so no matter the amount of items, all the shipping dimensions are arranged so they can be equivalent to:
$shippingitem1[length]
$shippingitem1[width]
$shippingitem1[height]
$shippingitem1[weight]
$shippingitem2[length]
$shippingitem2[width]
$shippingitem2[height]
$shippingitem2[weight]
and so on for the second page, counting every package in every item in the cart? I already tried adding $i (for amount of items) and $j (amount of packages per item) in brackets but it doesn't give any output.
I know it involves using Sessions for carrying arrays but I don't know how to make each package for each item count with an individual number when the second chunk of code I posted are entirely inside a for statement. Any solutions?