PHP URL Variable Appending - php

Hoping this is a simple and easy question. I've seen multiple examples of, and know how to append variables to the URL (i.e. mydomain.com/index.php?id=1&stat=0), but my question is this:
If I have a page on my site that already has variables in the URL (i.e. mydomain.com/tickets.php?stat=Open), how can I append a page number to the end of that URL (i.e. mydomain.com/tickets.php?stat=Open&page=2). This is for pagination purposes of a table with values from my database, that includes a search and select function (select open, closed, or all tickets, and search for a specific ticket number).
I've done several searches with google, and came up dry, as most topics regarding this have you hardcode the url with variables from the get go, and not append them. I may just be using the wrong search parameters as well, and am not sure what to search for exactly.
Any help or insight on this would be greatly appreciated, thank you.
Please note I wish to do this solely in PHP, HTML, and MySQLi. I want to refrain from using javascript or ajax if possible for my clients that may have those features disabled on their browsers.

Using this way:--
<?php
$domain = "mydomain.com";
$page = "tickets.php?";
$full_page_url = $domain.'/'.$page;
$arr = array('stat' => 'Open', 'page' =>2);
$add= http_build_query($arr);
$correct_url = $full_page_url. $add;
echo $correct_url;
?>
output:--mydomain.com/tickets.php?stat=Open&page=2

I would do it like this:
$page = 2;
$url = 'mydomain.com/tickets.php?stat=Open';
if( false !== strpos($url, '?')){
//if url has a ? split it.
$arr_url = explode('?', $url);
//convert query string to array, $array=['stat'=>'Open']
parse_str($arr_url[1], $array);
//add or replace page by array key
$array['page'] = $page;
//convert it back to a query string.
$query = http_build_query($array);
print_r($query);
}
Outputs
stat=Open&page=2
It's a simple matter of putting $query back with $arr_url[0] I'll leave this up to you. But I will give you a hint $arr_url[0].'?'.$query
The advantage here is that you don't have to worry about getting into a situation where you are adding page after page after page after...
Like this:
mydomain.com/tickets.php?stat=Open&page=1&page=2&page=3
You can't simply concatenate it onto the end of the url, and it's probably just as hard to remove it as it is to parse the query string.
As a side note, you could just use $_GET but where is the fun in that, as $_GET is the query string already parsed as an array ( so you could skip parse_str). But it may not be on a request, such as if you were just building the link from a string.
So I thought I would show it with parse_str to cover the "harder" case.
One last thing if you are just building a bunch of urls all the same except the page part. The obvious answer is to setup a base url and then just loop out the numbers.
$url = 'mydomain.com/tickets.php?stat=Open';
$pagedUrls = [];
$numberPages = 10;
for($i=1; $i<=$nubmerPages; $i++){
$pagedUrls[] = $url.='&page='.$i;
}
Or what have you for the number of pages.
It's really not that clear in your question exactly what you are trying to do..
Hope that helps.

Related

How would I generate query param key and value and append it to my URL completely dynamically?

anyone know a good way to dynamically append key value query params at the end of a url that would be generated from an incoming link?
for example: when navigating to mylocalhost.com/id123?firstParam=test1&secondParam=test2 - this should generate google.com/id123?firstParam=test1&secondParam=test2.
I then need to plug this into my anchor tag but that's the easy part.
I read about http_build_query(); but not sure how to use it in my case since I'm dealing with a string and not an array.
I've looked online but can't seem to find something related to this. I've also tried a lot of ways to rectify this and make it work but I've ran out of options. Any help would be greatly appreciated.
$queryString = "google.com/id123?";
$newUrl = "";
for($i = 0; $i < strlen($queryString); $i++) {
$newUrl = $queryString . "want=this" . "&" . "tobe=dynamicallyGenerated";
}
Based on your comments I think you want to use $_SERVER['REQUEST_URI'], so you get your path as well.
<?php
$newURL = 'https://google.com'.$_SERVER['REQUEST_URI'];
?>

save the last part of url in variable

I want to get the last part of an url that looks like this:
http://localhost:8888/blog/public/index.php/categories/Horror
I've tried it with
$endOfUrl = end(explode('/',$url));
but the thing is I get a notice that "Only variables should be passed by reference"
I need this "Horror" to get it's ID in my database and get all the posts with this id, since I'm trying to code a blog to get experience with php.
Another question linked to this: Is it possible to make it dynamic so it can be used for all the other categories as well? Or do I have to do this for every single category?
I'm new to the world of php so I would really appreciate it if someone could help me on this.
Try like this way for end() but If I were you I will try basename() to get my job done.
<?php
$url = 'http://localhost:8888/blog/public/index.php/categories/Horror';
$exploded = explode('/',$url);
$endOfUrl = end($exploded);
echo $endOfUrl;
?>
Reason why it is not working on single line:
end() requires a reference, because it modifies the internal
representation of the array (i.e. it makes the current element pointer
point to the last element).The result of explode('.', $url) cannot be
turned into a reference and this is a restriction in the PHP language itself.
DEMO: https://3v4l.org/ttKui
Using basename(),
$url = 'http://localhost:8888/blog/public/index.php/categories/Horror';
echo basename($url);
DEMO: https://3v4l.org/pt2cQ

