[英]switch $_GET variable else statement
我遇到了一個問題。 如果有可能,我可以為$ _GET ['topic']編寫else語句嗎?
case 'thread-view':
if(isset($_GET['topic'])){
$topicid = $_GET['topic'];
$psql = "SELECT * FROM posts WHERE th_p_link='$topicid' ORDER BY p_id ASC";
$presult = mysqli_query($db,$psql);
while($prow = mysqli_fetch_array($presult)){
$post = $prow['p_post'];
$author = $prow['p_author'];
$avatar = $prow['p_avatar'];
$gm = $prow['p_gm'];
$date = $prow['p_date'];
$link = $prow['th_p_link'];
echo '
<div class="post_box">
<div id="post-left">
<img src="'.$avatar.'" alt="">
<div id="post-about">
<span>'.$author.'</span>
<span>Member</span>
</div>
</div>
<div id="post-content">
<p>'.htmlspecialchars($post, ENT_QUOTES).'</p>
</div>
<div id="post-right">
<i>'.$date.'</i>
</div>
</div>
';
}
}
break;
default:
include 'template/forum_categories.php';
例如,如果我轉到URL
forums.php?page = thread-view&topic = testurlpost
它正確顯示頁面。 雖然我要實現的目標是,如果我去了一些不存在的事物,例如
forums.php?page = thread-view&topic = testurlthatdoesntexist
我希望它重定向到另一個頁面,例如錯誤頁面。 我將如何實現呢? 我為$ _GET ['topic']作了else語句,但是當我輸入不存在的URL時,該語句不起作用。
您應該添加
if(!mysqli_num_rows($presult)) {
header("Location: /error-page.html");
}
在While語句之前。 因此,您的代碼將如下所示:
case 'thread-view':
if(isset($_GET['topic'])){
$topicid = $_GET['topic'];
$psql = "SELECT * FROM posts WHERE th_p_link='$topicid' ORDER BY p_id ASC";
$presult = mysqli_query($db,$psql);
if(!mysqli_num_rows($presult)) {
header("Location: /error-page.html");
}
while($prow = mysqli_fetch_array($presult)){
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.