Solved

php count function not working latest edition of php 4

Posted on 2004-09-28
2
194 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now