Who knows PHP? If else...

Merlin

Expert Member
Joined
Jan 18, 2006
Messages
2,789
Reaction score
320
Location
Cape Town, South Africa
's up all?

If you know PHP, your help would be really appreciated...

I'm muchking around with a personal site of mine which has the following layout...

index
/css
/includes
/pages

...etc.

Curently everything to the end of <body> is in a PHP include.

The problem is that in this include are the calls for the CSS file.

<link rel="stylesheet" type="text/css" media="screen, projection" title="original" href="css/original.css" />
<link rel="alternate stylesheet" type="text/css" media="screen, projection" title="contrast" href="css/contrast.css" />
<link rel="stylesheet" type="text/css" media="print" href="css/print.css" />
<link rel="stylesheet" type="text/css" media="aural" href="css/aural.css" />

My PHP is really amateurish and I'm struggling with an If Else statement.

If page = 'index', or root folder, use css/file.css. If page = in /pages use ../css/file.css

Anyone have any ideas? :(

Thanks, N.
 
You might get better responses if this was in the correct section:

http://mybroadband.co.za/vb/forumdisplay.php?f=132


Never the less, the PHP super global $_SERVER['SCRIPT_NAME'] will have the relative path and name of the php file that was requested. That will help you determine what directory your page was requested from.

Does that help?
 
Take out the if..else statement and simply use absolute paths to your stylesheets.

eg.
<link rel="stylesheet" type="text/css" media="screen, projection" title="original" href="http://www.domain.com/css/original.css" />
<link rel="alternate stylesheet" type="text/css" media="screen, projection" title="contrast" href="http://www.domain.com/css/contrast.css" />
<link rel="stylesheet" type="text/css" media="print" href="http://www.domain.com/css/print.css" />
<link rel="stylesheet" type="text/css" media="aural" href="http://www.domain.com/css/aural.css" />

Or shorthand absolute paths:
<link rel="stylesheet" type="text/css" media="screen, projection" title="original" href="/css/original.css" />
<link rel="alternate stylesheet" type="text/css" media="screen, projection" title="contrast" href="/css/contrast.css" />
<link rel="stylesheet" type="text/css" media="print" href="/css/print.css" />
<link rel="stylesheet" type="text/css" media="aural" href="/css/aural.css" />
 
Franna is on the money. The / (slash) in front of the path forces the absoloute path from the site root to be used, instead of relative path to the document.
 
Thanks everyone,

I sat down and thought about the design and changed the code behind the site considerably...making it simpler in the long run.

As such this is no longer necessary, but I'm pulling my hair out over other issues...

It's a big one, sit down...

---

...the code sitting in the Head, which gathers the filename.ext currently, but needs to drop the .ext

The array also sits here.

HEAD said:
<?php

$currentFile = $_SERVER["SCRIPT_NAME"];
$parts = Explode('/', $currentFile);
$currentFile = $parts[count($parts) - 1];



$array = array ( $names[1],$names[2],$names[3],$names[4],$names[5],$names[6],$names[7],$names[8] );

$names[0] = "news";
$names[1] = "about";
$names[2] = "happenings";
$names[3] = "images";
$names[4] = "forum";
$names[5] = "gear";
$names[6] = "ride";
$names[7] = "skills";
$names[8] = "tech";

?>

...the code used to add /pages to each page other than 'news' as they sit in a subdir.

index.php
/pages

BODY said:
<?php

$switch = "$array";

if ( $switch == "$array" ) {
echo "pages/";
} else {
echo "none";
}

?>

...each page will hopefully be called by URL/?<name>

For simplicity's sake down the line, it would be nice to use the aforementioned array, in order to avoid changing 'names' throughout the code.

'.txt' must be added to the names now.

<?php

switch($_SERVER['QUERY_STRING']) {

case 'news':
include( $names[0] . ".txt" );
break;

case 'about':
include( $names[1] . ".txt" );
break;

case 'happenings':
include( $names[2] . ".txt" );
break;


case 'images':
include( $names[3] . ".txt" );
break;

case 'forum':
include( $names[4] . ".txt" );
break;

case 'gear':
include( $names[5] . ".txt" );
break;

case 'ride':
include( $names[6] . ".txt" );
break;

case 'skills':
include( $names[7] . ".txt" );
break;

default:
include( $names[0] . ".txt" );
}

?>

...the following is working nicely, but obviously we want to add ?<name> to each arrayed pagename automatically.

EG auto-generated ?name

<li<?php if ($currentFile=="$pages[0]") echo " class=\"active\""; ?>><a accesskey="n" href="<?php echo "$pages[0]"; ?>" title="home"><span class="active"><em>n</em>ews</span></a></li>
<li<?php if ($currentFile=="$pages[1]") echo " class=\"active\""; ?>><a accesskey="a" href="<?php echo "$pages[1]"; ?>" title="about"><em>a</em>bout</a></li>
<li<?php if ($currentFile=="$pages[2]") echo " class=\"active\""; ?>><a accesskey="h" href="<?php echo "$pages[2]"; ?>" title="happenings"><em>h</em>appenings</a></li>
<li<?php if ($currentFile=="$pages[3]") echo " class=\"active\""; ?>><a accesskey="i" href="<?php echo "$pages[3]"; ?>" title="images"><em>i</em>mages</a></li>
<li<?php if ($currentFile=="$pages[4]") echo " class=\"active\""; ?>><a accesskey="f" href="<?php echo "$pages[4]"; ?>" title="forum"><em>f</em>orum</a></li>
<li<?php if ($currentFile=="$pages[5]") echo " class=\"active\""; ?>><a accesskey="g" href="<?php echo "$pages[5]"; ?>" title="gear"><em>g</em>ear</a></li>
<li<?php if ($currentFile=="$pages[6]") echo " class=\"active\""; ?>><a accesskey="r" href="<?php echo "$pages[6]"; ?>" title="ride"><em>r</em>ide</a></li>
<li<?php if ($currentFile=="$pages[7]") echo " class=\"active\""; ?>><a accesskey="s" href="<?php echo "$pages[7]"; ?>" title="skills"><em>s</em>kills</a></li>
<li<?php if ($currentFile=="$pages[8]") echo " class=\"active\""; ?>><a accesskey="t" href="<?php echo "$pages[8]"; ?>" title="tech"><em>t</em>ech</a></li>

Obviously these all tie in each other somehow but I am really struggling to pull it together.

I do respect that it's a lot to advise on so any comments/help/direction on it is more than appreciated.

Thanks, N.
 
Can we access the site somewhere, in order to see how it looks, and to get a better idea of what you would like to accomplish?
 
I don't unfortunately have PHP hosting at the moment.

It's running on a LAMP module on a local PC.

I've left the damned thing on my USB stick at work...I'll see if I can organize a mate with a 'site to host it temp on Monday, or will post the source here for you.

Thanks, N.
 
Dude, without really knowing what you're trying to achieve, we're all probably going to be throwing stones in the dark here. Here's my stones - should help you some of the way, but if I'm offtrack then you're gonna have to give us at least something more - like a description of exactly what it is you want to achieve:

Code:
<?
// for testing only:
$pages = explode(",","news,news,about,about,news,gear,ride,ride,gear,tech");

function checkWhichItem($MyItem){
	$names[0] = "news";
	$names[1] = "about";
	$names[2] = "happenings";
	$names[3] = "images";
	$names[4] = "forum";
	$names[5] = "gear";
	$names[6] = "ride";
	$names[7] = "skills";
	$names[8] = "tech";
	for ($x=0;$x<count($names);$x++){
		if (in_array($MyItem,$names)){
			if ($MyItem == $names[$x] && $currentFile == (count(explode('/',$_SERVER["SCRIPT_NAME"])) - 1)){
				print "class=\"active\";";
			} else {
				print "class=\"inactive\";";
			}
		} else {
			return false;
		}
	}
}
?>
<ul>
<?
for ($y = 0; $y< count($pages); $y++){
        ?><li <? checkWhichItem($pages[$y]); ?>><a accesskey="n" href="<? echo "$pages[$y]"; ?>" title="<? echo "$pages[$y]"; ?>">
        <span class="active"><em><? echo substr($pages[$y],0,1)?></em><? echo substr($pages[$y],1)?></span></a></li>
<?
}
?>
</ul>
 
Last edited:
Wow! Thanks Bobby.

That's shown me just how little of PHP I do know. :|

I'm still trying to get the site online for you guys...will let you know when I do.

Will give that code a try a bit later on.

Thanks, N.
 
Top
Sign up to the MyBroadband newsletter
X