Loading...


Ask A Question?
Posted in Computers & Tech / Programming / Scripting / Miscellaneous Scripting Langua..
Author: caronthegerman Total-Replies: 14


Hey guys,

I've been trying to make a random image rotator that changes the url specifically so that the visitor can save the picture as a favorite or bookmark. All those I have tried will only show a random picture but the url continually remains at www.***.com/rotate.php or others. I know I could make a php script that chooses randomly from many urls but that would mean I have to create an html file for every single one of my 150+ pictures that I want to rotate through

Thanks for any help,
Caron

Thu Aug 12, 2010    Reply    New Discussion   
 

Posted in Computers & Tech / How-To's and Tutorials / Programming / PHP
Author: vujsa Total-Replies: 15


Overview:


Frequently people ask about Content Management Systems here and are looking for advice regarding which CMS would work best for them. Often, the user requesting the help doesn't realize that they can design their own CMS that will provide them with the results they are looking for without needing to install a bulky program. This usually stems from the relatively few features that users want from their CMS. Many users just want an easy way to manage their website and edit their content.

In this tutorial we'll discuss a very simple way to create a template driven CMS. While the system will require a new template for each page in the website. The method describe here will utillize the PHP include function. The include function has a very simple syntax to it and is shown as follows:

CODE

<html>
<head>
<title>My CMS</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" style="font-family: verdana;">
<table width="100%">
<tr>
<td colspan="2" bgcolor=silver>
<?php include("header.php"); ?> <!-- Used for Banner Advertising etc... -->
</td>
</tr>

<tr>
<td width="150" bgcolor=red valign="top">
<?php include("menu.php"); ?> <!-- Used for The Main Menu... -->
</td>

<td bgcolor=navy>
<?php include("main.php"); ?> <!-- Could be left out and actual content inserted instead. -->
</td>
</tr>

<tr>
<td colspan="2" bgcolor=purple>
<?php include("footer.php"); ?> <!-- Banner Ads, Copyright Info., etc... -->
</td>
</tr>
</table>
</body>
</html>


header.php

CODE

function make_menu($name, $url) { // Name and declare the function and assign variables to the arguments passed by the call.
$link = "<a href=\"http://" . $url . "\" style=\"font-size: 8pt;\">&nbsp;&diams;&nbsp;&nbsp;" . $name . "</a><br>\n"; // Assign a variable to hold your output until returned to the main script.
return $link; // Return the output data held in the variable $link to the place in the main script where it was called from.
}


Okay, there is our function that takes simple information and creates the menu link.
But wait a minute, the function was supposed to handle the menu header as well. We could write a new function just for the menu headers but instead we'll modify our existing function to handle the headers. First lets look at the function call:

CODE

function make_menu($name, $url) { // Name and declare the function and assign variables to the arguments passed by the call.
if ($url == "header") {
$link = "<span style=\"font-color: lime;\">" . $name . "</span><br>"; // Assign a variable to hold your menu header until returned to the main script.
}
else {
$link = "<a href=\"http://" . $url . "\" style=\"font-size: 8pt;\">&nbsp;&diams;&nbsp;&nbsp;" . $name . "</a><br>\n"; // Assign a variable to hold your menu link until returned to the main script.
}
return $link; // Return the output data held in the variable $link to the place in the main script where it was called from.
}


Now we have a function that will check and see if a header or link will be generated and output the required HTML. Now how do we use the call properlly? Any place you want to have a menu item or title shown, just drop in the function call with the correct arguments. As shown in our new menu.php file:

CODE

<?php
function make_menu($name, $url) {
if ($url == "header") {
$link = "<span style=\"font-color: lime;\">" . $name . "</span><br>";
}
else {
$link = "<a href=\"http://" . $url . "\" style=\"font-size: 8pt;\">&nbsp;&diams;&nbsp;&nbsp;" . $name . "</a><br>";
}
return $link;
}
?>

<?php echo make_menu("Search Engine", "header"); ?>
<?php echo make_menu("Alta Vista", "www.altavista.com"); ?>
<?php echo make_menu("Excite", "www.excite.com"); ?>
<?php echo make_menu("Google", "www.google.com"); ?>
<?php echo make_menu("Lycos", "www.lycos.com"); ?>
<?php echo make_menu("Yahoo", "www.yahoo.com"); ?>


And there we go, all done. While it seems to be more work than you would normally do, it will eventually save a lot of time in editing and reading. Imagine that your menu is rather large like this version of menu.php:

CODE

<?php
function make_menu($name, $url) {
if ($url == "header") {
$link = "<span style=\"font-color: lime;\">" . $name . "</span><br>";
}
else {
$link = "<a href=\"http://" . $url . "\" style=\"font-size: 8pt;\">&nbsp;&diams;&nbsp;&nbsp;" . $name . "</a><br>";
}
return $link;
}
?>

<?php echo make_menu("Search Engine", "header"); ?>
<?php echo make_menu("Alta Vista", "www.altavista.com"); ?>
<?php echo make_menu("Excite", "www.excite.com"); ?>
<?php echo make_menu("Google", "www.google.com"); ?>
<?php echo make_menu("Lycos", "www.lycos.com"); ?>
<?php echo make_menu("Yahoo", "www.yahoo.com"); ?>
<hr>
<?php echo make_menu("Public Forums", "header"); ?>
<?php echo make_menu("AstaHost", "www.astahost.com/index.php"); ?>
<?php echo make_menu("Trap17", "www.trap17.com"); ?>
<?php echo make_menu("AntiLost", "www.antilost.com"); ?>
<hr>
<?php echo make_menu("Online References", "header"); ?>
<?php echo make_menu("Dictionary", "www.dictionary.com"); ?>
<?php echo make_menu("Thesaurus", "www.thesaurus.com"); ?>
<?php echo make_menu("Maps", "www.mapquest.com"); ?>
<?php echo make_menu("Phone", "www.sbc.com"); ?>
<hr>


Now that is much easier to manage than an HTML encoded menu that would take more lines to write and if you want to change the bullet used before the link, then with this system, you only need to change the one bullet in the function instead of every menu item. Major time saver especially if the new bullet doesn't look good and you need to try several others before get the right one.

Now looking at the newest version of the menu.php file, you can start to see the beginnings of a simplified database. I'll post more information about converting this function to use an array then a flat file database. In the end, I hope to show how to build the menu from an SQL database table.

Additionally, I'll try to add information that will add more options to your starter template set and more management tools. Also, I'll show how use a single template for the entire website instead of the same template for each page. This will really get your website working for you instead of you working for your website.

Hope everyone gets some use from this.

Happy Programming ;)
vujsa

Notice from vujsa:
Edited a few minor PHP errors to be correct. I never tested the scripts prior to posting the tutorial. Only three bugs fixed. Next time I won't just do the programming in my head.

Mon Aug 29, 2005    Reply    New Discussion   
 

Ask a Question (w/o registration) to get Quick Answers!


astaHost