[英]else statement is displaying
在下面的代碼中,當我執行它時,它沒有顯示任何結果。但是我沒有在if-else條件下放置任何結果。實際結果是:文本框,提交按鈕並且沒有結果..我的預期結果是文本框和按鈕應該顯示而不是不顯示結果。如果不搜索數據,則不會顯示任何結果。因此,任何人都可以幫助我。
<form action="<?php echo site_url('search1_site/index');?>" method = "post">
<input type="text" name = "keyword" />
<input type="submit" id="opn" value = "Search" />
</form>
<?php
if($results){
?> <div id='hideme'>
CLOSE<a href='#' class='close_notification' title='Click to Close'>
<img src="<?php echo base_url('img/close.png'); ?>" width="15" height="15" alt="Close" onClick="hide('hideme')"/>
</a> <div style="background:#FFFFFF; width: 500px; height: 500px; position: absolute; left: 50%; top: 50%; margin-left: -100px; margin-top: -100px" id="modal" >
<table class="display2 table table-bordered table-striped">
<tr>
<th>course_code</th>
<th>course name</th>
</tr>
<tr><?php
foreach ($results as $row) {
?>
<td><?php echo $row->course_code;?></td>
<td><?php echo $row->course_name;?></td>
</tr>
<?php
}
}else{
echo "no results";
}
?>
</table></div></div>
<script>
$('a.modal').bind('click', function(event) {
event.preventDefault();
$('#modal').fadeIn(10);
});
function hide(obj) {
var el = document.getElementById(obj);
el.style.display = 'none';
}
</script>
您正在檢查是否設置了$results
變量。
相反,您需要檢查$results
是否為空。
更換
if($results){
用
if(!empty($results)){
並確保$results
包含數組。 我的意思是您的變量必須具有價值。
據我從您的代碼可以看出,未設置$ results。 因此, if ($results)
評估為false,則顯示else。
檢查請求是發布請求還是默認頁面加載
采用
else if($_SERVER['REQUEST_METHOD'] === 'POST')
{
echo "no results";
}
代替
else
{
echo "no results";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.