skip to Main Content

My database

Table

This my database “journal”

I have got the result:
Records

My problem is I have displayed the voucher number and date into only one time, it is repeated on my result.
How to display as one.

My controller code:

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


}

My view page code:

<?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 ?>

How to avoid this problem

2

Answers


  1. Chosen as BEST ANSWER

    This Controller Code

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

    My Answer Image enter image description here


  2. First, get unique Date and Vno from DB

    $this->db->select('Date, Vno');
    $this->db->distinct();
    $query = $this->db->get('journal')->result();    
    

    Now, use these to get all the records from DB

    $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();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search