TheFilmGod Posted April 25, 2008 Share Posted April 25, 2008 $name_ac{$i} = $_POST['name_ac{$i}']; So I have $i already set = 2, I want the php code to read now as: $name_ac2 = $_POST['name_ac2]; Will it parse correctly? Link to comment https://forums.phpfreaks.com/topic/102833-dynamic-variables/ Share on other sites More sharing options...
DarkWater Posted April 25, 2008 Share Posted April 25, 2008 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. Link to comment https://forums.phpfreaks.com/topic/102833-dynamic-variables/#findComment-526766 Share on other sites More sharing options...
Fadion Posted April 25, 2008 Share Posted April 25, 2008 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 More sharing options...
DarkWater Posted April 25, 2008 Share Posted April 25, 2008 I don't think it'll work for assigning a variable. I could be wrong though. Link to comment https://forums.phpfreaks.com/topic/102833-dynamic-variables/#findComment-526808 Share on other sites More sharing options...
Fadion Posted April 25, 2008 Share Posted April 25, 2008 I don't think it'll work for assigning a variable. I could be wrong though. I said it, he's using an array, not a variable. This code: $name_ac{$i} = $_POST['name_ac{$i}']; is the same as: $name_ac = array(); $name_ac[$i] = $_POST['name_ac{$i}']; Link to comment https://forums.phpfreaks.com/topic/102833-dynamic-variables/#findComment-526810 Share on other sites More sharing options...
DarkWater Posted April 25, 2008 Share Posted April 25, 2008 Hmm...Alright. That's weird, I never knew you could use {} for assigning an array element, but I knew you could use it for referencing an array element. P.S: Is your birthday really February 5th? Link to comment https://forums.phpfreaks.com/topic/102833-dynamic-variables/#findComment-526811 Share on other sites More sharing options...
Fadion Posted April 25, 2008 Share Posted April 25, 2008 P.S: Is your birthday really February 5th? [OFFTOPIC] lol yep it is. Im guessing it is yours too?! Link to comment https://forums.phpfreaks.com/topic/102833-dynamic-variables/#findComment-526814 Share on other sites More sharing options...
DarkWater Posted April 25, 2008 Share Posted April 25, 2008 Yeah, mine is too. What a coincidence. Link to comment https://forums.phpfreaks.com/topic/102833-dynamic-variables/#findComment-526815 Share on other sites More sharing options...
kenrbnsn Posted April 25, 2008 Share Posted April 25, 2008 Getting back to the original question. What the OP wants are variable variables. This works: <?php $i = 2; ${'name_ac'.$i} = $_POST['name_ac' . $i]; ?> Ken Link to comment https://forums.phpfreaks.com/topic/102833-dynamic-variables/#findComment-526820 Share on other sites More sharing options...
Riparian Posted April 25, 2008 Share Posted April 25, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.