Jump to content


Photo

Deleting array element from $_POST...


  • Please log in to reply
6 replies to this topic

#1 AncientSage

AncientSage
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 22 July 2006 - 11:19 PM

Hello,

I believe I am doing this correctly...anyway...

         $postrows = $_POST['rowsarr'];
         if(isset($_POST['row']))
           foreach($_POST['row'] as $i)
               unset($postrows[$i]);

Returns me this error: Fatal error: Cannot unset string offsets in /root/includes/functions.php on line 46

I get the same error if I don't assign $_POST to a variable. Before I assigned post to a variable, I had this for the unset...

unset($_POST['rowsarr'][$i])

Note: In the form, a variable containing an array is submitted as the value, would this effect anything?



#2 JaGeK

JaGeK
  • Members
  • PipPip
  • Member
  • 20 posts
  • LocationNRW, Germany

Posted 22 July 2006 - 11:30 PM

Note: In the form, a variable containing an array is submitted as the value, would this effect anything?


Obviously the variable ($postrows) contains a string and not an array. What exactly is the goal and why?

#3 AncientSage

AncientSage
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 22 July 2006 - 11:47 PM

 foreach($rows as $x => $row) {
       $arr = unserialize($row);
        if($arr == ""){
          continue;
         }
      echo '<input type="checkbox" name="row[]" value="'. $x .'">' . $arr . "<br /> ";
      echo '<input type="hidden" name="rowarr" value="' . $rows . '">';
   echo "<input type=\"hidden\" name=\"checksubmit\">";
   echo "<br /><input type=\"submit\" value=\"Delete Selected\">";
   echo "</form>";       
} 

$rows contains a few arrays, it's gathering the from a text file.

It's supposed to gather the array, and put it into a text file, that's all good, except this little array problem.

So the hidden input would be a string? Do I need to change rowarr to an array, or is it an array by default?

Edit: I attempted to put this as... rowsarr[] for the name attribute, will that work?


#4 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 23 July 2006 - 01:05 AM

Well, in your first script you refer to the variable $_POST['rowsarr'] but in the form in the second script you use the name 'rowarr', I don't know if that may have a bearing on your problem?
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#5 AncientSage

AncientSage
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 23 July 2006 - 03:28 AM

Yes, that was a problem, thanks for finding it.

However, it's still not unsetting the variable, or, at least, not writing it to the text file correct. As it re-writes the entire file (how I have it set), but it's all blank, no text is actualy written.

Here is the function I'm calling...

function delete_arr_element()
    {
     global $root;
     $textfile = $root . "filelist.txt";
     if(isset($_POST['checksubmit'])) {
      if(isset($_POST['rowsarr'])) {
         if(isset($_POST['row']))
          $rowsarr = $_POST['rowsarr'];
           foreach($_POST['row'] as $i)
               unset($rowsarr[$i]);
      $fp = fopen($textfile, "w");
      flock($fp, LOCK_EX);
      fwrite($fp, $rowsarr . "\n");
      flock($fp, LOCK_UN);
        }
      }  
    }


#6 kenrbnsn

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

Posted 23 July 2006 - 04:33 AM

If $owsarr is an array, you need to do something with the array to make it print, foreach or implode, otherwise you will just get the word array in your file. How do you want the elements in the array to be written into the file?

Ken

#7 AncientSage

AncientSage
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 23 July 2006 - 05:08 AM

Well, the data hasn't been unserialized when it was stored into the $rowsarr variable, would it need to be re-serialized when stored back in?

As well, I don't have a need to implement the implode future yet, as I've not added any form of template to it. The data is displayed as...

something|something|something

At the moment, when I do integrate some form of template, or more features to the script I am writing, I'll explode and implode on the | as well.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users