How to get the id in the URI when using CodeIgniter - php

I'm working on a CodeIgniter project and trying to get the id of this URI:
http://example.com/myDomain/index.php/public/calendar/gestionEvent?id=c2pnumqnqr0dg9tgmaklho5280#google.com
I want the controller to delete or update the event using this ID.
I tried $this->uri->segment(4)but it doesn't seem to work and doesn't show anything.

try this :
$id = $this->input->get_post('id');
echo $id;

in cotroller
$id = $this->input->get('id');
$data['id'] = $id ;
you can not use uri segment here
ex:-http://example.com/index.php/news/local/metro/crime_is_up
here you can use uri segments
The segment numbers would be this:
1.news
2.local
3.metro
http://codeigniter.com/user_guide/libraries/uri.html
UPDATE
if you want to get only the id with out #google.com you can simply use explode()
$id_string = $this->input->get('id');
$id_string_exp = explode("#", $id_string);
$id = $id_string_exp[0];

Then use
$id = $this->input->get('id');
$this->uri->segment(4) will work only if the url is in the form of segments like
http://example.com/myDomain/index.php/public/calendar/gestionEvent/c2pnumqnqr0dg9tgmaklho5280#google.com
Then you will get id using "URI segments".Thanku.i hope this helps you friend

Related

Get value from url codeigniter

I have a domain and id is coming in that domain like this
http://www.test.com/update_record_row/id/6356/
I want id value from the domain without passing ? instead of / in (query string)
Using $this->uri->segment() you get the URL parameter
$this->uri->segment(1)
For more ..https://www.codeigniter.com/userguide3/libraries/uri.html
ID
public function update_record_row($id)
{
echo "Id is :".$id;
}
or
Use URI segment
// if URL -http://www.test.com/update_record_row/id/6356/
$id = $this->uri->segment(3);
// if URL -http://www.test.com/update_record_row/6356/
$id = $this->uri->segment(2);
Use $this->uri->segment() to retreive passed value through url
If URL is http://www.test.com/update_record_row/id/6356/
You should use $this->uri->segment(3);
Codeigniter has awesome userguide.Just check this in in uri part.
https://www.codeigniter.com/userguide3/libraries/uri.html#uri-class

How to pass and retrieve parameters from url in codeigniter?

my url is
http://localhost/helpinghand_web/home/userAutologin?tval=Eo7TIhTJqQnfysn8mwu2nXOQ0yJvY36hprJ99GJH9NBZHTh1LU&page=profile
there are two parameter tval and page how can i get it.
following are not working..
$var=$_GET['tval'];$var=$this->input->get('tval');
use this
$tval = $this->input->get('tval');
$page = $this->input->get('page');
You can use this:
$params = $this->input->get(null, true);
to get all the params passed and then use:
$params['tval']
You can use $tval = $this->input->get('tval') for get single data & $tval = $this->input->get() for get all date in singel variable in array format.
I managed the query you can use it.
If you call www.example.com?tval=1100, it will echo 1100, provided the Home
controller is routed correctly.
You can also use the http://ellislab.com/codeigniter%... to get these parameters.
For your query: $this->input->get('tval', TRUE);

url_title codeigniter showing underscores instead of hyphens

Hi I am trying to insert my article title in database in the following format this-is-a-new-title for the input This is a new Title. For this I have written :
$title = $this->input->post('topic_title');
$topic_slug_title = url_title($title,'-',TRUE);
But the echo $topic_slug_title shows titles like this_is_a_new_title. Why the underscores are added wherein I have given hyphens ?
Ok I found the solution. Just leave the second parameter as default,add the third parameter. That is:
$title = $this->input->post('topic_title');
$topic_slug_title = url_title($title,TRUE,TRUE);
don't use the third parameter, use it like this
$title = $this->input->post('topic_title');
$topic_slug_title = strtolower(url_title($title));
don't use second and third parameter, write like this:
**$title = $this->input->post('topic_title');
$topic_slug_title = url_title($title);**

Is there another way to retrieve data in url rather than URI->SEGMENT?

Example:
...scms/contracts/set_pm/3/Monthly
this is the URL and i want to get the word monthly...I cant use uri->segment in my view so I'm asking if there's other way
Just retrieve the current_url() and explode it with /, but why do such cumbersome process when you have lots of options provided by CI.
If you cant use $this->uri in your view just put that segment in a variable and load the view like this:
$data['segment'] = $this->uri->segment(5); //your segment here you want in your view
$this->load->view('view', $data);
Now, you will get the segment in your view as $segment.
May be something like this
$actual_link = current_url();
$slashes = explode("/",$actual_link);
echo $element = $slashes[count($slashes)-1];
You can use explod function in PHP to get a tab with your string in input.
$tab = explode("/", $url);
$last = end($tab);
Considering contracts is your controller file name and set_pm is your function then you can also fetch it like this:
class Contracts extends CI_Controller {
function set_pm($id, $interval)
{
echo $id; //prints 3
echo $interval; // prints Monthly
}
}

fetching the url in php

my url is like below:
http://www.xyz.org/abc/list.php?id=1
now "$_SERVER['PHP_SELF']" gives me only "abc/list.php" this portion
and "$_SERVER['REQUEST_URI']" gives me "abc/list.php?id=1" this
now if i want to fetch only the portion "?id=1" how to do that.
coz im having problems in the paging query for this.
thanxx in advance...
It's $_SERVER['QUERY_STRING'].
Use $_GET['id'] to access the id parameter.
Take a look at http://php.net/parse_str, http://php.net/parse-url and http://php.net/http_build_query
Depending on what you're doing http://bradym.net/php/modify-query-string-parameters may also be helpful.
$_GET['id'] or $_REQUEST['id']
if your not sure which GET values u'll receive u also could
$fullRequest = exlode('?', $_SERVER['REQUEST_URI']);
$params = array();
foreach(explode('&', $fullRequest) as $part){
foreach(explode('=', $part) as $keyVal){
$params[$keyVal[0]] = $keyVal[1];
}
}

Categories