Log in

View Full Version : $increment++;$_GET;$WTF



PHPwnd
02-28-2009, 07:16 AM
I wont go into the details of my whole project. Simply present this code example and wonder aloud why it only counts to 1. I thought I had a good grasp on php. The initial design of a 3 table database, and a simple script to enter form data went great. I want to pass a paramater back to the initial form page from itself using get to increment the vaule of a variable. The variable is then used to dynamically add a text input form element for adding multiple childrens names per parent name.

This is the simplest example of the problem I am having without posting the whole script. I want to be able to click that link and watch the number add upon succesive clicks. I get undefined variable on line 7 and 10 and it does return the value 1 on first click but nothing on successive clicks. Any help would be awesome.

<?php

//check to see if variable was passed in query string
if (isset($_GET['increment']))

//if so, add 1 to the sum of $increment, unset$_get in prep for next add, //echo the variable for verification

{
$increment++;
unset($_GET);
}
echo $increment;
?>


<html>
<head>
<title>self.php</title>
</head>
<body>
<a href="self.php?increment=true">Count up</a>
</body>
</html>

JasonDFR
02-28-2009, 08:15 AM
Is this all your code? If so, how you have it written will effectively do nothing. Post all the code if there is more,and / or tell me if this is what you want:

Submit a form or click a link and show a number on the screen that increments by one everytime the form is submitted in succession.

If there is a more succinct way to say it, let me know.

J

JasonDFR
02-28-2009, 09:06 AM
I ran the code you have posted, and I think you have got some fundamental problems in your understanding of how $_GET and variables work.

After the link in your code is clicked, it sends the browser to the same page, and sets $_GET['increment'] equal to the word true. This is fine, but it says nothing about how you are going to increase the number 1 to 2. Or 2 to 3, etc.

When you code $increment++; by itself, everything about it is wrong, even though PHP handles it how PHP "thinks" it should be handled. This is one of the few cases where I think PHP's handling of variables is a weakness. Although, a basic knowledge of how variables work, should avoid something like this from ever happening.

$increment++ by itself is literally taking $increment which is not set, thus equal to nothing, or 0, and adding 1 to it. Which results in a variable that is now set and has a value of 1.

You should never increment a variable, or manipulate a variable in any way for that matter, until after the variable is initially set.

In this case you could code:


$increment = 0;
$increment++;
echo $increment;

// echos 1


Now, for your $GET[''] variable.

I think you are looking for a way to keep track of a number and add one to it each time a link is clicked. If this is the case, you'll need to pass the number so it can be set as a $_GET[''] variable. Your code currently passes the word true. This is fine if you intend to test whether or not increment is true or some other value then do something based on the result. Short of doing that, increment=true in the URL is meaningless. Better to set increment to a number that we can do something with.

So:


<?php

if (isset($_GET['increment'])) { // if GET increment exists

// Values passed in $_GET are strings, however strings can be numeric, so test it.
// Make sure increment is numeric, if not, it is worthless to us, so set it to 0.
// If it is numeric, add one to it.

is_numeric ($_GET['increment']) ? $_GET['increment']++ : $_GET['increment'] = 0;

}

?>


<html>
<head>
<title>self.php</title>
</head>
<body>

<?php
// If GET increment exists, echo it, else echo nothing
echo $_GET['increment'] ? $_GET['increment'] . '<br>' : '';
?>

<!-- In the code below, we echo GET increment in the URL if it is set, otherwise, echo 0 to initialize it in the URL. -->

<a href="<?php echo $_SERVER['PHP_SELF']; ?>?increment=<?php echo $_GET['increment'] ? $_GET['increment'] : 0; ?>">Count up</a>

</body>
</html>

Let me know what you aren't clear about and I'll do my best to help you out.

Good luck,

J

Twey
02-28-2009, 10:39 AM
Variables are not preserved through pages. For temporary per-user storage, use sessions:
<?php
session_start();

if (!isset($_SESSION['counter']))
$_SESSION['counter'] = 0;

