This PHP script exists soley so that I don't have to change dozens of pages whenever I change the location of a page, or remove it from my website. It reads the internal links in from a MySQL database (soon to be converted to use the PEAR framework), and lists the links by category wherever you call the function from.
I know this code is somewhat sloppy, but it's a first attempt at PHP so... comment away.
Also I have a css entry for my menu items set up, hence the class="menu".
Take a look at http://www.DanielGrantTaylor.com for a short view of this code in action. It creates the menu at the bottom of every page.
By : mulciber316
<?php
function printconstantlinks()
{
// Variables:
$lastcategory = FALSE; // The last link category seen. Cheat a bit here to say if we've seen a link category yet.
$serverlocation = "localhost";
$username = "mywebsiteusername";
$password = "mywebsiteuserpassword";
$db_name = "mywebsitedatabase";
// Now we want to display all the links we want on every page,
// Get a connection to the database first.
$conn = mysql_connect($serverlocation, $username, $password)
or die("Could Not Connect To Server");
mysql_select_db($db_name) or die("Could not use database for website");
// Now, we want to order our links by their category, and then their caption.
$query = "SELECT link_type, link_caption, link_location FROM internal_links ORDER BY link_type, link_caption";
$result = mysql_query($query) or die("Query failed.");
print "<p class=\"menu\">";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
extract($line);
if ($lastcategory != $link_type)
{
if ($lastcategory)
print "<br />\n";
if ($link_type != "Navigation")
print "$link_type: ";
else
print " ";
$lastcategory = $link_type;
}
if ($link_caption != "")
{
print " [ ";
for ($i = (15 - strlen($link_caption)) / 2; $i > 0; $i = $i - 1)
{
print " ";
}
print "<a href='$link_location' class='menu'>";
print "$link_caption</a>";
for ($i = (15 - strlen($link_caption)) / 2; $i > 0; $i = $i - 1)
{
print " ";
}
if ((15-strlen($link_caption)) % 2 == 0)
print " ";
}
else
{
print " [ ";
print "<a href='$link_location' class='menu'>";
print "$link_location</a>";
}
print "</a> ]";
}
print "<br /></p>\n";
mysql_free_result($result);
mysql_close($conn);
}
?>
///////////////////////////////////////////////
#MySQL syntax to create internal_links table:
CREATE TABLE internal_links (
_handle INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
link_caption TEXT,
link_location TEXT NOT NULL,
link_type ENUM("Business", "Personal", "Projects", "Scholastic", "Navigation")
);
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Site Navigation Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Visit IBM developerWorks to download a free trial of the latest release of IBM Lotus Sametime Standard V8.0. Lotus Sametime Standard V8.0 is a platform for unified communications and collaboration that combines security features with an extensible, open solution including integrated Voice over IP, geographic location awareness, mobile clients, and a robust Business Partner community offering telephony and video integration. FREE! Go There Now!
|
|
|
|
In this tutorial, you can learn how to install and configure the IBM Rational Asset Manager Eclipse client, explore the different views in the Asset Management perspective, learn various search techniques, work with existing assets, and submit a new asset. FREE! Go There Now!
|
|
|
|
This tutorial shows new users of IBM WebSphere Business Monitor Version 6.0.2 how to perform the "Hello World" equivalent for monitoring business process applications. It is intended to help you get familiar with the capabilities of the product. FREE! Go There Now!
|
|
|
|
Manage, govern, and share services across your organization by using WebSphere Service Registry and Repository. Follow the hands-on exercises to learn how to navigate the Web interface to publish, find, reuse, and update services. FREE! Go There Now!
|
|
|
|
As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br /> FREE! Go There Now!
|
|
|
|
This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today! FREE! Go There Now!
|
|
|
|
Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered! FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for process. The SOA Sandbox for process focuses on providing a trial environment with the necessary tooling and components required to gain a better understanding of business processes and how to best improve existing business processes to derive value quickly. FREE! Go There Now!
|
|
|
|
In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |