PHPBuilder - Variable Variables Page 2



RSS Twitter
Articles Tricks And Hacks

Variable Variables - Page 2

by: Robert Berkowitz
|
September 28, 2000

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.

« Previous Page
1
|
2
|
3
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Robert Berkowitz

Comment:



Comment:

(Maximum characters: 1200). You have characters left.