Skip to content
Advertisement

Is there a way to avoid duplicate data on the 1st and 2nd tab? PHP Codeigniter

I am creating a shopping cart like program, so after i request or sent a request for reservation then the data will be put on the 2nd tab. I have already done that, but the problem is that I need to remove the choosed data on the 1st tab. Here is an image for visual reference:

So the output should be: on the 1st tab, EO1 should only be available, and the highlited red circles should be placed on tab 2.

enter image description here

Then my 2nd Tab

enter image description here

I have already tried using new Array and Array push and in Array it but still giving the problem which the 1st tab data is still showing, how can I hide or remove the data that has been chosen from the 1st tab? here is my code

View

1st Tab

<?php $newArray = array(); ?>

 <?php foreach($dorms as $dorm): ?>
  
    <?php 
        $res = $dorm['code'];
        array_push($newArray, $res);
    ?>

  <a class="text-decoration-none" href="<?php echo base_url() ?>pages/view/<?php echo $dorm['id']; ?>">
    <div class="col-md-6 mt-4" style="margin-left: 6rem;padding-bottom: 1rem;">
      <div class="card top-shadow" style="width: 12rem; text-align:center;display:inline-block;">
        <div class="card-body">
          <img src="<?php echo base_url(); ?>assets/images/dorms/<?php echo $dorm['image'] ?>" style="border-radius: 15%;width:100px;height:100px;">
          <hr class="border-black">
          <h5 class="card-title color-black">Room Type: <b><?php echo $dorm['type']; ?></b></h5>
          <h5 class="card-title color-black">Room Cost: <br><b> P <?php echo $dorm['price']; ?>.00</b></h5>

          <?php if ($dorm['status'] == 0): ?>
              <h5 class="card-title color-black"> <b>  <span class="badge badge-success">Reserve Now</span>  </b></h5>
          <?php elseif ($dorm['status'] == 1): ?>
               <h5 class="card-title color-black"> <b>  <span class="badge badge-info">Pending Reservation</span>  </b></h5>
          <?php endif; ?>

        </div>
      </div>
    </div>
  </a>

<?php endforeach; ?>

2nd Tab

<?php foreach($dormreserved as $dormres): ?>
 
  <?php
      $pos = $dormres['code'];  
  ?>

  <?php if(in_array($pos, $newArray)): ?>

    <a class="text-decoration-none" href="<?php echo base_url() ?>pages/view/<?php echo $dormres['id']; ?>">
      <div class="col-md-6 mt-4" style="margin-left: 6rem;padding-bottom: 1rem;">
        <div class="card top-shadow" style="width: 12rem; text-align:center;display:inline-block;">
          <div class="card-body">
            <img src="<?php echo base_url(); ?>assets/images/dorms/<?php echo $dormres['image'] ?>" style="border-radius: 15%;width:100px;height:100px;">
            <hr class="border-black">

            <h5 class="card-title color-black">Room Type: <b><?php echo $dormres['code']; ?></b></h5>
            <h5 class="card-title color-black">Room Type: <b><?php echo $dormres['type']; ?></b></h5>
            <h5 class="card-title color-black">Room Cost: <br><b> P <?php echo $dormres['price']; ?>.00</b></h5>

            
              <h5 class="card-title color-black"> <b>  <span class="badge badge-info">Pending Reservation</span>  </b></h5>

          </div>
        </div>
      </div>
    </a>

<?php endif; ?>
<?php endforeach; ?>

Controller

public function home()
{
    // Check login
    if(!$this->session->userdata('student_logged_in')){
        redirect('students/login');
    }

    $data['title'] = 'Home';
    
    $data['dorms'] = $this->dorm_model->get_dorms();
    $data['dormreserved'] = $this->dorm_model->get_reserves();

    $this->load->view('templates/header', $data);
    $this->load->view('students/pages/home', $data);
    $this->load->view('templates/footer');

}

Model

public function get_dorms(){
    $this->db->order_by('id', 'DESC');
    $query = $this->db->get('dorms');
    return $query->result_array();
}


public function get_reserves(){
    $this->db->join('reserves', 'reserves.dorm_id = dorms.id');
    $this->db->where('reserves.tenant_id', $this->session->userdata('student_user_id'));
    $query = $this->db->get('dorms');
    return $query->result_array();
}

Advertisement

Answer

Please update get_dorms function

public function get_dorms(){
    $this->db->where('id NOT IN(select dorm_id from reserves)');
    $this->db->order_by('id', 'DESC');
    $query = $this->db->get('dorms');
    return $query->result_array();
 }
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement