John Davidson

php - How do I tidy up my url after sending lots of information?

0 comments
Message:


I am a beginner programmer/coder who is currently trying to get to grips with HTML and PHP. I currently have a locally hosted searchable database that (when used) brings up a list of the first twenty entries that correspond to your search terms, with buttons to send you to the next page, last page, etc. (you know... pages...) The search outputs information into the URL ([url]?q=Alphonse&f=Elric etc). I have two problems with this at the moment.


Problem A:
My URL contains information that is unused. If I don't put anything into the search term it simply comes out with "q=&x=&f=..." etc. This makes the URL absurdly long even on the most simple searches.



  1. Can this be cleaned up through just php?

  2. Would this method be different if I end up hosting this online?


Problem B:
The way my paging functions is to send the user to the following link '.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'&pn='.$nextPage.'. This outputs the current link but with "pn=1" at the end (Or whatever relevant page they click).


This method itself makes the URL quite messy. If they click through multiple pages, and perhaps go back and forth, the link ends up having "pn=1&pn=2&pn=3&pn=1...." etc at the end. I assume that this will be answered by the first query, but it is slightly different in that this is information that is actually present.


How do I remove this superfluous information, and just keep the (final) relevant one?


I am thinking that I can use parse_str to turn the URL into an array, then delete each entry of the array that are empty, then create a new string out of that array and make that the link the search/next page button goes to.


Does that sound like it would work? If so, how do I delete those specific array entries, and how would that array then be stored? Would the array lose those entries and calling a deleted entry "$array['1']" for example result in an error, or does deleting entries in an array move everything up one to fill the gap?


Apologies if I'm asking too many different things in one post here!



My Answer:
To clean up the URL in PHP, you can use the `http_build_query` function to remove any empty parameters from the query string. Here's an example of how you can clean up the URL:

php
$queryString = $_SERVER['QUERY_STRING'];
parse_str($queryString, $params);

// Remove empty parameters
$params = array_filter($params);

// Build the new query string
$newQueryString = http_build_query($params);

// Create the new URL
$newUrl = $_SERVER['PHP_SELF'] . '?' . $newQueryString;


This code will remove any empty parameters from the query string and create a new URL without those parameters.

As for removing the superfluous "pn" parameters in the paging function, you can simply update the "pn" parameter in the query string instead of appending a new one each time. Here's an example:

php
$nextPage = 2; // Example next page number
$params['pn'] = $nextPage;

// Build the new query string
$newQueryString = http_build_query($params);

// Create the new URL
$newUrl = $_SERVER['PHP_SELF'] . '?' . $newQueryString;


This will update the "pn" parameter in the query string instead of appending a new one each time the user clicks on a page. This way, you will only have the relevant "pn" parameter in the URL.

I hope this helps! Let me know if you have any further questions.

Rate this post

4 of 5 based on 2313 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us