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! |
CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
|
|
|
|
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!
|
|
|
|
Download a free trial version of IBM Rational Developer for System i V7.1, which provides a complete development environment for traditional i5/OS application development. IBM Rational Developer for System i is a new eclipse-based workstation offering for i5/OS application development that provides a comprehensive Integrated Development Environment for edit/compile/debug of traditional RPG/COBOL/C/C++ i5/OS applications. 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!
|
|
|
|
Ken Krugler, co-founder of code search company Krugle, and Laura Merling, vice president of Marketing and Business Development for Krugle, join to talk about the ins and outs of code search and what it means as a new feature for developerWorks users. FREE! Go There Now!
|
|
|
|
Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1. 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!
|
|
|
|
The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now. FREE! Go There Now!
|
|
|
|
The discipline of assembling and delivering software is maturing beyond standard developer-centric compile/test software builds. The end-to-end software development lifecycle is emerging as the new focus moves “Beyond the Build.” Join this on demand webcast to learn about methods for streamlining software delivery and key capabilities of the IBM Rational Build Forge framework for automating build and release management in environments of any size. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |