简体   繁体   English

Codeigniter If else语句条件

[英]Codeigniter If else statement condition

I am trying to include if else statement in my codeigniter. 我正在尝试在我的codeigniter中包含if else语句。 When user search a place, it can view the information of the place from database. 用户搜索地点时,可以从数据库中查看该地点的信息。 And I'm trying to add in if else statement. 我正在尝试添加if else语句。 If the reviews are more then 5 users rate that place as "Noisy", then the system will automatically rate it as "The place is Noisy". 如果评论多于5位用户,则将该地点评价为“嘈杂”,然后系统会自动将其评价为“地点嘈杂”。 (The noise review is input by the user using radio button when writing the review and save it into database). (噪声检查是在编写检查并将其保存到数据库时由用户使用单选按钮输入的)。 Below is the code that I working on. 下面是我正在处理的代码。

//view.php //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>

//controller //控制器

<?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);
 }
}
?>

//model //模型

<?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();

  }
}
?>

If I have understood your question correct: 如果我已正确理解您的问题:

Model: 模型:

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();
}

And I would include it in Controller: 我将其包含在Controller中:

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

And then in view: 然后来看:

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>

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM