Displaying nav if path equals path - php

I have a php page with two navs. One is an admin nav, the other is public. using the code below I'm trying to determine the directory i'm in and depending on that show the proper nav. I feel like this php snippet should work.
<?php
$public = APP_PUBLIC_PATH;
$admin = APP_ADMIN_PATH;
if(is_dir($public)) {
$publicnav = "showme";
$adminnav = "hideme";
}
else if (is_dir($admin)) {
$publicnav = "hideme";
$adminnav = "showme";
}
?>
<nav class="<?php echo $publicnav; ?">
<nav class="<?php echo $admin; ?">
I've also tried the following:
<?php
$public = APP_PUBLIC_PATH;
$admin = APP_ADMIN_PATH;
if(is_dir($public)) {
$publicnav = "showme";
$adminnav = "hideme";
}
else {
$publicnav = "hideme";
$adminnav = "showme";
}
?>
<nav class="<?php echo $publicnav; ?>">
<nav class="<?php echo $admin; ?>">

is_dir only checks to see if the path you provided is a directory or not. Your goal is to check whether the current path is for the admin or for a regular user?
Look into the $_SERVER superglobal variable on how to get the current URI. I believe something like this.
$currentPath = $_SERVER['REQUEST_URI'];
if ($currentPath == $public) {
// do logic
} else if ($currentPath == $admin) {
// other logic
}

Related

Menu created with MySQL only works within website not outside

I hope somebody can help me, because i got an menu that is auto generated via my MySQL db.
Because i got the menu to work inside the website and with that i mean it works with "test.dk/about" but the a href is empty when it's going out of the website like "http://google.com"...
btw it's just a very simple UL LI menu no dropdown or something.
Here is my script
static function build_menu()
{
$result = mysql_query("SELECT * FROM menu");
$menu = array();
while ($row = mysql_fetch_assoc($result)) {
if ($row["is_external"]) {
$url = $row["url"];
} else if (empty($row["is_external"])) {
$url = get_page_url($row["page_id"]);
}
$menu[] = array("name" => $row["name"], "page_id" => $row["page_id"], "is_external" => $row["url"], "url" => $url);
}
return $menu;
}
static function get_page_url($page_id)
{
$result = mysql_query("SELECT view_id FROM page WHERE id = '$page_id'");
$result = mysql_fetch_assoc($result);
$view_id = $result["view_id"];
$result = mysql_query("SELECT listen_path FROM view WHERE id = '$view_id'");
$result = mysql_fetch_assoc($result);
$listen_path = $result["listen_path"];
return $listen_path;
}
static function render()
{
$result = mysql_query("SELECT * FROM menu"); ?>
<div class="menu">
<ul><?php while ($item = mysql_fetch_assoc($result)) { ?>
<li><?php echo $item["name"] ?>
</li> <?php } ?></ul></div><?php
}
How can i fix it, so it works both internal and external?
<div class="menu"> <ul> <li>Homepage</li> <li>About</li> <li>Develop</li> <li>Support</li>
This should be <li>Support</li>; </ul> </div>
You only check for an external link in the function build_menu(), but this function isn't called anywhere from your render() function.
The render() function only calls get_page_url() which doesn't distinguish between internal and external links.
Href parameter of external URL must start with protocol declaration, so with "http://" in your case.
So change your code in condition inside the function "build_menu", if the URL is external, add "http://" to it, something like this:
$url = 'http://'.$row["url"];
I got it work after a while!
I simply just created an If else statement in the render function
static function render(){
$menu_items = self::get();
?><div class="menu"><ul><?php while ($item = mysql_fetch_assoc($menu_items)) { ?>
<li><a href="<?php
if(empty($item["is_external"]))
{
echo self::get_page_url($item["page_id"]);
}
else if($item["is_external"] = 1)
{
echo $item["url"];
}
?>"><?php echo $item["name"] ?></a>
</li> <?php } ?></ul></div><?php
}

Are PHP script read from Top to bottom?

