Include function in php - php

I have a navigation bar page that I can use for all the pages in my website. However, I built a new login page but it is having errors when I connect to the navigation bar page.
The structure of all my files:
I want to apply the navigation bar to my login.php under the uploadTesting2 file. The code for navigation bar I have put in my login.php is :
<?php include '../navHeader.php'; ?>
The code in the navHearder.php is:
<div class="collapse navbar-collapse navHeaderCollapse">
<ul class="nav navbar-nav navbar-right">
<li class="active">About</li>
<li>News</li>
<li class="dropdown">
Collection <b class="caret"></b>
<ul class="dropdown-menu">
<?php
include './data.php';
for ($i = 0; $i < count($collectionArr); $i++) {
echo '<li>';
echo '<a href=\'collectionPage.php?cat=' . $collectionArr[$i]['catCode'] . '\'>';
echo $collectionArr[$i]['catName'];
echo '</a>';
echo '</li>';
}
?>
</ul>
</li>
<li>Ordering</li>
<li>Contact</li>
<li>Admin</li>
</ul>
</div>
However, the code is not working. When I click the index.php, it will show error. the url is wrong and the url is showing: http://localhost:7777/ca2/CA2script/uploadTesting2/index.php
Should I apply one more navgation php in my uploadTesting2 or I can make changes in the navHeader.php?

For best practice define your main(base) url as constant then use it any where.
Suppose your base url is "http://localhost:7777/ca2/CA2script/"
You can do it like following:
define('BASE_URL',"http://localhost:7777/ca2/CA2script/");
then in any file you can use it for link
Ex: in your navHearder.php use like this:
<li class="active">About</li>
suppose your file is in directory then:
link

Problem with include
The include statement relates file according to the location relative to the entry point. For example, if you have a file structure like this:
/
/uploadTesting2
/login.php
/navHeader.php
/data.php
When you visits /uploadTesting2/login.php as the entry point, the location of navHeader.php and data.php should be ../navHeader.php and ../data.php. This is true even within the scripts included by login.php. So navHeader.php should only find data.php in as ../data.php.
A usual way to solve this issue is to use __DIR__ constant as a reference. This constant will always to be the script file's parent folder (not the entry file). If you echo __DIR__ inside navHeader.php, it will be the system path of your root no matter what is including it.
Thus instead of this include statement:
include './data.php';
you may do this:
include __DIR__ . '/data.php';
Problem with links
Links is also resolved relative to your current path. Thus
<a href="collectionPage.php?cat=abc">
in /uploadTesting2/index.php URL resolves to /uploadTesting2/collectionPage.php?cat=abc. Instead you should do
<a href="../collectionPage.php?cat=abc">

Related

Highlight a list element when I'm browsing the very page

