Variable variables, PHP and you

Source: Internet
Author: User
Variables in one of my recent projects, I found a new concept about using variables in PHP. In my program, I need to update multiple records on one page at a time, and after a long period of painful thinking, my mind accidentally flashes the concept of variable (variable variable), and all the confusion is swept.

Introduced

What is called a variable? According to the PHP manual, variables are variables that take a variable's value and use it as a variable name for another variable. This expression is quite straightforward and is easily confused with the word "variable" in a sentence. To give a simple example, you define a variable---x equals this---then define a variable, meaning that you take the value of x as the name of the new variable, in this case the value of the new variable is cake. Use PHP to represent the following:

<?php

$x = "this";
$ $x = "is cake";

?>

This symbol $$ is the representation of variables in PHP. Now we can refer to these two variables in two different ways $x and $ $x.

<?php

echo "$x ${$x}";

?>

<?php

echo "$x $this";

?>

Both of the above programs will output this is cake. Note that in the Echo statement $ $x is written as ${$x}, which is to let PHP know what you want to output is a variable rather than a $ character and $x variable.

Are you still confused? Oh, maybe, you want some more in-depth and useful examples? In the next section, I'll show you how to edit multiple records in one page with variable variables.


Example

Suppose you already have a MySQL database that holds links to some sites of interest, a table submissions in the library, and the fields are as follows:

Submissionid
Postedby
Link
Description
Approved

Now you want to show all the created but not recognized links in the table, this edited page should be able to correct some of the input errors and use the appropriate radio button to set the permission for each record (Approved), and then submit the updated records to the table one at a time.

First, when you extract all the records from the database and display them, you have to set a unique name for each record, which allows us to iterate over the values of each record at the time of submission. The code is as follows:

<?php

The register of the initialization variable

$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 ";


/*********
Suppose we have retrieved records from the database into an array
$myrow = Mysql_fetch_array ().
The following Do...while loop assigns a value to each $XSTR variable based on the name and connects the $index value to the end, starting with 0.
So, the first time this loop, the $SUBMISSIONIDSTR value is SubmissionID0, the second is SubmissionID1, and so on.
***********/

do {

$SUBMISSIONIDSTR = Submissionid. $index;
$POSTEDBYSTR = Postedby. $index;
$LINKSTR = Link. $index;
$DESCRIPTIONSTR = Description. $index;
$APPROVEDSTR = aprroved. $index;


This section will display a value on the screen, with one record per line.

printf ("<tr><td><input type=hidden name=%s value=%s><input type=text name=%s Td>
<td><input type=text name=%s value=%s></td><td><input type=text name=%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);


Each loop register plus 1

$index + +;
$index _count++;

while ($myrow = Mysql_fetch_array ($result));

Create an index register Index_count to track the number of records

echo "<input type=hidden name=counter value= $index _count>n";

echo "<input type=submit></form>n";

?>

Once submitted, we use the $index_count variable to iterate through all the variables on the page, and then assign those variables to other variables, which uses the variable.

<?php

This loop iterates through the records displayed on all pages

for ($index = 0; $index <= $counter; $index + +) {


/*****
This section sets a new variable with the name we created earlier
Starting from 0, until $index_count
*****/

$varSubmissionID = ' Submissionid '. $index;
$varPostedBy = ' postedby '. $index;
$varLink = ' Link '. $index;
$varDescription = ' Description '. $index;
$varApproved = ' Approved '. $index;


/******
This is the variable part of the variable, assigning each value to the name of each new variable.
For example, when the first loop is assigned to the record SubmissionID0 is the value from the front, we use the variable variable to get it.
*******/

$SubmissionIDvalue = $ $varSubmissionID;
$PostedByvalue = $ $varPostedBy;
$Linkvalue = $ $varLink;
$Descriptionvalue = $ $varDescription;
$Approvedvalue = $ $varApproved;


Update Database

$sql = "UPDATE submissions SET postedby= ' $PostedByvalue ', link= ' $Linkvalue ',".
"Description= ' $Descriptionvalue ' WHERE submissionid= $SubmissionIDvalue '";
$result = mysql_query ($sql);


If this record is set to Approved, update the corresponding field Approved.

if ($Approvedvalue = = '-1 ') {
$sql = "UPDATE submissions SET approved= '-1 ' WHERE submissionid= $SubmissionIDvalue";
$result = mysql_query ($sql);
}

}

?>

I hope this will help you understand the basic usage of variable variables and provide some ideas for using them in your future work. Variable variable this concept, at first also gives me a headache, but once you know the basic principles of their work, they become a piece of delicious pizza.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.