How to send id value in codeigniter?

Member

by samara , in category: PHP Frameworks , 8 months ago

How to send id value in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raphael_tillman , 8 months ago

@samara 

To send an id value in CodeIgniter, you can use the URI Routing feature. Here's an example of how to send an id value in CodeIgniter:

  1. In your controller, define a method that accepts an id parameter:
1
2
3
public function profile($id) {
    // Your code here
}


  1. In your routes.php file (located in application/config folder), define a route that maps the URL to the controller method with the id parameter:
1
$route['profile/(:num)'] = 'ControllerName/profile/$1';


  1. In your view file, create a link that sends the id value:
1
<a href="<?php echo base_url('profile/'.$user_id); ?>">View Profile</a>


In the above example, when the user clicks on the link, they will be redirected to the "profile" method in the specified controller with the id parameter. The id value will be passed in the URL and can be accessed in the controller method using the $id parameter.


Make sure to validate and sanitize the id value to prevent security vulnerabilities such as SQL injection.