Closed Thread Icon

Topic awaiting preservation: Simple PHP question Pages that link to <a href="https://ozoneasylum.com/backlink?for=30559" title="Pages that link to Topic awaiting preservation: Simple PHP question" rel="nofollow" >Topic awaiting preservation: Simple PHP question\

 
Author Thread
Radical Rob
Paranoid (IV) Inmate

From: Lost Angeles Kalifornia, via Hawaii....
Insane since: Jun 2001

posted posted 09-23-2008 22:20

I'm in the process of trying to setup a menu that reflects a certain style if the current page is active. Here is the code for that part...

<div id="menu">
<ul>
<li <?php if ($currentPage == 'master.php') {echo 'class="active"';} ?>> <a href="master.php"Home>Home</a></li>
<li <?php if ($currentPage == 'about.php') {echo 'class="active"';} ?>><a href="about.php" title="">About Us</a></li>
<li <?php if ($currentPage == 'sermons.php') {echo 'class="active"';} ?>><a href="sermons.php" title="">Sermons</a></li>
<li <?php if ($currentPage == 'articles.php') {echo 'class="active"';} ?>><a href="articles.php" title="">Articles</a></li>
<li <?php if ($currentPage == 'gallery.php') {echo 'class="active"';} ?>><a href="gallery.php" title="">Gallery</a></li>
<li <?php if ($currentPage == 'churches.php') {echo 'class="active"';} ?>><a href="churches.php" title="">Churches</a></li>
<li <?php if ($currentPage == 'directory.php') {echo 'class="active"';} ?>><a href="directory.php" title="">Directory</a></li>
<li <?php if ($currentPage == 'contact.php') {echo 'class="active"';} ?>><a href="contact.php" title="">Contact</a></li>
</ul>
</div>

What I'd like to do is if the current page is being viewed it disables the link.

My other issue is there will be articles in the site under the articles page. How would I have the articles with the title 092308.php cause the current page to be active.

I'm stuck and this php thing is killing me.

thanks!

DL-44
Lunatic (VI) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 09-24-2008 00:33

you have an "if", now you need an "else"
There are a lot of ways to go about it. I would get the logic out of the menu though. something along the lines of...

code:
if ($currentPage == 'master.php') {
$homelink = "<li class="active">Home</li>";
}
else {
$homelink = "<li><a href="master.php">Home</a></li>";
}



then in the appropriate place in the HTML

code:
<?php  echo $homelink; ?>



As for the other part - I would do this by having the articles.php page be the active page, and have it include the content of the article instead.
so you would link to article.php?id=092308 or the like.

(Edited by DL-44 on 09-24-2008 00:38)

Radical Rob
Paranoid (IV) Inmate

From: Lost Angeles Kalifornia, via Hawaii....
Insane since: Jun 2001

posted posted 09-24-2008 00:56

As usual DL you are a Godsend! Thanks so much.

Radical Rob
Paranoid (IV) Inmate

From: Lost Angeles Kalifornia, via Hawaii....
Insane since: Jun 2001

posted posted 09-24-2008 01:18

ok... tried it but got an error....

Here is my complete code.... please correct me if I placed something in wrong...


<div id="logo">
<img src="img/logo.jpg" alt="The Empire City ICC" width="1000" height="122" /></div>
<?php $currentPage = basename($_SERVER['SCRIPT_NAME']); ?>


<div id="menu">

<?php if ($currentPage == 'index.php') {
$homelink = "<li class="active">Home</li>";
}
else {
$homelink = "<li><a href="index.php">Home</a></li>";
}
?>

<?php if ($currentPage == 'about.php') {
$aboutlink = "<li class="active">About</li>";
}
else {
$aboutlink = "<li><a href="about.php">About</a></li>";
}
?>

<?php if ($currentPage == 'sermons.php') {
$sermonlink = "<li class="active">Sermons</li>";
}
else {
$sermonlink = "<li><a href="sermons.php">Sermons</a></li>";
}
?>

<?php if ($currentPage == 'articles.php') {
$articlelink = "<li class="active">Articles</li>";
}
else {
$articlelink = "<li><a href="articles.php">Articles</a></li>";
}
?>

<?php if ($currentPage == 'gallery.php') {
$gallerylink = "<li class="active">Gallery</li>";
}
else {
$gallerylink = "<li><a href="gallery.php">Gallery</a></li>";
}
?>

<?php if ($currentPage == 'churchess.php') {
$churchlink = "<li class="active">Churches</li>";
}
else {
$churchlink = "<li><a href="churches.php">Churches</a></li>";
}
?>

<?php if ($currentPage == 'directory.php') {
$directorylink = "<li class="active">Directory</li>";
}
else {
$directorylink = "<li><a href="directory.php">Directory</a></li>";
}
?>

