Solved

php count function not working latest edition of php 4

Posted on 2004-09-28
2
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

627 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