由于布局我无法使用其他<form>
标记,因此我尝试使用javascript来更新数据库。
由于代码只是使用window.location.href
更改了网址,因此不会重新加载页面以运行if
语句。
这是我的代码:
$check_view = $member->getUsersView($user_id);
if((isset($_GET['view-all'])) && $check_view != $_GET['view-all'])
{
$view_all = $_GET['view-all'];
$database->query('UPDATE users SET view_all = :viewall WHERE id = :userid', array(':viewall' => $view_all, ':userid' => $user_id));
}
<div onclick="javascript:window.location.href = \'page.php?action=list&'.(isset($_GET['view-all']) && $_GET['view-all']=="No" && $check_view=="No" ? 'view-all=Yes' : 'view-all=No').'\';">'.(isset($_GET['view-all']) && $_GET['view-all']=="No" && $check_view=="No" ? 'Show All' : 'Hide').'</div>
任何人都可以帮我解决这个问题吗?
编辑:感谢Matt的帮助我能够让它运转起来:
if(isset($_GET['view-all']))
{
$check_view = $member->getUsersView($user_id);
if($check_view != $_GET['view-all'])
{
$view_all = $_GET['view-all'];
$database->query('UPDATE users SET view_all = :viewall WHERE id = :userid', array(':viewall' => $view_all, ':userid' => $user_id));
}
}
$users_view = $member->getUsersView($user_id);
$view_all = "No";
$hide_show = "Hide";
if((isset($_GET['view-all']) && ($_GET['view-all']=="No")) || ($users_view=="No"))
{
$view_all = "Yes";
$hide_show = "Show All";
}
$data = '<a href="page.php?action=list&view-all='.$view_all.'">'.$hide_show.'</a>';
答案 0 :(得分:1)
<a href="page.php?action=list&view-all=<?=(isset($_GET['view-all']) && $_GET['view-all']=="No" && $check_view=="No" ? "Yes" : "No"?>">
<?=isset($_GET['view-all']) && $_GET['view-all']=="No" && $check_view=="No" ? 'Show All' : 'Hide'?>
</a>
以上代码可以使用,但 UGLY AS SIN 。尝试清理它:
<?php
$view_all = "No";
$hide_show = "Hide";
if (isset($_GET['view-all']) && $_GET['view-all']=="No" && $check_view=="No") {
$view_all = "Yes";
$hide_show = "Show All";
}
?>
<a href="page.php?action=list&view-all=<?= $view_all ?>"><?= $hide_show ?></a>