Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

php count function not working latest edition of php 4

i thought eveything was ok but it seems that it doesnt want to allow me to add more then one item ?
everytime i do it overwrites the other item????
im trying to get this shopping cart working !!!





        function removeallitems(){
             unset($_SESSION['itemdata']);
             unset($_SESSION['totalamount']);

             if(isset($_SESSION['customerid'])){
                   unset($_SESSION['customerid']);
                   unset($_SESSION['customername']);

                    }

         }



         function additem(){


         if(isset($_SESSION['customerid'])){
                  //keep customer displaying in invoice

                  //Already Set please dont do anything !

                    }else{
                  //set the customer to a session !!
                   $_SESSION['customerid'] = $_REQUEST['customerid'];
                   $_SESSION['customername'] = $_REQUEST['customername'];

                   }

          $itemdata = array();

          $itemdata['qty']  = $_REQUEST['qty'];
          $itemdata['interchangenumber']  = $_REQUEST['interchangenumber'];
          $itemdata['interchangeid']  = $_REQUEST['interchangeid'];
          $itemdata['description']  = $_REQUEST['description'];
          $itemdata['rate']  = $_REQUEST['rate'];
          $itemdata['amount']  = $_REQUEST['amount'];

            if(!isset($_SESSION['itemdata']))
            $_SESSION['itemdata'] = array();

        if(!is_array($_SESSION['itemdata']))
        $_SESSION['itemdata'] = array();



        $_SESSION['itemdata'][] = $itemdata;

        $tmp = count($_SESSION['itemdata']['totalitems']);

         if(count($_SESSION['itemdata']['totalitems']) >= 0){
          $_SESSION['itemdata']['totalitems'] = ++$tmp;

         }else{
          $_SESSION['itemdata']['totalitems']=0;

         }




         }


         function removeitem($removeid){


            for($a=0; $a <= $_SESSION['itemdata']['totalitems']; $a++){

                     if($removeid == $a){
                     unset($_SESSION['itemdata'][$a]);
                     }
            }



         }





         function showallitems(){


                        for($a=0; $a <= $_SESSION['itemdata']['totalitems']; $a++){

                          if(isset($_SESSION['itemdata'][$a])){

                           $qty = $_SESSION['itemdata'][$a]['qty'];
                           $interchangeid = $_SESSION['itemdata'][$a]['interchangeid'];
                           $interchangenumber = $_SESSION['itemdata'][$a]['interchangenumber'];
                           $description = $_SESSION['itemdata'][$a]['description'];
                           $rate = $_SESSION['itemdata'][$a]['rate'];
                           $amount = $_SESSION['itemdata'][$a]['amount'];

                           $totalamount = $amount + $totalamount;
                           $_SESSION['totalamount'] = $totalamount;



echo '
<table width="73%" border="1" cellspacing="0" cellpadding="0" bordercolor="#000066">
  <tr bgcolor="#99CCFF" bordercolor="#000066">
    <td width="10%" align="center" valign="center" true styleid="StyleQty" bgcolor="#990000" bordercolor="#FFFFFF"><font color="#FFFFFF">&nbsp;</font></td>
    <td width="10%" align="center" valign="center" true styleid="StyleQty"><font face="Verdana" size="-1" color="#000000">Quantity</font></td>
      <td width="10%" align="center" valign="center" true styleid="StyleQty">PartID</td>
    <td width="14%" align="center" valign="center" true styleid="StyleQty">Interchange
      # </td>
    <td width="37%" align="center" valign="center" true styleid="StyleItemDesc">
      <font face="Verdana" size="-1" color="#000000">Description</font> </td>
    <td width="8%" align="center" valign="center" true styleid="StyleRate"> <font face="Verdana" size="-1" color="#000000">Rate</font>
    </td>
    <td width="6%" align="center" valign="center" true styleid="StyleItemAmount">
      <font face="Verdana" size="-1" color="#000000">Amount</font> </td>
    <td width="15%" align="center" valign="center" true styleid="StyleTaxFlag">
      <font face="Verdana" size="-1" color="#000000">Tax&nbsp;</font> </td>
  </tr>
  <tr bgcolor="#99CCFF" bordercolor="#000066">
    <td width="10%" align="center" valign="center" true styleid="StyleQty" bgcolor="#990000" bordercolor="#FFFFFF" height="13">&nbsp;
    </td>
    <td width="10%" align="center" valign="center" true styleid="StyleQty" height="13">
      <input type="text" name="qty.'.$a.'" size="4" maxlength="3" value="'.$qty.'" readonly>
    </td>
    <td width="10%" align="center" valign="center" true styleid="StyleQty" height="13">
        <input type="text" name="interchangeid.'.$a.'" value="'.$interchangeid.'" size="9" readonly>
      <br>
      </td>
    <td width="14%" align="center" valign="center" true styleid="StyleQty" height="13"><font face="Verdana" size="-1" color="#000000">
      <input type="text" name="interchangenumber'.$a.'" value="'.$interchangenumber.'" size="13" readonly>
      </font></td>
    <td width="37%" align="center" valign="center" true styleid="StyleItemDesc" height="13"><font face="Verdana" size="-1" color="#000000">
      <input type="text" name="description'.$a.'" size="45" value="'.$description.'" readonly>
      </font></td>
    <td width="8%" align="center" valign="center" true styleid="StyleRate" height="13"><font face="Verdana" size="-1" color="#000000">
      <input type="text" name="rate'.$a.'" size="8"  value="'.$rate.'" readonly>
      </font></td>
    <td width="6%" align="center" valign="center" true styleid="StyleItemAmount" height="13"><font face="Verdana" size="-1" color="#000000">
      <input type="text" name="amount'.$a.'" size="7"  value="'.$amount.'" readonly>
      </font></td>
    <td width="15%" align="center" valign="center" true styleid="StyleTaxFlag" height="13">NT</td>
  </tr>
</table>
<table width="73%" border="1" cellspacing="0" cellpadding="0" bordercolor="#000066">
  <tr>
    <td bgcolor="#990000" width="29%">
      <div align="center"><font color="#FFFFFF"></font></div>
    </td>
    <td bgcolor="#990000" width="38%">
      <div align="center"><font color="#FFFFFF"> </font></div>
    </td>
    <td bgcolor="#990000" width="33%">
      <div align="center"><font color="#FFFFFF">Remove Item</font></div>
    </td>
  </tr>
  <tr>
    <td bgcolor="#990000" width="29%">
    <div align="center"><font color="#FFFFFF">  </font></div>
    </td>
    <td bgcolor="#990000" width="38%">
    <div align="center"><font color="#FFFFFF">  </font></div>
     </td>
    <td bgcolor="#89E1FD" width="33%">
    <!--form name="removeitem" action="invoiceadd.php" method="post"-->
     <!--input type="hidden" name="removeitem" value="true"-->
    <!--input type="hidden" name="removeid" value='.$a.'-->
   <a href="?removeid='.$a.'">REMOVE ITEM</a>

      <!--input type="submit" name="removebutton" value="Remove Item"-->
    </td>
  </tr>
</table>
 <!--/form-->
';



                        }


         }



         }


0
aot2002
Asked:
aot2002
1 Solution
 
peyoxCommented:
count($_SESSION['itemdata']['totalitems']) will always return 1, because you are trying to count elements in non-array variable.

BTW, to check how many items you have, you dont need to use count. $_SESSION['itemdata']['totalitems'] keeps that information.

for($a=0; $a <= $_SESSION['itemdata']['totalitems']; $a++) <--- if you have 5 items, this loop will go through 0...5 (6 iterations).
0
 
aot2002Author Commented:
But doesnt that Zero seem to throw things off should i be setting it to one on the first item ?


>>for($a=0; $a <= $_SESSION['itemdata']['totalitems']; $a++) <--- if you have 5 items, this loop will go through 0...5 (6 iterations).

ok so take out that = then GOT IT...

>>count($_SESSION['itemdata']['totalitems']) will always return 1, because you are trying to count elements in non-array variable.

Right but when i first setup an item should i check for isset on itemdata totalitems then if it is set it as one

if(isset($_SESSION['itemdata']['totalitems'])){

    $_SESSION['itemdata']['totalitems']=$_SESSION['itemdata']['totalitems']++; //increase it    

     }else{

    $_SESSION['itemdata']['totalitems']=1;

}


0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now