Pass data from view to controller |
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!
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 )
(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.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() <?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 PHP Code: public function car() Also, are you sure about the $id? try it first with this: PHP Code: public function car() Plus, in your view use them this way PHP Code: <?php echo $vehicle_item->description; ?>
(09-09-2019, 11:23 AM)demyr Wrote: 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?
If $id works, please check your Model then
PHP Code: public function get_vehicle_by_id($id) { 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
Database results are like this:
PHP Code: /** What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
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> 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)
You can always add data to the view using:
PHP Code: $data['test'] = 'Testing'; 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 )
|
Welcome Guest, Not a member yet? Register Sign In |