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

Hi, I'm new to codeigniter, and I'm struggling with this: 
I have my view "vehicles" in which all the available vehicles cards are displayed. If I click on a card I'd like to be routed to a view of the requested vehicle showing all the details of the car. So how can I pass to the view the information about the car I selected? I just need the id of the vehicle, all the other infos can be retrieved form the database....
Thanks!
Reply
#2

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#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
#4

(This post was last modified: 09-09-2019, 11:25 AM by demyr.)

PHP Code:
public function car()
    {  
            $id 
$this->uri->segment(2);
            $this->load->model('Vehicle_Model);
            $data['
vehicle'] = $this->Vehicle_Model->get_vehicle_by_id($id);
            $this->load->view('
car', $data);
    } 


Also, are you sure about the $id?
try it first with this:


PHP Code:
public function car()
    {  
            $id 
$this->uri->segment(2);
  echo $id;
  die();


Plus, in your view use them this way

           
PHP Code:
<?php echo $vehicle_item->description?>
Reply
#5

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

(09-09-2019, 11:23 AM)demyr Wrote:
PHP Code:
public function car()
    {  
            $id 
$this->uri->segment(2);
            $this->load->model('Vehicle_Model);
            $data['
vehicle'] = $this->Vehicle_Model->get_vehicle_by_id($id);
            $this->load->view('
car', $data);
    } 


Also, are you sure about the $id?
try it first with this:


PHP Code:
public function car()
    {  
            $id 
$this->uri->segment(2);
  echo $id;
  die();


Plus, in your view use them this way

           
PHP Code:
<?php echo $vehicle_item->description?>


If I use echo $id I actually get the id of the vehicle displayed... But if i comment that echo, use my controller as it is and use in the view <?php echo $vehicle_item->brand;?> (for example) i get Undefined variable: vehicle_item
EDIT: it didn't work because i have to use <? php echo $vehicle->brand;?> but can you please explain why this works with the sintax "variable->name" and not with variable['name']. And is it better to use this method or something like jquery? 
Reply
#6

If $id works, please check your Model then

       
PHP Code:
public function get_vehicle_by_id($id) {
$query $this->db->select('*')
->
from('vehicles')
->
where('id_vehicle'$id)
->
get()
->
row();

return 
$query;



If you use it in your way $variable['data'], you will probably have this error: 



PHP Code:
Cannot use object of type stdClass as array 
Reply
#7

Database results are like this:

PHP Code:
/**
 * Result Objects - $row->title;
 * result();
 * row();
 *
 * Result Arrays - $row['title'];
 * result_array();
 * row_array();
 */ 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#8

In the view that shows a list of vehicles, include the $id for each vehicle in the link to your controller.
Let's say you have a table with 20 vehicles (generated in a for each loop); the last column of each row has a link or button that says 'Details'.
The link for each 'Details' button should be like this:
PHP Code:
<td><?= anchor('vehicles/car/' $car->id'Details');?></td> 
Remember to load the url helper (in your controller).
Now, if the user clicks the link, he is pointed to the car function in the vehicles controller, and the $id for the car is passed to the controller.

In the controller:
PHP Code:
public function car($id NULL)
{
  if (! $id) {
      show_error('You did not pass an id to retrieve car details!');
  }
  else {
    // get the details for the car with id = $id from your database, pass the data to another view to display them.
  }

Reply
#9

You can always add data to the view using:

PHP Code:
$data['test'] = 'Testing';

$this->load->vars($data); 

With out using a view, makes it global to all views.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#10

(09-11-2019, 05:59 PM)InsiteFX Wrote: You can always add data to the view using:

PHP Code:
$data['test'] = 'Testing';

$this->load->vars($data); 

With out using a view, makes it global to all views.
Yeah, that is what worked in the final place
Reply




Theme © iAndrew 2016 - Forum software by © MyBB