Dealing with online newspaper headline link in PHP

I have seen on most online newspaper websites that when i click on a headline link, e.g. two thieves caught red handed, it normally opens a url like this: www.example.co.uk/news/two-thieves-caught-red-handed.
How do I deal with this url in php code, so that I can only pick the last part in the url. e.g. two-thieves-caught-red-handed. After that I want to work with this string.
I know how to deal with GET parameters like "www.example.co.uk/news/headline=two thieves caught red handed".
But I do not want to do it that way. Could you show me another way.
You can use the combination of explode and end functions for that
for example:
<?php
$url = "www.example.co.uk/news/two-thieves-caught-red-handed";
$url = explode('/', $url);
$end = end($url);
echo "$end";
?>
The code will result
two-thieves-caught-red-handed
You have several options in php to get the current url. For a detailed overview look here.
One would be to use $_SERVER[REQUEST_URI] and the use a string manipulation function for extraction of the parts you need.
Maybe this thread will help you too.

GET form in PHP. How to link to the 2nd+ page of results?

I have a POST form in PHP that I'm converting to GET.
The form works and gives me the first page of results without any problems.
But how do I link to the second page? I assume I have to replicate all the GET parameters into the "Next Page" link plus the page number (which the script already handles), but how would I do that?
CLARIFICATION: How do I get all the GET variables from a form onto a link in the page?
simpliest way is too to do something like:
$get = preg_replace("/page=\d+/i", "", $_SERVER['QUERY_STRING']);
$link = "somepage.php?".$get."&page=".($_GET['page']+1);
echo "<a href='".$link."'>Next Page</a>";
That will simply take the get string, remove the page then add the page back in as +1. Please note this would be insecure as people could pass anything in the query string. A better option would be to build the the URL explicitly by checking for each expected $_GET key=>value pair, validating it, then adding it to a link variable. That way any additional bits in the query string wont be echo'd to the page.
EDIT:
Ok so heres a very quick example.
$category = (int)$_GET['cat'];
$keyword = trim($_GET['keyword']);
$keyword = filter_var($keyword, FILTER_SANITIZE_STRING);
$nextlink = "somepage.php?";
$nextlink .= http_build_query(array(
"cat" => $category,
"keyword" => $keyword,
"page" => $page+1
));
So basically you get the GET var's you want, validate them, then just use http_build_query and an associative array to build your query string for the link. The security i put in their is very basic, but typecasting numbers and limiting the amount of crud you can stick into a string is a place to start
Simplistically speaking, you would read them out of the request like this:
$link = 'mypage/?someitem=' . $_GET['someitem'] . '&page=' . ($page + 1);
Although you may not wish to trust the parameters as they may contain an HTML injection or other nasty tricks designed to attack your website.
Isn't it just URL?param1=val1&param2=val2&...?

Simpledom Interurl variables

Out company operate numerous servers
We are using a internal script to filter out various pieces of information , The frontend users will be say entering a postcode , it will then process a list of 5 pages in 1 page of information .
Note: we know our code may not be the most simplistic but it seems to work at the moment , any 1 that knows a easier way to do it then please do say
<?php $postcode = $_POST['postcode']; ?>
// get DOM from URL or file
$html = file_get_html('http://xx.com/en/search/records/search.pub?Surname=willi*&Location=$postcode&x=39&y=9&Page=1');
$html1 = file_get_html('http://xx.com/en/search/records/search.pub?Surname=willi*&Location=$postcode&x=39&y=9&Page=2');
$html2 = file_get_html('http://xx.com/en/search/records/search.pub?Surname=willi*&Location=sa11&x=39&y=9&Page=3');
we have further code which filters out our servers results , but for some reason when one of our users posts the form to this script , only the bottom result is coming back purely because the location is hardcoded - the others are coming back unknown and we think that it isnt passing the postcode variable to the urls correctly
could it be because the $postcode variable is already inside the $html variable , is there a way to get around this issue ?
Thanks
the string beetwen single quotes (') don't get evaluated. try something like
$html = file_get_html('http://xx.com/en/search/records/search.pub?Surname=willi*&Location='.$postcode.'&x=39&y=9&Page=1');
$html1 = file_get_html('http://xx.com/en/search/records/search.pub?Surname=willi*&Location='.$postcode.'&x=39&y=9&Page=2');
and check the $postcode value for valid values before using it in your programs, please

Categories