简体   繁体   English

无法从阵列删除数据行。 PHP + Javascript

[英]Trouble deleting row of data from array. PHP + Javascript

I have a screen like this: notifications example 我有一个这样的屏幕: 通知示例

And it deletes the row, but only from the screen. 并且它删除行,但仅从屏幕上删除。 Because if I refresh then it appears back again. 因为如果我刷新,它将再次出现。 I am not sure how to delete it from the actual array. 我不确定如何从实际数组中删除它。 The array is taken out of a csv file- and I know how to add it back in etc. But what I don't know is deleting rows from the array. 该数组是从csv文件中取出的,我知道如何将其添加回等。但是我不知道是从数组中删除行。

Heres what I have: 这是我所拥有的:

// Grabs the csv file (and its existing data)  and makes it into an array so the new data can be added to it.
$Notifications = array();
$lines = file('data/AdminNotifications.csv', FILE_IGNORE_NEW_LINES);
foreach ($lines as $key => $value)
{
    $Notifications[$key] = str_getcsv($value);
}

echo array2table(array_reverse($Notifications));


// FUNCTION ---------------------------------------------------------------------

//This converts an array to a table
function array2table($array, $recursive = false, $null = ' ')
{
    // Sanity check
    if (empty($array) || !is_array($array)) {
        return false;
    }

    if (!isset($array[0]) || !is_array($array[0])) {
        $array = array($array);
    }

    // Start the table
    $table = "<table>\n";

    // The header
    $table .= "\t<tr>";

    // Take the keys from the first row as the headings
    foreach (array_keys($array[0]) as $heading) {

    }

    $table .= "</tr>\n";

    // The body
    foreach ($array as $row) {
        $table .= "\t<tr>" ;

        foreach ($row as $cell) {
            $table .= '<td>';

            /*
            if($cell ==0 && $heading==1){
            $cell = $cell.":  ";
        }
            */

            $details = $cell;


            // Cast objects
            if (is_object($cell)) { $cell = (array) $cell; }

            if ($recursive === true && is_array($cell) && !empty($cell)) {
                // Recursive mode
                $table .= "\n" . array2table($cell, true, true) . "\n";
            } else {
                $table .= (strlen($cell) > 0) ?
                    htmlspecialchars((string) $cell) :
                    $null;
            }

 $table .= '</td>';


        }
            $table .= '<td>';


            $table .= '<input type="submit" value="Delete" onclick="deleteRow(this)" name="delete"/>';


            $table .= '</td>';



        $table .= "</tr>\n";
    }

    $table .= '</table>';
    return $table;
}


//If the delete button is pressed, then it does this.
if (isset($_POST['delete'])) {

}

?>

//What happens when it is pressed. (This is javascript)
<script>
function deleteRow(btn) {
  var row = btn.parentNode.parentNode;
  row.parentNode.removeChild(row);

}

</script>

Any help would be greatly appreciated. 任何帮助将不胜感激。 I am not too sure whether I can delete a row using javascript? 我不太确定是否可以使用javascript删除行? Or in php and java... 或在PHP和Java中...

Thanks 谢谢

Php being a server side language will execute only on server. php是服务器端语言,将仅在服务器上执行。 When you are pressing delete button here a javascript function is being called which is actually deleting your row just from client side as it's a client side language that means it actually exist yet on server in that file that's why when you refresh it shows again. 当您按下删除按钮时,将调用一个javascript函数,该函数实际上只是从客户端删除行,因为这是一种客户端语言,这意味着该行实际上已存在于该文件中的服务器上,这就是刷新时再次显示的原因。

Now you can make an ajax call or place that delete button inside a form to post that request to server to actually delete that. 现在,您可以进行ajax调用或将删除按钮放置在表单内,以将该请求发布到服务器以实际删除它。

  //If the delete button is pressed, then it does this.
if (isset($_POST['delete'])) {
    $arr_index = $_POST['delete']; //you need to pass the row number
    unset($array[$arr_index]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM