Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Web Design & Development Web Design & Development
Search Search
Search for:
Tech Support Guy > > >

Solved: PHP not counting correctly


(!)

namenotfound's Avatar
Member with 3,009 posts.
THREAD STARTER
 
Join Date: Apr 2005
Location: New York
28-Dec-2010, 11:03 PM #1
Solved: PHP not counting correctly
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.
ehymel's Avatar
ehymel   (Ernie) ehymel is offline
Member with 695 posts.
 
Join Date: Aug 2007
Location: Texas
28-Dec-2010, 11:13 PM #2
Do you set $x to zero before counting?

$x = 0;
namenotfound's Avatar
Member with 3,009 posts.
THREAD STARTER
 
Join Date: Apr 2005
Location: New York
28-Dec-2010, 11:18 PM #3
Quote:
Originally Posted by ehymel View Post
Do you set $x to zero before counting?

$x = 0;
I didn't think to do that. It works, thanks!
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
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.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Similar Threads
Title Thread Starter Forum Replies Last Post
mysql_num_rows(): supplied argument is not a valid MySQL result resource - login.php Ethanw Web Design & Development 0 26-Sep-2010 10:18 AM
PHP function not working...help please. mtlhd Web Design & Development 3 14-Nov-2008 10:29 AM
Solved: Creating PHP file in PHP - Not really working knight_47 Web Design & Development 3 13-Jan-2007 03:12 PM
PERL/PHP not passing correctly hambone Software Development 1 01-Jul-2005 02:32 AM
{SOLVED} Pc not counting RAM at bootup Manohar Hardware 2 25-Feb-2000 03:03 PM

WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