PHP help please.

Soldato
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
Hi all,

Have spent a couple of days attempting to learn some further PHP coding as my current learn of knowledge is some what lacking.

I know a little about case and switch, how to create an array, use includes and call content etc.....

Now i've got a site (just personal, hobby) which i'm running locally via a WAMP server which i'm having a few headaches with.

I'll explain the layout so you get the jist of where i need help.

index.php - of course the main page, simply consists of includes
header.php - the header of my document
footer.php - the footer
content.php - to be used to load varying content into an area of the page, i don't want to keep creating new pages and include the header/footer again and again.

Included in the header is a further include for a css tab menu (menu.php) that i'd like to have change dependant on the page loaded.

I have split the the active and non-active links of the header into arrays inside this file, like so...

PHP:
<?php
// Tab menu array

// First array to be called when 
// the page is current loading a none 
// linked tab title
$menu_active = array(
                        'Home' => '<li><span>Home</span></li>'
                        ,'Projects' => '<li><span>Projects</span></li>'
                        ,'Guides' => '<li><span>Guides</span></li>'
                        ,'Other' => '<li><span>Other</span></li>'
                        );
// Second array is called for page 
// not currently in view
$menu_page = array(
                        'Home' => '<li><a href="index.php">Home</a></li>'
                        ,'Projects' => '<li><a href="#">Projects</a></li>'
                        ,'Guides' => '<li><a href="#">Guides</a></li>'
                        ,'Other' => '<li><a href="#123">Other</a></li>'
                        ,'' => '<li><a href="#">None</a></li>'
                        );

?>
With the menu i have a <li> element containing <a href> will be a normal tab, and those containing a <span> element are the current/active tab.

In the header is the following....
PHP:
<div id="tabs">
        <ul>
<?php 
echo     
        $menu_active['Home'], 
        $menu_page['Projects'],
        $menu_page['This'],
        $menu_page['That'],    
?>
        </ul>
</div>
Aware the linsk mostly point nowhere yet... ;) Want to make it work first... :)

Inside the content.php i have the following.... (early stage)

PHP:
<?php

$page = $_GET['page'];

if ($page=="") 
{ $page = "projects/index.php"; }


switch($page){
                case "1":
                include('projects/1.php');

                case "2":
                include('projects/2.php');
}


$blah = $_GET['page'];

if ($blah=="") 
{ $blah = "blah/index.php"; }


switch($blah){
                case "1":
                include('blah/1.php');

                case "2":
                include('blah/2.php');
}

?>
Can anyone explain how i can firstly have the content.php return content 'x' when no variable/case is present ($page or $blah), so basically when i'm on the index, $page and $blah will not be in the URL and as such i'd like something else displayed.

Secondly how do i then have the menu check to see if $page of $blah has been called.

As said, i know only little about php, and this is some of what i've been learning over the last few days.

I've found it hard to find a guide that isn't too basic or too advanced to help, though some have given me pointers, they don't really apply to what i'm wanting to do.

I'm wanting to create a site with PHP, HTML and CSS, no database for my personal projects and/or guides etc...

I can find info on using case/switch to change the content, but do not know how to check what the value or variable is to display different code into the menu.

I think i need something that simple says.....

If page is equal to index....

PHP:
<?php 
echo     
        $menu_active['Home'], 
        $menu_page['Projects'],
        $menu_page['Guides'],
        $menu_page['Other'],    
?>
else if page is Projects (when the project case is equal to anything - index.php?project=1 <-- example)...
PHP:
<?php 
echo     
        $menu_page['Home'], 
        $menu_active['Projects'],
        $menu_page['Guides'],
        $menu_page['Other'],    
?>

Sorry for the long winded post, trying to explain best i can....

If anyone has any pointers, links or suggestions they are all welcome, thanks...
 
I have split the the active and non-active links of the header into arrays inside this file, like so...

why not store it in just the one array like this....

PHP:
$pages = array(
'Home' => '/index.php',
'Projects' => '/projects.php',
'Guides' => '/guides.php',
'Other' => '/other.php');

now loop through it and display either a "span" or an "a href" depending on if the current page matches the page in the array.....

