How to pass and retrieve parameters from url in codeigniter? - php

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);

Related

Getting the second part of a URL through PHP

I'm trying to setup a page that pulls just a part of the URL but I can't even get it to echo on my page.
Since I code in PHP, I prefer dynamic pages, so my urls usually have "index.php?page=whatever"
I need the "whatever" part only.
Can someone help me. This is what I have so far, but like I said, I can't even get it to echo.
$suburl = substr($_SERVER["HTTP_HOST"],strrpos($_SERVER["REQUEST_URI"],"/")+1);
and to echo it, I have this, of course:
echo "$suburl";
If you need to get the value of the page parameter, simply use the $_GET global variable to access the value.
$page = $_GET['page']; // output => whatever
your url is index.php?page=whatever and you want to get the whatever from it
if the part is after ? ( abc.com/xyz.php?......) , you can use $_GET['name']
for your url index.php?page=whatever
use :
$parameter= $_GET['page']; //( value of $parameter will be whatever )
for your url index.php?page=whatever&no=28
use :
$parameter1= $_GET['page']; //( value of $parameter1 will be whatever )
$parameter2= $_GET['no']; //( value of $parameter2 will be 28 )
please before using the parameters received by $_GET , please sanitize it, or you may find trouble of malicious script /code injection
for url : index.php?page=whatever&no=28
like :
if(preg_match("/^[0-9]*$/", $_GET['no'])) {
$parameter2= $_GET['no'];
}
it will check, if GET parameter no is a digit (contains 0 to 9 numbers only), then only save it in $parameter2 variable.
this is just an example, do your checking and validation as per your requirement.
You can use basic PHP function parse_url for example:
<?php
$url = 'http://site.my/index.php?page=whatever';
$query = parse_url($url, PHP_URL_QUERY);
var_dump(explode('=', $query));
Working code example here: PHPize.online

$GET query to directory?

i want to fetch youtube videos from the above script but the above code is getting keyword from GET parameter example.com/s=keyword and i want it to get from a example.com/HERE
i mean you can see there is a $_GET['s']
So this function works like this
example.com/s=keyword
and i want it to work like this
example/page/keyword
sorry for my bad english
$keyword = $_GET['s'];
file_get_contents("https://www.googleapis.com/youtube/v3/search?part=snippet&q=$keyword&type=video&key=abcdefg&maxResults=5");
Have a look at $_SERVER[REQUEST_URI]
This will return you the current url. Then process it using simple string or array functions to get the params, like
$current_url = $_SERVER['REQUEST_URI'];
$url_arr = explode("/", $current_url);
Then access the parameters using the array indexes
like $page = $url_arr[0];

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
}
}

Url Explode for value

Currently I have a url thats like this,
http://website.com/type/value
I am using
$url = $_SERVER['REQUEST_URI'];
$url = trim($url, '/');
$array = explode('/',$url);
this to get the value currently but my page has Facebook like's on it and when it is clicked it adds all these extra variables. http://website.com/type/value?fb_action_ids=1234567&fb_action_types= and that breaks that value that I am trying to get. Is there another way to get the specific value?
Assuming you know that this will always be a valid URL, you can use parse_url.
list(, $value) = explode('/', parse_url($url)['path']);
I'd use a preg_replace
explode('/', preg_replace('/?.*$/', '', $url));
You could also use:
$array = explode('/',$_SERVER['PATH_INFO']);
Or, this:
$array = explode('/',$_SERVER['PHP_SELF']);
With this, you do not need the trim() call or the temp var $url - unless you use it from something else.
The reason for two options is I don't know if /type/value is being passed to an index.php or if value is in fact a php file. Either way, one of the two options will give you what you need.

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