我想根据用户选择的年份和月份选择数据。但结果并没有显示正确的输出。它只显示数据库中所有数据的列表。
有人可以帮我修改我的代码
查看
<div class="large-3 columns ">
<?php echo form_open("announcement/announcement_result");?>
<?php echo form_dropdown('m', $m, set_value('m'), 'id="m"'); ?>
<?php echo form_dropdown('q', $q, set_value('q'), 'id="q"'); ?>
<?php echo form_submit('search', 'SEARCH', 'class="button expand"'); ?>
<?php echo form_close(); ?>
</div>
CONTROLLER
function announcement_list()
{
$q = '';
$data['q'] = $q;
$this->data['q'] = array(
'' => ' Select Year',);
for ($i = 0; $i < 10; $i++)
{
$date = date('Y') - $i;
$data['q'][$date] = $date;
}
$m = '';
$data['m'] = $m;
$data['m'] = array(
'' => 'Select Month',
);
for ($m = 1; $m <= 12; $m++) {
$month = date("F", mktime(0, 0, 0, $m));
$data['m'][$month] = $month;
}
$data['results'] = $this->news_model->get_announcement_list($config['per_page'], $page);
}
public function announcement_result($q='default', $m='default')
{
if ($q == '') $q = $this->input->post('q');
$this->data['q'] = $q;
if ($q == '') redirect(base_url().'announcement/announcement_list');
$q = '';
$data['q'] = $q;
$this->data['q'] = array(
'' => ' Select Year',);
for ($i = 0; $i < 10; $i++)
{
$date = date('Y') - $i;
$data['q'][$date] = $date;
}
$m = '';
$data['m'] = $m;
$data['m'] = array(
'' => 'Select Month',
);
for ($m = 1; $m <= 12; $m++) {
$month = date("F", mktime(0, 0, 0, $m));
$data['m'][$month] = $month;
}
$data['results'] = $this->news_model->get_results($m, $q, $limit=6, $offset=0);
}
MODEL
function get_results($m ='default', $q='default', $limit, $offset)
{
$sql = "SELECT *
FROM ArkibBerita
WHERE code='PENGUMUMAN' AND Enable = 'Y' AND Lang ='EN' AND BeritaDate LIKE '%$q%'
ORDER BY position ASC
OFFSET {$offset} ROWS
FETCH NEXT {$limit} ROWS ONlY";
$query = $this->db->query($sql);
return $query->result();
}
答案 0 :(得分:0)
您应该检查发送的内容:'%$ q%'
AND BeritaDate LIKE '%$q%'
请尝试回显$ sql变量。然后从sql表中选择一行并比较BeritaDate
字段的结果。
如果没有结果,我认为您以不同的格式发送数据。您应该将完全相同的数据发送到sql表中。
答案 1 :(得分:0)
而不是您模型中的BeritaDate LIKE '%$q%' use YEAR(BeritaDate)='{$q}' AND MONTH(BeritaDate)='{$m}
',我在这里考虑的是月份和q作为年份的视图