PHP:
$current_page = $_SERVER['SCRIPT_NAME'];
foreach($pages as $key => $value) {
    if($value == $current_page) {
        echo '<li><span>' . $key . '</span></li>';
    } else {
        echo  '<li><a href="' . $value . '">' . $key . '</a></li>';
    }
}
 
Well i've figured out how to work the content area, via use of isset.

Now all i need to do is adapt your code to check for the variable $project so when i have index.php?project=1 the tabs switch.

The code as it stands function with exception to the fact i'm loading content into an area via include rather then including my header, footer etc in various index.php files etc...

EDIT: This is my content file at the moment.

UPDATED AGAIN:

PHP:
<?php

$project = $_GET['project'];
$guides = $_GET['guides'];

if (isset($project)) 
{
    if ($project=="") 
    {
    $project = include('projects/index.php'); 
    }
        else
        switch($project)
        {
        case "1":
        include('projects/1.php');
        break;
        case "2":
        include('projects/2.php');
        break;
        }
unset($project);
}
elseif (isset($guides)) {
    
    if ($guides=="") 
    {
    $guides = include('guides/index.php'); 
    }
        else
        switch($guides)
        {
        case "1":
        include('guides/1.php');
        break;
        case "2":
        include('guides/2.php');
        break;
        }
unset($guides);
}



else 
{
    include('main.php');
}


?>
All i need now is for the menu to see if $guides or $projects is set....

I'm using your code from before...
PHP:
<?php 
$current_page = $_SERVER['SCRIPT_NAME'];

foreach($pages as $key => $value) {
    if($value == $current_page)
    {
        echo '<li><span>' . $key . '</span></li>';
    } else {
        echo  '<li><a href="' . $value . '">' . $key . '</a></li>';
    }
} 

?>
If $project is set then that item should be within the SPAN element, if $guides is set then that should be within SPAN, anything else needs to be enclosed into <a href="">.

Menu is currently...
PHP:
<?php
               
$pages = array(
'Home' => '/index.php',
'Projects' => 'index.php?project',
'Guides' => 'index.php?guides',
'Other' => 'index.php?other');  


?>
I'm still learning though, appreciate the response to, i posted on a busy php forum and have so far 0 responses.... :(
 
Last edited:
ah my method won't work if you're using query strings in your links.... :o

if i'm browsing 'index.php?page=blah', $_SERVER['SCRIPT_NAME'] will only return '/index.php'.
 
Decided to have a little think about how i'm going to layout everything, subfolders, index files etc...

I spent all last night trying to work the current way and nearly pulled out what little hair i have left.

So i have a few ideas first.

functions.php - contain header() and footert() functions which will be called in every index file, then each index will simply have a different ID attached to the BODY tag, this should be far easier to check.
header & footer .html files with the obvious content.
index.php files with then simply contain <body id="example">content</body>
case.php - somewhere to store the cases and switches.

I can simply just create a new index.php each time calling the functions as needed. It means a little more work each time but should only mean literally a few lines of code.

Is there a way to have php check if a given file is in a sub-directory, and if so apply an ID named after that directory. So basically if i have an index file in a sub-folder called Guides, then index.php in that folder is automatically given <body id="Guides">, but of course it would be something like <body <?php function thisfunction() ?> >.

I'm trying to keep the design very easy to use for another admin, so i can basically say to him when you want to create a page or section, copy sub-folder "template", where template is a sub-folder with a basic index.php for him to modify.

Or, could i have a php script run the body inside the header function applying the sub-folder name if one applies, and if not, do not apply one, or a default(default being the home page in the root), .... some how.... ;)

Thanks for the help this far guys.....
 
$_SERVER['QUERY_STRING'] returns the query string.

So you could use:
PHP:
$requested = $_SERVER['SCRIPT_NAME'] . "?" . $_SERVER['QUERY_STRING'];

How would i apply this, i may keep what i have this far and refine later if this will work then.

PHP:
$current = $_SERVER['SCRIPT_NAME'] . "?" . $_SERVER['QUERY_STRING'];

foreach($mylinks as $key => $link) 
{    
    if($link == $current)
    { echo '<li><span>' . $key . '</span></li>'; } 
    else 
    { echo '<li><a href="' . $link . '">' . $key . '</a></li>'; }
}
Would this be right?, if so, it's still not working unfortunately... :(
 
