Need help conditionally including php in page - php

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

Related

Replace Content on Page Based on URL Parameter with PHP

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

Last 5 Page Names User Viewed

I'm trying to code the last 5 page names the user viewed on my site and produce it into a list. I'm currently able to get the current page name, but I don't know how to get the previous pages. This is the code I'm using to get the current page name:
<?php
$pageName = basename($_SERVER['PHP_SELF']);
echo $pageName;
?>
PHP Sessions should get you going in the right direction. For example:
session_start();
if(!isset($_SESSION['pages'])) {
$_SESSION['pages'] = array();
}
if(count($_SESSION['pages']) < 5) {
$_SESSION['pages'] [] = $_SERVER['PHP_SELF'];
} else {
echo "Limit reached";
}
print_r($_SESSION['pages']);
I recommend you use PHP Sessions to accomplish this.
So, save the current page name that you want to the sessions variable like so:
<?php
$pageName = basename($_SERVER['PHP_SELF']);
$_SESSION['pageName'] = $pageName;
?>
And then continue to save these names. #Len_D just beat me to the punch with an answer that uses arrays and is likely what you need.

How to hide pagination from url using htaccess?

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

Using PHP Get function to show content on webpage, yet still using same PHP file?

I'm new to PHP, and just creating a simple website.
At the moment, I have a header with some links (i.e. blog, faq, home, gaming etc). I'm trying to use GET functions to show new content in a container on the webpage. I've tried having them link to the index and to a specific page, like
Home
and then having some PHP in the html body...
<?php
if ($_GET[page] == "faq") {
$result === 'FAQ';
} else {
$result === 'Non-FAQ';
}
echo $result;
?>
just to see if it would work, and lo and behold, it doesn't.
So, that's basically the gist of what's happening. It's baffled me for the past few hours, and would really appreciate some help
Thanks
You aren't using the assignment operator to assign a value to $result. Use a single equals sign, ie
<?php
if ($_GET['page'] == "faq") {
$result = 'FAQ';
} else {
$result = 'Non-FAQ';
}
echo $result;
?>
<?php
if ($_GET[page] === "faq") {
$result = 'FAQ';
} else {
$result = 'Non-FAQ';
}
echo $result;
?>
If you want to DEFINE a string you use one "=", if you want to compare it you use "===" (or ==) :)

php pagination using class

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>&nbsp ";
echo "<a href=$self?page=$prev>Prev</a>&nbsp ";
}
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/

Categories