Using querystrings to increment a counter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dangre
Forum Newbie
Posts: 9
Joined: Fri Jan 13, 2006 5:50 pm

Using querystrings to increment a counter

Post by dangre »

hawleyjr | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Greetings board members!

What I'm trying to do at first seemed very simple, but I'm having quite a difficult time accomplishing the task. I am attempting to have a counter start at zero, and every time a link is clicked (which references the current page), have the counter increment by 1. For instance, here's the code I started with:

Code: Select all

<?
$x = $GET_["counter"];
$x ++;
echo "<a href=test.php?counter=1>Add One</a>"
?>
I see that the code is fundamentally flawed, but am not sure where to start correcting it. Any help would be greatly appreciated.

Thanks!


hawleyjr | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

You may want to use sessions for this. You can also do it by modifying your code like this:

Code: Select all

echo 'Counter is at: ' . $_GET['counter'] . '<br />';

echo '<a href="test.php?counter=' . ( $_GET["counter"] + 1 ) . '">Add One</a>';
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$x = max((isset($_GET['counter']) ? intval($_GET['counter']) : 0),0);
$x++;
echo '<a href="test.php?counter='.$x.'">Add one</a>';
dangre
Forum Newbie
Posts: 9
Joined: Fri Jan 13, 2006 5:50 pm

Post by dangre »

You guys are awesome, works perfectly! Thanks a million! And I will be more mindful of my posting ediquette in the future.

Thanks again,
Dan
Post Reply