<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DOC776.org &#187; tutorial</title>
	<atom:link href="http://www.doc776.org/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.doc776.org</link>
	<description>This is the era of information.</description>
	<lastBuildDate>Thu, 16 Jul 2009 17:29:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Make Code</title>
		<link>http://www.doc776.org/2009/06/make-code/</link>
		<comments>http://www.doc776.org/2009/06/make-code/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 07:03:03 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[useful]]></category>

		<guid isPermaLink="false">http://www.doc776.org/?p=149</guid>
		<description><![CDATA[Very simple handy little function to generate a random code of number and lower and upper case letters.
Straight forward stuff&#8230;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function makecode($chars = 6)
{
	for($i=0; $i&#60;=($chars-1); $i++)
	{
		$r0 = rand(0,1);
		$r1 = rand(0,2); // frequency of lower case
		if($r0==0)
			$r .= chr(rand(ord('A'),ord('Z')));
		elseif($r0==1)
			$r .= rand(0,9);
		if($r1==0)
			$r = strtolower($r);
	}
	return $r;
}

$r0 controls if the next digit is a number or a letter
$1 controls if teh [...]]]></description>
			<content:encoded><![CDATA[<p>Very simple handy little function to generate a random code of number and lower and upper case letters.</p>
<p>Straight forward stuff&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">function makecode($chars = 6)
{
	for($i=0; $i&lt;=($chars-1); $i++)
	{
		$r0 = rand(0,1);
		$r1 = rand(0,2); // frequency of lower case
		if($r0==0)
			$r .= chr(rand(ord('A'),ord('Z')));
		elseif($r0==1)
			$r .= rand(0,9);
		if($r1==0)
			$r = strtolower($r);
	}
	return $r;
}</pre></td></tr></table></div>

<p>$r0 controls if the next digit is a number or a letter<br />
$1 controls if teh letter will be upper or lower. Set it to 0 if you want it to always be lower or change the range to what ever suits you.<br />
Also a number maybe passed to the function to change how many digits in the code to make.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.doc776.org/2009/06/make-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Converting Bytes To Better Format</title>
		<link>http://www.doc776.org/2009/06/converting-bytes-to-better-format/</link>
		<comments>http://www.doc776.org/2009/06/converting-bytes-to-better-format/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 06:42:18 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[resize]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.doc776.org/?p=143</guid>
		<description><![CDATA[Every now and then we need a simple function that could take any number in bytes taken from filesize() or other source and convert it for better reading. Here are some functions that do the exact same thing but with different methods.
Method 1:  Very simple straight forward stuff. Can be customized to different sizes [...]]]></description>
			<content:encoded><![CDATA[<p>Every now and then we need a simple function that could take any number in bytes taken from filesize() or other source and convert it for better reading. Here are some functions that do the exact same thing but with different methods.</p>
<p><strong>Method 1:</strong>  Very simple straight forward stuff. Can be customized to different sizes with different limits.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> convert_size<span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">1073741824</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1073741824</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">100</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">' gb'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">1048576</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1048576</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">100</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">' mb'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1024</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">100</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">' kb'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #000088;">$num</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">' b'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$num</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Method 2:  (best way)</strong>  More advance using a loop to count size depth. Very small and compact all the way to YB. Unlike method 1 it runs by a single constant.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> convert_size<span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$iec</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; B&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot; KB&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot; MB&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot; GB&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot; TB&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot; PB&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot; EB&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot; ZB&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot; YB&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$size</span><span style="color: #339933;">=</span><span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1024</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #000088;">$iec</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>As you see if you wish to have a large range conversion using the first method then you will have lots of IF statements unlike method two. Iif you are using method one you have more simple flexibility to have different off-sets for sizes.</p>
<p>Method two uses a single while loop to take the size to its lowest by every 1024 and each time it loops it reduces a single multiple of 1024 and counts how many times it reduces it, and at the end shows the corresponding unit size from the array using that count. The array must have the unit sizes in correct order. Good thing about this is if you give it a number that is like 7^20 (that&#8217;s 20 zeroes), then it still works just fine as long as the array has that many unit sizes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.doc776.org/2009/06/converting-bytes-to-better-format/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Displaying Page Load Time</title>
		<link>http://www.doc776.org/2009/06/displaying-page-load-time/</link>
		<comments>http://www.doc776.org/2009/06/displaying-page-load-time/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 06:18:30 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[info]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.doc776.org/?p=139</guid>
		<description><![CDATA[How to display page load time at end of the HTML page using PHP.]]></description>
			<content:encoded><![CDATA[<p>It is very simple to show page load times in PHP.</p>
<p><strong>Method 1:</strong><br />
Place this at the very begin of your PHP code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'STARTING_MICROTIME'</span><span style="color: #339933;">,</span> get_microtime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Depending on how you like to organize your code have those functions some where, they are pretty straight forward:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> execution_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%01.4f</span>&quot;</span><span style="color: #339933;">,</span> get_microtime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> STARTING_MICROTIME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> get_microtime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">doubleval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$time</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$time</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And finally just use the execution_time() function to show the total time elapsed since the script started.</p>
<p><strong>Method 2:</strong><br />
Place this at very beginning of your code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PAGE_LOAD_START'</span><span style="color: #339933;">,</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And this where you want to display the render time.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> PAGE_LOAD_START<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This way is easier IMO. Using constants is not necessary, it just avoids problems in namespaces if your code is OOP like.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.doc776.org/2009/06/displaying-page-load-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image Thumbnail / Resize Function</title>
		<link>http://www.doc776.org/2009/06/image-thumbnail-resize-function/</link>
		<comments>http://www.doc776.org/2009/06/image-thumbnail-resize-function/#comments</comments>
		<pubDate>Sat, 06 Jun 2009 19:45:02 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[gd]]></category>
		<category><![CDATA[resize]]></category>
		<category><![CDATA[thumbnail]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.doc776.org/?p=104</guid>
		<description><![CDATA[Log time ago I wrote this function for my friend&#8217;s gallery website. The function is straight forward and uses the GD library of PHP. You may resize the image by giving it the maximum width and height you want the thumbnail to have. The function acts as an extension of the GD library so to [...]]]></description>
			<content:encoded><![CDATA[<p>Log time ago I wrote this function for my friend&#8217;s gallery website. The function is straight forward and uses the GD library of PHP. You may resize the image by giving it the maximum width and height you want the thumbnail to have. The function acts as an extension of the GD library so to speak, so you have to pass the image resource. An additional argument may be passed stating if you want the thumbnail to be square or not, by default it is false. If it is true the width:height ratio will not be change, it only fills the empty space with black color and places the thumbnail image in the center of the square.</p>
<p>imagethumb(image resource, max width, max height, fill to make square)<br />
1 &#8211; (GD image resource) Straight forward teh image resource loaded by GD library.<br />
2 &#8211; (int) maximum width of thumbnail<br />
3 &#8211; (int) maximum width of thumbnail<br />
4 &#8211; (true/false) make thumbnail square</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> imagethumb<span style="color: #009900;">&#40;</span><span style="color: #000088;">$old_im</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limit_w</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limit_h</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fill</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//get original dimensions</span>
	<span style="color: #000088;">$old_w</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$old_im</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$old_h</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$old_im</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//first resize by width if overflow else set old to new</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$old_w</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$limit_w</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//get reduction percent</span>
		<span style="color: #000088;">$reduce_percent</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$limit_w</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$old_w</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//set new w and h</span>
		<span style="color: #000088;">$new_w</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$old_w</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$reduce_percent</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$new_h</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$old_h</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$reduce_percent</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//set as the old dimensions</span>
		<span style="color: #000088;">$new_w</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$old_w</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$new_h</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$old_h</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//second resize by height if still overflows</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_h</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$limit_h</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//get reduction percent</span>
		<span style="color: #000088;">$reduce_percent</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$limit_h</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$new_h</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//set new w and h</span>
		<span style="color: #000088;">$new_w</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_w</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$reduce_percent</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$new_h</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_h</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$reduce_percent</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fill</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//height offset picture, to keep centered</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_h</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$limit_h</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000088;">$h_offset</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$limit_h</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$new_h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">else</span>
			<span style="color: #000088;">$h_offset</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//width offset picture, to keep centered</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_w</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$limit_w</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000088;">$w_offset</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$limit_w</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$new_w</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">else</span>
			<span style="color: #000088;">$w_offset</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//make new image and copy to it</span>
		<span style="color: #000088;">$new_im</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$limit_w</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limit_h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span>
		<span style="color: #000088;">$new_im</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_w</span><span style="color: #339933;">,</span> <span style="color: #000088;">$new_h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">imagecopyresampled</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_im</span><span style="color: #339933;">,</span> <span style="color: #000088;">$old_im</span><span style="color: #339933;">,</span> <span style="color: #000088;">$w_offset</span><span style="color: #339933;">,</span> <span style="color: #000088;">$h_offset</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$new_w</span><span style="color: #339933;">,</span> <span style="color: #000088;">$new_h</span><span style="color: #339933;">,</span> <span style="color: #000088;">$old_w</span><span style="color: #339933;">,</span> <span style="color: #000088;">$old_h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$new_im</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.doc776.org/2009/06/image-thumbnail-resize-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Pagination Guide</title>
		<link>http://www.doc776.org/2009/01/php-pagination-and-sorting/</link>
		<comments>http://www.doc776.org/2009/01/php-pagination-and-sorting/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 19:34:31 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[pagination]]></category>
		<category><![CDATA[sorting]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.doc776.org/?p=32</guid>
		<description><![CDATA[At some point I needed to paginate my results of a large database for a more friendly view. Now I use PHP so this tutorial will fully explain how to paginate using php and SQL database, or just an array with all the data. That way you can use the technique with any other database [...]]]></description>
			<content:encoded><![CDATA[<p>At some point I needed to paginate my results of a large database for a more friendly view. Now I use PHP so this tutorial will fully explain how to paginate using php and SQL database, or just an array with all the data. That way you can use the technique with any other database you like. I will go over the basic mechanics and the add nice things on top like pagination and sorting at the same time and how to build a wicked page navigation for the listing. And also I shall show you how to make  a so called server side pagination/sorting using Ajax JavaScript at the end.<span id="more-32"></span></p>
<p><strong>Basic Pagination</strong></p>
<p>For total pages, get total amount of rows in your list and divide it by how many rows you want per page and then ceil() the outcome for a nice rounded answer. The names of variables should be straight forward.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//get our data from a database</span>
<span style="color: #000088;">$total_rows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select null from `table`&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//find total pages</span>
<span style="color: #000088;">$total_pages</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$total_rows</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Ceil() rounds the number up in case it is 4.5 so that means the last page will have maybe 10 rows of items left of the 20 per page.</p>
<p>Now  you are all set. Get the current page that the user is viewing or set it to 1 by default.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//if empty, set to 1 if not use what is available</span>
<span style="color: #000088;">$current_page</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$current_page</span><span style="color: #339933;">--;</span> <span style="color: #666666; font-style: italic;">//zero is number! read farther...</span></pre></div></div>

<p>Now you are all set to contract the navigation and the listing.</p>
<p>One more thing you should watch out for is the offset, since in arrays and databases it starts from 0 and the human interface should always start from 1. The code will fully be done in the favor of stating at 0 so there is less complication; it is up to you basicaly if you understand how it works.</p>
<p><strong>Navigation</strong></p>
<p>Best for navigation is to make it as a function, that way you can easily stick it in multiple places by just calling it, and it also makes your code more clean.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//make the function, it will need total pages and current page to work properly</span>
<span style="color: #000000; font-weight: bold;">function</span> pagination_nav<span style="color: #009900;">&#40;</span><span style="color: #000088;">$total_pages</span><span style="color: #339933;">,</span> <span style="color: #000088;">$current_page</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//set the empty var, so then we can just add to it like $nav .= 'something'</span>
	<span style="color: #000088;">$nav</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//(optional) this will display a [Prev] button that will flip one page backwards, if already on first page it will show a null link instead.</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_page</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//if we can go back wards show link</span>
		<span style="color: #000088;">$nav</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;a href=&quot;listing.php?page='</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_page</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;[Prev]&lt;/a&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #666666; font-style: italic;">//if not show null link or delete this whole line all togeather</span>
		<span style="color: #000088;">$nav</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;a href=&quot;#&quot;&gt;[Prev]&lt;/a&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//the main part of the nav</span>
	<span style="color: #666666; font-style: italic;">//loop throught all the posible pages from 1 too total ammount</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> <span style="color: #000088;">$total_pages</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_page</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//if on current page display null link or none at all or make it more fency</span>
			<span style="color: #000088;">$nav</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;a href=&quot;#&quot;&gt;[-'</span><span style="color: #339933;">.</span><span style="color: #000088;">$i</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'-]&lt;/a&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #666666; font-style: italic;">//if not on the current page then display it as a regular link to that page</span>
			<span style="color: #000088;">$nav</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;a href=&quot;listing.php?page='</span><span style="color: #339933;">.</span><span style="color: #000088;">$i</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;['</span><span style="color: #339933;">.</span><span style="color: #000088;">$i</span><span style="color: #339933;">.</span><span style="color: #0000ff;">']&lt;/a&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//(optional) this will display a [Next] button that will flip one page forward, if already on last page it will show a null link instead.</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_page</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #000088;">$total_pages</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000088;">$nav</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;a href=&quot;listing.php?page='</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_page</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;[Next]&lt;/a&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #666666; font-style: italic;">//if not show null link or delete this whole line all togeather</span>
		<span style="color: #000088;">$nav</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;a href=&quot;#&quot;&gt;[Next]&lt;/a&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//give back the goodness</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$nav</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//to use it</span>
<span style="color: #b1b100;">echo</span> apps_page_nav<span style="color: #009900;">&#40;</span><span style="color: #000088;">$total_pages</span><span style="color: #339933;">,</span> <span style="color: #000088;">$current_page</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//notice we have +1, that's because the function is doing this for the human interface, but total pages does not aply to the rule here as it is not used directly with data array or database.</span></pre></div></div>

<p>The whole process is straight forward, the optional buttons can be manipulated and you can add a button to skip to last page and to first page also.</p>
<p><strong>Listing</strong></p>
<p>This part is more simple then the rest. Now we have some useful variable set and navigation out of the way. Do a quic validation that the current page selected is in between zero or total pages amount or equal to zero or total.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//validate the page number is with in range</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pageID</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;=</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #000088;">$pageID</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> <span style="color: #000088;">$total_pages</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//calculate the rows offset</span>
	<span style="color: #666666; font-style: italic;">//the start will be the page number * number of rows per page</span>
	<span style="color: #000088;">$limit_start</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$current_page</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// * here there is a special case for the last page calculation.</span>
	<span style="color: #666666; font-style: italic;">//If on last page then (total rows - the starting rows) else its 20 per page</span>
	<span style="color: #000088;">$limit_end</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_page</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$total_pages</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900;">&#40;</span><span style="color: #000088;">$total_rows</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$limit_start</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span></pre></div></div>

<p>* For the last page: If there is only like 5 items on the last page then the $limit_end cannot be 20, it will have to be 4. With mysql query it does not matter but in some cases it will matter a lot, if you get data from array for instance. Different databases may have a different syntax for limiting the results.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//get rows of items for that specific page, notice the 'limit'</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select * from `table` limit <span style="color: #006699; font-weight: bold;">{$limit_start}</span>, <span style="color: #006699; font-weight: bold;">{$limit_end}</span> &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>All straight forward. Different databases may have a slightly different syntax. Here if we are on page 1 then it will be limit 0, 20. (the second # is actualy teh ammount of rows starting from the first #) Straight forward.</p>
<p><strong>Displaying</strong></p>
<p>Nothing complicated, you may want to use a table for displaying or a list I will just spit out the results with a &lt;br /&gt; at the end of each line.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//You can use for loop or while loop, with for loop you can make every other row diff color easily.</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'.$row['</span>id<span style="color: #0000ff;">'].'</span> <span style="color: #0000ff;">'.$row['</span>field2<span style="color: #0000ff;">'].'</span> <span style="color: #0000ff;">'.$row['</span>field3<span style="color: #0000ff;">'].'</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>br <span style="color: #339933;">/&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">';
}</span></pre></div></div>

<p>That concluded the basics of pagination.</p>
<p><strong>Ordering and Sorting</strong></p>
<p>Sorting the results is easiest using your database. For mysql you can add &#8220;order by `field` asc&#8221; before giving the limit. To keep pagination and order variables in the URL you will need to add the current ordering to each URL that is generated by the pagination function. Also for the order by option links that you may add you will need to keep the current page teh user is on in the URL so it does not reset to 1.</p>
<p><strong>Ajax / JavaScript</strong></p>
<p>Using JavaScript to make a server side paginating server side table is easy. All you need is a good JavaScript that allows you to load content into a DIV element of the HTML and you are set to go. make a main page like index.html and make pages.php. Index.html will have the actual JavaScript and the DIV element. The pages.php will be the actual pagination and ordering script that will point all links to the JavaScript that will load the content in the DIV. That is server side ordering and pagination, the index stays intact while you can list thought any page and so on.</p>
<p>Ups and downs: Well so much for people who&#8217;s browsers cannot use JavaScript but that&#8217;s a very small fraction. Will save bandwidth, you only show them what they need. If you use server side you make less stress on the user but if you let the client&#8217;s browser do all the work it will put more stress on them (thought around 1000 results wont matter much depending on the size).</p>
<p>There is an example given at the bottom of the tutorial.</p>
<p><strong>Examples:</strong></p>
<p>I have went through this very carefully so that any beginner at PHP may understand this, or so I hope. Remember php.net is your friend.</p>
<p>Simple Pagination: <a title="Demo" href="http://www.doc776.org/examples/paginate.php" target="prevframe">[demo]</a> <a title="Source" href="http://www.doc776.org/examples/syntax_hilight.php?file=paginate.php" target="prevframe">[source]</a></p>
<p>Preview Frame:<br />
<iframe name="prevframe" src ="http://www.doc776.org/examples/blank.htm" width="100%" height="300px">Your browser does not support iframes.</iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://www.doc776.org/2009/01/php-pagination-and-sorting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Website Structure and Design</title>
		<link>http://www.doc776.org/2009/01/php-website-structure-and-design/</link>
		<comments>http://www.doc776.org/2009/01/php-website-structure-and-design/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 09:12:43 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[structure]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.doc776.org/?p=14</guid>
		<description><![CDATA[My first website was the most confusing piece of code ever. During my time of writing it I learned some usual things and the common used things. I’ll share some of the nice ways to organize your website structure. Those structures may be simple but effective for your own use. Since I am a PHP [...]]]></description>
			<content:encoded><![CDATA[<p>My first website was the most confusing piece of code ever. During my time of writing it I learned some usual things and the common used things. I’ll share some of the nice ways to organize your website structure. Those structures may be simple but effective for your own use. Since I am a PHP power user, I’ll make it a PHP based tutorial. In my point of view, having all pages loaded from index is the best for massive websites. That is because you do not share the internal folders with users and everybody uses one single page for viewing, meaning you can password protect everything else on your server.<span id="more-14"></span></p>
<p>So here is a basic good directory structure of the website:</p>
<pre>/
/include/pages
/include/libraries
/images</pre>
<p>This in my point of view is a really simple organized and safe way to create a website that can have a massive amount of pages and easy indexed. Included folder is where we store all our libraries, layouts, our custom functions, classes and pages. And the images folder speaks for it self. The slash at front means root of the website.</p>
<p>It may look good but I can still make it more better. Even thought people will not be able to access those folders we can make it shorter to save some time ans space, even thought its just few bytes of data. Shorten the “include” to “inc” and “images” to “img” for a start. There is no point having a big word like “libraries” because we can make a mistake in the spelling when writing scripts that deal with files in that folder.</p>
<p>So here is what we have now:</p>
<pre>/
/inc/pages
/inc/lib
/img</pre>
<p>Now it looks and feels very simple.</p>
<p>There are 4 major php pages that we need to create first:</p>
<ul>
<li>index.php<br />
- the page that loads configuration and all our things in the lib/ folder we made and finally the content page that the user wants to see like home or a list of something</li>
<li>config.php<br />
- place to hold critical configurations like folder names and database user name and password</li>
<li>layout.php<br />
-how our site will look, much better to have a layout in one place rather then writing in every single display page (the beauty of PHP)</li>
<li>home.php<br />
- the default page that is shown if no page was selected</li>
</ul>
<p>Here is what the structure should look like now:</p>
<pre>/
/config.php
/index.php
/inc/pages
/inc/pages/home.php
/inc/lib
/inc/lib/layout.php
/img</pre>
<p>Now lets get into more detail of those pages.</p>
<h4>config.php</h4>
<p>You must have a one place where you can put the critical information. This information is basically passwords and folder names. You can store the folder names in a database but the root things like the password for is best be stored here. This is not required but is best for flexibility.</p>
<p>For instance it is much easier to use $dbpass $dbname $dbuser for entering you main database because if you were to change a database name or pass word all you need to do is edit the config.php where you declared those 3 variables. if you got the pass and the name written in every page then its more of a security risk and you will be in pain updating every page where you connected to the database.</p>
<p>Same goes for folders. Make an array for folder places so then if for some reason you want to change the whole folder or select a new version of files you can just edit this variable instead of updating each page of your site.</p>
<p>Make sure that this page must be included to operate, I talk more about this for the index page. Very important.</p>
<h4>index.php</h4>
<p>Instead of having lots of pages in the root fo our website like index.php as a home page and gallery.php for the gallery and so on we will have just one single page “index.php”. And instead of writing our home page in it we make this our page server, it listens what page clients wants and it loads them. This means that this page will have the highest load.</p>
<p>Basic code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>php
<span style="color: #666666; font-style: italic;">//Set variable that this is the index of the site.</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'THEINDEX'</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Load configuration file.</span>
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Array of website required files.</span>
<span style="color: #000088;">$required_files</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'layout.php'</span> <span style="color: #666666; font-style: italic;">//Page layout</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Include website required files from array or die.</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$required_files</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #990000;">is_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$di</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lib'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$di</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lib'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Required File Not Found: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Page request handle.</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #990000;">is_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$di</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pages'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.php'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$di</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pages'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$di</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pages'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'home.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Define some complex variable for security measures. This is how it works, this variable is defines only in this page and it better be something big just in case. Basically every other PHP page on your site at the top of the whole code will have one if statement that checks to see if this was defined. Since this is only defined in the index then pages included by the index will be able to see this variables and pass the if statement true. If the included page at work does not see this variable set then we can simple use die() or redirect the user to the home page. This is just one of the simple ways to prevent code execution, there is also lots of other ways. This is not necessary but works for me.</p>
<p>Require the configuration. (config.php) &#8211; Writing the PHP script we will first call the configuration file (config.php) that holds all critical data.</p>
<p>Require the your library scripts. (ex: layout.php) &#8211; Then knowing all the folder names that we got from the config.php we can include all of the libraries.</p>
<p>The page selection is done by passing URL variables. So if this is the URL “www.example.com/index.php?page=home” then there is one variable by the name of “page” that has a value of “home” (aside the variables set by the server). Simple right (those who know all this probably falling asleep, but I take newbies into consideration). Choosing what variable name you will use for passing the page name is important because it must be the same for every link that you will write in your website. if you got like 30 pages of dense code then its a pain modifying the variable name. Usually this name will have to be something simple like ‘p’ for page or ‘a’ for action. Lets assume we will use “page” as the variable name to keep things simple. So now its basically things we will pass in the page variable will be just the name of the pages that are located in the /inc/pages folder. So if we want the home.php we will use “home” not the whole thing. The ending is unnecessary. Concluding that first we add .php to the value in the “page” variable and check if that page exist in the /inc/pages folder and if it does then load it. If it does not exist then load our default page “home.php”.</p>
<h4>layout.php</h4>
<p>This is where we store the HTML header and the fotter of the website. Now this may seem very simple and limited but it is not. This is just a mere start. A lot of modifications can be done.</p>
<p>Basic example code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Layout <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> top<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;html&gt;&lt;head&gt;&lt;title&gt;Example&lt;/title&gt;&lt;/head&gt;&lt;body&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> foot<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/body&gt;&lt;/html&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>There you have it, a simple page. This means you will not have to create all teh headers and stuff in each page you make, instead you can just use Layout::top(); to show the top part. This is not limited so your your imagination to how far you can modify this and make it more advance.</p>
<h4>home.php</h4>
<p>Our simple default page that will be shown if no page is selected or page that was selected is not found.</p>
<p>Example code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//Cheking for constants defined in index to prevent direct page usage.</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ININDEX'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'no peeking'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello World!'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The first chunk is what I talked about when creating the index.php. This little piece of code will check to see if that constant was defined. If the constant does not exist it will stop working the rest of script and display “no peeking” as the code says.</p>
<p>You can download all those pages already constructed and at their correct place here. I have also added tiny bit of things to the code and a 1 bar layout.</p>
<p><a title="DOWNLOAD" href="http://www.box.net/shared/yrq55x4aok" target="_blank">DOWNLOAD</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.doc776.org/2009/01/php-website-structure-and-design/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
