Page 1 of 1

problem: Involving recalling variables in a for function

Posted: Wed Jan 19, 2011 4:36 pm
by Cyberen
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:

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

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

Re: problem: Involving recalling variables in a for function

Posted: Wed Jan 19, 2011 6:12 pm
by bunglero
Have you tried something like:

... above the for loop:

Code: Select all

$_SESSION['itemshippinginfo'] = array();
... very last line inside the loop:

Code: Select all

$_SESSION['itemshippinginfo'][] = itemshippinginfo;
... notice the empty brackets...

This will add the itemshippinginfo array into separate keys in the $_SESSION['itemshippinginfo'] array.
To access it on the next page, try:

Code: Select all

for($i=0; $i < sizeof($_SESSION['itemshippinginfo']); $i++){
    var_export($_SESSION['itemshippinginfo'][$i]);
}

Re: problem: Involving recalling variables in a for function

Posted: Wed Jan 19, 2011 7:41 pm
by Cyberen
Thanks bunglero, but I tried putting it in the places you recommend and it just returned "itemshippinginfo" as many times as there were entries.
Here's four more problems I've encountered, true to the nature of programming :banghead:

I managed to pass values from one page to another but the success stops there. I even tried to make a rudimentary while statement to number it. But numerous problems remain. If you can help I would like to know what to do about these:

1. Maybe its because of the page viewcart5.php leads to (being a very simple one which I use to check the output) but when I remove something from the cart, all the values are gone but the code to count it and leave a "remove from cart" and a horizontal line there stay. I tried copying the stuff up to and including the for statement which seemingly counts the thing and still no luck. I tried putting in line 245 but I need the $i to count sequentially for the SESSION's sakes. I'm sure there's other problems with it too. The button for removing from cart is on line 404. Could the SESSION info I put in for shipping be interfering with the GET?

2. For items with multiple packages, I just couldn't add +1 to the $i, because what if there were items after that in the cart? I added plus fifty but for some reason the code is miscalculating the values, and it might be easier to program some way so packages, no matter how many are in what order in what items, are added sequentially to the next page.

3. How do I calculate how many times package dimensions are added to a page, so I know how many numbers to make $g equal (on the latter test page).

4. How do I convert the output of testhidden.php into being numbered correctly so the string . $_SESSION['shiplength'.$g] . can be turned into a
$package$g->setParameter('length',$_SESSION['shiplength'.$g]); series of dimensions? Notice how I'm using $g for incremental adding of values. Are arrays any part of this?

Any help would be appreciated.

the shopping cart PHP:

Code: Select all

 <?PHP
$total=0;
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);
  //if (isset($line)){
  mysql_free_result($result);   
?>
          
        <br />
        <hr />
        <span class="style1">
        <img src="<?PHP echo $line['thumbnail']?>" alt="product" /><br />
        <?= stripslashes($line['category'])?>
        </span>
        <?= stripslashes($line['name']) ?>
        <? if($line['dims']!=''){ ?>
        <br />
        <span class="style1">Dimensions:</span>
        <?=
stripslashes (substr(($line['dims']), 0, 70))?>
        <?
 //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> &#36;" .$shipcost;
}
echo "<br /><br />";
	$numbcheck = substr($itemdims, 0, 1);
	if(is_numeric($numbcheck)){
	echo $itemdims."<br /><br />";
var_export($itemshippinginfo);}
else{echo $shipcost;}
echo $i;
echo $j."<br />";
$_SESSION['shiplength$i']=$itemshippinginfo[0]['length'];
$_SESSION['shipwidth$i']=$itemshippinginfo[0]['width'];
$_SESSION['shipheight$i']=$itemshippinginfo[0]['height'];
$_SESSION['shipweight$i']=$itemshippinginfo[0]['weight'];
 
echo $_SESSION['shiplength$i']."<br />";
echo $_SESSION['shipwidth$i']."<br />";
echo $_SESSION['shipheight$i']."<br />";
echo $_SESSION['shipweight$i']."<br />";
if (strlen($itemshippinginfo[2]['length'])>0) {
$_SESSION['shiplength($i+50)']=$itemshippinginfo[2]['length'];
$_SESSION['shipwidth($i+50)']=$itemshippinginfo[2]['width'];
$_SESSION['shipheight($i+50)']=$itemshippinginfo[2]['height'];
$_SESSION['shipweight($i+50)']=$itemshippinginfo[2]['weight'];

echo $_SESSION['shiplength($i+50)']."<br />";
echo $_SESSION['shipwidth($i+50)']."<br />";
echo $_SESSION['shipheight($i+50)']."<br />";
echo $_SESSION['shipweight($i+50)']."<br />";
}
	?>
        <br />
        <span class="style1">Glass Color:</span>
        <?= stripslashes($items[1]) ?>
        <?}?>
        <? if($items[2]!=''){ ?>
        <br />
        <span class="style1">Finish:</span>
        <?= stripslashes($items[2]) ?>
        <?}?>
        <? if($items[3]!=''){ ?>
        <br />
        <span class="style1">Stocks:</span>
        <?= stripslashes($items[3]) ?>
        <?}?>
        <? if($items[4]!=''){ ?>
        <br />
        <span class="style1">Cord Color:</span>
        <?= stripslashes($items[4]) ?>
        <?}?>
        <? if($items[5]!=''){ 
 $ceiling= split("\|",stripslashes($items[5]));
?>
        <br />
        <span class="style1">Ceiling Height:</span>
        <?= $ceiling[0] ?>
        '
        <?}?>
        <? if($items[6]!=''){ 
?>
        <br />
        <span class="style1">Glass Shape:</span>
        <?= $items[6] ?>
        <?}?>
        <? if($items[7]!=''){ 
?>
        <br />
        <span class="style1">Leaf Option:</span>
        <?= $items[7] ?>
        <?}?>
        <? if($items[8]!=''){ ?>
        <br />
        <span class="style1">Ceiling Cap:</span>
        <?=  stripslashes($items[8]) ?>
        <?}?>
        <br />
        <span class="style1">Price:</span> $
        <? 
