codeigniter php搜索日期明智的不显示所有结果

wlsrxk51  于 2022-12-07  发布在  PHP
关注(0)|答案(2)|浏览(110)

我有一个codeigniter网站,我有简单的搜索框,这是像下面:

<form method="post" action="<?php echo base_url()?>homecontroller/search">
  <label>Month: </label>

  <select class="form-select" id="basicSelect" name="month">
    <option value="01">January</option>
    <option value="02">February</option>
    <option value="03">March</option>
    <option value="04">April</option>
    <option value="05">May</option>
    <option value="06">June</option>
    <option value="07">July</option>
    <option value="08">August</option>
    <option value="09">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>

  </select>

  <label>Year: </label>

  <select class="form-select" id="basicSelect" name="year">
    <option value="2022">2022</option>
    <option value="2021">2021</option>
    <option value="2020">2020</option>
    <option value="2019">2019</option>
    <option value="2018">2018</option>
    <option value="2017">2017</option>
    <option value="2016">2016</option>
    <option value="2015">2015</option>
    <option value="2014">2014</option>
    <option value="2013">2013</option>
    <option value="2012">2012</option>

  </select>

  <input style="margin-top:24%" type="submit" class="btn btn-primary ml-1" name="billing" value="SEARCH">

</form>

我的控制器看起来像:

$month=$this->input->post('month');
    $year=$this->input->post('year');

    $stotal=$year.'-'.$month;
   
    $data['search'] = $this->user->searchbilling($stotal);

我在model中执行了以下代码来获取结果:

public function searchbilling($stotal) { 
$this->db->select('*, SUM(quantity) as quantity'); 
$this->db->from('delivered'); 
$this->db->like('date', $stotal); 
$this->db->group_by('YEAR(date), MONTH(date), customer'); 
$query = $this->db->get(); 
$result = $query->result(); 
return $result; }

然而,这是noy显示所有的结果,从该特定的一个月,谁能请告诉我什么是错的在这里,提前感谢

moiiocjp

moiiocjp1#

你可以这样试试where("DATE_FORMAT(date,'%Y-%m') =", $stotal)

public function searchbilling($stotal) { 
  $this->db->select('*, SUM(quantity) as quantity'); 
  $this->db->from('delivered'); 
  $this->db->where("DATE_FORMAT(date,'%Y-%m') =", $stotal); 
  $this->db->group_by('YEAR(date), MONTH(date), customer'); 
  $query = $this->db->get(); 
  $result = $query->result(); 
  return $result; 
}
s5a0g9ez

s5a0g9ez2#

您可以这样做where(“DATE_FORMAT(date,'FORMAT YOU NEED')=",$stotal)

public function searchbilling($stotal) { 
  $this->db->select('*, SUM(quantity) as quantity'); 
  $this->db->from('delivered'); 
  $this->db->where("DATE_FORMAT(date,'FORMAT YOU NEED') =", $stotal); 
  $this->db->group_by('YEAR(date), MONTH(date), customer'); 
  $query = $this->db->get(); 
  $result = $query->result(); 
  return $result; 
}

相关问题