Jump to content


Photo

...variable within an array?


  • Please log in to reply
6 replies to this topic

#1 AncientSage

AncientSage
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 20 July 2006 - 10:52 PM

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...

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


#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 20 July 2006 - 11:02 PM

are you saying you don't know the form element's name?

if you do:

$array["{$_POST['element_name']}"];

should work just fine.

#3 AncientSage

AncientSage
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 20 July 2006 - 11:13 PM

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.



#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 July 2006 - 11:22 PM

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:
<?php
echo '<input type="checkbox" name="row[' . $x . ']">' . $row . "<br /> ";
?>
Then in your processing script:
<?php
     if(isset($_POST['checksubmit'])) {
        if(isset($_POST['row']))
           foreach($_POST['row'] as $i)
               unset($rows[$i]);
     }
?>

Note: untested

Ken


#5 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 20 July 2006 - 11:28 PM

just a change to his code to make it work the way he has the PHP written:

<input type="checkbox" name="row[]" value="'. $x .'">


#6 AncientSage

AncientSage
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 20 July 2006 - 11:33 PM

Thanks for the help everyone, it's working now.

#7 Eugene

Eugene
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 21 July 2006 - 12:26 AM

[$x] show go on the outside.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users