if($line['ceiling']=='Y' && $line['static']=='N'){
	
	$query = "SELECT * from ceiling where deleted=0 and pid=" . $items[0];
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$ceil_cost = mysql_fetch_array($result, MYSQL_ASSOC);
	$line['price']=$ceil_cost['h' . $ceiling[0]];
	mysql_free_result($result); 
}

echo number_format($line['price'],2);
?>
        Select one of the following options to modify this item<br />
        <br />
        <? echo '<a class="sub" href="viewcart5.php?back=' . urlencode($_GET['back']) . '&action=rem&id=' . $i . '"></a> '; ?><br />
      <? 
  $total+=$line['price'];
 }
//}
?>
the destination file PHP:

Code: Select all

<?PHP session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?
$g = 0;
while ($g <= 100) {
if (strlen($_SESSION['shiplength'.$g])>0) {
echo "The next product's shipping length was " . $_SESSION['shiplength'.$g] . " inches.<br />";
echo "The next product's shipping width was " . $_SESSION['shipwidth'.$g] . " inches.<br />";
echo "The next product's shipping height was " . $_SESSION['shipheight'.$g] . " inches.<br />";
echo "The next product's shipping weight was " . $_SESSION['shipweight'.$g] . " pounds.<br />";
}
echo "<br />";
echo $g++;
}

//for($i=0; $i < sizeof($_SESSION['itemshippinginfo']); $i++){
//    var_export($_SESSION['itemshippinginfo'][$i]);}
?>
</body>
</html>

Re: problem: Involving recalling variables in a for function

Posted: Thu Jan 20, 2011 3:40 pm
by bunglero
OOPS! I messed up on the line for the very last line inside the loop:

$_SESSION['itemshippinginfo'][] = $itemshippinginfo; //(was missing the '$')

What this should do is take all the information from the $itemshippinginfo array and put it into another array in the $_SESSION variable. On your next page you can loop through the $_SESSION['itemshippinginfo'] to see all the values that were there in the array at the time of the loop. I had left the '$' off and that's why it just echoed out "itemshippinginfo" over and over. This all assumes that I'm seeing your problem correctly.

As for the other questions, it would help a lot to see the code in action, and as I don't have a working test environment, is there a way you can paste a link to the cart on your server?

Re: problem: Involving recalling variables in a for function

Posted: Thu Jan 20, 2011 7:29 pm
by Cyberen
Thanks for your help, I eventually got it to work!

What I did was create a new variable, $g, and made it add once for each item and more than once if the item has two packages.
Then I used $_SESSION to create variables in conjunction with $g so each value was different.
At the end I counted $g to determine how many times the while loop would have to be in effect to show the whole string.
Here's the code I modified to make it work.
Keep in mind that the for loop counts $i. This is all inside a for loop that counts the amount of products on the page.

Code: Select all

echo $i;
echo $j."<br />";
$_SESSION['shiplength'][$g]=$itemshippinginfo[0]['length'];
$_SESSION['shipwidth'][$g]=$itemshippinginfo[0]['width'];
$_SESSION['shipheight'][$g]=$itemshippinginfo[0]['height'];
$_SESSION['shipweight'][$g]=$itemshippinginfo[0]['weight'];
 
echo $_SESSION['shiplength'][$g]."<br />";
echo $_SESSION['shipwidth'][$g]."<br />";
echo $_SESSION['shipheight'][$g]."<br />";
echo $_SESSION['shipweight'][$g]."<br />";
if (strlen($itemshippinginfo[2]['length'])>0) {
echo "<br /> g is".$g++."<br />";
$_SESSION['shiplength'][$g]=$itemshippinginfo[2]['length'];
$_SESSION['shipwidth'][$g]=$itemshippinginfo[2]['width'];
$_SESSION['shipheight'][$g]=$itemshippinginfo[2]['height'];
$_SESSION['shipweight'][$g]=$itemshippinginfo[2]['weight'];

echo $_SESSION['shiplength'][$g]."<br />";
echo $_SESSION['shipwidth'][$g]."<br />";
echo $_SESSION['shipheight'][$g]."<br />";
echo $_SESSION['shipweight'][$g]."<br />";
}
Getting the latter page to work was tough but it turns out I had to make the variable on the next page equal the $_SESSION data, not the other way around. Order counts!

Code: Select all

<?PHP session_start(); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?
echo $_SESSION['packagenummers']."the session variable";
$e = $_SESSION['packagenummers'];
echo $e."theactuale";
$f = 1;
echo "<br />";
while ($f <= $e) {
echo "The next product's shipping length was " . $_SESSION['shiplength'][$f]." inches.<br />";
echo "The next product's shipping width was " . $_SESSION['shipwidth'][$f]." inches.<br />";
echo "The next product's shipping height was " . $_SESSION['shipheight'][$f]." inches.<br />";
echo "The next product's shipping weight was " . $_SESSION['shipweight'][$f]." pounds.<br />";
echo "<br />";
$f++;
}
//for($i=0; $i < sizeof($_SESSION['itemshippinginfo']); $i++){
//    var_export($_SESSION['itemshippinginfo'][$i]);}
?>
Now that the variables are all here ordered I can do with them as I may. :drunk: