【问题标题】:Codeigniter If else statement conditionCodeigniter If else 语句条件
【发布时间】:2016-05-22 17:57:12
【问题描述】:

我正在尝试在我的 codeigniter 中包含 if else 语句。当用户搜索一个地点时,它可以从数据库中查看该地点的信息。我正在尝试添加 if else 语句。如果评论超过 5 个用户将该地方评为“嘈杂”,则系统将自动将其评为“该地方嘈杂”。 (噪音评论由用户在编写评论时使用单选按钮输入并将其保存到数据库中)。下面是我正在处理的代码。

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

  }
}
?>

【问题讨论】:

标签: php html codeigniter


【解决方案1】:

如果我理解你的问题是正确的:

型号:

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>

希望这会有所帮助。

【讨论】:

  • 谢谢。但是,如果用户使用“是”或“否”来评价场所的噪音。如何添加?
  • 请检查控制器中的更新,让我们都知道它是如何工作的。
  • 它不起作用。它只会回显“嘈杂”,甚至改变条件。对于“$something”,我实际上应该放什么?我对那部分不太了解。
  • 好吧,可能是你从控制器中删除了“某事”。在模型中正确使用“是”或“是”(取决于你在 db 中的方式)这个词。在我的更新也回答..看看..
  • 非常感谢。它对我有很大帮助。我知道它现在是如何工作的。但又来了一个问题。它计算我的整个数据库,而不是用户搜索的特定位置。
猜你喜欢
  • 2012-12-17
  • 1970-01-01
  • 1970-01-01
  • 2014-09-06
  • 2021-03-28
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 2021-11-30
相关资源
最近更新 更多