Page 1 of 1

Dynamic SSI...

Posted: Thu May 18, 2006 2:31 pm
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!

Posted: Thu May 18, 2006 2:49 pm
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?

Posted: Thu May 18, 2006 2:53 pm
by ebbatten
It's the way the site is programmed. I like to stay consistent. It is used throughout the site, however.

Posted: Thu May 18, 2006 2:58 pm
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.

Posted: Thu May 18, 2006 3:08 pm
by ebbatten
Thanks...

Not capable of doing the database part yet. Will keep trying.

Posted: Thu May 18, 2006 4:09 pm
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.

Posted: Fri May 19, 2006 9:19 am
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?

Posted: Fri May 19, 2006 10:20 am
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.

Posted: Fri May 19, 2006 11:35 am
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! :?

Posted: Fri May 19, 2006 12:08 pm
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 />";
	}
}
?>

Posted: Fri May 19, 2006 12:14 pm
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!

Posted: Fri May 19, 2006 1:07 pm
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.

Posted: Fri May 19, 2006 1:19 pm
by ebbatten
i'll give it a shot and let you know how it goes...thank you.