I have an original link:
http://example.com/catalog/catagoryname/
When I click on 2-th page, url like this:
http://example.com/catalog/catagoryname/1
Ok, how to hide number of page in the end url ?
Best regards.
As far as I understand you don't want to have anywhere:
http://example.com/catalog/catagoryname/1 but http://example.com/catalog/catagoryname/ but you want to use urls with other numbers http://example.com/catalog/catagoryname/2, http://example.com/catalog/catagoryname/3 and so on. You don't need to do it in .htaccess. You should do it in your PHP code
Simple in place where you use pagination (you probably have loop for this), you should use condition:
for ($pageNr = 1; $pageNr < $totalPages; ++$pageNr) {
if ($pageNr == 1) {
echo 'http://example.com/catalog/catagoryname/';
}
else {
echo 'http://example.com/catalog/catagoryname/'.$pageNr;
}
}
Of course it's only sample, because you haven't put full code in your question
Related
I'd like to replace content within my page based on the URL parameter.
Ideally I'd like to use PHP to get:
if {{parameter is X}} display {{content X}}
if {{parameter is Y}} display {{content Y}}
..for a few pages.
Current set up:
<?php if ($CURRENT_PAGE == "Index") { ?>
<div id="firstDiv">this is the standard page</div>
<?php } ?>
<?php if ($CURRENT_PAGE == "p1") { ?>
<div id-"secondDiv">this is a variation of the page</div>
<?php } ?>
And using include("includes/content.php"); to call the html blocks to the page
The firstDiv displays in index.php as expected, but adding the URL parameter changes nothing - the same div still shows (I'd like it to be replaced with the secondDiv)
It seems $CURRENT_PAGE doesn't like URL parameters - what is the alternative?
Hopefully this makes sense, I'm pretty new to PHP. Happy to provide more details if required.
Thanks in advance for any help.
-- UPDATE --
Thank you for the answers so far!
It seems I missed part of my own code (Thanks to vivek_23 for making me realise this - I'm using a template, excuse me!!)
I have a config file that defines which page is which, as so:
<?php
switch ($_SERVER["SCRIPT_NAME"]) {
case "index.php/?p=1":
$CURRENT_PAGE = "p1";
break;
default:
$CURRENT_PAGE = "Index";
}
?>
Before I learn $_GET, is there a way I can use my current set up?
Thanks again.
-- UPDATE 2 --
I have switched to using the $_GET method, which seems to be working well so far. My issue now is when the parameter is not set it is giving an undefined error. I'll try to remember to update with the fix.
$p = ($_GET['i']);
if($p == "1"){
echo '<div id="firstDiv"><p>this is the first div</p></div>';
}
Thanks to the two answerers below who suggested using $_GET
You can used $_GET like
if($_GET['p']==1){
echo '<div id="firstDiv">this is the standard page</div>';
}else if($_GET['p']==2){
echo '<div id="secondDiv">this is a variation of the page</div>';
}
The other way! you can used basename() with $_SERVER['PHP_SELF']
//echo basename($_SERVER['PHP_SELF']); first execute this and check the result
if(basename($_SERVER['PHP_SELF']) == 'index'){
echo '<div id="firstDiv">this is the standard page</div>';
}else{
echo '<div id="secondDiv">this is a variation of the page</div>';
}
You need to send the parameters on the URL query string, like:
yourdomain.com?p=1
So, with this URL, the query string is "?p=1", where you have a GET parameter named 'p' with a value of '1'.
In PHP to read a GET parameter you can use the associative array $_GET, like this:
$current_page = $_GET['p'];
echo $current_page; // returns '1'
The rest of your logic is OK, you can display one div or the other based on the value of the p parameter.
You can read more about how to read query string parameters here: http://php.net/manual/en/reserved.variables.get.php
my code is related to breadcrumbs.. that is it should display previous page or from where it is navigated and i achieved it partially , while im refreshing 2-3 times im getting the current page not the previous page.. so pl help me on this
my code lies in session.php as
$add = $_SERVER['PHP_SELF'];
if($_SESSION['pageadd'][1]!= $_SESSION['pageadd'][2])
{ $_SESSION['pageadd'][2]= $_SESSION['pageadd'][1];
}
echo $_SESSION['pageadd'][2];
if(($_SESSION['pageadd'][1]!= $add) )
{ $_SESSION['pageadd'][1]= $_SESSION['pageadd'][0];
$_SESSION['pageadd'][0]=$add;
}
What you want isn't a breadcrumb - it's a history for visited pages! This could be achieved with something like this:
if (!isset($_SESSION['pageadd'])) {
$_SESSION['pageadd'] = array();
}
// add page
$_SESSION['pageadd'][] = $_SERVER['PHP_SELF']
// only save last 5 pages
if (count($_SESSION['pageadd'])) > 5) {
array_shift($_SESSION['pageadd']);
}
Try to use $_SERVER['HTTP_REFERER'] it will return you previous url. However you need to store it in some hidden field or session like you are doing now.
Hope this help :)
I am working on PHP code for automatically redirecting a URL to an affiliate URL based on words in the URL.
This is the code I am using:
if (stripos($mylink, "amazon.in") > 0) {
if (strpos($mylink, "?") > 0) {
$tmp = $mylink."&affiliate_id";
} else {
$tmp = $mylink."?affiliate_id";
}
$loc = "Location:".$tmp;
header($loc);
exit();
}
But the problem is that when the Amazon URL is like: http://www.amazon.in/English-Movies-TV-Shows/b/ref=sa_menu_movies_all_hindi?ie=UTF8&node=4068584031 it adds the affiliate id like this: ie=UTF8&affiliate_id (it automatically removes the node and its value) but I want it to be like: ie=UTF8&node=4068584031&affiliate_id.
This means that I need to add &affiliate_id at the end of the URL every time if it is an Amazon link.
Actually I just want to achieve the following simple thing: if the URL doesn't have any ?s, I need to place ?affiliateid and if the URL has a ?, I just need to append &affiliateid at the end.
Can you suggest the very simple method for implementing what I require?
Let PHP do the string parsing for you. Let's say your affiliate id is XYWZ.
if (strpos($mylink, "amazon.in") !== 0) {
$urlarray=parse_url($mylink);
$urlarray['query']='affiliate_id=XYWZ&'.$urlarray['query'];
$newurl= $urlarray['scheme'].'://'.$urlarray['host'].$urlarray['path'].'?'.$urlarray['query'];
echo $newurl;
}
will output
http://www.amazon.in/English-Movies-TV-Shows/b/ref=sa_menu_movies_all_hindi?affiliate_id=XYWZ&ie=UTF8&node=4068584031
see? I didn't need to worry if there's a question mark. It will be stripped in the output of parse_url;
Edit: using $mylink instead of $theurl
I've been trying to conditionally include a banner which incorporates a form into a php site. Previously the banner was part of the header but I need to exclude it from certain pages in order for other elements to be included.
I'm using $_SERVER['SCRIPT_NAME'] to define a variable on the page and then check for this. I'm a little confused though as to where the variable should be placed. Will it work if placed in the header or does it need to be included in every separate page?
This is what I've got:
<?php $currentPage = $_SERVER['SCRIPT_NAME']; ?>
<?php if ($currentPage != "/ecoProcess.php") {
include ("includes/banner.php");
} else {
echo "ECO";
}
?>
For further info, I've updated the code because of my schoolboy error with -= instead of != but neither of the conditions works now (no banner, no "ECO"). The pages are being called using this method:
<?php if($_GET['action']=='boiler_installation_replacement') {?> class="active"<? php } ?>>Boiler Installation and Replacement
I've been advised to try var_dump which reveals this:
' string(14) "/ths/index.php" ' everytime. I guess this is due to the way the pages are called but I don't know how to get around this.
-= means subtract right value from left side. Eg.
$a = 5;
$a -= 3;
echo $a; // will print 2 because 5 - 3 = 2 :)
Change it to comparison == or better ===. You can also use !=/ !== for opposite cases.
Change your condition like this.
<?php
$currentPage = $_SERVER['SCRIPT_NAME'];
if ($currentPage == "/ecoProcess.php") {
include ("includes/banner.php");
} else {
echo "ECO";
}
?>
Try this:
just change your if statement because it is not logically correct.
Use this
if ($currentPage == "/ecoProcess.php")
instead of
if ($currentPage -= "/ecoProcess.php")
Thanks
hello everyone plz help me out with this script.
I am calling my pages in this way in main page
if(isset($_GET['page'])=='view_user' && $_GET['page']=='view_user')include("user/crud_user.php");
i have user class in which i am doing pagination
$self = $_SERVER['PHP_SELF'];
if($this->openPage<=0) {
$next = 2;
}
else {
$next = $this->openPage+1;
}
$prev = $this->openPage-1;
$last = $this->pages;
if($this->openPage > 1) {
echo "<a href=$self?page=1>First</a>  ";
echo "<a href=$self?page=$prev>Prev</a>  ";
}
The url of the page which is using this class is lie this
admin.php?page=view_user
Now i can get pagination no according to the records but when i click on no it rather take me to admin.php?page=3 because of self . I want to stick to that page for showing the result . I tried in this way also $self="admin.php?page=view_user";but no luck . please help me out
To use one variable for serving two different purposes is quite unusual idea.
Let me suggest you to use 2 different variables, one to designate included page and another to point to the actual page in pagination.
Hope this helps.
do
if(isset($_GET['page']) && $_GET['page'] == 'view_user')
{
include("user/crud_user.php");
exit;
}
because of many reasons.
for pagination try the following: http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/