AncientSage Posted July 20, 2006 Share Posted July 20, 2006 Hello,I'm attempting to contain a variable number within an array, for example...$array[$x]$x is a number, the number is sent through a form. However, since I do not know that number, I am unsure of how to put it into the array slot.$array[$_POST[$x]]Won't work, perhaps if I pass it via a GET method or something it will work? Or is there another way?Perhaps it's because $x is no longer in the script once it is executed, $x is in the name element of a form.My code...[code] $textfile = "filelist.txt"; if (file_exists($textfile) && is_file($textfile)) { $fp = fopen($textfile, "r"); $file = file_get_contents($textfile, False, NULL); $rows = explode("\n", $file); if(isset($_POST['checksubmit'])) { unset($rows[$x]); } $x = 0; echo "<form action=\"" . append_sid("admin.php") . "\" method=\"POST\">"; foreach($rows as $row) { if($row == ""){ continue; } echo "<input type=\"checkbox\" name=\"" . $x . "\">" . $row . "<br /> "; $x++; } echo "<input type=\"hidden\" name=\"checksubmit\">"; echo "<br /><input type=\"submit\" value=\"Delete Selected\">"; echo "</form>"; }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15203-variable-within-an-array/ Share on other sites More sharing options...
akitchin Posted July 20, 2006 Share Posted July 20, 2006 are you saying you don't know the form element's name?if you do:[code]$array["{$_POST['element_name']}"];[/code]should work just fine. Quote Link to comment https://forums.phpfreaks.com/topic/15203-variable-within-an-array/#findComment-61413 Share on other sites More sharing options...
AncientSage Posted July 20, 2006 Author Share Posted July 20, 2006 Yes, as seen in my code... echo "<input type=\"checkbox\" name=\"" . $x . "\">" $x is auto-incremented, and there are multiple input fields. I need to somehow find the name attribute out, and then allow it to be accessed by the $_POST global variable. Quote Link to comment https://forums.phpfreaks.com/topic/15203-variable-within-an-array/#findComment-61420 Share on other sites More sharing options...
kenrbnsn Posted July 20, 2006 Share Posted July 20, 2006 Your code isn't going to work anyway, since the names you are generating are all number and you can't have a variable in PHP which is a number (at least I don't think so). A much better way would be to make the $x an index into an array, such as:[code]<?phpecho '<input type="checkbox" name="row[' . $x . ']">' . $row . "<br /> ";?>[/code]Then in your processing script:[code]<?php if(isset($_POST['checksubmit'])) { if(isset($_POST['row'])) foreach($_POST['row'] as $i) unset($rows[$i]); }?>[/code]Note: untestedKen Quote Link to comment https://forums.phpfreaks.com/topic/15203-variable-within-an-array/#findComment-61423 Share on other sites More sharing options...
akitchin Posted July 20, 2006 Share Posted July 20, 2006 just a change to his code to make it work the way he has the PHP written:[code]<input type="checkbox" name="row[]" value="'. $x .'">[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15203-variable-within-an-array/#findComment-61426 Share on other sites More sharing options...
AncientSage Posted July 20, 2006 Author Share Posted July 20, 2006 Thanks for the help everyone, it's working now. Quote Link to comment https://forums.phpfreaks.com/topic/15203-variable-within-an-array/#findComment-61434 Share on other sites More sharing options...
Eugene Posted July 21, 2006 Share Posted July 21, 2006 [$x] show go on the outside. Quote Link to comment https://forums.phpfreaks.com/topic/15203-variable-within-an-array/#findComment-61443 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.