add live ID to BODY with PHP - php

I'm using the php code:
<body id="<?php echo str_replace("index.php?","",(basename($_SERVER['REQUEST_URI'],".php"))); ?>">
and
<?php
if(isset($_GET['start'])){
include('includes/start.php');
}else if(isset($_GET['help'])){
include('includes/help.php');
}else{
include('includes/start.php');
}
?>
It works great - cutting index.php?help to "help" and index.php?start to "start" in body ID. But when I enter index.php in body ID is "index" not "start". Is there any way to tell index.php with included start.php to display "start" id body ID?
UPDATE
It should work dynamically - body ID is name of included .php file, something like this code:
<?php
$page = str_replace(array( 'server_name', 'index', '?', '/', '.php'), '', $_SERVER['REQUEST_URI']);
$page = $page ? $page : 'start';
?>
<body id="<?php echo $page ?>">

Am not sure i get you 100% but you can try
$id = $_SERVER['QUERY_STRING'];
$bodyID = $id;
switch ($id) {
case "help" :
include ('includes/help.php');
break;
default :
$bodyID = "start";
include ('includes/start.php');
break;
}
printf("<body id=\"%s\" >", $bodyID);
If you run index.php?help it would include include ('includes/help.php'); and also output
<body id="help" >

It would be best to use this instead:
<?php
$ids = 'start,help,something';
$ids = explode(',',$ids);
$included = 0;
foreach ($ids as $id) {
if (isset($_GET[$id])) {
include('includes/'.$id.'.php');
$included = 1;
break;
}
}
if (!$included) include('includes/'.$ids[0].'.php');
?>
Then:
<body id="<?php echo $id; ?>">

Related

Best way for passing string while including file

I'm having a trouble with passing string while using include() function. I have index.php which serves as template file and is including any other pages.
For example, if I want to go to the home page, the address is http://somethingsomething.com/index.php?page=home and included file is pages/home.php. Bellow you can find how the template system works. I want to change of the page you're currently are but I don't know what is the best way to pass that string from the title of the page. So if I'm on the Catalogue page - http://somethingsomething.com/index.php?page=catalogue, it will include file pages/catalogue.php and I've tried to pass string as SESSION but it didn't worked. Could you please help me?
Source URL: http://somethingsomething.com/index.php?page=catalogue
index.php file
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1>Welcome, world!</h1>
<?php
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 'home';
}
if (preg_match('/^[a-z0-9]+$/', $page)) {
$insert = include('pages/' . $page . '.php');
if (!$insert) {
$insert = include('pages/error.php');
}
} else if (empty($page)) {
$insert = include('pages/error.php');
} else {
$insert = include('pages/error.php');
}
?>
</body>
</html>
pages/catalogue.php file
<?php
$title = "Catalogue";
?>
Something like this?
<?php
ob_start();
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 'home';
}
if (preg_match('/^[a-z0-9]+$/', $page)) {
$insert = include('pages/' . $page . '.php');
if (!$insert) {
$insert = include('pages/error.php');
}
} else if (empty($page)) {
$insert = include('pages/error.php');
} else {
$insert = include('pages/error.php');
}
$output = ob_get_clean();
?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1>Welcome, world!</h1>
<?php
echo $output;
?>
</body>
</html>

Add the page name to the body tag

I have the following PHP script that will add a class of .Active to the current open page - this bit works but I am also trying to also add the page name to the body tag as an ID "#", but it does not seem to be working how I do it. Can anyone please advice me?
<!--add class .active to current page-->
<?php
$directoryURL = $_SERVER['REQUEST_URI'];
$path = parse_url($directoryURL, PHP_URL_PATH);
$components = explode('/', $path);
$currentPage = preg_replace("/\\.[^.\\s]{3,4}$/", "", end($components));
if ($currentPage == "") {
$currentPage = "index";
}
function href($url) {
global $currentPage;
$path = explode('/', $url);
$page = preg_replace("/\\.[^.\\s]{3,4}$/", "", end($path));
echo 'href="' . $url . '" ';
if ($page == $currentPage) {
echo 'class="active"';
}
}
?>
Here is the menu:
<li><a <?php href('index.php'); ?>>Home</a></li>
<li><a <?php href('about.php'); ?>>About</a></li>
<li><a <?php href('treatments.php'); ?>>Treatments</a></li>
And the HTML code:
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
It is very simple add that variable $page in echo
echo "class='active' id='$page'";
Basically, if you don't want to modify the template, you can't.
The solution would be to do this in your template, but it must be obvious to you:
<body <?php echo $id; ?>>
What seems less obvious to you, is just that you can't do this without touching the template.
Also, you should avoid global variables.

setting a value for index.php page

