Jump to content

Deleting array element from $_POST...


AncientSage

Recommended Posts

Hello,

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

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

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?

Link to comment
https://forums.phpfreaks.com/topic/15360-deleting-array-element-from-_post/
Share on other sites

[quote author=AncientSage link=topic=101508.msg401844#msg401844 date=1153610343]
Note: In the form, a variable containing an array is submitted as the value, would this effect anything?[/quote]

Obviously the variable ($postrows) contains a string and not an array. What exactly is the goal and why?
[code]
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>";     
}
[/code]

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

[code]
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);
        }
      } 
    }
[/code]
If $owsarr is an array, you need to do something with the array to make it print, [b]foreach[/b] or [b]implode[/b], otherwise you will just get the word [color=red]array[/color] in your file. How do you want the elements in the array to be written into the file?

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

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.