Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Increment a variable by using a hyperlink 3

Status
Not open for further replies.

scousethemoose

Programmer
Jul 14, 2002
69
AU
I'm trying to create a hyperlink which will when clicked increment a variable and then reload the page using the variables new value. The variable is used to to control how many times a file upload field is displayed.

The example below is what I'm trying to do.

<a href="test.php?variable=$variable++">test<\a>

I'm fairly new to PHP, about a week so any help would be greatly appreciated.
 
Something like this might do the trick...

[tt]
<?php

# initialize variable
$num_fields = 1;

# if the url contains the value, set the variable
if ($_GET['num_fields'])
$num_fields = $_GET['num_fields'];

# write the link
echo '<a href="test.php?num_fields=' . $num_fields .'">test</a>';

?>
[/tt]

Hope that helps.

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
Except for the increment......


Code:
<?php
print '<html><body><a href="test_increment.php?variable=';

if (isset($_GET['variable']))
{
	print ++$_GET['variable'];
}
else
{
	print 1;
}

print '">click</a></body></html>';
?>


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Doh!

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
It's been a public holiday in the UK, that's my excuse for today !
 
Hey me again this code is working great for the increment, i've been able to restrict it to a maximum of 5.

I've also been playing around with it trying to decrement the value this time but I cant see to get it to work. I wanted to set the minimum decrement value to 1 and so when clicked if for example the variable was 1 already it would stay as 1 or if it was 5 it would end up as 4 etc.

Any tips?
 
I'm now able to perform an imcrement and decrement however it only seems to work correctly when I only use one of the hyperlinks and not a combination.

They will not function correctly when I for example increment till 4 is reached using the add link then use the remove link it will only increment to 3.

Heres the code. Is there a problem in the way my if clauses are structured?

Code:
<?php
print '<html><body><a href="test_increment.php?number_of_fields=';

// check to see if the number_of_fields has been set
if(isset($_GET['number_of_fields']))
{
	// allow a maximum of 4 fields
	if ($_GET['number_of_fields'] >= 4)
	{
		print 4;
	}
	// maximum not reached, increment
	else
	{
		print ++$_GET['number_of_fields'];
	}
}
else
{
	print 2;
}

print '">Add</a>&nbsp;';

print '<a href="test_increment.php?number_of_fields=';

// check to see if the number_of_fields has been set
if (isset($_GET['number_of_fields']))
{
	// allow a minimum of 1 fields
	if ($_GET['number_of_fields'] <= 1)
	{	
		print 1;
	}
	// minimum not reached, decrement
	else
	{
		print --$_GET['number_of_fields'];
	}
}
else
{
	// allow a minimum of 1 fields
	print 1;
}

print '">Remove</a></body></html>';
?>
 
When you calcuate the value for the increment value you actually increment $_GET['number_of_fields'] by 1. Then in the decrement code you decrease it by 1, thus returning it to its orginal value.
Try this code:
Code:
<?php
print '<html><body><a href="test_increment.php?number_of_fields=';

// check to see if the number_of_fields has been set
if(isset($_GET['number_of_fields']))
{
    // allow a maximum of 4 fields
    if ($_GET['number_of_fields'] >= 4)
    {
        print 4;
    }
    // maximum not reached, increment
    else
    {
        [b]print $_GET['number_of_fields'] + 1;[/b]
    }
}
else
{
    print 2;
}

print '">Add</a>&nbsp;';

print '<a href="test_increment.php?number_of_fields=';

// check to see if the number_of_fields has been set
if (isset($_GET['number_of_fields']))
{
    // allow a minimum of 1 fields
    if ($_GET['number_of_fields'] <= 1)
    {    
        print 1;
    }
    // minimum not reached, decrement
    else
    {
        [b]print $_GET['number_of_fields'] - 1;[/b]
    }
}
else
{
    // allow a minimum of 1 fields
    print 1;
}

print '">Remove</a></body></html>';
?>

This code doesn't actually alter the value of $_GET['number_of_fields']

Westbury

If it aint broke, redesign it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top