【发布时间】:2018-06-26 00:23:05
【问题描述】:
我正在将文章加载为输入值中的 id 列表:
$idThisPost = "19999";
<input id="input_post_id" type="hidden" name="varPostId" value="<?php echo $idThisPost; ?>">
每篇文章都有一个删除按钮:
<form id="saveId" action="" method="POST" class="" autocomplete="off">
<input class="idsuser" type="hidden" name="save_post_value" value="<?php echo $userPosts; ?>">
<button type="submit" class="removeJiku save_post btn btn-danger">Remove</button>
</form>
当我们单击删除按钮时,它应该从数组中删除其 id 并在自定义字段上运行更新。运行以下命令会给我一个字符串 Array 作为值。
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (isset($_POST['save_post_value'])) {
$userPostsQuery = array_diff($userPostsQuery, array($_POST['save_post_value']));
update_user_meta( $user_id, 'save_post', $userPostsQuery );
$removed = $_POST['save_post_value'];
unset($removed);
}
}
像下面这样进行爆炸仍然给我Array,而不是实际的ids
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (isset($_POST['save_post_value'])) {
$userPostsQuery = array_diff($userPostsQuery, array($_POST['save_post_value']));
$userPostsQuery = explode(',', $userPostsQuery);
update_user_meta( $user_id, 'save_post', $userPostsQuery );
$removed = $_POST['save_post_value'];
unset($removed);
}
}
【问题讨论】:
-
我们也很想看到
var_export($_POST['save_post_value'])和$userPostsQuery。我没有看到任何echo是从哪里打印的Array?update_user_meta()? -
@mickmackusa 我会把它放在哪里?如果我放入循环中,我会得到与
19999相同的值 -
我也没有看到任何
loop。我无法重现您的问题。如果您的explode()确实有效,那么$userPostsQuery是一个逗号分隔的字符串(不是数组——array_diff()所期望的)。你检查过你的错误日志吗?请给我一些示例数据。你的代码肯定是“坏了”我只是不知道多早开始纠正。 -
@mickmackusa 基本上我是在循环以获取文章,上面的代码是我在每个循环中的代码
-
我认为你必须取消设置($userPostsQuery[$removed]);这就是从您保存帖子 id-s 的数组中删除元素的原因(我猜这是数组名称 $userPostsQuery)