我的数据库
这是我的数据库“期刊”
我得到了结果: Records
我的问题是,我只显示了凭证号和日期一次,结果重复出现。 如何显示为一个。
我的控制器代码:
public function Journal_Print(){
$data['startdate'] =$this->input->post('SDate');
$data['enddate'] = $this->input->post('EDate');
$PName = $this->input->post('TName');
$startdate = $this->input->post('SDate');
$enddate = $this->input->post('EDate');
$date = str_replace('/', '-', $startdate);
$newDate = date("Y-m-d", strtotime($date));
$date2 = str_replace('/', '-', $enddate);
$newDate2 = date("Y-m-d", strtotime($date2));
$data['startdate'] = $startdate;
$data['enddate'] = $enddate;
$this->db->where('Date >=', $newDate);
$this->db->where('Date <=', $newDate2);
$query = $this->db->get('journal');
$data['PName']=$query->result_array();
// print_r($data);
$this->load->view('BookKeeping/Journal_Print', $data, FALSE);
}
我的查看页面代码:
<?php foreach ($PName as $row): ?>
<tr>
<td><?php echo date('d/m/Y', strtotime($row['Date']));?></td>
<td>
<?php echo $row['Vno'];?></td>
<td><?php echo $row['Parti'];?></td>
<td class="price"><?php echo $row['Debit'];?></td>
<td class="price1"><?php echo $row['Credit'];?></td>
</tr>
<?php endforeach ?>
如何避免这个问题
答案 0 :(得分:3)
此控制器代码
$this->db->select('*');
$this->db->distinct( );
$this->db->where('Date >=', $newDate);
$this->db->where('Date <=', $newDate2);
$result = $this->db->get('journal')->result_array();
if($result){
$finalArray = array();
$checkArray = array();
foreach ($result as $key => $value) {
if(in_array($value['Vno'], $checkArray)){
$finalArray[$value['Vno']][] = $value;
} else {
$checkArray[] = $value['Vno'];
$finalArray[$value['Vno']][] = $value;
}
}
$data['query'] = $finalArray;
我的答案图像 enter image description here
答案 1 :(得分:0)
首先,从数据库获得唯一的Date
和Vno
$this->db->select('Date, Vno');
$this->db->distinct();
$query = $this->db->get('journal')->result();
现在,使用它们从数据库获取所有记录
$unique_date = array(); $unique_vno = array();
foreach($query as $row){
array_push($unique_vno, $row->Vno);
array_push($unique_date, $row->Date);
}
$this->db->select('*');
$this->db->where_in('Date', $unique_date);
$this->db->where_in('Vno', $unique_vno);
$query = $this->db->get('journal');
$data['PName']=$query->result_array();