is there a way set a value for the index.php page on its first load?
i would like index.php it to load like so "index.php?subject=1" when it loads for the first time.
as this value will change as they move around in the site i don't want it to be a fixed value.
some one sugested
if(empty($_SESSION['visited'])) {
//DO STUFF
$_SESSION['visited'] = true; }
i cant seem to get that to work with my function.
find_selected_page()
function find_selected_page () {
global $current_subject;
global $current_page;
if (isset($_GET["subject"])) {
$current_subject = find_subject_by_id ($_GET["subject"]);
$current_page = find_default_post($current_subject ["id"]);
} elseif (isset($_GET["page"])) {
$current_subject = null;
$current_page = find_page_by_id ($_GET["page"]);
} else {
$current_page = null;
$current_subject = null;
}
}
index.php
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/session/session.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/db/dbcon.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/inc/functions.php');
$context = "public";
find_selected_page ();
?>
<html>
<head>
<title>Home</title>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/style.php'); ?>
<body>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/header.php'); ?>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/nav_ribbon.php');?>
<div id="p2dbg">
<div id="p2dcontent">
<div class="p2dcontent">
<h1><?php echo htmlentities($current_subject ["menu_name"]); ?></h1><br />
<?php if ($current_page) { ?>
<p><?php echo nl2br($current_page ["content"]); ?></p><br />
<?php } else { ?>
This page does not exist! please slect a page from the menu.
<?php } ?>
</div>
</div>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/footer.php'); ?>
</div>
</body>
</html>
Instead of setting everything to null if your value isn't passed, just assume the default value:
// Set this to the value you want as your default
define("DEFAULTSUBJECT",1);
function find_selected_page () {
global $current_subject;
global $current_page;
if (isset($_GET["subject"])) {
$current_subject = find_subject_by_id ($_GET["subject"]);
$current_page = find_default_post($current_subject ["id"]);
} elseif (isset($_GET["page"])) {
$current_subject = null;
$current_page = find_page_by_id ($_GET["page"]);
} else {
$current_subject = find_subject_by_id (DEFAULTSUBJECT);
$current_page = find_default_post($current_subject ["id"]);
}
}

Different page titles with one header file?

How do you do multiple page titles with on header file? Theres one thing though. For the index page, i've got
error_reporting(0);
if ($_GET["error"]=="404") {
include("forum/styles/art_air/web_template/overall_header.php");
include("include/404");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
} else {
include("forum/styles/art_air/web_template/overall_header.php");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
}
So i would have the header before anything else. So how would i manage to make so that
index?error=404 and index have different titles? Thanks in advance.
In overall_header.php
<?php
$title = "Hello, wolrd!";
if ( $_GET["error"] == "404" ) {
$title = "Error";
}
?>
<title><?php echo $title; ?></title>
Use JavaScript and document.title.
Example:
<script language="javascript">document.title = "My Title"</script>
JS can be used in body.
Another method is to set a $GLOBAL variable before including everything.
Example:
error_reporting(0);
$GLOBALS['404'] = 0;
if ($_GET["error"]=="404") {
$GLOBALS['404'] = 1;
include("forum/styles/art_air/web_template/overall_header.php");
include("include/404");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
} else {
$GLOBALS['404'] = 0;
include("forum/styles/art_air/web_template/overall_header.php");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
}
In your overall_header.php:
if($GLOBALS['404'] == 1) echo '<title>404: Not Found</title>';
else echo '<title>My Title</title>';
You could try a switch
<?php
$page = $_SERVER['PHP_SELF'];
switch($page) {
case "index.php":
echo "<title>My Homepage</title>";
break;
case "apples.php":
echo "<title>The Best Apples!</title>";
break;
case "bananas.php":
echo "<title>We Sell Bananas</title>";
break;
}
?>

If-statement: how to pull 2nd GET variable

How do I get this to pull my 2nd variable? (I already have a switch setup)
<body id="<?php if (! isset($_GET['page'])) { echo "home"; } else { $_GET['page']; echo $page; } ?>">
I have a switch statement that pulls the pages from
index.php?page=#####
and I have just added this part to my switch:
index.php?page=####&section=#####
Right now, if I am on page=photos, my code ends up being:
<body id="photos">
I need to make it so that if any link has the "sections" variable on it like this page=photos&section=cars it uses the same ID:
<body id="photos">
First of all, a HTML element can only have one id. So if you want to create a hybrid (e.g. page-section) you can do something like this:
<body id="<?php echo isset($_GET['page']) ? $_GET['page'] : "home"; echo isset($_GET['section']) ? ("-".$_GET['section']) : ''; ?>">
For more information on Ternary Operators in PHP (the ? and : I used in the echo statement) see http://php.net/manual/en/language.operators.comparison.php
I am not entirely sure I understand your question, but where you're doing:
$_GET['page']; echo $page;
What do you think is happening? You're echoing a variable that has no definition. If you want to echo the value passed in the url, just do:
echo $_GET['page'];
GET doesnt mean your getting the varible, its the method by which the variable was passed to he page. The possible methods are get (in the url) or post (not).
Wouldn't that be an if to find out it if the section was defined? i.e.
if(isset($_GET['section'])){
//create div
} elseif(isset($_GET['page']){
//create fallback div
}
Move the PHP code outside the body's id attribute for readability, and use else if. Make sure your code isn't vulnerable to injection by sanitizing or validating input from $_GET. For example:
<?php
function isValidID($x) {
return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
}
if (isset($_GET['section']) && isValidID($_GET['section'])) {
$bodyID = $_GET['section'];
} else if (isset($_GET['page']) && isValidID($_GET['page'])) {
$bodyID = $_GET['page'];
} else {
$bodyID = 'home';
}
?>
...
<body id="<?php echo $bodyID; ?>">
Alternatively,
<?php
function isValidID($x) {
return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
}
$bodyID='home';
foreach (array('section', 'home') as $key) {
if (isset($_GET[$key]) && isValidID($_GET[$key])) {
$bodyID = $_GET[$key];
break;
}
}
?>
...
<body id="<?php echo $bodyID; ?>">
In this case, I'd use the first, unrolled version. If you had to check more input keys, use the loop-based approach.
If you decide you want both page & section in the ID, you can try something like:
<?php
function isValidID($x) {
return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
}
if (isset($_GET['page']) && isValidID($_GET['page'])) {
$bodyID = $_GET['page'];
} else {
$bodyID = 'home';
}
if (isset($_GET['section']) && isValidID($_GET['section'])) {
$bodyID .= '_' . $_GET['section'];
}
?>
...
<body id="<?php echo $bodyID; ?>">

Categories