Is there a better way for this switch statement?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Is there a better way for this switch statement?

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$pages = array(
  'index' => 0,
  'taf' => 1,
  '...' => ...,
);
if (!isset($pages[$page_name])) {
  $i = -1;
} else {
  $i = $pages[$page_name];
}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

It would help if you told us where $page_name comes from and what $i is used for?
(#10850)
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post 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";
}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Ok ... now I am even more confused! What are $classes[2] through $classes[9] used for?
(#10850)
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Moved to PHP code
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post 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 :oops:
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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";
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Before the loop starts, echo out $page_name and see what it shows.
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post 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.
Post Reply