Jump to content

Dynamic Variables


TheFilmGod

Recommended Posts

No.  Make it an array, that's the whole point of them.  It won't parse like that.  You can't dynamically assign the variable name in that manner.

 

Actually he is using an array :) and yes it will parse like that. For arrays, instead of square brackets, u can also use curly braces. Try it:

 

<?php
$arr = array('php', 'sql');
echo $arr{0}; //it will echo 'php'
echo $arr[1]; //and normally it will echo 'sql'
$id = 2;
$arr{$id} = 'html'; //add 'html' as a third value
echo $arr{$id}; //it will echo 'html'
?>

Link to comment
https://forums.phpfreaks.com/topic/102833-dynamic-variables/#findComment-526806
Share on other sites

I may be completely off track here but this is code I found a number of years ago that I have found invaluable with dynamic varaibles. Everyone told me it could not be done and maybe there are better ways to do it but this is how I remember from DBase1V days.

 

Obviously you need to pull out the bits you need :)

 

<?php

//This loops through all the records that have been displayed on the page.

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

    
    /*
    This part sets a variable with the names we created in the first section.  
    We start with 0 and go until the number saved in the $index_count variable.
    */ 
    
    $varSubmissionID = 'SubmissionID'.$index;
    $varPostedBy = 'PostedBy'.$index;
    $varLink = 'Link'.$index;
    $varDescription = 'Description'.$index;
    $varApproved = 'Approved'.$index;

    
    /*
    This is the variable variable section.  We take the value that was assigned 
    to each name variable.  For example the first time through the loop we are 
    at the record assigned with SubmissionID0.  The value given to SubmissionID0 
    is set from the first section.  We access this value by taking the variable 
    variable of what SubmissionID0 is.
    */

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


    //Update the database

    $sql = "UPDATE submissions SET PostedBy='$PostedByvalue',Link='$Linkvalue',".
        "Description='$Descriptionvalue' WHERE SubmissionID=$SubmissionIDvalue'";
//   $result = mysql_query($sql);


    //If the link was marked approved set the value of the Approved field

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

}

?>


<?php

//Initialize counter variables

$index = 0;
$index_count = 0;
?>
<form method=post action=$PHP_SELF>\n";
<table>\n";
<tr>
<td><b>Posted By</b></td><td><b>Link</b></td>
   <td><b>Description</b></td><td><b>Approved</b>
   </td>
</tr>
<?php

/*
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

<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>
<?php
sprintf($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";

?>

 

(edited by kenrbnsn to add the


tags)

Link to comment
https://forums.phpfreaks.com/topic/102833-dynamic-variables/#findComment-526849
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.