This question already has answers here:
How to add url parameter to the current url?
(7 answers)
Closed 7 years ago.
I have an search method who redirect to something like this:
mysite.ro/search.php?a=1&b=2&c=3
When i have pagination i need to redirect me to something like this:
mysite.ro/search.php?a=1&b=2&c=3&page=2
I dont know the solution to have an href who redirect to page=2 and keep the same search criteria.
I tried <a href="&page=2"> and href="<?php echo $_SERVER[REQUEST_URI];?>&page=1" not usefull both.
Try this, Its worked for me.
<?php
$actual_link = '';
$delimiter = '?&page=';
$link = array();
$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$links = explode($delimiter, $actual_link);
?>
<a href="<?php echo $links[0];?>?&page=1" >Link </a>
Related
This question already has answers here:
How to remove the querystring and get only the URL?
(16 answers)
Closed 2 months ago.
My goal: compare the actual url to a stored url in MySQL DB. If there's a match the page title and meta description info is loaded from the DB into the fields on the page.
Problem: I need to remove the query string so the url is clean for the comparison.
The existing code works for the main pages that list the blogs/lessons/etc, but when I go to page 2, 3, 4, etc the query string breaks the comparison.
In the code you will see $actual_link - that needs to have the query string removed and that is what I have yet to find a solution that actually works, as for what I've tried, there have been too many iterations to remember them.
Server Info: PHP 7.3.17; MySQL MariaDB-10.2.32; Apache-2.4.43/
$actual_link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$sql = "SELECT * FROM meta WHERE siteLink = '$actual_link'";
$result = mysqli_query($link, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<?php include_once "myHead.php"; ?>
<title><?php echo $row["title"]; ?></title>
<meta name="description" content="<?php echo $row["meta_desc_content"]; ?>">
<?php
}
}
else
{
echo "Shit, it ain't working.";
}
?>
PHP provides a nice helper method called parse_url to get certain parts of an url:
// $actual_link = 'www.stackoverflow.com/test';
$urlParts = parse_url($actual_link);
$formattedUrl = $urlParts['host']; // www.stackoverflow.com
Docs: https://www.php.net/manual/en/function.parse-url.php
Use strtok()
like this: $baseUrl = strtok($actual_link, '?');
This question already has answers here:
get file name for url php
(2 answers)
Closed 3 years ago.
I want to separate IMDb id from the link
my php code is this
<?php print $movie_info[0]->imdb_link; ?>
the URL seems like this on every page .
https://imdb.com/title/tt1206885
i want to show this id separate from the url on my page => tt1206885
please help me
i try but fail to extract the id
<?php if($movie_info[0]->imdb_link){?>
ID <?php $str = explode("/",$movie_info[0]->imdb_link);
/* if url in php variable then $str = explode("/",$imdb_id); */
print $str[count($str)-2];
?>">
<?php }?>
$link_array = explode('/', $movie_info[0]->imdb_link);
$id = end($link_array);
This question already has answers here:
Question mark in the middle of a URL variable
(3 answers)
Closed 8 years ago.
i want to pass value that contain question mark
i have 2 php example example1.php and example2.php
in my example1.php the code like this
<a href="example2.php?title=what is this?" />
in my example2.php the code like this
<php
if(isset($_GET['title']))
{
$title = $_GET['title'];
echo $title;
}
?>
but in example2.php the title become like this what is this the question mark disappear
i already try to use str_replace my question mark become like this what is this"?" but still same at example2.php the question mark disappear
please help me how to pass value that contain question mark
thanks
change <a href="example2.php?title=what is this?" /> into
<a href="example2.php?title=what is this%3F" />
then add
<?php
if(isset($_GET['title']))
{
$title = $_GET['title'];
echo $title;
}
?>
this will work for you.
This question already has answers here:
What is the "?" symbol in URL used for in php?
(7 answers)
Closed 9 years ago.
Sorry if this is a stupid question but I want to include two variables in an href
the href is not in a form
<div class="button">
View
</div>
int he page I am sending it to i have a Get method
if (isset($_GET['Name']) && isset($_GET['pid'])) {
$id2 = $_GET['Name'];
$id = $_GET['pid'];
Summary
I want to include $id in the a href so i will be able to get both name and id in the page product.php
You can append as many URL parameters as you like using &:
<a href="<?php fetchdir($ipages); ?>product.php?Name=<?php echo $name; ?>&id=<?php echo $id; ?>" ...
product.php?Name=$name&pid=$pid
You can use & to add two or more GET parameters to a URL.
One good way to build a property encoded is to use the http_build_query function, this will ensure that the data sent in the request is properly encoded.
<?php
$qstr = http_build_query(array("Name"=>$row["Name"], "id"=>$row["pid"]));
?>
<a href="<?php fetchdir($ipages) ?>product.php?<?= $qstr ?>">
This of course can also be done inline
<a href="<?php fetchdir($ipages) ?>product.php?<?= http_build_query(array("Name"=>$row["Namw"], "id"=>$row["pid"])) ?>">
This question already has answers here:
Get the full URL in PHP
(27 answers)
Closed 3 years ago.
I need to get the path with query string from the URL of the current request. For example, if the current URL is:
"http://www.example.com/example/test/hi.php?randomvariable=1"
I would want this:
"/example/test/hi.php?randomvariable=1"
You want $_SERVER['REQUEST_URI']. From the docs:
'REQUEST_URI'
The URI which was given in order to access this page; for instance, '/index.html'.
it should be :
$_SERVER['REQUEST_URI'];
Take a look at : Get the full URL in PHP
<?php
function current_url()
{
$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$validURL = str_replace("&", "&", $url);
return $validURL;
}
//echo "page URL is : ".current_url();
$offer_url = current_url();
?>
<?php
if ($offer_url == "checking url name") {
?> <p> hi this is manip5595 </p>
<?php
}
?>