if (isset($_GET['increment']))
++$_SESSION['counter'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>self.php</title>
</head>
<body>
<p>
Counter is: <?php echo $_SESSION['counter']; ?>
</p>
<p>
<a href="self.php?increment">
Count up
</a>
</p>
</body>
</html>For shared or permanent storage, consider a database.

PHPwnd
02-28-2009, 04:15 PM
My "completed" example


<?php

$initial_row_count=3;
$student_fields = array();
session_start();

if (!isset($_SESSION['counter']))
$_SESSION['counter'] = 0;

if(!isset($_SESSION['student_row_count']))
$_SESSION['student_row_count']= 0;

if (isset($_GET['addrow']))
++$_SESSION['student_row_count'];





for ($y=1 ; $y<($initial_row_count+$_SESSION['student_row_count']); $y++){
$student_row_data = "$y";
array_push($student_fields, $student_row_data);
}



//print_r($student_fields);
//print_r ($_REQUEST);
//echo $student_row_count;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<table>
<form>
<?php
foreach ($student_fields as $key){
echo <<<EOD
<tr>
<td><input name="student_name_first_$key" type="text" /></td>
<td><input name="student_name_last_$key" type="text" /></td>
<td><select name="child_class_$key a" id="child_class_$key a">

<option value="Beginning Band">Beginning Band</option>
<option value="Intermediate Band">Intermediate Band</option>
<option value="Concert Band">Concert Band</option>
<option value="Jazz Band">Jazz Band</option>
<option value="Sign Language">Sign Language</option>
<option value="Pep Band ">Pep Band </option>
<option value="Strings">Strings</option>
<option value="Beginning Violin">Beginning Violin</option>
<option value="Advanced Violin">Advanced Violin</option>
<option value="Acts 1 Choir">Acts Choir</option>
<option value="Concert Choir">Concert Choir</option>
<option value="Keyboard">Keyboard 1</option>
<option value="Keyboard">Keyboard 2</option>
<option value="Keyboard">Keyboard 3</option>
<option value="General Music">General Music</option>
<option value="Percussion Ensemble">Percussion Ensemble</option>
<option value="Beginning Percussion">Beginning Percussion</option>
</select> </td>
<td><select name="child_class_$key b" id="child_class_$key b">
<option value="NULL">Blank</option>
<option value="Beginning Band">Beginning Band</option>
<option value="Intermediate Band">Intermediate Band</option>
<option value="Concert Band">Concert Band</option>
<option value="Jazz Band">Jazz Band</option>
<option value="Sign Language">Sign Language</option>
<option value="Pep Band ">Pep Band </option>
<option value="Strings">Strings</option>
<option value="Beginning Violin">Beginning Violin</option>
<option value="Advanced Violin">Advanced Violin</option>
<option value="Acts 1 Choir">Acts Choir</option>
<option value="Concert Choir">Concert Choir</option>
<option value="Keyboard 1">Keyboard 1</option>
<option value="Keyboard 2">Keyboard 2</option>
<option value="Keyboard 3">Keyboard 3</option>
<option value="General Music">General Music</option>
<option value="Percussion Ensemble">Percussion Ensemble</option>
<option value="Beginning Percussion">Beginning Percussion</option>
</select>
</td>
<td><textarea name="child_comment_$key" id="child_coment_$key" cols="45" rows="5"></textarea></td>

</tr>
EOD;
}
echo <<<EOD
<tr><td><a href="test.php?addrow">Get empty row</a></td></tr>
EOD;
?>
</form>
</table>
<body>

</body>
</html>

going to integrate that into the bottom of a form. This would probably be easier in javascript with canned code, but the last code I wrote was 18 years ago on a tandy Color Computer 3. Anyway, I like both examples, both cleared up some muddy beginner questions I had about php. Thanks for the help.

JasonDFR
03-01-2009, 10:51 AM
Twey,

You think it is a good idea to use SESSION for this?

Twey
03-01-2009, 08:03 PM
That depends what you intend.
For temporary per-user storage, use sessions; for shared or permanent storage, consider a database.

JasonDFR
03-01-2009, 08:27 PM
Looking at what he is doing, you think SESSION is the right choice?

Twey
03-01-2009, 09:00 PM
Based on the attempted code, I think $_SESSION was what was intended.