PHP help please.

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
Joined
6 Feb 2004
Posts
20,675
Location
England
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. :)
 
Last edited:
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
Joined
6 Feb 2004
Posts
20,675
Location
England
it seems you want the whole site to be accessed through index.php ? i'd use that and rid of most of the includes and put your html header/footer in there too.

index.php
PHP:
<html>
<head></head>
<body>
<div id="menu">
//php to do tabs
</div>
<div id="content">
//switch statement here to include content (not content.php but the actual project/guide page)
</div>
//maybe add the html for a footer here
</body>
</html>

edit: here's my bodge. first off i've changed the paging system slightly in the array.... :p

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

now we're going to use "article" to load our content. eg

Code:
index.php?page=projects&article=1

now here's the code for it (assuming same folder structure as before "projects/1.php" etc)
PHP:
$page = $_GET['page'];
switch($page) {
	case 'projects': //putting these 2 cases together is like using if/or
	case 'guides':
		$article = intval($_GET['article']); //check article is a number. if not it will be assigned the value of 0
		if(file_exists($page . '/' . $article . '.php')) { //rather than be lumbered with a static case system of 1,2,3 - which would be a little tedious to update, we simply check if the file exists and include it if it does
			include $page . '/' . $article . '.php'; 
		} else {
			include $page . '/index.php'; //if page doesn't exist, we include the index
		}
		break;
	case 'other':
		include 'other.php';
		break;
	default: //if none of the above conditions are met, we do this.....
		include 'home.php';
		break;
}
 
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
Joined
6 Feb 2004
Posts
20,675
Location
England
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'.

works fine for me?? i've even uploaded a sample here.... :)

http://www.marc2003.ukfsn.org/test/index.php?page=home

add anything you like to the querystring and it still displays the correct tab. :)

like this....

http://www.marc2003.ukfsn.org/test/index.php?page=projects&article=212
 
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
Joined
6 Feb 2004
Posts
20,675
Location
England

had to put in a bodge to fix that... :o

anyway, here's the whole code for that page. and i'm not including any files at this point - just the menu (which i've added the css for below)

PHP:
<?php ob_start(); ?>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="tabs.css" />
</head>
<body>
<div id="header">
<ul id="primary">
<?php
$mylinks = array(
'Home' => "/index.php?page=home",
'Projects' => '/index.php?page=projects',
'Guides' => '/index.php?page=guides',
'Other' => '/index.php?page=other');
if(!array_key_exists(ucfirst($_GET['page']), $mylinks)) header("Location: index.php?page=home");
$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>';
	}
}
?>
</ul>
</div>
<div id="main">
<div id="contents">
</div>
</div>
</body>
</html>

and tabs.css

Code:
#main {
border: 1px solid #666;
clear: both;
background: #FFF3B3;
padding-top: 2em;
}

#contents {
background: #FFF;
border-top: 1px dotted #333;
min-height: 500px;
font-size: small;
padding: 1.5em;
}

#header {
position: relative;
height: 2em;
width: 60em;
}

#header ul#primary {
position: absolute;
bottom: -1px;
width: 60em;
margin: 0;
padding: 0;
}

#header ul#primary li {
display: inline;
list-style: none;
}

#header ul#primary a,#header ul#primary span,#header ul#primary a.current {
width: 8em;
display: block;
float: left;
text-align: center;
font-family: verdana, sans-serif;
font-size: 85%;
text-decoration: none;
color: #333;
margin: 1px 2px 0 0;
padding: 4px 0;
}

#header ul#primary span,#header ul#primary a.current,#header ul#primary a.current:hover {
border: 1px solid #666;
border-bottom: none;
background: #FFF3B3;
padding-bottom: 6px;
margin-top: 0;
}

#header ul#primary a {
background: #FFFAE1;
border: 1px solid #AAA;
border-bottom: none;
}

#header ul#primary a:hover {
margin-top: 0;
background: #FFF7CD;
padding-bottom: 5px;
border-color: #666;
}

#header ul#secondary {
position: absolute;
bottom: -1.4em;
left: 1px;
width: 60em;
margin: 0;
padding: 0;
}

#header ul#secondary li a,#header ul#secondary li span {
width: auto;
display: block;
float: left;
border: none;
background: none;
margin: 0;
padding: 0 10px;
}

#header ul#secondary li a {
color: #06C;
text-decoration: underline;
}

#header ul#secondary li a:hover {
color: #333;
background: transparent;
border: none;
padding: 0 10px;
}

#header ul#secondary li a:active {
color: #000;
background: transparent;
}


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

oh right. i did that based purely on what you were trying to do with your own code..... :p
 
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
Joined
14 Feb 2006
Posts
4,644
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.
 
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
Joined
14 Feb 2006
Posts
4,644
Location
Surrey, UK
Ah, so you want to check if there is the "=xyz.php" bit on the end of guides?

