Forum Moderators: coopster

Message Too Old, No Replies

Fun with elementary PHP

Looking for some inspiration to learn more

         

anax

11:46 pm on Dec 19, 2005 (gmt 0)

10+ Year Member



I run a couple of medium-sized information/education sites - no e-commerce and no database to speak of. I'm an old skool HTML hand coder, and have never really worked with any scripting languages.

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?

ramoneguru

1:04 am on Dec 20, 2005 (gmt 0)

10+ Year Member



I'd recommend PHP 5 for Dummies. Good explinations with code examples. And if you can't understand something or aren't sure what a piece of code does, just post on webmasterworld (that's what I do ahaha).

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

wsmeyer

2:46 am on Dec 20, 2005 (gmt 0)

10+ Year Member



You could practice baking cookies :)

Combined with PHP you could have one of those "new" flags appear for things that have been added since their last visit.

William.

PeteM

12:22 pm on Dec 20, 2005 (gmt 0)

10+ Year Member



Write your own form handler. Very easy to do in PHP and will teach you a lot.

proper_bo

2:48 pm on Dec 22, 2005 (gmt 0)

10+ Year Member



I think that using cookies can be very simple and rewarding.

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");

so you could for example use:

setcookie("menu","small",time() + 3600); // 3600 = one hour!

(because cookies are site specific you do not need to worry about it matching the name another site uses)

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!

whoisgregg

4:13 pm on Dec 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Php bag of tricks [webmasterworld.com] and Bag-O-Tricks for PHP II [webmasterworld.com] are good to read through. If you actually read through and experiment with the code, there is a lot to learn. :)