Mega Code Archive

 
Categories / Php / Site Navigation
 

Website Navigation via PHP

<?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 "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; $lastcategory = $link_type; } if ($link_caption != "") { print " [ "; for ($i = (15 - strlen($link_caption)) / 2; $i > 0; $i = $i - 1) { print "&nbsp;"; } print "<a href='$link_location' class='menu'>"; print "$link_caption</a>"; for ($i = (15 - strlen($link_caption)) / 2; $i > 0; $i = $i - 1) { print "&nbsp;"; } if ((15-strlen($link_caption)) % 2 == 0) print "&nbsp;"; } 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") );