The legal tricks-Learn Your Self

Latest gadgets,softwares,hardware,reviews,programming and campuses, game cheats ext......

Creating a menu system - PHP tutorials

In this tutorial I will show you how to create a simple menu system with 2 levels. You can easy integrate it into your site to get a nice and easy editable site navigation system.

Step 1.

To make a menu system easy changeable and easy readable we will separate the code into more parts. A basic realization contains 3 files which are the following:
menuStruct.php: Contains only the navigation structure. It will be used by the handler function.

menuHandler.php: This file contains the PHP code which generates the menus with corresponding submenus as well.

index.php: The main page which integrates the navigation system.

Step 2.

First let's design the menu system structure. To handle the menu element easy we will use a multi dimensional array. In the array the key will be the name of the menu and the value is the link for it. So we can define the main menu system as follows:

Code:
// Main menu items
$mainMenu['Home'] = 'link-1';
$mainMenu['About us'] = 'link-2';
$mainMenu['Projects'] = 'link-3';
$mainMenu['Contact'] = 'link-4';
$mainMenu['Others'] = 'link-5';
?>


Now we have all the main menu items defined. However usually we need some submenus as well. To sore it in the array we will create a new array which stores the main menu - submenu relations. As you will see here we use a little bit more complex array to store information. You have to take care that the keys - key strings - in the main array and in the sub array must be identical. So the submenu array looks like this:

Code:
// Sub menu items
$subMenu['Projects']['SubMenu-1'] = 'sub-link-1';
$subMenu['Projects']['SubMenu-2'] = 'sub-link-2';
$subMenu['Projects']['SubMenu-3'] = 'sub-link-3';
$subMenu['Projects']['SubMenu-4'] = 'sub-link-4';

$subMenu['Others']['SubMenu-1'] = 'sub-link-11';
$subMenu['Others']['SubMenu-2'] = 'sub-link-12';
$subMenu['Others']['SubMenu-3'] = 'sub-link-13';
?>


At the end store this information in a file let's named it to menuStruct.php.

Step 3.

After you stored the menu structure in a file we can focus on the application logic. We will implement it in the second file
named menuHandler.php. In this file we will implement only one function called createMenu(). This function will have only one parameter which tells what is the actual page link. From this information the function will decide whether or not to display relevant submenus. So the final result is this:

Code:
function createMenu($actLink){

}
?>


Now let's start the most important part. First the function needs to include the menuStruct.php file to access the menu arrays. As second we open a table tag where we will display the menu elements as rows (tr tags).

As next step we check where the input parameter - which is the actual link - can be found in the arrays. It is important to know to decide which submenu the code should display. So the code is the following:

Code:
// Get actual Main menu
$actMenu='';
foreach ($mainMenu as $menu => $link) {
if ($link == $actLink) $actMenu = $menu;
if (isset($subMenu[$menu])){
foreach ($subMenu[$menu] as $menuSub => $linkSub) {
if ($linkSub == $actLink) $actMenu = $menu;
}
}
}
?>


After we got the actual menu we can begin with displaying the menu items row by row. We start it with the main menu items. In all steps we check whether the actually selected link in the array and the menu is the same or not. It is because if they are the same then we should display the relevant submenus before displaying the next main menu item. To make it more pretty we use other CSS class for the submenu rows as the main menu rows. The code looks like this:

Code:
foreach ($mainMenu as $menu => $link) {
echo ''.$menu.'';
if ( ($actMenu == $menu) && (isset($subMenu[$menu])) ){
foreach ($subMenu[$menu] as $menuSub => $linkSub) {
echo ''.$menuSub.'';
}
}
}
?>

Step 4.

The last step is a demonstration how to use it in the real life. To do this we will create a new file. Let's called it to index.php. This is quite straightforward. We include the menuHandler.php and create our HTML page as we want. Where you want to display the navigation block you only need to call our function with the actual link as

Code:

Formatting Dates in PHP

Ok, so how do you format dates in PHP so it outputs the date format you want? Well thanks to PHP’s date() and strtotime() function, we can do all that! To kick off, lets take the most common date format ‘YYYY-MM-DD HH:II:SS‘. This date format seems to be most favoured as it increments in such a way that allows you to query a database that has multiple records in a useful way, such as:

Code:
SELECT * FROM dates WHERE date > '2006-03-05 11:00:00';


Whereas if you were to use a different date format, such as ‘DD-MM-YYYY HH:II:SS‘ the incrementation wouldn’t work after each month as the starting value (the DD - Day) will start from 01 again. If you don’t get me, it doesn’t matter because we’re just making dates look nice in this post anyway.

Human Readable

When I say ‘Human Readable’ I don’t mean that the particular date (for example) ‘2006-03-05 11:00:00′ isn’t readable, but you wouldn’t necessarily read it out as ‘two-zero-zero-six zero-three zero-five one-one zero-zero zero-zero’ would you? A human readable version of this date would be something like Sunday, 05 March, 2006 @ 11:00:00. So how do we convert from one format to the next? Easy! Like this:

