Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

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 ]

[Page 1]  [Page 2]  


Comments:
Problem in codeSandy05/23/07 05:18
Help with php scriptDavid Sargent09/26/05 12:30
RE: how can i validate textfildphpphp08/10/05 14:09
RE: Variable Dosn't Show Up HELP!alimac07/28/05 10:17
RE: how can i use javascript variables in phpLogan Kriete07/11/05 17:03
RE: how can i use javascript variables in phpraman01/28/05 01:42
Creating a session variableManish Patel11/20/04 02:23
Creating a session variableManish Patel11/20/04 02:16
VARIABLE VARIABLE ARE GREATvince08/03/04 12:52
RE: Variable Dosn't Show Up HELP!Sammy06/16/04 17:12
RE: Variable Variables Arrays with dynamic indexThoTruck06/11/04 18:37
RE: Combining JavaScript With PHPmidnight helper02/24/04 04:22
Solution To Variable VariablesEeeve!01/09/04 11:08
RE: Variable Dosn't Show Up HELP!John03/04/03 10:26
RE: Passing PHP variables in "mailto" tag of HTMchetan sheth11/13/02 03:40
Problemshabbir10/30/02 09:27
RE: Variable Dosn't Show Up HELP!Gary P10/22/02 08:10
RE: Variable Dosn't Show Up HELP!Daryl10/07/02 15:17
how can i use javascript variables in phpeyup09/26/02 10:35
RE: querystring variablesCarlos09/13/02 17:07
querystring variablesJim09/07/02 12:04
RE: Variable Dosn't Show Up HELP!Mark09/05/02 11:33
RE: Creating a session variableDanny08/25/02 21:20
RE: Combining JavaScript With PHPBasf08/12/02 08:08
Wonderful, amesomeGanesh08/09/02 12:12
php soursemohsen khodaparastan08/07/02 03:45
Creating a session variableHari Ashok08/07/02 02:50
deleteing records from a while { }jason07/22/02 14:44
RE: Variable Dosn't Show Up HELP!Herberto Graça07/18/02 16:05
problemsKaten07/05/02 10:38
Variable Dosn't Show Up HELP!Zachary Cook06/21/02 11:34
need help with graph.....loop06/20/02 00:36
RE: help me!!!Connetic06/12/02 15:41
Variable Variables Arrays with dynamic indexConnetic06/12/02 15:07
Creating a session variableDan06/12/02 14:57
Passing PHP variables in "mailto" tag of HTMfaisal06/12/02 12:19
help me!!!jolomi siloko06/12/02 10:28
Variable not defineWaribam05/24/02 05:48
A QUESTIONdaniel alejo05/20/02 21:08
RE: thanks! but need more help!Muhammad Akbar05/14/02 17:06
Holy S**T! this is what ive allways needed!!!Gillis Danielsen05/14/02 13:46
$$variable $variablesVaeda05/14/02 03:38
RE: thanks!freeda05/14/02 02:08
thanks!nick04/24/02 13:05
RE: MySQL Results Shortcutwuxiao04/18/02 12:01
YAHOO! exactly what i needed!thaVader03/20/02 13:46
RE: page=blahjason03/18/02 01:32
Great article!Randy02/25/02 14:12
RE: session level variable variables?mbaweb02/25/02 10:44
session level variable variables?mbaweb02/22/02 10:28
RE: Win98 Apache and .phpsGMalenko02/18/02 22:43
Newbie question on HTML forms and VariablesBandi10/06/01 17:08
RE: MySQL Results Shortcutjerry09/04/01 01:44
what about class variables?David Chan08/30/01 13:58
registersZack08/29/01 03:41
page.php?content=value&var2=valueIan Gordon08/10/01 15:52
How can I restrict fill-in boxes ?Vincent Hayward07/18/01 09:33
RE: getting varaibles Philip Choy07/12/01 02:40
RE: Enlighten me, please!Ryan06/11/01 15:55
RE: page=blahMichael Mann05/24/01 15:29
Variable variables and arraysAndy Hewitson02/18/01 03:51
RE: Variable variables and Formsrod k02/11/01 13:19
Variable variables and FormsTerje Skjaeveland02/09/01 03:36
Win98 Apache and .phpsYasin02/08/01 07:23
RE: Anyone for a form array? - Jeroen Keppensrod k01/28/01 22:42
page=blahGravity01/23/01 03:48
more exmaple..enterume01/15/01 01:46
RE: MySQL Results ShortcutLarry01/10/01 20:24
Great Article!Fred Barringer12/24/00 14:09
Var Varsdave spencer12/14/00 18:50
incrementing a variable through formsjownew11/27/00 05:56
Variable Variable ArraysDave Gribler11/06/00 17:33
Enlighten me, please!Jag11/02/00 23:07
RE: Anyone for a form array?Franz Graf10/31/00 06:28
RE: Anyone for a form array?Jeroen Keppens10/26/00 04:31
advanced variablesJeroen Keppens10/26/00 04:24
Combining JavaScript With PHPlaxman10/25/00 16:40
RE: Very Much Useful...Krish Mandal10/18/00 18:10
Use it for cookiesDavid Roessli10/16/00 02:40
Anyone for a form array?Onno Benschop, ITmaze.com.au10/12/00 16:27
MySQL Results ShortcutJames Moore10/08/00 14:16
RE: Very Much Useful...philip olson10/06/00 22:44
RE: Well.Robert10/05/00 14:47
RE: Well.Franz Graf10/05/00 13:38
variable functions :)lexa10/04/00 13:34
Quick Array > variables w/ thisTom Anderson10/03/00 17:35
Very Much Useful...jayaraj10/03/00 13:04
RE: Well.Paul10/02/00 13:16
field referenceJoe brown10/01/00 22:25
Hey, neat!lowbatteries10/01/00 18:20
variable variable variableJohn Pertalion10/01/00 00:27
Well.Jon Coulter09/29/00 20:44
Excellent!Brian Popp09/29/00 13:03
shortcutGiorgio09/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.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.