Hi marc, it prevents the links working if i store the site in a sub-directory that, which is no problem i've plonked it back in the root.

However the problem still remains in that Home is always found to be the current page no matter what page i move onto, since they all contain the same page (index.php) in the url.

Could i have your code here...
PHP:
$current = $_SERVER['SCRIPT_NAME'];

foreach($mylinks as $key => $link) 
{    
    if($link == $current)
    { echo '<li><span>' . $key . '</span></li>'; } 
    else 
    { echo '<li><a href="' . $link . '">' . $key . '</a></li>'; }
}
... check the url (or if a switch is set) instead?

Again thanks for the help guys, got absolutely no response still on a so called PHP forum... :(
 
However the problem still remains in that Home is always found to be the current page no matter what page i move onto, since they all contain the same page (index.php) in the url.

er yes i already posted about that and geforce kindly posted a fix. and in my previous post, i was just saying the array needs to be modified like this.....

PHP:
<?php
               
$pages = array(
'Home' => '/index.php',
'Projects' => '/index.php?project',
'Guides' => '/index.php?guides',
'Other' => '/index.php?other');  
?>

have the script output the variables on screen temporarily for debugging/testing. should make things a little easier.
 
Last edited:
OMG! it works..... with 1 exception...

If the index.php page without any switch...

If i echo the $currentpage it shows index.php? instead of index.php.
 
PHP:
$mylinks = array(
'Home' => "/index.php",
'Projects' => '/index.php?project',
'Guides' => '/index.php?guides',
'Other' => '/index.php?other');  

$current = $_SERVER['SCRIPT_NAME'] . "?" . $_SERVER['QUERY_STRING'];

foreach($mylinks as $key => $link) 
{    
    if($link == $current)
    { echo '<li><span>' . $key . '</span></li>'; } 
    else 
    { echo '<li><a href="' . $link . '">' . $key . '</a></li>'; }
} 
echo $current;
So yes now working with exception of the main page which seems to be returning index.php? and not showing the current tab like the other pages now do.

However the addres bar does not show the main page as index.php? it simply comes up as index.php, so why there's a question mark in the echo out is beyond me... :(

Thanks guys....
 
Last edited:
that's because of this....

PHP:
$current = $_SERVER['SCRIPT_NAME'] . "?" . $_SERVER['QUERY_STRING'];

you're adding '?' regardless of if there is a query string or not...

change it to

PHP:
$current = ($_SERVER['SCRIPT_NAME']);
if(isset($_SERVER['QUERY_STRING'])) $current .= '?' . $_SERVER['QUERY_STRING'];
 
Will give that a try, found another problem if say the url is index.php?Guides=1 then the tab is not active, only when index.php?Guides.

I'll try what you posted marc, thanks...
 
Ok marc, your code works, but no different to the previous code, main index does not show active and echo out returns index.php? still.

Also the switch= problem still remains. The tabs are only active if i am calling a URL like so...

index.php?guides or index.php?projects
and not

index.php?guides=1 or index.php?projects=1

I'd like for it to be active in both cases. I imagine it is possible (PHP is awesome in this sense), just have headaches trying to wrap my head around it all, lol.... i'm only use to making modifications to PHP not writing it.

Thanks..
 
Last edited:
Will give that a try, found another problem if say the url is index.php?Guides=1 then the tab is not active, only when index.php?Guides.

indeed. you're matching it against the value in the array. it has to be an exact match for it to work. this is the line of code....

PHP:
if($link == $current)

of course you can modify this to make it more flexible. eg

PHP:
if(stristr($current, $link))
 
Ok marc, your code works, but no different to the previous code, main index does not show active and echo out returns index.php? still.

ok. try this instead...

PHP:
$current = ($_SERVER['SCRIPT_NAME']);
if(strlen($_SERVER['QUERY_STRING']) > 0) $current .= '?' . $_SERVER['QUERY_STRING'];
 
Thanks, that works, except for links where a case is supplied to the switch.

index.php - works
index.php?switch - works
index.php?switch=case - no tabs show as active :(

Any ideas matey?
 
Back
Top Bottom