Setting the URL for the pagination numbers - php

I have some problems generating the url i want to put in my pagination numbers. I'm setting the url inside a class with
$this->url = rtrim($_SERVER['REQUEST_URI'], " /");
then inside another class I'm setting the href value on the pagination number with
echo "<a class='active' href='".$this->page->url."/".$i."/'>".$i."</a>";
So when i now navigate to my page the url is like this
localhost/designv2/blog/
Then when i click on number 1 in the pagination i get
localhost/designv2/blog/1/
But, then when i click on number 2 in the pagination i get
localhost/designv2/blog/1/2
And if i click on number 3 i get
localhost/designv2/blog/1/2/3
Why, does it keep on adding numbers to the url instead of replacing the old number?
I could split up the url, run it through a for loop and remove the last parameter but I'm using this url for other things on my pages aswell so i cant just remove the last parameter.
Any suggestions?

Before appending ID at the last in url, check and remove ID if exist.
Instead
$this->url = rtrim($_SERVER['REQUEST_URI'], " /");
Replace last occurence of /\/[0-9]\/$/ here (/1/ or /2/) to /.Try something like this
$url = $_SERVER['REQUEST_URI'];
$regex = '/\/[0-9]\/$/';
$this->url = preg_replace($regex, '/', $url);
Live demo

Try this code
echo "<a class='active' href='/designv2/blog/".$i."/'>".$i."</a>";

Related

remove part of the new url php from link post

I would like to be able to click on a link that has specific information within it -
<a href="index.php?content=quiz-demo-1id=series-99">
id=series-99 is the information that I would like to be able to use as a variable on page - index.php?content=quiz-demo-1
1-page#1 - mysite.com/Page_1
2- Link on mysite.com/Page_1: click here - <a href="index.php?content=quiz-demo-1id=series-99">
3-new page url - mysite.com/index.php?content=quiz-demo-1
4 - new page code:
<?php
$quizname = $_GET['id'];
?>
<h2><?php echo $quizname?></h2>
I want the: id=series-99 to drop off of the new url.
Thanks!
You could use a regular expression on this (I have corrected your url and added an ampersand):
$link = "<a href='index.php?content=quiz-demo-1&id=series-99'>";
$regex = '/(id=[^&"\']+)/i'; // capture everything except the characters in the brackets
$link = preg_replace($regex, "", $link);
echo $link;
However, it would be probably safer to use the mentionned parse_url() if you are not familiar with regular expressions.

php - regex query string in a tag href attribute

I would like to regex a query string which is contained in a href attribute of a tag.
My aim is to get, for example, the page number of this a tag:
<a class="page-numbers" href="/website/admin/back.php?page=my_page&pagenum=2&id=12">next</a>
I would like to get the query pagenum. Is there a way to directly get it with a regex or someting like this?
Here my code:
$page_link = '<a class="page-numbers" href="/website/admin/back.php?page=my_page&pagenum=3&id=12">next</a>';
$page_nb = preg_match('/<a href="(.+)">/', $page_link, $matches);
$page_nb = (isset($matches[1])) ? $matches[1] : $page_nb;
$page_nb = parse_str(parse_url($page_nb, PHP_URL_QUERY), $params);
$page_nb = $params['pagenum'];
EDIT: I need this number because I will use it for a query. The page number text not necessarily corresponds to the query page number. And the pagination can be a little bit complex. I just need to retrieve this information for some stuff not relating on clicking on the button.
Where do you get that string from? do you get it from a db? api? is it hardcoded?
In your current code it looks like it's hardcoded, so why don't just do:
$pagenum = 3;
$page_link = '<a class="page-numbers" href="/website/admin/back.php?page=my_page&pagenum=". $pagenum . "&id=12">next</a>';

Extract links from mysql and make it clickable?