<?php if ($currentPage == 'contact.php') {
$contactlink = "<li class="active">Contact</li>";
}
else {
$contactlink = "<li><a href="contact.php">Contact</a></li>";
}
?>
<ul>
<?php echo $homelink; ?>
<?php echo $aboutlink; ?>
<?php echo $sermonlink; ?>
<?php echo $articlelink; ?>
<?php echo $gallerylink; ?>
<?php echo $churchlink; ?>
<?php echo $directorylink; ?>
<?php echo $contactlink; ?>
</ul>
</div>

Suho1004
Maniac (V) Mad Librarian

From: Seoul, Korea
Insane since: Apr 2002

posted posted 09-24-2008 02:35

Grrr...
___________________________
Suho: www.liminality.org | Cell 270 | Sig Rotator | the Fellowship of Sup

(Edited by Suho1004 on 09-24-2008 02:50)

Suho1004
Maniac (V) Mad Librarian

From: Seoul, Korea
Insane since: Apr 2002

posted posted 09-24-2008 02:50

[Edit 1: OK, let's try this again. Apologies for making everyone scroll way to the right, but the only way I could prevent the numbers in square brackets from turning into links was by using the "code" tags. And no, unchecking the "Enable linkwords" checkbox before posting did absolutely nothing. Someone needs to look into that.]

[Edit2: If it makes it any easier, you can hit the edit button on this post and just copy and paste the text from there. How mind-blowingly annoying.]

For starters, what sort of error are you getting. To be honest, the code is making me dizzy... all that unnecessary opening and closing of php sections, etc.

Secondly, you have a typo: "churchess" for "churches" in the corresponding php section.

Thirdly, would you mind if I offered you a more streamlined solution? On my own website I have a very similar set-up. Actually, it's a lot more complex than what you're trying to do, so it should be even easier for you. Try this out. Put all the PHP code at the start of the document, before you even have your doctype declaration. This is not necessary, but I find that keep the logic separate, as DL put it, makes things neater (right now you have the code inside the menu). The array definition obviously should go on one line.

_____

code:
<?php

$menu_link = array("<a href=\"index.php\">Home</a>", 
"<a href=\"about.php\">About</a>", 
"<a href=\"sermons.php\">Sermons</a>", 
"<a href=\"articles.php\">Articles</a>", 
"<a href=\"gallery.php\">Gallery</a>",
"<a href=\"churches.php\">Churches</a>",
"<a href=\"cirectory.php\">Directory</a>",
"<a href=\"contact.php\">Contact</a>")

switch($currentPage)
	{
	case "index.php":
		$menu_item[0] = "Home";
		break;
	case "about.php":
		$menu_item[1] = "About";
		break;
	case "sermons.php":
		$menu_item[2] = "Sermons";
		break;
	case "articles.php":
		$menu_item[3] = "Articles";
		break;
	case "gallery.php":
		$menu_item[4] = "Gallery";
		break;
	case "churches.php":
		$menu_item[5] = "Churches";
		break;
	case "directory.php":
		$menu_item[6] = "Directory";
		break;
	case "contact.php":
		$menu_item[7] = "Contact";
		break;
	}

?>

(The following is within the HTML document, obviously)

<div id="menu">
     <ul>
          <li><?php echo($menu_item[0]); ?></li>
          <li><?php echo($menu_item[1]); ?></li>
          <li><?php echo($menu_item[2]); ?></li>
          <li><?php echo($menu_item[3]); ?></li>
          <li><?php echo($menu_item[4]); ?></li>
          <li><?php echo($menu_item[5]); ?></li>
          <li><?php echo($menu_item[6]); ?></li>
          <li><?php echo($menu_item[7]); ?></li>
     </ul>
</div>


_____

And that's it. Basically what it does is it sets up an array where everything is linked by default. Then it goes through a switch statement and changes the corresponding menu item with a non-linked item. Then you just display the menu and presto! This should work as is, but I typed it up on the spot, so it is possible a syntax error or two crept in. If you have any problems, let me know.

(Edited by Suho1004 on 09-24-2008 02:56)

(Edited by Suho1004 on 09-24-2008 02:57)


Edit: TP: here, have a couple of new lines . I'll look into it eventually. Now report in for shock treatment, please

(Edited by Tyberius Prime on 09-24-2008 09:57)

Radical Rob
Paranoid (IV) Inmate

From: Lost Angeles Kalifornia, via Hawaii....
Insane since: Jun 2001

posted posted 09-24-2008 03:56

So let me make sure I get this right...

I put the first section in a file called menu.inc.php then I put that at the top of the page with.
Then I drop the second part where the menu would go.

Tried that but I got a quirky error... http://www.caicc.net/ecicc/v2/header2.php

I'm baffled trying to figure out the code. I have a handy dandy php book with simple examples, but they leave out the complex stuff like what I'm trying to do.

Suho1004
Maniac (V) Mad Librarian

From: Seoul, Korea
Insane since: Apr 2002

posted posted 09-24-2008 09:42

You would get an unexpected T_SWITCH error if the variable in the switch statement is not defined.

[Edit: In more detail... I think what this means is that the $currentPage variable is not being defined. How are you obtaining this variable?]
___________________________
Suho: www.liminality.org | Cell 270 | Sig Rotator | the Fellowship of Sup

(Edited by Suho1004 on 09-24-2008 09:44)

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 09-24-2008 09:59

now, you'd get the 'unexpected T_SWITCH' if you're missing a ; after the array(...).
The other one would be undefined variable.

and

code:
<div id="menu">
     <ul>
          <li><?php echo($menu_item[0]); ?></li>
          <li><?php echo($menu_item[1]); ?></li>
          <li><?php echo($menu_item[2]); ?></li>
          <li><?php echo($menu_item[3]); ?></li>
          <li><?php echo($menu_item[4]); ?></li>
          <li><?php echo($menu_item[5]); ?></li>
          <li><?php echo($menu_item[6]); ?></li>
          <li><?php echo($menu_item[7]); ?></li>
     </ul>
</div>


should really read

[code]
<div id="menu">
<ul>
<?php
foreach ( $menu_item as $strMenuItem )
print '<li>'. $strMenuItem. "</li>\n";
?>
</ul>
</div>

so long

->Tyberius Prime

Suho1004
Maniac (V) Mad Librarian

From: Seoul, Korea
Insane since: Apr 2002

posted posted 09-24-2008 10:08

Ah, did I forget the semicolon after the array? ...it would appear that I did. Always check the semicolons...

And yes, a loop would be more efficient. Possibly more confusing to someone trying to learn PHP, but definitely more efficient.

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 09-24-2008 17:28

efficent for the programmer, at least.

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 09-24-2008 22:06

also, the array needs to be $menu_item not $men_link. Good stuff Suho.

I used something similar and in using something like this it makes it easy to set the current state of the link in the CSS. Which is nice for users so they know where they are

Later,

C:\

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

posted posted 09-24-2008 22:14

On a site I was maintaining I used:

code:
// $scriptNameNavEquivalent is the name of the script requested
$navArray = array
(
	"home" => "home.php",
	"articles" => "articles.php",
	"random" => "random.php",
	"links" => "links.php",
	"faq" => "faq.php",
	"goodies" => "goodies.php",
	"contact" => "contact.php",
);

echo "<ul>";
foreach( $navArray as $currentNavItem => $currentNavItemUrl )
	echo "\t\t\t<li><a href='". $currentNavItemUrl ."'". ($currentNavItem==$scriptNameNavEquivalent?" class='selected'":"") .">". $currentNavItem ."</a></li>\n";
echo "</ul>";

Of course you can remove the A altogether if you want.

Suho1004
Maniac (V) Mad Librarian

From: Seoul, Korea
Insane since: Apr 2002

posted posted 09-25-2008 02:01
quote:

CPrompt said:

also, the array needs to be $menu_item not $men_link.



Eh, good point. I knew I shouldn't have tried just typing that up on the spot. I always screw things up the first time around.

Can someone change that for me? I think it's too late to edit now.

Radical Rob
Paranoid (IV) Inmate

From: Lost Angeles Kalifornia, via Hawaii....
Insane since: Jun 2001

posted posted 09-25-2008 08:55

Wow guys I really appreciate all the input. Just got in, it's about Midnight so I'll get to tweaking tomorrow morning. But big thanks for the contributions! May not seem like alot to you guys, but for me it's HUUUGE!

Suho1004
Maniac (V) Mad Librarian

From: Seoul, Korea
Insane since: Apr 2002

posted posted 09-25-2008 10:27

Just make sure that, if you use my code (or a variation thereof), you take into account the subsequent corrections to all the stupid little mistakes I made.

Radical Rob
Paranoid (IV) Inmate

From: Lost Angeles Kalifornia, via Hawaii....
Insane since: Jun 2001

posted posted 09-25-2008 20:11

It works now, but how would I disable the link if you're on the current page? I was told that best practice would be to have the link not be active. If it's a pain, I'm not going to worry about it, but if it's relatively simple then I'll go for it.

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 09-25-2008 23:07

I'm not sure why that would be the "best practice". If anything, I like to set the current state of the menu according to what page they are on.

You can do that easily with the script Suho provided.

Just add something like $bodyClass = "home"; for the index page and so on. Then echo that in the html part. In the css, set the current state like the link above. I can give more details once I get back home if you'd like.

Later,

C:\

Radical Rob
Paranoid (IV) Inmate

From: Lost Angeles Kalifornia, via Hawaii....
Insane since: Jun 2001

posted posted 09-26-2008 03:15

The current state works, I'm fine with it being active, but I thought it optimum to deactivate current page link so it doesnt link to itself. thats all. :-)

DL-44
Lunatic (VI) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 09-26-2008 04:36

Nope, I would say not optimum.

Mark it with CSS in a way to show that it is the current section, but do not deactivate it....Ideally.

« BackwardsOnwards »

Show Forum Drop Down Menu