Page 1 of 2
Is there a better way for this switch statement?
Posted: Wed Jun 13, 2007 2:51 pm
by smudge
Code: Select all
switch($page_name){
case 'index':
$i=0;
break;
case 'taf':
$i=1;
break;
case 'wwb':
$i=2;
break;
case 'hwb':
$i=3;
break;
case 'loc':
$i=4;
break;
case 'email':
$i=5;
break;
case 'jobopps':
$i=6;
break;
case 'events':
$i=7;
break;
case 'voc':
$i=8;
break;
case 'mostwanted':
$i=9;
break;
default:
$i=-1;
break;
}
Maybe something like:
Code: Select all
$pages['index']=0;
$pages['taf']=1;
//...
Just an idea. Trying to get the most efficient means of doing this.
Posted: Wed Jun 13, 2007 2:56 pm
by Weirdan
Code: Select all
$pages = array(
'index' => 0,
'taf' => 1,
'...' => ...,
);
if (!isset($pages[$page_name])) {
$i = -1;
} else {
$i = $pages[$page_name];
}
Posted: Wed Jun 13, 2007 3:30 pm
by Christopher
It would help if you told us where $page_name comes from and what $i is used for?
Posted: Wed Jun 13, 2007 8:17 pm
by smudge
$page_name is the name of the file without path or extension.
There is an array of 10 empty strings.
$i is a reference to one of the entries, which is changed to a css class of 'curPage', then i echo a large block of links and one of them has the curPage class. for example:
Code: Select all
switch($page_name){
...
}
$classes=array("","","","","","","","","","");
if($i>=0){
$classes[$i]=" curPage";
echo "
<div>
<a href='index.php' class='{$classes[0]}'>Index</a>
<a href='taf.php' class='{$classes[1]}'>TAF</a>
etc...
</div>";
} else {
echo "Error";
}
Posted: Wed Jun 13, 2007 9:39 pm
by Christopher
Ok ... now I am even more confused! What are $classes[2] through $classes[9] used for?
Posted: Wed Jun 13, 2007 10:02 pm
by smudge
2-9 are for other links like for index.php and taf.php, so $classes[2] is for <a href='wwb.php' class='{$classes[2]}'>wwb</a>, and 3 is a link to hwb.php, 4 is to loc.php, etc.
Posted: Thu Jun 14, 2007 2:06 am
by Christopher
Why do you have $classes and $i? It seems like all you need is $page_name and just create file and class names from the parameter value. Convention over configuration.
Posted: Thu Jun 14, 2007 3:13 am
by JayBird
Moved to PHP code
Posted: Thu Jun 14, 2007 2:45 pm
by smudge
this is for a link bar on the side of a page, and the class curPage highlights the link on the sidebar, denoting where in the website you are. It's the same linkbar for all pages it's included in, but the actual display differs by a css class, then only depending on which page you are actually looking at. the only other option I can think of is to have css selectors like so:
Code: Select all
a[href="index.php"]{
<? echo $index_style ?>
}
a[href="taf.php"]{
<? echo $taf_style ?>
}
/* same for the rest of the links */
And $x_style is just a string with various styles that changes based on the page you're on.
By the way, I just noticed that i never really had a question on the OP except for the title. Sorry about that

Posted: Thu Jun 14, 2007 3:05 pm
by smudge
ok, I've worked on it a little more, and this is what i've got:
Code: Select all
$classes = array('index'=>'','taf'=>'','wwb'=>'','hwb'=>'','loc'=>'','email'=>'','jobopps'=>'','events'=>'','voc'=>'','mostwanted'=>'');
$classes[$page_name]=' curPage';
then i out put like so:
Code: Select all
echo "<a href='index.php' class='{$classes['index']}'>index</a>";
same goes for all the other links.
Posted: Thu Jun 14, 2007 3:19 pm
by superdezign
You could do it more along the lines of:
Code: Select all
$pages = array('Home' => 'index.php', 'Page 1' => 'somePage.php');
foreach($pages as $pageName => $fileName)
{
echo '<a href="' . $fileName . '"';
echo ($pageName == $currentPageName) ? ' class="current">' : '>';
echo $pageName . '</a>' . "\n";
}
Posted: Thu Jun 14, 2007 3:48 pm
by RobertGonzalez
Is the styling for each page link different for each page that is current? Normally what I do is create a class called 'current', then as the nav is built, I check the output of the nav (which I keep in a database) against the current page name (
dirname(__FILE__)) and if it matches inside the nav loop, I assign the class 'current' to that link.
Is this what you are looking to do?
Posted: Fri Jun 15, 2007 6:28 pm
by smudge
Ok, so I've taken super's code and modified it to fit my needs:
Code: Select all
$pages=array(
'Home'=>'index',
'Tell A Friend'=>'taf',
...);
//The array continues in this fashion with the same values as in above posts
foreach($pages as $link_text => $name)
{
echo '<a href="' . $name . '_pc.php"';
echo
(($name!=$page_name) && ($name=='index'))?
' class="firstLink">':
(($name==$page_name) && ($name=='index'))?
' class="firstLink curPage">':
(($name==$page_name) && ($name!='index'))?
' class="curPage">': //this is the only condition being applied
'>';
echo $link_text . '</a>' . "\n";
}
echo '</div>';
The problem though, is that it highlights the 'index' link no matter what page I'm on, and when I look at the html source, the only class assigned to any of the links is 'curPage' to 'index'. It's not the extra lines in the conditionals is it? They're just there to look neater and help me think a little better. Basically, I just want it to give the link 'firstLink' if it's 'index' or 'firstLink curPage' if it's the index link and you're on index, or 'curPage' if your on anything else. The css dictates that firstLink gets a border all the way around and anything else in the nav bar gets a border everywhere except the top.
Any ideas on why this isn't working? I'm not coming up with anything. I'll try if statements while I wait for a reply.
Posted: Fri Jun 15, 2007 6:39 pm
by RobertGonzalez
Before the loop starts, echo out $page_name and see what it shows.
Posted: Fri Jun 15, 2007 7:50 pm
by smudge
hmmm...
Yeah, ok, for whatever reason, there was suddenly a directory on $page_name, but I fixed that. Now, if you're on any other page than home, it highlights both that page's link and the index link.