Php 使用group_by-Codeigniter显示所有行
我正在与codeigniter合作。下面是我的代码Php 使用group_by-Codeigniter显示所有行,php,codeigniter,Php,Codeigniter,我正在与codeigniter合作。下面是我的代码 public function get_content(){ $this->db->select('*'); $this->db->from('content_table'); $this->db->where('status', "Active"); $this->db->group_by('MONTH(content_p
public function get_content(){
$this->db->select('*');
$this->db->from('content_table');
$this->db->where('status', "Active");
$this->db->group_by('MONTH(content_publish_date)');
$query = $this->db->get();
return $query->result();
}
我想按月提取行&结果将按月显示。。
但是根据上面的查询,我每个月只能得到一行。。我需要所有的行显示每月的结果。。请帮忙
您需要使用
按顺序
而不是按分组
这样做:
public function get_content(){
$this->db->select('*');
$this->db->from('content_table');
$this->db->where('status', "Active");
$this->db->order_by('MONTH(content_publish_date)', 'DESC');
$query = $this->db->get();
return $query->result_array();
}
在视图文件中编写以下代码:
$month = "";
foreach($paaed_arr AS $row){
$dbmonth = date('m', strtotime($row['content_publish_date']);
if($month != $dbmonth){
echo date('F Y', strtotime($row['content_publish_date']));
$month = $dbmonth;
}
// Put your code here to display records of this month ....
}
用于对相同的数据进行分组,而用于对数据进行排序
让我知道更多的帮助 您需要使用
orderby
而不是groupby
这样做:
public function get_content(){
$this->db->select('*');
$this->db->from('content_table');
$this->db->where('status', "Active");
$this->db->order_by('MONTH(content_publish_date)', 'DESC');
$query = $this->db->get();
return $query->result_array();
}
在视图文件中编写以下代码:
$month = "";
foreach($paaed_arr AS $row){
$dbmonth = date('m', strtotime($row['content_publish_date']);
if($month != $dbmonth){
echo date('F Y', strtotime($row['content_publish_date']));
$month = $dbmonth;
}
// Put your code here to display records of this month ....
}
用于对相同的数据进行分组,而用于对数据进行排序
让我知道更多的帮助 要做到这一点,您应该订购日期栏。。试试这个
public function get_content(){
$this->db->select('*');
$this->db->from('content_table');
$this->db->where('status'), "Active");
$this->db->order_by("MONTH(content_publish_date)", "asc");
$query = $this->db->get();
return $query->result();
}
要实现这一点,您应该订购date colmn。。试试这个
public function get_content(){
$this->db->select('*');
$this->db->from('content_table');
$this->db->where('status'), "Active");
$this->db->order_by("MONTH(content_publish_date)", "asc");
$query = $this->db->get();
return $query->result();
}
首先,where子句在语句中有额外的括号
$this->db->where('status'), "Active");
像这样改变它
$this->db->where('status', "Active");
其次,添加order by而不是group by,但是order by应该是日期
$this->db->order_by('DATE(content_publish_date)', 'DESC');
第三,在select as中获取当前记录的月份
$this->db->select('*,MONTH(content_publish_date) as content_publish_month');
表示您的最终查询将是
$this->db->select('*,MONTH(content_publish_date) as content_publish_month');
$this->db->from('content_table');
$this->db->where('status', "Active");
$this->db->order_by('DATE(content_publish_date)', 'DESC');
$query = $this->db->get();
return $query->result_array();
然后,在视图中,您必须添加一个变量,该变量将检查月份是否与前一条记录的月份相同,然后只显示记录,否则也显示月份标题,例如:
$lastMonth = 0;
foreach($result as $oneRec)
{
if(($lastMonth == 0) || ($lastMonth != $oneRec['content_publish_month'])
{
//show header
echo date('F Y', strtotime($row['content_publish_date']));
//check if its first record or not
if($lastMonth != 0)
{
echo '</table>';
}
//then add table start tag
echo = '<table><tr>
<th>Date</th>
<th>Title</th>
<th>Publication</th>
</tr>';
}
//show record here
echo = '<tr>
<td>'.$oneRec['content_publish_date'].'</td>
<td>'.$oneRec['title'].'</td>
<td>'.$oneRec['publication'].'</td>
</tr>';
//then set current record's month as last month as
$lastMonth = $oneRec['content_publish_month'];
}
echo '</table>';
$lastmount=0;
foreach(结果为$oneRec)
{
如果($lastmount==0)| |($lastmount!=$oneRec['content\u publish\u month']))
{
//显示标题
回显日期('fy',strotime($row['content\u publish\u date']);
//检查是否有第一条记录
如果($lastMonth!=0)
{
回声';
}
//然后添加表开始标记
回声
日期
标题
出版
';
}
//在这里显示记录
回声
“.$oneRec[“内容发布日期”]”
“.$oneRec[‘头衔’]”
“.$oneRec[‘出版物’]”
';
//然后将当前记录的月份设置为上个月
$lastmount=$oneRec['content\u publish\u month'];
}
回声';
首先,where子句在语句中有额外的括号
$this->db->where('status'), "Active");
像这样改变它
$this->db->where('status', "Active");
其次,添加order by而不是group by,但是order by应该是日期
$this->db->order_by('DATE(content_publish_date)', 'DESC');
第三,在select as中获取当前记录的月份
$this->db->select('*,MONTH(content_publish_date) as content_publish_month');
表示您的最终查询将是
$this->db->select('*,MONTH(content_publish_date) as content_publish_month');
$this->db->from('content_table');
$this->db->where('status', "Active");
$this->db->order_by('DATE(content_publish_date)', 'DESC');
$query = $this->db->get();
return $query->result_array();
然后,在视图中,您必须添加一个变量,该变量将检查月份是否与前一条记录的月份相同,然后只显示记录,否则也显示月份标题,例如:
$lastMonth = 0;
foreach($result as $oneRec)
{
if(($lastMonth == 0) || ($lastMonth != $oneRec['content_publish_month'])
{
//show header
echo date('F Y', strtotime($row['content_publish_date']));
//check if its first record or not
if($lastMonth != 0)
{
echo '</table>';
}
//then add table start tag
echo = '<table><tr>
<th>Date</th>
<th>Title</th>
<th>Publication</th>
</tr>';
}
//show record here
echo = '<tr>
<td>'.$oneRec['content_publish_date'].'</td>
<td>'.$oneRec['title'].'</td>
<td>'.$oneRec['publication'].'</td>
</tr>';
//then set current record's month as last month as
$lastMonth = $oneRec['content_publish_month'];
}
echo '</table>';
$lastmount=0;
foreach(结果为$oneRec)
{
如果($lastmount==0)| |($lastmount!=$oneRec['content\u publish\u month']))
{
//显示标题
回显日期('fy',strotime($row['content\u publish\u date']);
//检查是否有第一条记录
如果($lastMonth!=0)
{
回声';
}
//然后添加表开始标记
回声
日期
标题
出版
';
}
//在这里显示记录
回声
“.$oneRec[“内容发布日期”]”
“.$oneRec[‘头衔’]”
“.$oneRec[‘出版物’]”
';
//然后将当前记录的月份设置为上个月
$lastmount=$oneRec['content\u publish\u month'];
}
回声';
如果一个月有多行,您想要一行吗?表格中会有多行具有不同的月份。在这种情况下,您应该执行order\u by
查看答案如果您一个月有多行,您想要一行吗?表格中会有多行具有不同的月份。在这种情况下,您应该执行order_by
查看答案,但格式化并不容易。我想用行分别显示每个月。好的。给我一些文档/图形视图,看看您需要如何显示记录,这样我可以帮助您。谢谢,但它不起作用按月格式化和提取数据并不容易。它不会对具有相同月份的数据进行分组。但它会格式化不容易..我想单独显示每个月的行..好的。给我一些文档/图形视图,看看你需要如何显示记录,这样我可以帮助你。谢谢,但它不工作按月格式化和提取数据不容易。它不分组具有相同月份的数据。你是否尝试过?如果尝试过,你能给我发送结果吗快照?我正在寻找类似的内容。这节省了很多麻烦。您是否尝试过?如果尝试过,是否可以向我发送结果快照?我正在寻找类似的内容。这节省了很多麻烦。