I saw a few tutorial about this problem but none of them satisfied me. I want to highlight the single element of my list which matches the page that I'm browsing. I created the code with php, is a wordpress based website, and the code actually works because when I echo the uri which I'm on it will display the right uri, but the if statement I created to add a class when I'm on the website won't output anything.. and I don't understand why.. anyway.. here's the code:
header.php
<ul class="nav nav-pills sliding" id="Jcollapse">
<li class="<?php if ($current == "/why-chickapea/"){ echo "current";}?>"><span>Why Chickapea?</span></li>
<li class="<?php if ($current == "/our-pasta/"){ echo "current";}?>"><span>Our story</span></li>
<li class="<?php if ($current == "/shop-chickapea/"){ echo "current";}?>"><span>Shop</span></li>
<li class="<?php if ($current == "/recipes/"){ echo "current";}?>"><span>Recipes</span></li>
<li class="<?php if ($current == "/blog/"){ echo "current";}?>"><span>Blog</span></li>
</ul>
In each page I added a php snippet:
<?php $current = $_SERVER["REQUEST_URI"]; ?>
If I echo the var $current I will obtain the right url in this format: /pagename/
At the end I style the class current with a yellow color
.current {
color:yellow;
}
.current a {
color:yellow;
}
Does anyone know where my mistake is?
this is the page website: choosechickapea.com
As you can see the class that my code will generate is empty, but if I echo each value the uri I will obtain is the right one
The simplest explanation would be, that you print the header before $current is set.
The second simplest explanation is different scopes, meaning either you set $current in a non-global scope or you read it in a non-global scope, and those two (whatever they are) are different. Since someone said wordpress, I guess there is some encapsulation into functions (thus changing the scope). Using the global keyword may be a solution, but a dirty one. But since you're already avoiding wordpress functions ...
The actual code is:
Before declaring in the header the if statement, SET the value of the variable. If you'll declare in the body, even before loading the header with a, for example require once or in wordpress:
<?php get_header(); ?>
It won't work, the variable has to be set in the header like this:
<?php $current = $_SERVER["REQUEST_URI"]; ?>
<header class="navbar-fixed-top">
<ul class="nav nav-pills sliding">
<li class="<?php if ($current == "/your-url/"){ echo "current";}?>"><span>your url</span></li>
<li class="<?php if ($current == "/other-url/"){ echo "current";}?>"><span>/other url/</span></li>
</ul>
</header>

Catch dynamic page without use database ID

This is my content.php. I want to show sectors/sector_id=1.php if some one click one my side bar sectors/sector_id=1.php then sector pass an ID 1 then it shows some content then another menu then it goes to another page, but it shows only one page sector_id=0.php and when ID is 1 or 2 it shows sector_id=0.php this page:
<?php
if($id==1) include('sectors/sector_id=1.php');
if($id==2) include('sectors/sector_id=0.php');
?>
This is my side bar
<ul id="sector-nav" class="nav">
<li >
<!--Fibre-->
Fibre
</li>
<li >
<!--Hand Protection-->
Hand Protection
</li>
<li >
<!--Purification Products-->
Purification Products
</li>
<!-- others <li>'s -->
</ul>
The problem here is how you're trying to pass the variables. If you're Link is:
Fibre
This will not get through to your PHP the way you want. I would advise creating the link like so:
Fibre
When this link is followed, sectors.php will be able to get that value from $_GET['id'] variable. You can then do:
<?php
if(isset($_GET['id'])){
include('sectors/sector_id=' . $_GET['id'] . '.php');
}
?>
Now, if you do something like:
Fibre
This can be used too, but will generate a NULL value at the index called 1, $_GET['1']. So you could grab the Key versus the value if you wanted.

Change menu active state on different pages

I am using a bootstrap pagination menu on my website to navigate from one page to another page.
I copied the pagination menu from :
http://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_pagination_active&stacked=h
and added this to all pages of my website .
It's works fine and I can set the Active Status using:
<li class="active">
1</li>
according to the page link where the code is.
For some reasons ,I want to have only one menu file and include it to all pages .
Is there a way to automatically add ".active" class to the links in included menu?
menu.php
<ul class="pagination">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
Please help!
You can do it depending on your setup. In javascript, using jQuery for example:
var currentPage = 2; //say the page is defined like this
$('.pagination li').eq((currentPage-1)).addClass('active');
If you generating pages server side, for example in PHP
$currentPage = 2;
$output = '<ul class="pagination">';
//iterating pages
if($pageNum == $currentPage){
$output .= <li><a class="active" href="pages/'+$pageNum+'.php">'+$pageNum+'</a></li>
} else {
$output .= '<li>'+$pageNum+'</li>';
}
$output .= '</ul>'
echo $output;

See if one item in array is true

