Jump to content

Beginner problem: Involving recalling variables in a for function

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Cyberen

Cyberen

    Newbie

  • Members
  • PipPip
  • 19 posts
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

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);

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:

 //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;}

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?

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?

#2
sam_l

sam_l

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
The big problem here is how you have your data stored. Consider breaking the items weight, height, length, etc. into separate fields to prevent that ugly parsing you have going on. In the end this will produce more readable code, an easily readable table, and make updating or modifying your tables much easier as well.


// pseudo code

$ShippingInformation = array();

for (all items in $_SESSION)

{

  $result = SELECT * FROM `ItemTable` WHERE `ItemTable.name` = 'CurrentItemName/Designation'

  $ShippingInfo[CurrenItemName/Designation][height/width/weight/etc.] = $result[height/width/weight/etc.]

}


You can also make displaying much easier as well.


for(all $ShippingInformation)

{

  echo 'Shipping Dimensions (Length x Width x Height): '.$ShippingInformation['length'].' x '.$ShippingInformation['width'].' x '.$ShippingInformation['height'].'\n'.

}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users