Code:
$sNewDate = date("D, d F, Y @ H:i:s", strtotime('2006-03-05 11:00:00'));


Now if we output the value of the new date variable ‘$sNewDate’:

Code:
print $sNewDate;



We get:

Sun, 05 March, 2006 @ 11:00:00

We could of course use:

Quote:
date("D, d F, Y @ H:i:s")


Which would output a nicely formatted date for the current date and time. You can also use other time strings for the second parameter, as long as you use strtotime() to format it into a nice UNIX timestamp for the date() function to process.

The Magic

So how did I make PHP output the particular parts of the date with extras like commas (,) and the at sign (@)? Let’s look at PHP’s date() function, which takes 2 arguments:
string formatThe format that date() should output
int timestamp (optional)The timestamp for date to format the date. If this is left blank, it will default to the current date and time.

The string format takes preset PHP characters, and will allow other characters provided they’re not in the ‘preset’ list, and if they are they must be escaped. Taking our example into account, lets see what characters we used:

Quote:
"D, d F, Y @ H:i:s"


We used:
D - A textual representation of a day, three letters
d - Day of the month, 2 digits with leading zeros
F - A full textual representation of a month, such as January or March
Y - A full numeric representation of a year, 4 digits
H - 24-hour format of an hour with leading zeros
i - Minutes with leading zeros
s - Seconds, with leading zeros

View the full list of characters to learn more about PHP’s date() function. Now how did we get the commas and @ signs in there? Simple, you can just add these anywhere you like within the ‘format string’ as I did above. If you wanted to actually output text within the formatted string, you could do something like this:

Quote:
"\D\a\\t\e\: D, d F, Y @ H:i:s"


Notice how we ‘escape’ each literal character with a backslash (\), and how we had to ‘double-escape’ the ‘t’ as ‘\t‘ sends a TAB to the page, so if we want a ‘t’ we must use ‘\\t‘. The above outputs:

Date: Sun, 05 March, 2006 @ 11:00:00

I hope this has given you some insight on how easy it is to format dates using PHP.

Simple PHP Class Tutorial

I’m not sure how to start this one… It can be quite difficult to understand PHP classes at first, but hopefully I’ll make everything seem easy!

The two files used can be found below:
time.php
class.Time.php

Right here we go…

Brief overview…

Ok so you’re actually reading this brief overview? You must be serious…

PHP classes can be used to group together a set of ‘like’ functions used within a bigger application. Their main advantage is the fact that you can edit the particular class function, or functions and make a site-wide change. Classes also help give you a more structured approach to programming, and those that like to hack with some GPL released web applications will have a much better understanding of the workings of them.

This may not be the best example of explaining why to use classes in PHP, but it’s an example of how to use them.

Let’s get stuck in

Let’s start by creating a new file called time.php. Within this file, let’s add some code:

File: time.php

Code:
$sTime = gmdate("d-m-Y H:i:s");
print 'The time is: ' . $sTime;
?>


This will simply assign the current date and time to the variable $sTime and then print the string ‘The time is ‘ with the variable value at the end (i.e. The time is: 09-02-2007 21:42:28)

How would we do this, using a class? Well there’s many ways, however I would recommend using the class file to generate the time, then use the acutal ‘action page’ (time.php) to output the time. Let’s create our class file!

Get in class!

Create a new file (keep it in the same directory for this tutorial). Let’s call it class.Time.php. Add the following code:

File: class.Time.php

Code:
class Time {
function GenerateCurrentTime(){
$sTime = gmdate("d-m-Y H:i:s");
return $sTime;
}
}
?>


Lets do this line by line… The first line, class Time {,declares the class as open (exactly the same as a function in PHP, but without the brackets in this case). The next line declares a new function. The difference here is that it exists ONLY within the scope of the class (i.e. it’s built WITHIN the class). We then generate the time as we did before, assigning it to the variable $sTime and then return the value of this variable. The function then closes, followed by the class closure (the squiggly brackets ‘}’). Note that our class needs to also be wrapped in php tags ().

Now open the original file, time.php, and change the code to match the following:

File: time.php

Code:
include ('class.Time.php');
$oTime = new Time;
$sTime = $oTime->GenerateCurrentTime();
print 'The time is: ' . $sTime;
?>


Now, the first line here includes the time class file (include ('class.Time.php');). We must include all the class files we wish to take advantage of, otherwise how the hell would PHP know about these files?

The next line, $oTime = new Time, creates the class object and stores it in the variable $oTime. Notice, to store the class in an object variable, we use VARIABLE = NEW CLASSNAME. VARIABLE can be anything, then there must be an equals sign ‘=’. NEW must use ‘new’ or ‘&new’, and the CLASSNAME must match the name of the class. In this case, the name of the class is Time (case sensitive - as PHP is throughout). The class name is ‘Time’ because we created the class using:

