1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: PHP not counting correctly

Discussion in 'Web Design & Development' started by namenotfound, Dec 28, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. namenotfound

    namenotfound Thread Starter

    Joined:
    Apr 30, 2005
    Messages:
    3,012
    On one of my pages I list items, and for each item I add a bit of PHP so I can quickly total up the items at the end.

    Example:

    a
    b
    c
    d
    e
    f
    g

    "I have 7 items"

    The PHP looks like this:

    <?php $x++;?>a
    <?php $x++;?>b
    <?php $x++;?>c
    <?php $x++;?>d
    <?php $x++;?>e
    <?php $x++;?>f
    <?php $x++;?>g

    <?php echo "<div class='center'>I have <b>$x</b> items.</div> \n"; ?>

    It will list it as "10 items" instead of "7 items"

    Why would it be adding +3 to the number?

    Also, if you have a better way of doing this, please let me know.
     
  2. ehymel

    ehymel

    Joined:
    Aug 12, 2007
    Messages:
    695
    Do you set $x to zero before counting?

    $x = 0;
     
  3. namenotfound

    namenotfound Thread Starter

    Joined:
    Apr 30, 2005
    Messages:
    3,012
    I didn't think to do that. It works, thanks! (y)
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/971298