I'm trying to put a menu together where it will sense what page is loaded. This site is not done in a CMS and is straight PHP/HTML code.
I currently have the navigation working for the primary links. There is a dropdown where I am having problems. I need to be able to see if the parent or any dropdown children are active. If one of the children are active. In the example below this is "FAQ" and the children are "FAQ1," "FAQ2," and "FAQ3."
For this example I'm using a CSS state called "active."
<style>
a{color:red;}
.active{color:blue;}
</style>
Here is the script used for the menu. The links for Home, Products, and Contact are working as expected.
<ul>
<li><a href="index.php" id="homenav" <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="active"';?>>Home</a></li>
<li><a href="products.php" id ="prodnav" <?php if (strpos($_SERVER['PHP_SELF'], 'products.php')) echo 'class="active"';?>>Products</a></li>
<li><a href="faq.php" id ="faqnav" <?php if (strpos($_SERVER['PHP_SELF'], 'faq.php, faq1.php, faq2.php, faq3.php')) echo 'class="active"';?>>FAQ</a>
<ul>
<li>FAQ1</li>
<li>FAQ2</li>
<li>FAQ3</li>
</ul>
</li>
<li><a href="contact.php" id ="connav" <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="active"';?>>contact us</a></li>
</ul>
Can I please get some help on how I should be writing this line to let it work the way the others are?
<li>
<a href="faq.php" id ="faqnav"
<?php
if (strpos($_SERVER['PHP_SELF'], 'faq.php, faq1.php, faq2.php, faq3.php'))
echo 'class="active"';
?>
>FAQ</a>
</li>
strpos() only accepts one string per parameter, you can not give it a list.
Try this:
<?php if (in_array(basename($_SERVER['PHP_SELF']), array('faq.php', 'faq1.php', 'faq2.php', 'faq3.php'))) echo 'class="active"';?>
basename() strips the path from the filename, so you only have the pure file name
in_array() then checks if this path is in an array
array() generates an array of strings to be handed over to in_array(). Note that there are 4 separate strings, not one long one as in your code.
Use in_array for this purpose:
if (in_array($_SERVER['PHP_SELF'], array('faq.php', 'faq1.php', 'faq2.php', 'faq3.php')) echo 'class="active"';
http://php.net/manual/de/function.in-array.php
If you don't have any other pages that contain faq, you can simply use:
if (strpos($_SERVER['PHP_SELF'], 'faq') !== false)
Note that I am using !== false as strpos can return 0 when faq is found at the beginning of your string. You should probably use that for your other comparisons too.
Otherwise, go with the in_array solution of #MrTweek.

PHP get path and all subpaths (Drupal)

I have a hard-coded menu in Drupal (as it's too complex for the standard Menu system in Drupal).
I would like to be able to say: If this page is contained within the /about/ directory, apply the class "active", so that all new pages created within this directory automatically highlight the current section.
Currently I have:
$current_page = $_SERVER['REQUEST_URI'];
<ul class="main">
<li class="home">Home</li>
<li class="about
<?php if ($current_page == "/xxxxxxx.com/dev/about/")
{
echo "active";
}
?>">About</li>
<li class="services">Services</li>
<li class="work">Work</li>
<li class="awards">Awards</li>
<li class="environment">Environment</li>
<li class="contact">Contact</li>
</ul>
I have tried a few variations of strpos and explode to get the right variable, but with no luck so far.
Thanks :)
I don't know anything about Drupal or your URL scheme, but the task of checking whether $current_page contains "/about/" you can do with:
if (strpos($current_page, '/about') !== false) echo "active";
You should probably listen to googletorp though.
Try this function. It's like arg function, but parse real path.
function real_arg($index = NULL) {
$ofset = strlen(base_path());
$q = explode('?', substr($_SERVER['REQUEST_URI'], $ofset));
$q = explode('/', trim($q[0], '/'));
return isset($index) ? $q[$index] : $q;
}
In your case:
if(real_arg(0) == 'about') echo 'active';
Use the menu api then theme your links to match what you want. You won't need to duplicate functionality that already exists. You'll acquire a skill you can reuse.
See:
http://api.drupal.org/api/function/theme_menu_item/6
http://api.drupal.org/api/function/theme_menu_item_link/6
It shouldn't take long and you'll remove a layer of workarounds.

Categories