Quote:
class Time {


If we had used:

Quote:
class HelloWorld {


As you can guess, the class name would be ‘HelloWorld’.

Anyway… now we’ve created our class, we have also included it within the page we want to make use of it. Not only that, we have ALSO initalised our class by defining it in an object variable - $oTime.

So, the next line:

Quote:
$sTime = $oTime->GenerateCurrentTime();


This simply assings the variable $sTime with the result of the function GenerateCurrentTime() within the Time class. How does it do this? Simple… We want to use the function GenerateCurrentTime() within the class $oTime so we simply us:

Quote:
$oTime->GenerateCurrentTime()


This tells PHP exactly what we want to do. The ‘->’ explains to PHP that the prefix (in this case $oTime, which we know holds the class object) is the parent of the latter (again, in this case the latter is GenerateCurrentTime()). So it basically means, run GenerateCurrentTime() within the $oTime class. Thus assigning whatever is returned by the function GenerateCurrentTime() to the variable $sTime.

The last line does what we did from scratch… print out the results with the prefixed string ‘The time is ‘.

Samsung L210

Image

Quote:
Key Features :-
1. Size
87.7x55.97x20.3 (mm)
2. LCD Resolution (pixels):
230,000
3. Firewire
No
4. Video sound
Yes
5. Sensor size
1/2.33-inch
6. Maximum Video Resolution
800x592
7. Maximum shutter (sec)
1/1500
8. Exposure compensation
-2EV - +2EV with 1/3EV steps
9. White balance
Auto / Cloudy / Daylight / Fluorescent / Incandescent / Manual
10. Self-timer
No
11. Minimum resolution
1024x768
12. Video Out
Yes
13. LCD display
Yes
14. Brand
Samsung
15. Video function
Yes
16. Zoom tele (mm)
111
17. Zoom wide (mm)
37
18. Built-in flash
Yes
19. Metering
Centre weighted / Multi Spot / Spot
20. Sensor type
CCD
21. Minimum aperture wide
f2.8
22. Frames per second (fps)
30
23. Shutter Priority
No
24. Macro focus range (cm)
10
25. Bluetooth
No
26. Minimum video resolution
320x240
27. File formats
JPEG
28. Aperture Priority
No
29. Electronic viewfinder
No
30. Minimum shutter (sec)
8
31. Minimum aperture tele
f5.2
32. Model Type
L210
33. Maximum resolution
3648x2736
34. ISO ratings
auto, 80, 100, 200, 400, 800, 1600
35. Energy
Li-Ion
36. Focus range (cm)
80
37. USB
USB 2.0
38. Storage types
MultiMedia / SDHC / Secure Digital
39. Voice recording
Yes
40. Optical Viewfinder
No
41. Resolution
10.20 Mpixel
42. Optical Zoom
Yes
43. Digital Zoom
Yes
44. Auto Focus
Yes
45. Manual Focus
No
46. LCD Size (in)
2.5-inch
47. Flash Modes
anti red-eye / auto / fill in / off / slow flash
48. External Flash
No

Samsung GX-10

Image

Quote:
Key Features :-
1. Size
142x101x71.5 (mm)
2. Firewire
No
3. LCD Size
2.5-inch
4. Video sound
No
5. Sensor size
23.5x15.7mm
6. Sequence (fps)
3
7. Maximum shutter (sec)
1/4000
8. Exposure compensation
-2EV - +2EV in 1/3 - 1/2 steps
9. White balance
Auto / Cloudy / Daylight / Flash / Fluorescent / Incandescent / Manual / Shadow / Sunny
10. Self-timer
Yes
11. Minimum resolution
1824x1216
12. Video Out
Yes
13. LCD display
Yes
14. Brand
Samsung
15. Video function
No
16. LCD resolution (pixels)
210,000
17. Built-in flash
Yes
18. Metering
Centre weighted
19. Sensor type
CCD
20. Shutter Priority
Yes
21. Weight
710g.
22. Bluetooth
No
23. File formats
JPEG / RAW
24. Aperture Priority
Yes
25. Electronic viewfinder
No
26. Minimum shutter (sec)
Bulb+30
27. Model Type
GX-10
28. Maximum resolution
3872x2592
29. ISO ratings
auto, 100, 200, 400, 800, 1600
30. Energy
Li-Ion
31. External flash type
Hot-shoe
32. USB
USB 2.0 Hi-Speed
33. Storage types
Secure Digital
34. Voice recording
No
35. Focal length multiplier
1.5
36. Optical Viewfinder
Yes
37. Resolution
10.20 Mpixel
38. Digital Zoom
No
39. Auto Focus
Yes
40. Manual Focus
Yes
41. Flash Modes
anti red-eye / auto / fill in / off / rear curtin / slow flash
42. External Flash
Yes