PHP:
if( strpos( $_SERVER['QUERY_STRING'], "=" ) === false ) {
// no "=" was found in the query string
$section = $_SERVER['QUERY_STRING'];
} else {
$sspl = explode("=" , $_SERVER['QUERY_STRING'], 3);
$section = $sspl[0];
$value = $sspl[1] . ".php";
}

Putting xxx.php?guides into this will result in $section = "guides". Putting xxx.php?guides=1 will result in $section = "guides" and $value = "1.php".
 
Last edited:
Soldato
Joined
6 Feb 2004
Posts
20,675
Location
England
Which works the tabs perfectly, except in cases when it has =xxx on the end.

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:
 
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:
Soldato
Joined
6 Feb 2004
Posts
20,675
Location
England
i already explained why i changed your method. if home is '/index.php' then it will always show as active because when comparing against '/index.php?article=1', it's always going to return a match. :p

edit: i've bodged together a fix so it works how you want but it's horrible. :(

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)) {
		if($current == $mylinks['Home']) echo '<li><span>' . $key . '</span></li>';
		elseif($link == $mylinks['Home']) echo '<li><a href="' . $link . '">' . $key . '</a></li>';
		else echo '<li><span>' . $key . '</span></li>';
	} else {
		echo '<li><a href="' . $link . '">' . $key . '</a></li>';
	}
}

this only fixes the home tab always showing as actibve. it still doesn't address the issue of the code not working on your setup when you have guide=1 or whatever on your server. works fine for me.
 
Last edited:
Soldato
OP
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
I could have sworn i wrote a detailed reply earlier outlining some ideas for changing my initial layout to something else.

Has the forum been down today?

I was simply saying that i was having a total rethink of how everything is laid out, and putting the header and footer into functions, then calling them in various index files, those in sub-directories etc...

Also thinking about using arrays for the content instead of case/switch and if/else.

Thanks for all the help marc, i think i'll take some of your earlier comments and later ons onboard and change how i'm laying out the structure of the files.

Would you mind just explaining a few pieces of code for me....

$current
= $_SERVER['SCRIPT_NAME'];

What does SCRIPT_NAME translate to.
PHP website says...

'SCRIPT_NAME' Contains the current script's path. This is useful for pages which need to point to themselves. The __FILE__ constant contains the full path and filename of the current (i.e. included) file. Why is that line required?

Secondly this area.

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

'QUERY_STRING' The query string, if any, via which the page was accessed.

So what exactly are you saying in this string, if string length is more than 0? Then current is equal to the string + a question mark?

if(stristr($current, $link))

What does this line do exactly.
http://uk.php.net/manual/en/function.stristr.php

Not sure i fully understand what it's doing still though.

Just want to understand what this is doing, sorry a bit of an impatient learner, lol.... just need a break down of the code, the PHP website itself is very poor (or at least i feel) at explaining things without giving overkill or poor examples, or examples that won't apply to my uses.... :(
 
Last edited:
Soldato
Joined
6 Feb 2004
Posts
20,675
Location
England
basically all that code is a filthy hack to make your entire site work around this stupid (imho) way of loading through "index.php?jsdjsdjg=sdfsdfsdjf". it's just a royal pain in the arse to deal with.

instead i'd use

index.php //home
articles.php //articles
etc

so much easier to work with and no faffing about at all.

PHP:
$current = $_SERVER['SCRIPT_NAME'];
foreach($pages as $title => $page) {
    echo ($current == $page) ? '<li><span>' . $title . '</span></li>' : '<li><a href="' . $page . '">' . $title . '</a></li>';
}

if you don't understand this.....

PHP:
echo ($current == $page) ? '<li><span>' . $title . '</span></li>' : '<li><a href="' . $page . '">' . $title . '</a></li>';

it's just a shorter way of writing

PHP:
if($current == $page) {
    echo '<li><span>' . $title . '</span></li>';
} else {
    echo  '<li><a href="' . $page . '">' . $title . '</a></li>';
}

as for functions for headers/footers, i honestly have no idea what you're on about. :confused:

headers/footers are typically html so i'd use standard includes for those (obviously our dynamic method of generating the menu above would go in the header)

index.php
PHP:
<?php
include 'header.php';
//home page content here
include 'foot.php';
?>

articles.php
PHP:
<?php 
include 'header.php';
//articles content goes here
include 'foot.php';
?>

header.php
PHP:
<html>
<head>
//call external css/js files here
</head>
<body>
//our menu code could go here

foot.php
PHP:
//copyright notice or maybe common text/links you want at the bottom of each page
</body>
</html>

now any changes to the header/footer/css/js files will be global and will affect all pages. and creating a newpage is simply done...

newpage.php
PHP:
<?php 
include 'header.php';
//newpage content goes here
include 'foot.php';
?>

easy eh? :)

lastly, i'd be looking at mysql to store the content of articles/guides. still you haven't got that far yet. :p
 
Back
Top Bottom