2016-05-22 231 views
0

我想包含if else語句在我的codeigniter中。當用戶搜索一個地方時,它可以從數據庫中查看該地點的信息。我試圖添加if else語句。如果評論超過5位用戶評價爲「嘈雜」,那麼系統會自動評定爲「該地方很吵」。 (噪音評論是由用戶在寫評論時使用單選按鈕輸入並保存到數據庫中)。以下是我工作的代碼。Codeigniter If else語句條件

//view.php

<style> 
#searchbutton{ 
position: absolute; 
left:300px; 
top:30px; 
} 
fieldset { 
background-color:#EFEAEA; 
margin: 0px 0px 10px 0px; 
padding: 20px; 
border-radius: 1px; 
width:900px; 
margin-left:220px; 
margin-top:-10px; 
} 
#user{ 
font-style:italic; 
font-size: 12px; 
text-align:right; 
} 
#titlereview { 
font-style: italic; 
font-size:20px; 
} 
#review { 
font-size:16px; 
} 
</style> 

<?=form_open_multipart('viewreview/view');?> 

<?php $search = array('name'=>'search',);?> 
<?php $noise = array('name'=>'noise',);?> 

<div id = "searchbutton"> 
<?=form_input($search);?><input type=submit value="Search" /></p> 
</div> 
<?=form_close();?> 

<div class = "tablestyle"> 

<fieldset> 
<?php foreach ($query as $row): ?> 

<div id = "user">User: <?php echo $row->name; ?><br> 
Visited time: <?php echo $row->visitedtime; ?><br> 
</div> 

<div id = "titlereview">"<?php echo $row->titlereview; ?>"<br></div> 

<div id = "noise"><?php echo $row->noise; ?><br></div> 

<div id = "review"><?php echo $row->yourreview; ?><br><hr><br></div> 

<?php endforeach; ?> 
</fieldset> 

<!--$noise is the field form database!--> 
<?php if ($noise='yes'>5){ 
echo 'The place is Noisy'; 
} 
else { 
echo 'The place is Not Noisy'; 
} 
?> 
</div> 

//控制器

<?php 
class viewreview extends CI_Controller { 

public function view($page = 'viewreview') //writereview page folder name 
{ 
    $this->load->model('viewreview_model'); 
    $data['query'] = $this->viewreview_model->get_data(); 
    $this->load->vars($data); 
    if (! file_exists('application/views/viewreview/'.$page.'.php')) //link 
    { 
     // Whoops, we don't have a page for that! 
     show_404(); 
    } 

    $data['title'] = 'View Review'; 
    //$data['title'] = ucfirst($page); // Capitalize the first letter 
    $this->load->helper('html'); 
    $this->load->helper('url'); 
    $this->load->helper('form'); 
    $this->load->view('templates/header', $data); 
    $this->load->view('viewreview/'.$page, $data); 
    $this->load->view('templates/footer', $data); 
} 
} 
?> 

//模型

<?php 
class viewreview_model extends CI_Model { 

public function __construct() 
{ 
    $this->load->database(); 
} 
public function get_data() 
{ 
    $match = $this->input->post('search'); 
    $this->db->like('sitename',$match); 
    $this->db->or_like('titlereview',$match); 
    $this->db->or_like('yourreview',$match); 
    $this->db->or_like('suggestion',$match); 

    $query = $this->db->get('review');  //pass data to query 
    return $query->result(); 

    } 
} 
?> 
+1

只是一個說明我已經注意到你的控制器和模型沒有第一個字母大寫的類和文件名,如果你使用CI3它建議。 http://www.codeigniter.com/user_guide/general/models.html#anatomy-of-a-model和http://www.codeigniter.com/user_guide/general/controllers.html#let-s-try- it-hello-world – user4419336

回答

0

如果我明白你的問題正確:

型號:

public function number_of_noise_report() { 
     $this->db->select('id'); // Change it to what column name you have for id 
     $this->db->from('table'); 
     $this->db->where('noise', 'Yes') // 'Yes' or 'yes', depending on what you have in db 
     $query = $this->db->get(); 
     return $query->num_rows(); 
} 

而且我會包括在控制器:

// Code before 
$data['query'] = $this->viewreview_model->get_data(); 
$data['noise_stat'] = $this->viewreview_model->number_of_noise_report(); // ** UPDATED 
// Code after 

然後在視圖:

if($noise_stat > 5){ 
    echo '<div id = "noise">Noisy<br></div>'; 
} else { 
    echo '<div id = "noise">Do Something Here<br></div>'; 
} 

// OR SIMPLY 
<div id = "noise"> 
<?php if($noise_stat > 5){ echo 'Noisy<br>'; 
} else { echo 'Somrthing<br>'; } ?> 
</div> 

希望這有助於。

+0

謝謝。但是,如果用戶使用「是」或「否」來評估場所的噪音。我如何添加? – user3676306

+0

請檢查更新控制器,讓我們都知道它是如何工作的。 – Sebastian

+0

它不起作用。它只會迴應「嘈雜」甚至改變條件。對於「$東西」我應該怎麼說呢?我不太瞭解那部分。 – user3676306