Solved

php count function not working latest edition of php 4

Posted on 2004-09-28
2
196 Views
Last Modified: 2012-05-05
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
Comment
Question by:aot2002
2 Comments
 
LVL 6

Accepted Solution

by:
peyox earned 500 total points
ID: 12176449
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
 
LVL 1

Author Comment

by:aot2002
ID: 12176552
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question