我有这个代码在joomla上查看php :(此代码生成月份下拉列表)
<form action="<?php echo JRoute::_('index.php?option=com_mycom'); ?>" method="post" name="adminForm" id="adminForm" class="modelList">
<select name="filter.search" class="inputbox" onchange="this.form.submit()">
<?php
for ($i = 0; $i <= 12; ++$i) {
$time = strtotime(sprintf('-%d months', $i));
$value = date('Y-m', $time);
$label = date('F Y', $time);
printf('<option value="%s">%s</option>', $value, $label);
}
?>
</select>
</form>
我有一个mysql表,其中“date”行调用“date” 所以我正在寻找的是 - 当我选择月份时,本月所有数据将加载到此页面
那么如何才能从sql查询中获取它?我在模型上尝试此代码,但这给了我一个错误:
$search = $this->getState('filter.search');
if(!empty($search))
{
if(stripos($search, 'id:') === 0)
{
$query->where('id = '.(int) substr($search, 3));
}
else
{
$search = $db->Quote('%'.$db->escape($search, true).'%');
$query->where('(date LIKE '.$search.')');
}
}
我试试这段代码:
if(!empty($search))
{
if(stripos($search, 'id:') === 0)
{
$query->where('a.id = '.(int) substr($search, 3));
}
else
{
$searchParts = explode('-', $search);
// day 1 of selected month
$searchStartDate = date('Y-m-d', mktime(0, 0, 0, $searchParts[1], 1, $searchParts[0]));
// day 0 of next month is last day of selected month
$searchEndDate = date('Y-m-d', mktime(0, 0, 0, $searchParts[1] + 1, 0, $searchParts[0]));
$query->where("(a.date BETWEEN '$searchStartDate' AND '$searchEndDate' LIKE '.$search.')");
}
}
但它返回错误:
Fatal error: Call to a member function where() on a non-object in
就在这一行 - $query->where("(a.date BETWEEN '$searchStartDate' AND '$searchEndDate' LIKE '.$search.')");
答案 0 :(得分:0)
使用DATE字段时,您应该这样做。您正在尝试将其作为字符串使用,这可能但非常低效。
而不是要求数据库返回所有记录WHERE date LIKE '%2013-07%'
,而不是要求所选月份的开头和结尾之间的所有记录。
SELECT *
FROM tbl
WHERE `date` BETWEEN '2013-07-01' AND '2013-07-31'
您可以将PHP代码更改为类似的内容 -
else
{
$searchParts = explode('-', $search);
// day 1 of selected month
$searchStartDate = date('Y-m-d', mktime(0, 0, 0, $searchParts[1], 1, $searchParts[0]));
// day 0 of next month is last day of selected month
$searchEndDate = date('Y-m-d', mktime(0, 0, 0, $searchParts[1] + 1, 0, $searchParts[0]));
$query->where("(date BETWEEN '$searchStartDate' AND '$searchEndDate')");
}