Welcome Guest, Not a member yet? Register   Sign In
Pass data from view to controller
#3

(This post was last modified: 09-09-2019, 09:31 AM by mandiatutti.)

(09-09-2019, 08:03 AM)InsiteFX Wrote: Each of your vehicle cards should have a link that goes to the controller passing the vehicle id to it.

You can then assign it to the views $data array and pass it to the view

It would help us a lot if you posted the code to what your trying to do.
The vehicle cards are create with a foreach cycle, but creating the link is not the problem... But the main problem is how do I pass the vehicle id to the car detail view... I found various methods, but I can't figure out which one is best for me... down here you can find my vehicle and car controllers (as they are right now with all my trials). I write down also the vehicle view and my (temporary) car view. 
Controllers:
Code:
public function vehicles()
    {  
    $data['vehicles'] = $this->vehicle_model->get_vehicles();
    $title['page_title'] = ucfirst('vehicles');
    $this->load->view('header', $title);
    $this->load->view('vehicles', $data);
    $this->load->view('footer');
    }

public function car()
    {   
            $id = $this->uri->segment(2);
            $data['vehicle'] = $this->vehicle_model->get_vehicle_by_id($id);
            $this->load->view('car', $data);
    }
Vehicles view: 
<?php foreach ($vehicles as $vehicle_item): ?>
<div class="col-md-4 mt-5">
      <div class="card text-center">
        <img class="card-img-top picture_crop" src="assets/images/cars/<?php echo $vehicle_item['id_vehicle']?>/<?php echo $vehicle_item['picture']?> " alt="<?php echo$vehicle_item['model']  ?>" href = "<?php echo base_url().'/car'.$vehicle_item['id_vehicle']?>">
        <div class="card-body">
          <h5 class="card-title"><?php echo $vehicle_item['brand']. ' ' .$vehicle_item['model']?></h5>
          <hr>
          <p>
            <li><a href = <?php echo base_url('car/'. $vehicle_item['id_vehicle']) ?>"> Bottone <a>
          </p>
          <div class="collapse" id="collapseExample">
            <div class="card card-body">
            <p class="card-text"><?php echo $vehicle_item['description']?></p>
            <p class="card-text">Motore: <?php echo $vehicle_item['engine']?></p>
            <p class="card-text">Anno: <?php echo $vehicle_item['year']?></p>
            </div>
          </div>
        </div>
        <div class="card-footer text-muted">
          <div class="row">
            <div class="col">
              <a href=""><i class="fas fa-map"></i></a>
            </div>
            <div class="col">
              <a href="mailto:[email protected]"><i class="fas fa-envelope"></i></a>
            </div>
            <div class="col">
              <a href="tel:+123456789"><i class="fas fa-phone"></i></a>
            </div>
          </div>
        </div>
      </div>
    </div>
<?php endforeach?>
</div>
</div>


Car view: 
<?php echo $vehicle['model']?>

Code:
Controller
public function get_vehicle_by_id($id_vehicle) {
        
            /**$this->db->from('vehicles');
            $this->db->where('id_vehicle', $id_vehicle);*/
            $query = $this->db->query("SELECT * FROM vehicles WHERE id_vehicle = '$id_vehicle'");
            $row = $query->row();
            return $row;/*$this->db->get()->row();*/
           
        }
Reply


Messages In This Thread
RE: Pass data from view to controller - by mandiatutti - 09-09-2019, 08:31 AM
RE: Pass data from view to controller - by demyr - 09-09-2019, 11:23 AM
RE: Pass data from view to controller - by demyr - 09-11-2019, 06:35 AM



Theme © iAndrew 2016 - Forum software by © MyBB