Dynamic SSI...

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

Post Reply
ebbatten
Forum Newbie
Posts: 11
Joined: Thu May 18, 2006 2:20 pm

Dynamic SSI...

Post by ebbatten »

Hello,

I have an include file, x.inc.php. In it, I have a table with a menu full of navigation links set up.

Is there a way to make the links dynamic in the include file so that when it is called by the parent page, each link bolds separately so that the user knows which page they are on?

I tried the following:


<?php
if ( $PAGE_TITLE == "Resources: Books" ) {
echo "<strong>";
} else {
echo "";
} ?>
<a href="/resources/books/index.php">Books</a>

<?php
if ( $PAGE_TITLE == "Resources: Books" ) {
echo "</strong>";
} else {
echo "";
} ?>
This bolds every link when the inc. is called

The following Doesn't work at all:
if ($PAGE_TITLE = "Resources: Books") echo '<strong>';

??????

Thanks!
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Do you think that $PAGE_TITLE is a super global which holds the current page's title, or you just like to use capital letters for you variables names?
ebbatten
Forum Newbie
Posts: 11
Joined: Thu May 18, 2006 2:20 pm

Post by ebbatten »

It's the way the site is programmed. I like to stay consistent. It is used throughout the site, however.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What I have done in the past to do this was to store the sites content in a database and have the content be relative to a page name. Then when the user lands on a certain page, use the basename() PHP function to find the name of the page and as you are looping through the links from the database, find the link whose page is the same as the one you are on and bold it.

It is a pretty easy thing to do, you just need to carefully think of the logic in the display process.
ebbatten
Forum Newbie
Posts: 11
Joined: Thu May 18, 2006 2:20 pm

Post by ebbatten »

Thanks...

Not capable of doing the database part yet. Will keep trying.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I guess the next question would be, how are you displaying the links? Are they in an array? Maybe post a little bit of code and we can see if we can't point you in the right direction.
ebbatten
Forum Newbie
Posts: 11
Joined: Thu May 18, 2006 2:20 pm

Post by ebbatten »

Well, I'm posting the links in a table. Nothing Complicated.

<td valign="middle" class="x">
<?php
if ( $PAGE_TITLE == "y" ) {
echo "<strong>";
} else {
echo "";
}
?>
<a href="/z/index.php">Link Name</a><?php

if ( $PAGE_TITLE == "y" ) {
echo "</strong>";
} else {
echo "";
}
?>

</td>

Does this give you the answer to the question you are asking?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Not exactly. Where are the links coming from? Are they hard coded into your script...

Code: Select all

<a href="lik1.php">Go to link 1</a><br />
<a href="lik2.php">Go to link 2</a><br />
<a href="lik3.php">Go to link 3</a><br />
<a href="lik4.php">Go to link 4</a><br />
<a href="lik5.php">Go to link 5</a><br />
Or are they generated dynamically through a loop of some sort...

Code: Select all

<?php
foreach ($link_array as $href => $text)
{
    echo "<a href=\"$href.php\">$text</a><br />";
}
?>
In order to do what you want you are going to need to dynamically generate your links so you can check each one against your current page name. If it checks, then bold it. Otherwise, let it ride.

On a side note, can you wrap your posted code in the appropriate "Syntax" BBCode tags? Thanks. It helps us all see your code a little better.
ebbatten
Forum Newbie
Posts: 11
Joined: Thu May 18, 2006 2:20 pm

Post by ebbatten »

The links are coming from an inc. file. In the inc. file that has the table with the links, I have:

Code: Select all

<td valign="middle" class="inthissection">
		 <?php if ( $PAGE_TITLE == "x" ) {
   echo  "<strong>";
} else {
    echo "";
} ?>
         <a href="/resources/index.php">x</a>          <?php if ( $PAGE_TITLE == "x" ) {
    echo "</strong>";
} else {
    echo "";
} ?>

</td>

<td valign="middle" class="inthissection">
		 <?php if ( $PAGE_TITLE == "y" ) {
   echo  "<strong>";
} else {
    echo "";
} ?>
         <a href="/facts/index.php">y</a>          <?php if ( $PAGE_TITLE == "y" ) {
    echo "</strong>";
} else {
    echo "";
} ?>

</td>
The index page calls the include file: <?php include('resources_other.inc.php');?>
I know that this is causing the bolding to occur on some of the links. So it's writing the HTML, just not selectively.

Is this an any better explanation? Thanks for your patience! :?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

My personal opinion... this is an horribly inefficient way of looping links.

My typicaly process is to database as much as I can, then pull what I need, loop through the result and match as needed. For what you are doing, I might step it down to an array of links as opposed to a db resultset, but I would still be inclined to loop through them and match as needed.

Code: Select all

<?php
$link_array = array(
	'about' => 'About Us',
	'contact' => 'Contact Us',
	'info' => 'More Info',
	'address' => 'Our Location');

$this_page = basename($_SERVER['PHP_SELF']);
	
foreach ($link_array as $href => $text)
{
    if ($href == $this_page)
	{
		echo "<a href=\"$href.php\"><strong>$text</strong></a><br />";
	}
	else
	{
		echo "<a href=\"$href.php\">$text</a><br />";
	}
}
?>
ebbatten
Forum Newbie
Posts: 11
Joined: Thu May 18, 2006 2:20 pm

Post by ebbatten »

So I would build this as the include file? Or forget that altogether? Or,

Build the array in a database, but every link in it, and put the php code in each index.php page?

Thank you!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You can include the code or hard code it into your page. I lean more toward modular development, so I would probably put these into a function or include file and call it.
ebbatten
Forum Newbie
Posts: 11
Joined: Thu May 18, 2006 2:20 pm

Post by ebbatten »

i'll give it a shot and let you know how it goes...thank you.
Post Reply