【问题标题】:How to avoid the repetation of date and voucher number?如何避免日期和凭证号重复?
【发布时间】:2019-06-22 13:57:55
【问题描述】:

我的数据库

Table

这是我的数据库“日志”

我得到了结果: 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 ?>

如何避免这个问题

【问题讨论】:

  • 存在重复的凭证号,但您想避免它。对吗?
  • @DanishAli 日期也

标签: php codeigniter phpmyadmin


【解决方案1】:

首先,从 DB 中获取唯一的 DateVno

$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();

【讨论】:

  • 它不会改变任何东西,先生
  • 同样的结果可用
  • @Tom 我的回答做了一些改动
  • 使用 group_by 其他信息不显示
  • @Tom 另一个变化
【解决方案2】:

此控制器代码

  $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

【讨论】:

    猜你喜欢
    • 2015-01-02
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多