str_replace with $_GET value and query results - php

I want to replace my query result according to the $_GET value like
If I have URL like
URL/?userid=3255
then the output of the userid from the database should replace with $_GET value when printing the database data
PS: I don't want to update the userid in database i just want to replace userid on the front end when printing the data from database
database table:
userid | name
-----------|
userid=4332|ron
Code:
<?php str_replace("userid=","userid=echo $_GET['userid'],"$row['userid']);?>
$row['userid'] is the data that i fetch from the database

Try with this one:
<?php str_replace("userid=","userid=".$_GET['userid'], $row['userid']);?>
but if user changes $_GET['userid'] param printed result will also change according to the new userid param.

You can use parse_url on the url that return an associative array. Then use parse_str that parse the string into variables. Make any changes you need. Then reassemble the address with the changes using http_build_query.
$url = 'http://www.example.com/?userid=3255';
$parsed_url = parse_url($url);
parse_str($parsed_url['query'], $parsed_str);
if(isset($parsed_str['userid']))
{
// make any changes you want
$parsed_str['userid'] = "new id";
// reassemble the address with the changes
$link = "http://$_SERVER[HTTP_HOST]/?";
$http_builded = $link . http_build_query($parsed_str);
}
I hope this can help you.

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

How get data from string like query string with php?

I have url like this
index.php?name=aya&age=29
index.php?name:aya&age:29
I want get name and age from this url.
I think if i can get query string, perhaps i can render it.
How can do it?
first use $_SERVER['QUERY_STRING'] to get querystring from url
and then get data from string via parse_str function.
code:
$str = $_SERVER['QUERY_STRING'];
$str = str_replace(":","=",$str);
parse_str($str, $get);
echo $get['name'];
echo $get['age'];

$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];

Issue with URL in CodeIgniter

I am facing an issue with my site on cars - Now the problem i am facing with CodeIgniter is that on "new car" section if I select make, the URL is like below.
http://carandme.com/new-cars/ashok-leyland-cars-in-india?car_type=new&car_type_id=1&maker_id=106&maker_identifier=ashok_leyland&model_id=&model_identifier=
but I want a URL like this.
http://carandme.com/new-cars/ashok-leyland-cars-in-india
Similarly I am also facing issue in refine search on same page i.e if I select two car, the URL doesn't changes but the parameters do. Is there any way for this to be without parameter.
$input1 = $this->input->post('user_search1');
$input2 = $this->input->post('user_search2');
$user_input = array($input1, $input2); //making array of user search Inputs
$search_field_name = array("search_col_name1", "search_col_name2"); //original column name of table fro searching
//combining to send it as uri latter
$searchparams_uri = array_combine($search_field_name, $user_input);
//removing the keys and values from the array if Values is null or not set
$searchparams_uri = array_filter($searchparams_uri);
// Convert to associative array $this->terms_uri to segments to append to base_url
$keys = $this->uri->assoc_to_uri($this->terms);
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/controller/method_name/'.'/'.$keys.'/';
You will get URL like this:
index.php/controller/method_name/search_col_name1/input1/search_col_name1/input1
Break and Make according to your need. Question are welcome.

Remove query strings from url

How do I remove query strings from a url?
Example: I have a url such as the following:
http://example.com?q1=v1&q2=v2&q3=v3
I want to remove q1 and q3, leave q2, and add q4 with its value v4
How can I do this?
Kinda depends on if you know what the keys are or not. If you do, it'd be something like this:
$params = $_GET;
unset($params['q1']);
unset($params['q3']);
$params['q4'] = 'v4';
$my_new_param_string = http_build_query($params);
$new_url = 'http://example.com?' . $my_new_param_string;
You can manipulate $_GET directly, but I don't like to do that personally.
More info on http_build_query:
http://php.net/manual/en/function.http-build-query.php
If you just have that url as a string, you can do this instead:
$url_bits = parse_url('http://example.com/?q1=v1&q2=v2&q3=v3');
$params = parse_str($url_bits['query']);
And the rest works the same as the bit after $params = $_GET up above.
You can also use $url_bits to turn it back into a string later if you need to.

Categories