|
Variable Variables, PHP, and You
Example
Let's say you have a MySQL database that stores link submissions for sites of interest.
In that database there is a table called submissions. The fields in the submissions table are as follows:
SubmissionID PostedBy Link Description Approved
You want to be able to display all the submissions that have been made but are not yet approved in
a table. The editor will then be able to correct any typing mistakes, select the proper radio
button next to each link for approval, and submit them all at once.
First, when you pull the data from the database and display it on the page you must set each name
for every record to be unique. This will enable us to loop through the records and be able to
identify what value goes where once the submit button has been pressed. To do this we do the following:
<?php
//Initialize counter variables
$index = 0;
$index_count = 0;
echo "<form method=post action=$PHP_SELF>\n";
echo "<table>\n";
echo "<tr><td><b>Posted By</b></td><td><b>Link</b></td>".
"<td><b>Description</b></td><td><b>Approved</b></td></tr>\n";
/*
Assuming we already have retrieved the records from the database into an array setting
$myrow = mysql_fetch_array(). The do...while loop assigns a value to the $xstr variable
by taking the name and concatenating the value of $index to the end starting with 0. So
the first time through the loop $SubmissionIDStr would have a value of SubmissionID0 the
next time through it would be SubmissionID1 and so forth.
*/
do {
$SubmissionIDStr = SubmissionID.$index;
$PostedByStr = PostedBy.$index;
$LinkStr = Link.$index;
$DescriptionStr = Description.$index;
$ApprovedStr = Aprroved.$index;
//This section would print the values onto the screen one record per row
printf("<tr><td><input type=hidden name=%s value=%s><input type=text name=%s value=%s></td>
<td><input type=text name=%s value=%s></td><td><input type=text name=%s value=%s></td>
<td><input type=radio name=%s value=-1>Yes<input type=radio name=%s value=0 checked>No</td></tr>\n",
$SubmissionIDStr, $myrow["SubmissionID"], $PostedByStr, $myrow["PostedBy"], $LinkStr, $myrow["Link"],
$DescriptionStr, $myrow["Description"], $ApprovedStr, $ApprovedStr);
//Increase counter values by 1 for each loop
$index++;
$index_count++;
} while ($myrow = mysql_fetch_array($result));
// I also had to create an index count to keep track of the total number of rows.
echo "<INPUT TYPE=hidden NAME=counter VALUE=$index_count>\n";
echo "<INPUT TYPE=submit></form>\n";
?>
After the submit button is pressed to we have to loop through all the variables on the page again.
We are able to do this with the $index_count variable we created. Next, we assign the value of
those variables to yet another variable in the second step. This is where variable variables come into play.
[ Next Page ]
| Comments: | ||
| Problem in code | Sandy | 05/23/07 05:18 |
| Help with php script | David Sargent | 09/26/05 12:30 |
| RE: how can i validate textfild | phpphp | 08/10/05 14:09 |
| RE: Variable Dosn't Show Up HELP! | alimac | 07/28/05 10:17 |
| RE: how can i use javascript variables in php | Logan Kriete | 07/11/05 17:03 |
| RE: how can i use javascript variables in php | raman | 01/28/05 01:42 |
| Creating a session variable | Manish Patel | 11/20/04 02:23 |
| Creating a session variable | Manish Patel | 11/20/04 02:16 |
| VARIABLE VARIABLE ARE GREAT | vince | 08/03/04 12:52 |
| RE: Variable Dosn't Show Up HELP! | Sammy | 06/16/04 17:12 |
| RE: Variable Variables Arrays with dynamic index | ThoTruck | 06/11/04 18:37 |
| RE: Combining JavaScript With PHP | midnight helper | 02/24/04 04:22 |
| Solution To Variable Variables | Eeeve! | 01/09/04 11:08 |
| RE: Variable Dosn't Show Up HELP! | John | 03/04/03 10:26 |
| RE: Passing PHP variables in "mailto" tag of HTM | chetan sheth | 11/13/02 03:40 |
| Problem | shabbir | 10/30/02 09:27 |
| RE: Variable Dosn't Show Up HELP! | Gary P | 10/22/02 08:10 |
| RE: Variable Dosn't Show Up HELP! | Daryl | 10/07/02 15:17 |
| how can i use javascript variables in php | eyup | 09/26/02 10:35 |
| RE: querystring variables | Carlos | 09/13/02 17:07 |
| querystring variables | Jim | 09/07/02 12:04 |
| RE: Variable Dosn't Show Up HELP! | Mark | 09/05/02 11:33 |
| RE: Creating a session variable | Danny | 08/25/02 21:20 |
| RE: Combining JavaScript With PHP | Basf | 08/12/02 08:08 |
| Wonderful, amesome | Ganesh | 08/09/02 12:12 |
| php sourse | mohsen khodaparastan | 08/07/02 03:45 |
| Creating a session variable | Hari Ashok | 08/07/02 02:50 |
| deleteing records from a while { } | jason | 07/22/02 14:44 |
| RE: Variable Dosn't Show Up HELP! | Herberto Graça | 07/18/02 16:05 |
| problems | Katen | 07/05/02 10:38 |
| Variable Dosn't Show Up HELP! | Zachary Cook | 06/21/02 11:34 |
| need help with graph..... | loop | 06/20/02 00:36 |
| RE: help me!!! | Connetic | 06/12/02 15:41 |
| Variable Variables Arrays with dynamic index | Connetic | 06/12/02 15:07 |
| Creating a session variable | Dan | 06/12/02 14:57 |
| Passing PHP variables in "mailto" tag of HTM | faisal | 06/12/02 12:19 |
| help me!!! | jolomi siloko | 06/12/02 10:28 |
| Variable not define | Waribam | 05/24/02 05:48 |
| A QUESTION | daniel alejo | 05/20/02 21:08 |
| RE: thanks! but need more help! | Muhammad Akbar | 05/14/02 17:06 |
| Holy S**T! this is what ive allways needed!!! | Gillis Danielsen | 05/14/02 13:46 |
| $$variable $variables | Vaeda | 05/14/02 03:38 |
| RE: thanks! | freeda | 05/14/02 02:08 |
| thanks! | nick | 04/24/02 13:05 |
| RE: MySQL Results Shortcut | wuxiao | 04/18/02 12:01 |
| YAHOO! exactly what i needed! | thaVader | 03/20/02 13:46 |
| RE: page=blah | jason | 03/18/02 01:32 |
| Great article! | Randy | 02/25/02 14:12 |
| RE: session level variable variables? | mbaweb | 02/25/02 10:44 |
| session level variable variables? | mbaweb | 02/22/02 10:28 |
| RE: Win98 Apache and .phps | GMalenko | 02/18/02 22:43 |
| Newbie question on HTML forms and Variables | Bandi | 10/06/01 17:08 |
| RE: MySQL Results Shortcut | jerry | 09/04/01 01:44 |
| what about class variables? | David Chan | 08/30/01 13:58 |
| registers | Zack | 08/29/01 03:41 |
| page.php?content=value&var2=value | Ian Gordon | 08/10/01 15:52 |
| How can I restrict fill-in boxes ? | Vincent Hayward | 07/18/01 09:33 |
| RE: getting varaibles | Philip Choy | 07/12/01 02:40 |
| RE: Enlighten me, please! | Ryan | 06/11/01 15:55 |
| RE: page=blah | Michael Mann | 05/24/01 15:29 |
| Variable variables and arrays | Andy Hewitson | 02/18/01 03:51 |
| RE: Variable variables and Forms | rod k | 02/11/01 13:19 |
| Variable variables and Forms | Terje Skjaeveland | 02/09/01 03:36 |
| Win98 Apache and .phps | Yasin | 02/08/01 07:23 |
| RE: Anyone for a form array? - Jeroen Keppens | rod k | 01/28/01 22:42 |
| page=blah | Gravity | 01/23/01 03:48 |
| more exmaple.. | enterume | 01/15/01 01:46 |
| RE: MySQL Results Shortcut | Larry | 01/10/01 20:24 |
| Great Article! | Fred Barringer | 12/24/00 14:09 |
| Var Vars | dave spencer | 12/14/00 18:50 |
| incrementing a variable through forms | jownew | 11/27/00 05:56 |
| Variable Variable Arrays | Dave Gribler | 11/06/00 17:33 |
| Enlighten me, please! | Jag | 11/02/00 23:07 |
| RE: Anyone for a form array? | Franz Graf | 10/31/00 06:28 |
| RE: Anyone for a form array? | Jeroen Keppens | 10/26/00 04:31 |
| advanced variables | Jeroen Keppens | 10/26/00 04:24 |
| Combining JavaScript With PHP | laxman | 10/25/00 16:40 |
| RE: Very Much Useful... | Krish Mandal | 10/18/00 18:10 |
| Use it for cookies | David Roessli | 10/16/00 02:40 |
| Anyone for a form array? | Onno Benschop, ITmaze.com.au | 10/12/00 16:27 |
| MySQL Results Shortcut | James Moore | 10/08/00 14:16 |
| RE: Very Much Useful... | philip olson | 10/06/00 22:44 |
| RE: Well. | Robert | 10/05/00 14:47 |
| RE: Well. | Franz Graf | 10/05/00 13:38 |
| variable functions :) | lexa | 10/04/00 13:34 |
| Quick Array > variables w/ this | Tom Anderson | 10/03/00 17:35 |
| Very Much Useful... | jayaraj | 10/03/00 13:04 |
| RE: Well. | Paul | 10/02/00 13:16 |
| field reference | Joe brown | 10/01/00 22:25 |
| Hey, neat! | lowbatteries | 10/01/00 18:20 |
| variable variable variable | John Pertalion | 10/01/00 00:27 |
| Well. | Jon Coulter | 09/29/00 20:44 |
| Excellent! | Brian Popp | 09/29/00 13:03 |
| shortcut | Giorgio | 09/29/00 10:04 |
|
If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly. | ||


