Forum Moderators: coopster
In the last week or two I've learned to do a little elementary PHP scripting to support a common sidebar now used throughout the sites. My learning source was the "Keeping Navigation Current With PHP" article on AListApart, which was very helpful; it shows how to create a <? php $thispage="youarehere";? > variable that will alter the sidebar as needed on each page.
So, I'm wondering if anyone has any other elementary PHP-based features that might be fun to learn/add to a basically static site. At the moment I'm not thinking of any database-related work; just looking for simple things to get me to the next level.
I realize this is a vague question, but since I don't know the capabilities well, I'm stuck with being vague. The "Keeping Navigation Current" article was something I hadn't realized you could do this way, so that's maybe an example of the kind of thing I'm looking for.
Also, is there a good PHP book for beginners that people can recommend?
Scripting lets you do things automatically. Example: say I wanted to email 900 people at once, but I want to make the email seem personalized like "Hay <someone's name>, how's it going I'm having a little get together, you wanna come over?"
You could mail all 900 seperately or use the PHP mail function in a loop that mails it out to a list of people's name. Less than like 12 lines of code (depending on style/comments of course).
Simple example, but when you make a database driven site you'll see why PHP is cool :-)
--Nick
For this example I will use a menu system as that is what you have used, although anything could be substituted for this.
To set a cookie in php you simply say:
setcookie("Cookiename","cookievalue","timecookielasts");
setcookie("menu","small",time() + 3600); // 3600 = one hour!
then to check for our cookie we only need use (php4 onwards)
if($_COOKIE['menu'] == 'small'){ //do stuff };
this could be used to let the users of the site pick between a large or small menu.
the link to allow them to choose need only be on the index page. it could also link back to the index page to do the cookie setting. something like index.php?setmenu=small would do the trick
so using the simplest of 'if' statements we will put all the code together:
index.php link for users: (we use a big menu as default here)
<?php
if($_COOKIE['menu'] == 'small'){
print '<a href="index.php?setmenu=big">big menu please</a>';
}else{
print '<a href="index.php?setmenu=small">small menu please</a>';
}?>
the code to set the cookie in index.php (put this at the very top of the page before any html)
<?php
if($_REQUEST['setmenu'] =='small'){
setcookie("menu","small"); //no timeout
}elseif($_REQUEST['setmenu == 'big'){
setcookie("menu",""); //no value as big is default
}?>
then where your menu file will be included in the code of all your pages:
<?php
if($_COOKIE['menu'] == 'small'){
include('menusmall.html');
}else{
include('menu.html');
}?>
hope that this isn't to complicated. If you can use includes then you should be able to grasp simple 'if' 'else' and cookies.
good luck!