I have a database table that stores URL.What I need is grab those URL's from table and make it click-able with the URL's title as anchor.
This is what I have tried:
while($row4 = mysql_fetch_assoc($result4))
{
echo "".$row4['Title1']. "";
}
It displays for example my tilte1 that is youtube and Url1 is www.youtube.com.
But when I click on it it is going to localhost/mysite/www.youtube.com
How can I fix this?
try:
echo "".$row4['Title1']. "";
Add http:// in front of the link. Then it will go to where you wanted.
you need http:// in front.
echo ''.$row4['Title1']. '';
Can you check if your Url1 field is a proper url? see if it has http:// protocol in the url. if not you will need to add it to prepend it to your table or programmatically prepend http:// protocol to your link.
Additionally you can use below function taken form codeigniter framework. It prepares your link for url. do prep_url($row4[Url1]) instead of just $row4[Url1];
function prep_url($str = '')
{
if ($str == 'http://' OR $str == '')
{
return '';
}
$url = parse_url($str);
if ( ! $url OR ! isset($url['scheme']))
{
$str = 'http://'.$str;
}
return $str;
}
Try with this
while($row4 = mysql_fetch_assoc($result4))
{
echo "<a href ='http://".$row4['Url1']."'>".$row4['Title1']. "</a>";
}
You should make an absolute link from that, and don't forget to put attributes' values in quotes.
I suggest this:
echo ''.$row4['Title1']. '';
//by doing this you also won't need any of \ slashes
I enter urls enclosed in quotes, example:
"http://google.com"
Then I use:
.$row['date']."< a href=".$row['title'].">".$row['title']."< /a>".
the result is a clickable link in the form of:
http://google.com
remove the space between < and a, ( i had to add a space for the code to post.

PHP: How to get only the current folder of a website

For example I have a website that points to a page like this:
http://www.mysite.com/folder/file
How can I get determine /folder so that I can go further an quote an if statement like
if /folder then echo something
Why do I need this?
I am trying to tell facebook which image to pick from a page. Actually I have a pretty simple page structure and the image that facebook should take is always at first but somehow it does choose another one from time to time. I guess because the other images are loaded faster. And the old way to rel="img_src" doesn't seem to work anymore as that I could just add it to the wanted image.
So well of course I use the open graph protocol to tell facebook which Image it should use.
I am working with a cms were I can output the path of the image depending on the id the image has. I have two different id's for the different kind of pages living in two different folders.
This leads to:
if index --> echo meta og for index img
else if /folderone (with id1) --> echo meta og for id1
else if /foldertwo (with id2) --> echo meta og for id2
This is why I need to know the foldername.
Now with the answer I have following setup, just that you know:
<?php $folder = dirname($_SERVER['SCRIPT_NAME']); ?>
<?php if (dirname($_SERVER['SCRIPT_NAME']) == "/") echo "<meta property='og:image' content='http://www.mysite.com/img/img.jpg'/>" ;?>
<?php if (dirname($_SERVER['SCRIPT_NAME']) == "/folderOne") echo "<meta property='og:image' content='http://www.mysite.com/img/{$img_id1}'/> " ;?>
<?php if (dirname($_SERVER['SCRIPT_NAME']) == "/folderTwo") echo "<meta property='og:image' content='http://www.mysite.com/img/{$img_id2}'/> " ;?>
parse_url &
explode
$path = parse_url($url, PHP_URL_PATH);
gives you
/folder/file
then you can explode() to separate the path values and check the first one to see if it is 'folder'
Example here: http://tehplayground.com/#7TIKAwp6J
Example code:
$url = "http://www.mysite.com/folder/file";
$path = parse_url($url, PHP_URL_PATH);
$arr = explode("/",$path);
echo $arr[1]; // leading slash makes [0] ""
outputs
folder
$script = $_SERVER['SCRIPT_NAME'];
echo dirname($script);
Possibly use "get current working directory" function getcwd()?
Explode it by directory separator.
Then grab the last element like this:
$var = getcwd();
$var = explode('\\', $var); // your OS might use '/' instead
$var = end($var);
I suppose this assumes you're not using some kind of MVC framework that uses routing.
I hope that helps!
I think this is nicer than exploding the string:
function getCurrentDirectory(){
$curDirPath = getcwd();
return substr($curDirPath, strrpos($curDirPath, '/') + 1);
}
getcwd() gives you the current directory's path, and then you can truncate it starting right after the last occurrence of the / in its file path.
$dir_list = explode('/', dirname($_SERVER['SCRIPT_FILENAME']));
$this_folder = $dir_list[count($dir_list)-1];
...
if ($this_folder) == "folderOne") echo "...."
...
if(dirname('yoursite/folder')){

$_SERVER['REQUEST_URI'] generating extra & in URL

Having an issue here where the
$_SERVER['REQUEST_URI']
is spitting out:
/dev/nava2/store/index.php?route=product/product&product_id=48
the actual URL is /dev/nava2/store/index.php?route=product/product&product_id=48
obviously, the difference being the & on the top vs. the & on the bottom
full code looks like this:
$currentpage = $_SERVER['REQUEST_URI'];
$classic = "/dev/nava2/store/index.php?route=product/product&product_id=48";
if ($currentpage == $classic)
{ $classicclass = "current";
}
else { echo "";}
Any suggestions?
& is the html entity corresponding to &. You can obtain to original string back with html_entity_decode :
$original = html_entity_decode($_SERVER['REQUEST_URI']);
You can use html_entity_decode() to get the actual url but the top one should work. I dont think you need to change anything. You could also use str_replace or preg_replace if you really need to change some parts of your uri.
echo html_entity_decode($_SERVER['REQUEST_URI']);

Categories