Php关联数组在点击时删除一个值

发布于 2024-10-16 10:24:37 字数 1142 浏览 4 评论 0原文

我是一个新手,我正在学习 PHP,所以任何帮助将不胜感激。

我为此苦苦挣扎了一整天 我有一个代表用户最后操作的数组,它只是记录 天 - 时间 - “操作类型”(它被序列化并发送到 mySQL DB。)

我得到这样的数组:

---- 编辑 ----

Array ( [0] => Array ( [Date] => 2011-02-06 [Time] => 06:48 PM [Entry] => Email sent 0 test ) [1] => Array ( [Date] => 2011-02-06 [Time] => 06:48 PM [Entry] => Email sent 1 test ) [2] => Array ( [Date] => 2011-02-06 [Time] => 06:48 PM [Entry] => Email sent 2 test ) ) 

我的问题是,我想添加每行中有一个小按钮/链接(当显示数组时),以便用户可以删除该特定操作

foreach ( $actionhistory as $row ) {
  foreach ( $row as $key => $value ) {
   echo "foo delete record - $value";
  }
  echo '
'; }

问题: 用户现在在他的浏览器中看到这个:

2011-02-0606:48 PM Email sent 0 test
2011-02-0606:48 PM Email sent 1 test
2011-02-0606:48 PME mail sent 2 test

我想在每个日期之前添加一个链接。因此,当用户单击该链接时,将加载外部页面,并且需要从列表中删除该特定“记录”。

EG 用户想要删除条目,

[2] => Array ( [Date] => 2011-02-06 [Time] => 06:48 PM [Entry] => Email sent 2 test )

他将单击该条目旁边的链接......这就是我遇到的问题。 如何删除该条目但保留其余条目?

I'm a newbie and I'm learning PHP so any help would be greatly appreciated.

I've been struggling a whole day with this
I have an array that represents user last actions and it just records
day - time - "type of action" (and it's serialized and sent to mySQL DB.)

And I'm getting array like this:

---- EDITED ----

Array ( [0] => Array ( [Date] => 2011-02-06 [Time] => 06:48 PM [Entry] => Email sent 0 test ) [1] => Array ( [Date] => 2011-02-06 [Time] => 06:48 PM [Entry] => Email sent 1 test ) [2] => Array ( [Date] => 2011-02-06 [Time] => 06:48 PM [Entry] => Email sent 2 test ) ) 

And my question is, I want to add a small button/link in each row (when array is displayed) so that user can remove just that particular action

foreach ( $actionhistory as $row ) {
  foreach ( $row as $key => $value ) {
   echo "foo delete record - $value";
  }
  echo '
'; }

Question:
user now sees this in his browser:

2011-02-0606:48 PM Email sent 0 test
2011-02-0606:48 PM Email sent 1 test
2011-02-0606:48 PME mail sent 2 test

I want to add a link before each date. So when users clicks on that link - external page will be loaded and that particular "record" needs to be removed from the list.

E.G. User wants to remove entry

[2] => Array ( [Date] => 2011-02-06 [Time] => 06:48 PM [Entry] => Email sent 2 test )

he will click on the link beside that entry.... and that's where I have a problem.
How do I remove that entry but keeping the rest ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伴我老 2024-10-23 10:24:37

为什么有这些变量?而且你的数组构建错误。

$actionhistory[] = array(
  "Date" => date("Y-m-d"),
  "Time" => date("h:i A"),
  "Entry" => "Email sent ".$row." test" //This is actually user action
);

您想要的是通过 javascript 将操作历史记录的特定行发送到 PHP 脚本,该脚本将从数据库中删除该行,并且可能还会从表中动态删除该行。您需要向数组中添加另一个元素,这是数据库中的唯一 ID,除非您有一个包含日期/时间/操作的唯一键 - 这看起来非常愚蠢!然后使用 JS 库(例如 jQuery),在行中的删除按钮上使用简单的交互,如下所示:

<?php
while ( $data = $query->fetch() ){
  echo '<tr><td....</td>
    <td><input type="button" value="delete" onclick="
      $.post(\'myScript.php?id='.$data['id'].'\',function(d){
        if ( d == 1 ){
          $(this).parents("tr").eq(0).remove();
        }
      });" /></td</tr>';
}
?>

然后 myScript.php 应该处理 $_POST['id'],删除相应的行,如果是,则回显 1成功,因此该行被删除。 [已编辑,$_POST,不是 $_GET!]

Why all these variables? And your array is wrongly built.

$actionhistory[] = array(
  "Date" => date("Y-m-d"),
  "Time" => date("h:i A"),
  "Entry" => "Email sent ".$row." test" //This is actually user action
);

What you want is sending through javascript a particular row of actions' history to a PHP script, which will delete this row from the database, and maybe also dynamically remove the row from your table. You need to add another element to your array, which is the unique ID in the database, except if you've got a unique key with date/time/action - which seems pretty stupid! Then using a JS library such as jQuery, use a simple interaction on a delete button in your row like this:

<?php
while ( $data = $query->fetch() ){
  echo '<tr><td....</td>
    <td><input type="button" value="delete" onclick="
      $.post(\'myScript.php?id='.$data['id'].'\',function(d){
        if ( d == 1 ){
          $(this).parents("tr").eq(0).remove();
        }
      });" /></td</tr>';
}
?>

Then myScript.php should take care of $_POST['id'], delete the corresponding row, and echo 1 if it was successful, so the row is removed. [Edited, $_POST, not $_GET!]

旧时光的容颜 2024-10-23 10:24:37

我的理解是,您需要将数组传递给另一个负责删除它的文件。所有行在 MySQL 中都应该有一个唯一的标识符,因此您所需要做的就是将查询字符串中要删除的行的 ID 包含到删除文件中。

如果我误解了您的意思,并且您想将数组数据本身发送到另一个文件,您可以序列化数组,对其进行 urlencode 并通过查询字符串传递它。

另一种选择是将所有数组保留在 $_SESSION 中,并通过链接中的查询字符串传递索引。

我希望我有所帮助(并理解了这个问题)

The way I understood it was that you need to pass the array to another file which is responsible for deleting it. All the rows should have an unique identificator in MySQL, so all you need to do it include the ID of the row you want deleted in the query string to the deletion file.

In case i misunderstood you and you want to send the array data itself to the other file, you can serialize the array, urlencode it and pass it through a query string.

Another alternative could be keeping all the arrays in $_SESSION and pass the index through the query string in the link.

I hope I helped (and understood the question)

优雅的叶子 2024-10-23 10:24:37

非常感谢 - 我已经通过添加唯一 ID 对其进行了排序,因此单击时仅将 id 传递到外部文件,然后删除数组中的值。感谢 NAAB 的提示!

当我在此处粘贴代码时 - 系统会删除其中最重要的部分,但如果有人需要我的解决方案,只需询问,我就会发送...

Thanks a lot - I've sorted it by adding a unique ID, so on click only id is passed to the external file and then value from array is removed. Thanks NAAB for the tip !

When I paste a code here - system cuts the most important parts of it, but if somebody needs my solution, just ask and I'll send it...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文