Solved

php count function not working latest edition of php 4

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
insert into database through form with dynamic fields. 2 31
hasing a url 16 28
I wonder how people fake their ip address? 3 39
Php recording post 4 45
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

778 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