我有一个这样的屏幕:通知示例
它删除行,但只从屏幕上删除。因为如果我刷新,它会再次出现。我不知道如何从实际数组中删除它。数组是从csv文件中取出的-我知道如何将其添加回等。但我不知道如何从数组中删除行。
我有:
// 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>
任何帮助都将非常感激。我不太确定我是否可以删除一行使用javascript?或者在php和java中…
谢谢
Php是服务器端语言,只能在服务器端执行。当你按下删除按钮时,一个javascript函数被调用,它实际上是从客户端删除你的行,因为它是客户端语言,这意味着它实际上存在于服务器上的那个文件中,这就是为什么当你刷新时它会再次显示。
现在你可以通过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]);
}