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...
 
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
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:
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
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.....
 
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
$_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... :(
 
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
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... :(
 
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
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.
 
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
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:
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
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...
 
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
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:
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
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?
 
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
Hi marc,

The array i posted before is the full array as it stands...
The content area of my page is loaded via content.php which contains...
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');
}


?>
So index.php?guides is simply the index for that section, index.php?guides=1 (or whatever case i assign in the above - 1 in my example) is where the tabs don't show active.

The array is simply what was posted per your previous advice (thanks), i've only learnt what an array is within the last few days and i'm still very limited in understanding, but it's simply...
PHP:
$mylinks = array(
'Home' => "/index.php",
'Projects' => '/index.php?project',
'Guides' => '/index.php?guides',
'Other' => '/index.php?other');
nothing more...

If i could be doing this better another way please explain.... As long as i get the jist of what code is doing what i don't mind how it's done.
 
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
i've actually had a quick play with the code. and the home page will be always be active because it consists of just

'/index.php'

which will always be found when doing our compare (using stristr). you need to give your a home page a query string too. :p

oh and that code could do with a huge tidy up. i'll take a look later. :)

The tabs are working with exception to when the page is displayed with a case. ie.. index.php?guides=1 but index.php?guides works as it should do..

Feel free to modify anything, but please explain anything you change so i know what the code is doing, i can only work with what i understand after all... ;)

Thanks for the help thus far mate....

There was me all ready to make a website then i decided i wanted to learn a bit more php and i'm stuck trying to make one of the smallest areas work correctly with a minor detail......active tab, lol...
 
Last edited:
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
Yeah can do that easy enough, ideas change as you go along.... :(

Unfortuantely doesn't solve the tab problem, i'm thinking something just need be adjusted in the $content line so when the URL matches anything with the case, be it 'guides' or 'guides=1'.

I'll stick with what you've given me so far and focus on other areas and you never know i might learn something to fix it along the way.... :)

As said in opening post, it's hard to find decent guides for this kind of thing, they're all either too basic or too far gone i'm totally lost.

1 site has some good tutorials, however the more advanced areas cover things like using php with forms and/or working with a mysql database, not something i need to do at this stage. I don't want or need user interaction for the site, just simply for it to display content, additional features can be plonked in later.

That said i want the code to be as easy as possible to change as and when i want to and for the sake of another admin. Anything i know how to use i'll need to be able to explain to him to, which thus far i can.

It's not for a company or anything profiteering, just a personal site, where i can plonk anything i or the other admin make(s) or write(s) about.
 
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
Ok so what am i missing here...

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

$current = ($_SERVER['SCRIPT_NAME']);
if(strlen($_SERVER['QUERY_STRING']) > 0) $current .= '?' . $_SERVER['QUERY_STRING'];  

foreach($mylinks as $key => $link) 
{    
    if(stristr($current, $link))
    { echo '<li><span>' . $key . '</span></li>'; } 
    else 
    { echo '<li><a href="' . $link . '">' . $key . '</a></li>'; }
} 
echo $current;
With this the Home tab is always shown as active.

If i change if(stristr($current, $link)) for if($link == $current) it works as i described previously.
 
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
add anything you like to the querystring and it still displays the correct tab. :)

http://www.marc2003.ukfsn.org/test/index.php? - tab not shown...

Unfortunately i don't understand how your structure is laid out in comparison.

The whole article part isn't really required but looks a great idea.

Can you post the files you have there in the test folder so i can compare. Link to a zip would be easier if ok... ;)

Thanks...
 
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
Code works great but i don't see how to plonk that into my existing layout.

I'd ideally not want the home page as page=home, just simply as index.php.

It won't work with how i'm calling content into the design either, via include of content.php.

Very nice code but i don't understand what some of it is doing now ... :(

The code before was fine for me, just needed fine tuning...

PHP:
$current = ($_SERVER['SCRIPT_NAME']);
if(strlen($_SERVER['QUERY_STRING']) > 0) $current .= '?' . $_SERVER['QUERY_STRING'];

If i could just add something here that checks if anything follows ?guides, ?projects or whatever else, example being ?guides=thisguide.

I assume it's this area...
PHP:
$current .= '?' . $_SERVER['QUERY_STRING']
just needs a little extra to see if there is further data on the end of the url.

$_SERVER['QUERY_STRING']=something (which of course you can't do outright like that)
 
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
So you want to check if "guides" is anywhere in the querystring and if so, find what it's set to?

PHP:
if ( isset( $_GET['guides'] ) ) {
$chosen_guide = $_GET['guides'];
}
Sorry if I've misunderstood you.

Hi mate, no just need to check if anything else is on the end of the string....

I'm still using this...
PHP:
$mylinks = array(
'Home' => "/index.php",
'Projects' => '/index.php?project',
'Guides' => '/index.php?guides',
'Other' => '/index.php?other');  

$current = ($_SERVER['SCRIPT_NAME']);
if(strlen($_SERVER['QUERY_STRING']) > 0) $current .= '?' . $_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>'; }

}
Which works the tabs perfectly, except in cases when it has =xxx on the end.

index.php?guides the tab is active,
index.php?guides=1 tab not shown as active.

index.php?guides - displays an index page for the guides directory.
index.php?guides=1 - is a file called 1.php in the guides directory

index.php?guides is simply the first page you see when you click the guides section, other pages would be index.php?guides=apage , index.php?guides=anotherpage and so on.....

I like what you made in the other examples but i don't understand some of the PHP you're using fully so i'd rather stick to what i have in this post as i understand what it's doing (for the most part).

Just need that final piece of code to do as described.

You're a gem for helping on this on your Friday evening... ;)

EDIT: Posted assuming you were marc GeForce, thanks to both of you...
 
Last edited:
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
so even after all this, even uploading the exact code which works fine for me, it's not working for you? then something is very "different" with your setup. i can only assume it's the $_SERVER variables???? :confused:

Nah i'm not saying there's a problem with your code, just how i'm calling the content and how i'm using case and switch to create the URLs is different to the method you posted, which again was great but a little over my head in areas.

I only need help with making that small part work then it's all good, and i'll refine it later as i learn more, there's no need for me to refine code now into code that i won't understand how to use.

Geforce the url will always be...

index.php - main page
index.php?guides - guides main index
index.php?guides=xxx - if xxx is not an existing case then it also displays the guide index
index.php?guides=apage - an actual existing page(defined by case/switch) in that section (not with php extension on the end, typo by me)

or if it was the projects section then...

index.php?projects - projects main index
index.php?projects=xxx - if xxx is not an existing case then it also displays the projects index
index.php?projects=apage - an actual existing page(defined by case/switch) in that section (not with php extension on the end, typo by me)

The tabs are active when the first 2 apply but not the 3rd and 4th.
 
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
So when you had the test directory on your website loaded index.php?guides=1 worked?

The links you had up were all index.php?page=guides and so on.... which is not what i'm trying to do.

Your method differs to one i'd like to stick to. As said, the code is great, and i really appreciate the help, i just think you're misunderstanding what it is i'm saying doesn't work.

I can upload the files into a zip for you to look at if you like?

By all means if it's me doing something completely wrong i hang my head in shame.

NOTE: Is it just me or have some of the forum features disappeared? Smilies in reply, and the quick reply box.... :(

Fixed, bug.... think it was the WAMP server, but it's on again now and everything back to normal, odd because it caused the same issue with IE to.
 
Last edited:
Back
Top Bottom