Are PHP scripts reads from top to bottom? like HTML? because in this code
<?php require_once("./includes/connection.php")?>
<?php require_once("./includes/functions.inc.php"); ?>
<?php
if(isset($_GET['subj']))
{
$sel_subj = get_subject_by_id($_GET['subj']);
$sel_page = NULL;
}else if(isset($_GET['page']))
{
$sel_subj = NULL;
$sel_page = get_page_by_id($_GET['page']);
}else
{
$sel_subj = NULL;
$sel_page = NULL;
}
?>
<?php include("includes/header.inc.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<ul class = "subjects">
<?php
$subject_set = get_all_subjects();
while($subject = mysql_fetch_array($subject_set))
{
echo "<li";
if($subject['id'] == $sel_subj['id']) {echo " class =\"selected\"";}
echo "><a href=\"content.php?subj=" . urlencode($subject["id"]) .
"\">{$subject["menu_name"]}</a></li>";
echo "<ul class = 'pages'>";
$page_set = get_pages_for_subject($subject['id']);
while($page = mysql_fetch_array($page_set))
{
echo "<li";
if($page['id'] == $sel_page['id']){echo " class = \"selected\"";}
echo"><a href=\"content.php?page=" . urlencode($page["id"]) .
"\">{$page["menu_name"]}</a></li>";
}
echo "</ul>";
}
?>
</ul>
</td>
<td id="page">
<?php if(isset($sel_subj)){?>
<h2><?php echo "{$sel_subj['menu_name']}";?></h2>
<?php } ?>
<?php if(isset($sel_page)){?>
<h2><?php echo "{$sel_page['menu_name']}"?> </h2>
<?php }?>
</td>
</tr>
</table>
<?php require("includes/footer.inc.php"); ?>
specifically this part
if(isset($_GET['subj']))
{
$sel_subj = get_subject_by_id($_GET['subj']);
$sel_page = NULL;
}else if(isset($_GET['page']))
{
$sel_subj = NULL;
$sel_page = get_page_by_id($_GET['page']);
}else
{
$sel_subj = NULL;
$sel_page = NULL;
}
How is this if-else block being called if it's on top of the page?
It is run top-to-bottom one time per page view. On the initial view, assuming the URL has no parameters, then neither $_GET['subj'] or $_GET['page'] will be set.
If the link pointing back to the same page is clicked, then the entire PHP file will be reprocessed. If that link contained subj or page in the URL as a query variable, then the corresponding if block will be executed and the page will be altered accordingly.
Think of the PHP server as dynamically creating some HTML file that is sent to the web browser. Once it is sent, the server is done, and the PHP code is "gone." The only way to run more PHP code is to request a new page, where the process starts over.
(Even AJAX follows the same principles, although generally then you are dealing with partial data requests as opposed to full page views.)
Yes, scripts are run from top to bottom. I don't understand why you think that if-else block is any different? Those if clauses are run to set the $sel_subj and $sel_page variables before the rest of the page is executed and output.
PHP scripts are executed from top-to-bottom. What exactly is your problem?

Best way to highlight tabs according to the page you're on with CakePHP?

So prior to being introduced to CakePHP, I'd highlight the appropriate navigation tab according to the url with the following (rather sloppy) code I wrote (fyi absolute_url was a function I wrote to get the absolute path) :
$page = $_SERVER['PHP_SELF'];
// add all possible states for the navigation to an array
$checkNav = array(
"index" => "index",
"new" => "new",
"random" => "random",
"submit" => "submit"
);
$compareAgainst = strpos($page, $checkNav['index']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">Popular</span></li>\n";
} else {
echo "<li>Popular</li>\n";
}
$compareAgainst = strpos($page, $checkNav['new']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">New</span></li>\n";
} else {
echo "<li>New</li>\n";
}
$compareAgainst = strpos($page, $checkNav['random']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">Random</span></li>\n";
} else {
echo "<li>Random</li>\n";
}
$compareAgainst = strpos($page, $checkNav['submit']);
if ($compareAgainst == 0) {
echo "<li><span class=\"navBorder\">+ Submit a Link</span></li>\n";
} else {
echo "<li>+ Submit a Link</li>\n";
}
Now, I've noticed that in Cake, to determine the relative path, I can just go:
<?= $this->here; ?>
Is there a better way to do this, or should I just implement this (new) method with the old code?
You can do the following
Add this to app_helper.php if you need it in multiple pages. You feed this function with the controller and the action you want to check you want to compare against. The function compares it with the current page and return true if they match.
function isActive($controller, $actions = array())
{
foreach ($actions as $action)
{
if ($controller == $this->params['controller'] && $action == $this->params['action'])
{
return true;
}
}
return false;
}
And then generate your links like so:
<ul class="left">
<li <?php if($html->isActive('controller_name', array('index'))) { echo 'class="active"'; } ?>><?php echo $html->link('Index', '/index'); ?></li>
<li <?php if($html->isActive('controller_name', array('new'))) { echo 'class="active"'; } ?>><?php echo $html->link('New', '/new'); ?></li>
<li <?php if($html->isActive('controller_name', array('random'))) { echo 'class="active"'; } ?>><?php echo $html->link('Random', '/random'); ?></li>
<li <?php if($html->isActive('controller_name', array('submit'))) { echo 'class="active"'; } ?>><?php echo $html->link('Submit', '/submit'); ?></li>
</ul>
If the function returns true, the link will have class="active". Adapt it to your needs.
The way I've always done this is to give your body tag an id, and use css to target it. If your views are all separate then you can hard code the body id. If you are using some sort of template that adds in the header, content, footer etc., then just pass the id as a variable to the header view or wherever the body tag is (really any outer container/div that will be on every view and contain your navigation tabs). Also you will need to give your navigation tab id's to target each one.
Then just some css like this:
#homepage a#hometab,
#aboutpage a#abouttab,
#productpage a#productstab,
#contactpage a#contacttab
{
special active styling here
}

Weird problem with PHP Breadcrumb Script

I'm using Mick Sears' php breadcrumb script - found here:
http://www.roscripts.com/PHP_breadcrumbs-118.html
I've used this script several times with no problems. But with this one site I'm having the weirdest problem... Home page - fine. Level 1 page - fine. But every time I move to a level2 page, the correct level1 crumb is replaced by "Help". The link on the crumb is the correct one for the help page. This happens even if I clear all browser caches and don't go to the Help section of the site at all.
The site is http://www.fastexas.org. The script is there, but I gave the breadcrumb div display:none; until I can figure this out.
This script seems to have been around awhile and I'm wondering if anyone else has seen this problem.
The Breadcrumb Script:
<?php
class Breadcrumb{
var $output;
var $crumbs = array();
var $location;
function Breadcrumb(){
if ($_SESSION['breadcrumb'] != null){
$this->crumbs = $_SESSION['breadcrumb'];} }
function add($label, $url, $level){
$crumb = array();
$crumb['label'] = $label;
$crumb['url'] = $url;
if ($crumb['label'] != null && $crumb['url'] != null && isset($level)){
while(count($this->crumbs) > $level){
array_pop($this->crumbs); }
if (!isset($this->crumbs[0]) && $level > 0){
$this->crumbs[0]['url'] = "/index.php";
$this->crumbs[0]['label'] = "Home";}
$this->crumbs[$level] = $crumb;}
$_SESSION['breadcrumb'] = $this->crumbs;
$this->crumbs[$level]['url'] = null;}
function output(){
echo "<ul>";
foreach ($this->crumbs as $crumb){
if ($crumb['url'] != null){
echo "<li> <a href='".$crumb['url']."' title='".$crumb['label']."'>".$crumb['label']."</a></li> ";} else {
echo "<li class='last'>".$crumb['label']."</li> ";}}
echo "</ul>";}}
?>
Each page begins with something like:
<?php session_start();
$level= '1';
$label= 'Honors Circle';
$url= '/honors/'; include($_SERVER['DOCUMENT_ROOT']."/includes/Breadcrumb.php");
$trail = new Breadcrumb();
$trail->add($label, $url, $level); ?>
or
<?php
session_start();
$level= '2';
$label= 'Districts';
$url= '/honors/district.php';
include($_SERVER['DOCUMENT_ROOT']."/includes/Breadcrumb.php");
$trail = new Breadcrumb();
$trail->add($label, $url, $level);
?>
And to print the breadcrumb trail:
<div id="breadcrumb"><?php $trail->output(); ?></div>

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