Page 1 of 1

Using querystrings to increment a counter

Posted: Mon Feb 06, 2006 11:04 am
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]

Posted: Mon Feb 06, 2006 11:27 am
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>';

Posted: Mon Feb 06, 2006 11:30 am
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>';

Posted: Mon Feb 06, 2006 12:19 pm
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