<?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>Web International Awards &#187; tutorials</title>
	<atom:link href="http://www.webia.info/category/articles/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webia.info</link>
	<description></description>
	<lastBuildDate>Tue, 09 Aug 2011 15:42:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Coding a simple slider in no time</title>
		<link>http://www.webia.info/articles/code/coding-a-simple-slider-in-no-time/</link>
		<comments>http://www.webia.info/articles/code/coding-a-simple-slider-in-no-time/#comments</comments>
		<pubDate>Tue, 10 May 2011 15:00:51 +0000</pubDate>
		<dc:creator>Bogdan Pop</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[article]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[lightweight]]></category>
		<category><![CDATA[simple]]></category>
		<category><![CDATA[slider]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webia.info/?p=1879</guid>
		<description><![CDATA[

We've published a couple of articles on sliders in the past. We first had a tutorial on building a XML powered slider, then we released the source codes and demo for the xml slider and we finally released the latest revision as the xml slider plugin. Today we're going back to basics, as I am [...]]]></description>
			<content:encoded><![CDATA[<p class="imgpreview"><a href="http://www.webia.info/wp-content/uploads/2011/05/simple_slider.jpg"><img src="http://www.webia.info/wp-content/uploads/2011/05/simple_slider.jpg" alt="simple slider" title="simple slider" width="540" height="250" class="alignnone size-full wp-image-1868" /></a></p>
<p>
We've published a couple of articles on sliders in the past. We first had a <a href="http://www.webia.info/articles/tutorials/make-your-own-jquery-slider-using-xml/">tutorial on building a XML powered slider</a>, then we released the <a href="http://www.webia.info/articles/tutorials/awesome-slider-demo-and-source-codes/">source codes and demo for the xml slider</a> and we finally released the latest revision as the <a href="http://products.webraptor.eu/AwesomeSlider/">xml slider plugin</a>. Today we're going back to basics, as I am going to show you how to make a rather simple, lightweight slider in no time. Here's a <a href="http://demos.webia.info/simple_slider/">demo</a> of what we're building today.
</p>
<h2>HTML structure</h2>
<p>This structure will retain and contain the actual thumbs which will be displayed as the slider's content. As you'll probably figure out, I am using the <a href="http://960.gs/">960gs</a> to keep the structural integrity as simple as possible.</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="html" style="font-family:monospace;">&lt;div id=&quot;wrapper&quot; class=&quot;container_12&quot;&gt;
&nbsp;
	&lt;div id=&quot;featured-wrapper&quot; class=&quot;grid_12 all-rounded&quot;&gt;
		&lt;div class=&quot;arrowleft&quot;&gt;&amp;nbsp;&lt;/div&gt;
		&lt;div id=&quot;featured-thumb&quot; class=&quot;grid_12 alpha omega all-rounded&quot;&gt;	
			&lt;ul id=&quot;featured-thumb-list&quot;&gt;
&nbsp;
			&lt;/ul&gt;	
		&lt;/div&gt;
		&lt;div class=&quot;arrowright&quot;&gt;&amp;nbsp;&lt;/div&gt;
		&lt;div class=&quot;clear&quot;&gt;&amp;nbsp;&lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;</pre></td></tr></table></div>



<p>
The <em>wrapper</em> is used to hold the entire contents of the page in the center of it. <em>featured-wrapper</em> will hold the next, previous arrows as well as the <em>featured-thumb</em> div that wraps the thumbnail list. <em>featured-thumb</em> will have overflow set to hidden so that only a couple of thumbs are shown at a time. The unordered list will be shifting left to right when users click the arrows to reveal other caricatures. Before we get there, here's the code that is used to display the thumbs. The following piece of code will be repeated inside the unordered list depending on how many thumbs we want to display.
</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="html" style="font-family:monospace;">&lt;li class=&quot;grid_2&quot;&gt;
	&lt;a href=&quot;#&quot; class=&quot;all-rounded&quot;&gt;
		&lt;img src=&quot;thumb_featured.png&quot; width=&quot;120&quot; height=&quot;150&quot; alt=&quot;caption title&quot; class=&quot;all-rounded&quot; /&gt;
		caption title
	&lt;/a&gt;
&lt;/li&gt;</pre></td></tr></table></div>



<h2>Styling the slider with a sprinkle of css</h2>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#featured-wrapper</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">0px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">35px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin-bottom</span><span style="color: #00AA00;">:</span><span style="color: #933;">35px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">207px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#featured-wrapper</span> <span style="color: #6666ff;">.arrowleft</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">block</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">49px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">61px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span><span style="color: #933;">-25px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">55px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">100</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'arrow_left.png'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#featured-wrapper</span> <span style="color: #6666ff;">.arrowright</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">block</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">49px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">61px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin-right</span><span style="color: #00AA00;">:</span><span style="color: #933;">-27px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">55px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">100</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'arrow_right.png'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#featured-thumb</span>	<span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#FFF</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#CCC</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">center</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">50</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">15px</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span><span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#featured-thumb-list</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">20000px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">0px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">0px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#featured-thumb-list</span> li a <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#featured-thumb-list</span> li img <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#F1F1F1</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#CCC</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin-bottom</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>



<p>
I left out the code for the grid system. What this piece of css does is it sets the width and heigh of the div that wraps the slider controls and content, it positions the arrows and displays their background images (I used background images for the arrow design). It also sets the structure and overflow of the <em>featured-thumb</em> div that wraps the list of thumbnails. Finally, it applies some basic styles to the thumbnail placeholder and the links used for each thumbnail.
</p>
<h2>jQuery functionality</h2>


<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> featured_thumb<span style="color: #009900;">&#40;</span>maxwidth<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>	<span style="color: #006600; font-style: italic;">// featured thumb functionality</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> maxwidth<span style="color: #339933;">==</span><span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">||</span><span style="color: #009900;">&#40;</span>maxwidth<span style="color: #339933;">==</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>maxwidth<span style="color: #339933;">=</span><span style="color: #CC0000;">900</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.arrowright'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>parseFloat<span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#featured-thumb-list&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;margin-left&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;=-</span>maxwidth<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#featured-thumb-list'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#featured-thumb-list'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			    <span style="color: #3366CC;">'margin-left'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'-'</span><span style="color: #339933;">+</span>maxwidth
			  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			    <span style="color: #006600; font-style: italic;">// Animation complete.</span>
			  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">else</span>
		<span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#featured-thumb-list'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#featured-thumb-list'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			    <span style="color: #3366CC;">'margin-left'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'-=160'</span>
			  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			    <span style="color: #006600; font-style: italic;">// Animation complete.</span>
			  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.arrowleft'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>parseFloat<span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#featured-thumb-list&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;margin-left&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;=</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#featured-thumb-list'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#featured-thumb-list'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			    <span style="color: #3366CC;">'margin-left'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'0'</span>
			  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			    <span style="color: #006600; font-style: italic;">// Animation complete.</span>
			  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">else</span>
		<span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#featured-thumb-list'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#featured-thumb-list'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			    <span style="color: #3366CC;">'margin-left'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'+=160'</span>
			  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			    <span style="color: #006600; font-style: italic;">// Animation complete.</span>
			  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">// end featured thumb functionality</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	featured_thumb<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">850</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>



<p>
In the above javascript snippet we define a function which adds the required functionality to the slider. Its sole parameter is the <em>maxwidth</em> variable, which is the maximum width of the margin-left property which is modified to move the thumbnail inside the placeholder. So if the placeholder is 800 px in width and the <em>maxwidth</em> variable set to 850, the thumbs will move around in a 1650 box. There are two onclick events (lines 4 and 24) which basically trigger the same functionality that is applied in opposite directions (left and right). To move left, we add pixels to left margin. To move right, we subtract from the left margin.
</p>
<p>Because the margin-left attribute is defined as <em>X px;</em>, where <em>X</em> is a number, we parse this value into a float (to allow negative values also), then we round the number (to prevent anything bad going on) and compare the value with 0 or <em>maxwidth</em> depending on the direction we're headed to. If we're going left and by clicking the left arrow the margin would become greater than 0, we have reached the end of the slider and we therefor stop the animation from sliding the content even further. If you try many repeated clicks you'll  this code will even perform an effect similar to a bouncing effect. Similarly, if moving the content to the right, the margin is compared to negative <em>maxwidth</em> (<=-maxwidth). If we reach this threshold, once again we're at the end of the thumbnails box and the animation needs to be stopped to prevent moving the thumbnails further.
</p>
<p>
To conclude, I guess that in order to reduce the size of the code one can write an extra function for the <em>onclick</em> behavior and call it when needed instead of copy pasting it into each of the events and doing the minor customization I've done.
</p>]]></content:encoded>
			<wfw:commentRss>http://www.webia.info/articles/code/coding-a-simple-slider-in-no-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Start passenger standalone on server restart or on service failure</title>
		<link>http://www.webia.info/articles/code/start-passenger-standalone-on-server-restart-or-on-service-failure/</link>
		<comments>http://www.webia.info/articles/code/start-passenger-standalone-on-server-restart-or-on-service-failure/#comments</comments>
		<pubDate>Tue, 03 May 2011 15:00:12 +0000</pubDate>
		<dc:creator>Bogdan Pop</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[cronjob]]></category>
		<category><![CDATA[health]]></category>
		<category><![CDATA[passenger]]></category>
		<category><![CDATA[passenger standalone]]></category>
		<category><![CDATA[phusion]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[reboot]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://www.webia.info/?p=1854</guid>
		<description><![CDATA[
We've had this problem with our own servers and rails apps running on Passenger standalone and turns out there are a lot of you experiencing the same problems from time to time. This short tutorial will help you solve the following problems:


How can I keep Passenger Standalone up even after a server restart?
How do I [...]]]></description>
			<content:encoded><![CDATA[<p>
We've had this problem with our own servers and rails apps running on Passenger standalone and turns out there are a lot of you experiencing the same problems from time to time. This short tutorial will help you solve the following problems:
</p>
<ol>
<li>How can I keep Passenger Standalone up even after a server restart?</li>
<li>How do I run health checks on Passenger Standalone?</li>
<li>How do I automatically restart and spawn a rails app if it crashes under Passenger Standalone?</li>
</ol>
<p>
There are a number of fixes for the above issues, but there isn't a simple one to fix all in one. Or at least I haven't come across any of them. One fix for the first problem was placing a script in <em>/etc/init</em> so that passenger would start when the server is rebooted. This however didn't fix the other two issues as it would only start Passenger when the entire server would start, meaning Passenger wouldn't be started if for example the server ran out of memory, something crashed the rails apps etc.
</p>
<p>
The best fix I could think of was creating a script that checks whether Passenger standalone is running every minute and if it isn't, then start it. This fix would work for all scenarios: server reboot, application crash. Moreover, as the script would run as a cronjob every minute, it would also perform a health check on all running apps. Here's the code for the script.
</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="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>lsof <span style="color: #660033;">-i</span> :<span style="color: #000000;">13007</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>USER<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">/</span>rails_apps<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>APP1<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">test</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>USER<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">/</span>rails_apps<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>APP1<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
  <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #666666; font-style: italic;"># passenger down</span>
    <span style="color: #c20cb9; font-weight: bold;">date</span>
    . <span style="color: #ff0000;">&quot;/usr/local/rvm/scripts/rvm&quot;</span>
    rvm use 1.9.2
    <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>USER<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">/</span>rails_apps<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>APP1<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">/</span>
    passenger start <span style="color: #660033;">-a</span> 127.0.0.1 <span style="color: #660033;">-p</span> <span style="color: #000000;">13007</span> <span style="color: #660033;">-d</span> <span style="color: #660033;">-e</span> production <span style="color: #660033;">--user</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>USER<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>lsof <span style="color: #660033;">-i</span> :<span style="color: #000000;">13008</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>USER<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">/</span>rails_apps<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>APP2<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">test</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>USER<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">/</span>rails_apps<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>APP2<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
  <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #666666; font-style: italic;"># passenger down</span>
    <span style="color: #c20cb9; font-weight: bold;">date</span>
    . <span style="color: #ff0000;">&quot;/usr/local/rvm/scripts/rvm&quot;</span>
    rvm use 1.9.2
    <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>USER<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">/</span>rails_apps<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>APP2<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">/</span>
    passenger start <span style="color: #660033;">-a</span> 127.0.0.1 <span style="color: #660033;">-p</span> <span style="color: #000000;">13008</span> <span style="color: #660033;">-d</span> <span style="color: #660033;">-e</span> production <span style="color: #660033;">--user</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>USER<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div>



<p>
The above example is for a server running two apps on Passenger Standalone behind apache on ports 13007 and 13008. Replace <em>{USER} , {APP1}, {APP2}</em> with your own configuration. Once you've written your file, save it to <em>/home/{USER}/rails_apps/health_check.sh</em> and then add a cronjob to run it every minute, 5 minutes or whenever you want.
</p>
<p>
The code above checks whether there are any processes listening on the ports defined for each app. If there is a single process, the output will be written to a file which will have a file size greater than 0. If there are no processes listening on that port, the file size will be 0. The next step is to check the file size. If it is 0, our app is not running and we can start it. Simple as that. And it works for reboots, crashes, integrity and health checks and more. 
</p>
<p>
If you're wondering what's with the date command, here's the answer. In my cronjob I've set the output of the command to  <em>/home/{USER}/rails_apps/health_check.log</em>. If any app is started (meaning it was down for some reason) I will have the info and the timestamp in the log. I can then look throughout the apache / nginx / rails logs at that specific timestamp and identify the problem that caused the app to crash, if there were any bugs.
</p>]]></content:encoded>
			<wfw:commentRss>http://www.webia.info/articles/code/start-passenger-standalone-on-server-restart-or-on-service-failure/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Dealing with Magento&#8217;s Transactional Email preview issues</title>
		<link>http://www.webia.info/articles/tutorials/dealing-with-magentos-transactional-email-preview-issues/</link>
		<comments>http://www.webia.info/articles/tutorials/dealing-with-magentos-transactional-email-preview-issues/#comments</comments>
		<pubDate>Fri, 15 Apr 2011 07:17:59 +0000</pubDate>
		<dc:creator>Bogdan Pop</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[fix]]></category>
		<category><![CDATA[items.phtml]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[transactional email]]></category>

		<guid isPermaLink="false">http://www.webia.info/?p=1853</guid>
		<description><![CDATA[


We've recently had to tackle a issue with transactional emails in Magento for a client of ours. Magento has a nice way you can configure transactional emails in admin and preview them before using them in production. However, every once in a while things start to go really wrong and the following error is well [...]]]></description>
			<content:encoded><![CDATA[<p class="imgpreview"><a href="http://www.webia.info/wp-content/uploads/2011/04/magento_quick_fixes.jpg"><img src="http://www.webia.info/wp-content/uploads/2011/04/magento_quick_fixes.jpg" alt="" title="magento_quick_fixes" width="540" height="250" class="alignnone size-full wp-image-1868" /></a></p>

<p>
We've recently had to tackle a issue with transactional emails in Magento for a client of ours. Magento has a nice way you can configure transactional emails in admin and preview them before using them in production. However, every once in a while things start to go really wrong and the following error is well known throughout the Magento community:
</p>

<code>
PHP Fatal error: Call to a member function getAllItems() on a non-object in …template/email/order/items.phtml on line 38
</code>
<br />
<p>
If you look inside the code for items.phtml, on that specific line you'll find a for statement trying to loop through the items in an object that can be nil and it isn't tested for nil. I assume Magento's developers really considered that there can't be a single case when that object can be nil. Unfortunately it can and unfortunately it will throw nasty errors in the view.</p>

<h2>Attempt 1: New Order Confirmation Template settings</h2>
<p>The error may be triggered because you've missed to assign the (new) transactional email to the order in Magento's Admin section (<em>Admin >> System >> Configuration >> Sales >> Sales Emails >> New Order Confirmation Template</em>). If you get to Sales Emails tab in Magento's admin, you'll find a drop down for New Order Confirmation Template. Select the desired option then go to cache management and clear your cache. After you do these steps the previews should work just fine.</p>
<p>But what do you do if they don't work just fine?</p>
<h2>Attempt 2: copy original files from frontend</h2>
<p>It may be the case that the files required to process the preview got corrupted or messed up somehow. A fix for this issue would be to back up all the files that you have in your Magento installation <em> {root}/app/design/adminhtml/default/default/template/email/order/</em> . Once you've backed them up copy the contents of <em>/home/{user_account}/public_html/app/design/frontend/default/default/template/email/order</em> to <em>/home/{user_account}/public_html/app/design/adminhtml/default/default/template/email/order</em> and things should resume their normal behaviour. Perhaps you should once again clear your cache.</p>
<h2>Still not working?</h2>
<p>
Magento requires a lot of resources and some installations may be using Memcached. I even recommend you start using it if you don't. But when you're debugging Memcached may make you really unhappy if you forget about it. You should check if you're using Memcached or not, and if yes, you should log in to your server via ssh, then flush Memcached and voila, the transactional emails preview will work like a glove and the ugly "<em>PHP Fatal error: Call to a member function getAllItems() on a non-object in …template/email/order/items.phtml on line 38</em>
" error be gone.
</p>
<p>In order to flush Memcached you should telnet to the service. Usually it's easily done by running <em>telnet localhost 11211</em> and then <em>flush_all</em>. Once you do this you can quit telnet or just before that check your Memcached status using <em>stats</em></p>]]></content:encoded>
			<wfw:commentRss>http://www.webia.info/articles/tutorials/dealing-with-magentos-transactional-email-preview-issues/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drop ftp and switch to unison</title>
		<link>http://www.webia.info/articles/drop-ftp-and-switch-to-unison/</link>
		<comments>http://www.webia.info/articles/drop-ftp-and-switch-to-unison/#comments</comments>
		<pubDate>Tue, 15 Feb 2011 21:54:54 +0000</pubDate>
		<dc:creator>Bogdan Pop</dc:creator>
				<category><![CDATA[articles]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[bi-directional]]></category>
		<category><![CDATA[bidirectional]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[ftp]]></category>
		<category><![CDATA[rsync]]></category>
		<category><![CDATA[sync]]></category>
		<category><![CDATA[transfer]]></category>
		<category><![CDATA[unidirectional]]></category>
		<category><![CDATA[unison]]></category>

		<guid isPermaLink="false">http://www.webia.info/?p=1818</guid>
		<description><![CDATA[

This article is directed to those of you who use on a daily basis any FTP program to transfer files back and forth between computers, development and production environments and more. Doing it via FTP comes handy compared to file uploading plugin in CPanel or Plesk for example, but when it compares to rsync or [...]]]></description>
			<content:encoded><![CDATA[<p class="imgpreview"><a href="http://www.webia.info/wp-content/uploads/2011/02/unison_1.jpg" title="Preview of unison"><img src="http://www.webia.info/wp-content/uploads/2011/02/unison_1.jpg" alt="Preview of unison" /></a></p>
<p>
This article is directed to those of you who use on a daily basis any FTP program to transfer files back and forth between computers, development and production environments and more. Doing it via FTP comes handy compared to file uploading plugin in CPanel or Plesk for example, but when it compares to rsync or unison, FTP seems so old and time consuming.
</p>
<h2>Not familiar with rsync or unison?</h2>
<p>
Rsync is an open source utility that provides fast incremental file transfer. You can easily use it to backup your disk, upload files onto a test server while coding them locally. There are some great things you can do with this tool:</p>
<ol>
	<li>Initiate synchronization between two folders (and their sub-structure). These folders could be a local folder of a webapp and its public server brother. It may very well be your root folder on your pc and the root folder of an external backup disk.</li>
	<li>Start modifying files in any of the two folders, then sync the other one. This can help you easily write your code locally and deploying it on the server, or backing up vital data onto an external harddisk.</li>
	<li>Restore files you've deleted or ill modified from the secondary folder (backup).</li>
</ol>
<p>
And the list doesn't stop there. The possibilities of rsync are great...up to a point. Rsync doesn't do bi-directional transfer unfortunately and this has become, to me at least, a major drawback. You'd jump to say that I've stated the exact opposite just above. Yes, you can move files both ways with rsync, but in the case you've modified some in folder1 and some files in folder2, you won't be able to sync and keep all modifications in place by using merging. You'll have to choose which modifications to let go (which is unlikely to be the case) or do the sync yourself. But then, what would be the point of using rsync as it doesn't do much good?
</p>
<p>
Fortunately, there's a better tool called Unison which does mainly the same things as rsync, but is capable of synchronizing data both ways. If you modified files in both folders, you'll keep all modifications as Unison will transfer from one point to another the most recent files. If the same file has been modified on both ends, Unison will detect the conflict and ask you how you want to proceed. Now this is a really great and powerful feature.
</p>
<h2>Now why would you drop FTP in favor of these two?</h2>
<p>
Imagine you've got a project that has more than 100 folders, more than 1000 files, and after a bugfix you've modified files in 10 directories. The average user that hasn't any system setup or repository would write down modified files, and then browse the hard drive and the FTP account and copy/paste each file one by one, or in lucky cases, multiple at a time. </p>
<p>Anyway, you'd have to be really lucky to do all files in one shot. With rsync or Unison, you'd do them all in one shot, without to much worries. Moreover, if you find yourself out of the office and need to do a quick and simple fix, you can even do it directly on the server and then sync it back to your local folder. Wouldn't this be great?
</p>
<p>Check out some more information about <a href="http://samba.anu.edu.au/rsync/">rsync</a> or <a href="http://www.cis.upenn.edu/~bcpierce/unison/">Unison</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.webia.info/articles/drop-ftp-and-switch-to-unison/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a live news blogging system in JSP [II]</title>
		<link>http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-jsp-ii/</link>
		<comments>http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-jsp-ii/#comments</comments>
		<pubDate>Wed, 25 Aug 2010 10:00:21 +0000</pubDate>
		<dc:creator>Bogdan Pop</dc:creator>
				<category><![CDATA[tutorials]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[live news system]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webia.info/?p=1747</guid>
		<description><![CDATA[

Last week I started building a live news blogging system using JSP. This is a follow up tutorial I've decided to do after seeing the success of the previous series, which described how to build the same web application using php as backend.

Adding news to the database

In today's part I am going to code the [...]]]></description>
			<content:encoded><![CDATA[<p class="imgpreview"><a href="http://www.webia.info/wp-content/uploads/2010/03/lns_preview_full.jpg" title="Tutorial app preview"><img src="http://www.webia.info/wp-content/uploads/2010/03/lnw_preview.jpg" alt="Tutorial app preview" /></a></p>
<p>
Last week I started <a href="http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-jsp/">building a live news blogging system using JSP</a>. This is a follow up tutorial I've decided to do after seeing the success of the previous series, which described how to build the same web application using php as backend.
</p>
<h2>Adding news to the database</h2>
<p>
In today's part I am going to code the JSP files needed to add content to the database. The same functionality has already been developed using PHP in the third part of the original tutorial series located over <a href="http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-part-iii/">here</a>. The PHP version contained a function that was used to generate a select field into the add news form which would contain all available news categories. Differently, I've chosen not to include such a feature in a JSP function. Instead, I've added it directly into the HTML template. Not so fancy, but it works.
</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
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form action=&quot;addnews.jsp&quot; method=&quot;post&quot; accept-charset=&quot;utf-8&quot; id=&quot;form-overlay&quot;&gt;
	&lt;fieldset&gt;
		&lt;label for=&quot;title&quot;&gt;Title : &lt;/label&gt;
		&lt;input type=&quot;text&quot; name=&quot;title&quot; value=&quot;&quot; id=&quot;title&quot; class=&quot;all-rounded&quot;/&gt;
		&lt;label for=&quot;formcategory&quot;&gt;Category :&lt;/label&gt;
		&lt;select name=&quot;formcategory&quot; id=&quot;formcategory&quot; length=&quot;1&quot;&gt;
			&lt;% 						
			st = new StringTokenizer(categories,&quot;-&quot;);
			query = &quot;SELECT name FROM Categories WHERE id&gt;0 ORDER BY name ASC&quot;;
			rs = statement.executeQuery(query);
			count = 0;
				while (rs.next()) 
				{
					count++;
					String name = rs.getString(&quot;name&quot;);
					if(count==1) { %&gt; &lt;option selected=&quot;selected&quot; value=&quot;&lt;%=name%&gt;&quot;&gt;&lt;%=name%&gt;&lt;/option&gt; &lt;% }
					else { %&gt; &lt;option value=&quot;&lt;%=name%&gt;&quot;&gt;&lt;%=name%&gt;&lt;/option&gt; &lt;% }
				}
			if(count==0) { %&gt; &lt;option value=&quot;none&quot;&gt;none&lt;/option&gt; &lt;% }
			%&gt;
		&lt;/select&gt;
		&lt;label for=&quot;body&quot;&gt;Body text :&lt;/label&gt;
		&lt;textarea name=&quot;body&quot; rows=&quot;8&quot; cols=&quot;40&quot; class=&quot;all-rounded&quot;&gt;&lt;/textarea&gt;
		&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Add news&quot; id=&quot;submit&quot; class=&quot;submit all-rounded&quot; /&gt;
	&lt;/fieldset&gt;
&lt;/form&gt;</pre></td></tr></table></div>



<p>
As you've seen in the <a href="http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-jsp/">previous part</a>, the code on lines 8 to 10 is used to create a SQL query and run it. Lines 11/19 check if the SQL query returned an empty result. If yes, a default news category is created as the unique option; current case: <em>none</em>. If there's at least one category in the database, the code between line 12 and 17 gets the names of each category and adds each of them to the select field. The first category that's found is also pre-selected.
</p>
<h2>Processing the add news form</h2>
<p>
The form used to insert news in the database needs to be processed. The script is pretty straightforward, especially because I've already shown basic SQL queries in JSP just above ( or in the previous article in the series ). The first line of the script that processes the form is the usual line that imports all required libraries. All the following lines up to line 18 are used to create and instantiate the resources required to perform different operations on the mySQL database. Here's the full 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
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
</pre></td><td class="code"><pre class="jsp" style="font-family:monospace;">&lt;%@ page import=&quot;java.sql.*&quot; import=&quot;java.util.*&quot; session=&quot;true&quot; import=&quot;Javax.servlet.http.*&quot; import=&quot;java.security.*&quot; %&gt;
&nbsp;
&lt;%
&nbsp;
	String database = &quot;lab8&quot;;
	String user = &quot;root&quot;;
	String pass = &quot;thebeastlocal&quot;;
	String table = &quot;News&quot;;
&nbsp;
	String connectionURL = &quot;jdbc:mysql://localhost:3306/&quot;+database+&quot;?user=&quot;+user+&quot;;password=&quot;+pass;
	Connection connection = null;
	Statement statement = null;
	Statement update = null;
	ResultSet rs = null;
&nbsp;
	Class.forName(&quot;com.mysql.jdbc.Driver&quot;).newInstance();
	connection = DriverManager.getConnection(connectionURL,user,pass);
	statement = connection.createStatement();
&nbsp;
	if(session.getAttribute(&quot;loggedin&quot;)!=null)
	{
		String title = &quot;&quot;;
		String formcategory = &quot;&quot;;
		String body = &quot;&quot;;
		int error = 0;
&nbsp;
		if(request.getParameter(&quot;title&quot;)!=null) title = request.getParameter(&quot;title&quot;);
		else error = 1;
		if(request.getParameter(&quot;formcategory&quot;)!=null) formcategory = request.getParameter(&quot;formcategory&quot;);
		else error = 1;
		if(request.getParameter(&quot;body&quot;)!=null) body = request.getParameter(&quot;body&quot;);
		else error = 1;
&nbsp;
		if(error==1)
		{
			response.sendRedirect(&quot;index.jsp?error=Please input information in all fields&quot;);
		}
		else
		{
			String username = (String) session.getAttribute(&quot;username&quot;);
			try
			{
				String query = &quot;INSERT // INTO News(title,body,owner,publishing_date,publishing_time,category) VALUES('&quot;+title+&quot;','&quot;+body+&quot;','&quot;+username+&quot;',NOW(),NOW(),'&quot;+formcategory+&quot;')&quot;;
				// remove // between INSERT and INTO in above line
				statement.executeUpdate(query);				
			}
			catch (Exception e)
			{
				%&gt;&lt;%=e%&gt;&lt;%
			}
			response.sendRedirect(&quot;index.jsp?msg=News added to the system.&quot;);
		}
	}
	else response.sendRedirect(&quot;index.jsp?error=You must be logged in to access this feature.&quot;);
%&gt;</pre></td></tr></table></div>



<p>
Lines 20 and 54 in the above code check if the user trying to add a news to the database is logged in, as only logged in users are allowed to add items to the database. Lines 22 to 32 are used to initialize the variables which retain information users have inserted into the form fields on the previous page. On lines 34/36 errors are checked, and if any errors are found the users are redirected to the homepage of the web app and error messages are displayed. The code on line 40 gets the username of the person trying to add a news to the database as each news has an author and we need to know the info before inserting new items in the database. The <em>try catch</em> block between lines 41 and 50 is used to add the news to the database, or if any exceptions occur, catch and display them. If everything goes as planned the code on line 51 redirects users to the homepage and triggers a success message.
</p>
<p>That's all it takes to nicely add information to a database using Java Server Pages. If you enjoyed the article, you can stay updated to new content via our <a href="http://feeds2.feedburner.com/WebInternationalAwards" target="_new">RSS feed</a> or by <a href="http://feedburner.google.com/fb/a/mailverify?uri=WebInternationalAwards&#038;loc=en_US" target="_new">email</a>.
</p>]]></content:encoded>
			<wfw:commentRss>http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-jsp-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a live news blogging system in JSP</title>
		<link>http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-jsp/</link>
		<comments>http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-jsp/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 04:24:20 +0000</pubDate>
		<dc:creator>Bogdan Pop</dc:creator>
				<category><![CDATA[tutorials]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[live news system]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webia.info/?p=1736</guid>
		<description><![CDATA[

About 5 months ago I started a series of tutorials which described the process of building a live news blogging system in php. When the series ended, I promised that another series will follow and will describe the process of building the same web app using JSP. This article is the first of its series, [...]]]></description>
			<content:encoded><![CDATA[<p class="imgpreview"><a href="http://www.webia.info/wp-content/uploads/2010/03/lns_preview_full.jpg" title="Tutorial app preview"><img src="http://www.webia.info/wp-content/uploads/2010/03/lnw_preview.jpg" alt="Tutorial app preview" /></a></p>
<p>
About 5 months ago I started a series of tutorials which described the process of <a href="http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-part-i/">building a live news blogging system in php</a>. When the series ended, I promised that another series will follow and will describe the process of building the same web app using JSP. This article is the first of its series, and contains the introductory part of what's needed in this tutorial series, as well as some basic JSP code.
</p>
<p>First of all, please read the first part of the original series <a href="http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-part-i/">here</a> as it describes and shows the source codes for the user interface of the web application, namely HTML5, CSS and some jQuery.</p>
<p>You should also check out the second part of the original tutorial <a href="http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-part-ii/">here</a>, as this part contains the database model and some more jQuery. There's also some bits of PHP, but those aren't mandatory for today's article.</p>
<h2>login.jsp page</h2>


<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
57
58
59
60
61
62
63
64
65
66
</pre></td><td class="code"><pre class="jsp" style="font-family:monospace;">&lt;%@ page import=&quot;java.sql.*&quot; import=&quot;java.util.*&quot; session=&quot;true&quot; import=&quot;Javax.servlet.http.*&quot; import=&quot;java.security.*&quot; %&gt;
&nbsp;
&lt;%
&nbsp;
	String database = &quot;lab8&quot;;
	String user = &quot;user here&quot;;
	String pass = &quot;pass here&quot;;
	String table = &quot;Users&quot;;
&nbsp;
	String connectionURL = &quot;jdbc:mysql://localhost:3306/&quot;+database+&quot;?user=&quot;+user+&quot;;password=&quot;+pass;
	Connection connection = null;
	Statement statement = null;
	Statement update = null;
	ResultSet rs = null;
&nbsp;
	Class.forName(&quot;com.mysql.jdbc.Driver&quot;).newInstance();
	connection = DriverManager.getConnection(connectionURL,user,pass);
	statement = connection.createStatement();
&nbsp;
	String username = &quot;&quot;;
	String password = &quot;&quot;;
	int error = 0;
	if(request.getParameter(&quot;username&quot;)!=null) username = request.getParameter(&quot;username&quot;);
	else error = 1;
	if(request.getParameter(&quot;password&quot;)!=null) password = request.getParameter(&quot;password&quot;);
	else error = 1;
	if(error==1)
	{
		response.sendRedirect(&quot;index.jsp?error=Please input username and password&quot;);
	}
	else
	{
			MessageDigest mdAlgorithm = MessageDigest.getInstance(&quot;MD5&quot;);
			mdAlgorithm.update(password.getBytes());
&nbsp;
			byte[] digest = mdAlgorithm.digest();
			StringBuffer hexString = new StringBuffer();
&nbsp;
			for (int i = 0; i &lt; digest.length; i++) 
			{
			    password = Integer.toHexString(0xFF &amp; digest[i]);
&nbsp;
			    if (password.length() &lt; 2) 
				{
			        password = &quot;0&quot; + password;
			    }
&nbsp;
			    hexString.append(password);
			}
&nbsp;
			password = hexString.toString();
			String query = &quot;SELECT * FROM Users WHERE username='&quot;+username+&quot;' AND password='&quot;+password+&quot;'&quot;;
			rs = statement.executeQuery(query);
			if(rs.next())
			{
				session.setAttribute(&quot;loggedin&quot;,&quot;1&quot;);
				session.setAttribute(&quot;username&quot;,username);
&nbsp;
				response.sendRedirect(&quot;index.jsp?msg=Logged in succesfully.&quot;);
			}
			else 
			{
				response.sendRedirect(&quot;index.jsp?error=Invalid login data.&quot;);
			}
	}
%&gt;</pre></td></tr></table></div>



<p>The first line of the code above contains instructions to include different libraries that will be used throughout the entire jsp file. The following four lines of code are in fact the variables that will be used to store database connection settings. In the next set of lines we create the datatypes that we will use to create an sql connection, perform sql queries and handle the results returned by the queries. On lines 16 to 18 we instantiate the mysql driver and connect to the database.</p>
<p>The instructions between lines 20 and 30 test if the visitor has inputed his username and password information in the form that send him to the login page. If the user's hasn't filled in required information, the request is denied. Lines 33 to 50 are used to encrypt the password the user typed in the login form. Passwords in the database are encrypted to ensure that, even if third parties gain access to the database, the passwords aren't exposed.</p>
<p>
The following two lines make an SQL query to check if the user credentials are valid. If they are, lines 56 to 59 are used to create required login sessions and redirect the user to the homepage, while the code on line 63 redirects the user to the homepage after an invalid login.
</p>
<h2>logout.jsp</h2>
<p>The logout process is much more simple, and the following code snippet does the trick. This snippet also contains the imports found in the login.jsp file, one line of code that destroys the session and one that redirects the user to the homepage.</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="jsp" style="font-family:monospace;">&lt;%@ page import=&quot;java.sql.*&quot; import=&quot;java.util.*&quot; session=&quot;true&quot; import=&quot;Javax.servlet.http.*&quot; import=&quot;java.security.*&quot; %&gt;
&nbsp;
&lt;%
	session.removeAttribute(&quot;loggedin&quot;);
	response.sendRedirect(&quot;index.jsp?msg=Successfully logged out&quot;);
%&gt;</pre></td></tr></table></div>



<p>
While the php application also contained a functions.php file, this does not. Having that said, this is the end of the first part of the tutorial. You can stay updated to new content via our <a href="http://feeds2.feedburner.com/WebInternationalAwards" target="_new">RSS feed</a> or by <a href="http://feedburner.google.com/fb/a/mailverify?uri=WebInternationalAwards&#038;loc=en_US" target="_new">email</a>.
</p>]]></content:encoded>
			<wfw:commentRss>http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-jsp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Building a live news blogging system in php. Spiced with HTML5, CSS3 and jQuery [sources]</title>
		<link>http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-sources/</link>
		<comments>http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-sources/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 08:00:47 +0000</pubDate>
		<dc:creator>Bogdan Pop</dc:creator>
				<category><![CDATA[tutorials]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[live news system]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webia.info/?p=1701</guid>
		<description><![CDATA[

About a month ago the huge tutorial on building a live news blogging system was completed. Today I am releasing the source codes of the tutorial so that you can finally play with the web application however you like.

Demo

You can check out the demo part of the tutorial before you download the source code. Furthermore, [...]]]></description>
			<content:encoded><![CDATA[<p class="imgpreview"><a href="http://www.webia.info/wp-content/uploads/2010/03/lns_preview_full.jpg" title="Tutorial app preview"><img src="http://www.webia.info/wp-content/uploads/2010/03/lnw_preview.jpg" alt="Tutorial app preview" /></a></p>
<p>
About a month ago the huge tutorial on building a live news blogging system was completed. Today I am releasing the source codes of the tutorial so that you can finally play with the web application however you like.
</p>
<h2>Demo</h2>
<p>
You can check out the <a href="http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-demo/">demo part of the tutorial</a> before you download the source code. Furthermore, a demo of the application is available <a href="http://demos.webia.info/news_system">over here</a>.
</p>
<h2>Source code</h2>
<p>
The full source code of the tutorial is available for download <a href="http://demos.webia.info/news_system/LiveNewsSystem.zip">over here</a>.
</p>
<p>
If you liked this tutorial series, stay updated to new content via our <a href="http://feeds2.feedburner.com/WebInternationalAwards" target="_new">RSS feed</a> or by <a href="http://feedburner.google.com/fb/a/mailverify?uri=WebInternationalAwards&#038;loc=en_US" target="_new">email</a>, because a new series will start soon. This new series will be around coding the same webapp using JSP on Tomcat.
</p>]]></content:encoded>
			<wfw:commentRss>http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-sources/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a live news blogging system in php. Spiced with HTML5, CSS3 and jQuery [end]</title>
		<link>http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-end/</link>
		<comments>http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-end/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 13:51:29 +0000</pubDate>
		<dc:creator>Bogdan Pop</dc:creator>
				<category><![CDATA[tutorials]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[live news system]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webia.info/?p=1658</guid>
		<description><![CDATA[

Finally, the last part of the tutorial you have all been waiting for. Until now I've covered everything that deals with databases and the CRUD functionalities the system has. However, I haven't tackled the primary view of the app, the homepage, which automatically updates news as they are added in the database without requiring anything [...]]]></description>
			<content:encoded><![CDATA[<p class="imgpreview"><a href="http://www.webia.info/wp-content/uploads/2010/03/lns_preview_full.jpg" title="Tutorial app preview"><img src="http://www.webia.info/wp-content/uploads/2010/03/lnw_preview.jpg" alt="Tutorial app preview" /></a></p>
<p>
Finally, the last part of the tutorial you have all been waiting for. Until now I've covered everything that deals with databases and the CRUD functionalities the system has. However, I haven't tackled the primary view of the app, the homepage, which automatically updates news as they are added in the database without requiring anything from the end user. To bad I didn't finish the tutorial earlier; maybe some great online newspapers would have used it during yesterday's keynote address at WWDC.
</p>
<h2>HTML5 modifications, integration with php</h2>
<p>
In case you read all the previous parts of the tutorial, you know I used HTML5 to structure the layout of the app. While implementing some of the functionalities, small upgrades were required. I am going to start with the <em>head</em> of the document. There are two stylesheets added. One is for general styles, while the second one is used to style the datepicker used in the app and is the style that comes with jQuery's datepicker plugin. Having that said, you can see there's also the .js script for it and for jQuery as well. <em>date.js </em> is Jörn Zaefferer's and Brandon Aaron's date prototype extensions I've used in the app. The <em>sprinkle.js</em> file is where all the gizmoz performed by the app are stored.
</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="html" style="font-family:monospace;">&lt;head&gt;
	&lt;title&gt;Live News System&lt;/title&gt;
	&lt;link rel=&quot;stylesheet&quot; href=&quot;css/style.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; title=&quot;no title&quot; charset=&quot;utf-8&quot;&gt;
	&lt;link rel=&quot;stylesheet&quot; href=&quot;css/datepicker.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; title=&quot;no title&quot; charset=&quot;utf-8&quot;&gt;
&nbsp;
	&lt;script src=&quot;js/jquery.js&quot; type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
	&lt;script src=&quot;js/date.js&quot; type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
	&lt;script src=&quot;js/datepicker.js&quot; type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
	&lt;script src=&quot;js/sprinkle.js&quot; type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;	
&lt;/head&gt;</pre></td></tr></table></div>



<p>
Moving on to the body of the site the first items contained in the body are hidden to public view. It's a small php script that includes the functions file and calls for the initiateLastID() function. This function is newly added, and its code is provided just below.
</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;">&lt;body&gt;
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?</span> <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;includes/functions.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	   initiateLastID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
	&lt;header class=&quot;top-rounded&quot;&gt;
		Live News System &lt;!-- The title of our app --&gt;         
	&lt;/header&gt;</pre></td></tr></table></div>





<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> initiateLastID<span style="color: #009900;">&#40;</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: #339933;">!</span><span style="color: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lastID'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span> 
		<span style="color: #666666; font-style: italic;">// no previous call</span>
		<span style="color: #990000;">session_register</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lastID'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT id FROM News WHERE id&gt;0 ORDER BY id DESC LIMIT 1&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</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: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lastID'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$lastID</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>



<p>
What this function does is it creates a php session that contains the ID of the last news item added to the database. This session will be used to track new news added in the database while the user is on the homepage. This is a key variable in achieving a functionality similar to Twitter's new X items in search page.
</p>
<p>
Next, just after the details tag in the header of the page, the one which stores and displays information regarding selected filters, we add some more php code. The code is shown below and its purpose is to catch error and notification messages directly from the URL and display them nicely inside the webapp's UI.
</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$msg</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$msg</span><span style="color: #339933;">=</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'msg'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$error</span><span style="color: #339933;">=</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$msg</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;article class=&quot;no-hover message display&quot;&gt;
		&lt;h1&gt;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$msg</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/h1&gt;
	&lt;/article&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$error</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;article class=&quot;no-hover error display&quot;&gt;                      
		&lt;h1&gt;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$error</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/h1&gt;
	&lt;/article&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #009900;">&#125;</span>
list_news<span style="color: #009900;">&#40;</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>
<em>list_news()</em> is a bit complex, and will be presented in just a few minutes. 
</p>
<p>
In the right sidebar we have the start date and end date input fields. We need to store the information within these fields as users could accidentally click the refresh button of the web browser and loose data in those input fields. We protect the information in the fields by storing it using php sessions. The modified code for the two inputs is the following one:
</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="html" style="font-family:monospace;">&lt;fieldset&gt;
	&lt;label for=&quot;start_date&quot;&gt;Start date: &lt;/label&gt;
	&lt;input type=&quot;text&quot; name=&quot;start_date&quot; value=&quot;&lt;?=$_SESSION['start']?&gt;&quot; id=&quot;start_date&quot; class=&quot;date-pick&quot;&gt;
	&lt;label for=&quot;end_date&quot;&gt;End date: &lt;/label&gt;
	&lt;input type=&quot;text&quot; name=&quot;end_date&quot; value=&quot;&lt;?=$_SESSION['end']?&gt;&quot; id=&quot;end_date&quot; class=&quot;date-pick&quot;&gt;
&lt;/fieldset&gt;</pre></td></tr></table></div>



<p>
Just below the two date inputs the categories of the webapp are listed. This is achieved by using a php function: <em>list_categories()</em> which was presented in the <a href="http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-part-iii/">third part of the tutorial</a>. However, during that part the function returned <em>option</em> values for <em>select</em> tags. The updated function is shown below.
</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;ul&gt;&lt;!-- This UL will hold our categories --&gt;
	&lt;li class=&quot;title&quot;&gt;Browse by category&lt;/li&gt;                     
	&lt;? list_categories(&quot;&quot;) ?&gt;
&lt;/ul&gt;</pre></td></tr></table></div>





<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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> list_categories<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mode</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: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'categories'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$categories</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'categories'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$rows</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 name FROM Categories WHERE id&gt;0 ORDER BY name ASC&quot;</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</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;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mode</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;&quot;</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: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</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_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span><span style="color: #009900;">&#41;</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: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'categories'</span><span style="color: #009900;">&#41;</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: #990000;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$categories</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</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: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$categories</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;li&gt;&lt;a class=<span style="color: #000099; font-weight: bold;">\&quot;</span>selected-aside<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/a&gt;&lt;/li&gt;&quot;</span><span style="color: #339933;">;</span>
								<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;li&gt;&lt;a&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/a&gt;&lt;/li&gt;&quot;</span><span style="color: #339933;">;</span>
						<span style="color: #009900;">&#125;</span>
						<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;li&gt;&lt;a&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/a&gt;&lt;/li&gt;&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;li&gt;&lt;a&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/a&gt;&lt;/li&gt;&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;li&gt;no categories&lt;/li&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mode</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;select-options&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// code in part three of the tutorial</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>



<p>
What the function does is find out which categories are already selected by the user, then display them as <em>list</em> items. If some of them are selected, the <em>selected-aside </em>class is appended to the <em>li</em> tag. Moreover, the <em>$mode</em> param used in this function determines if the function should return <em>li</em> tags or <em>option</em> tags.
</p>
<p>
The footer has also been updated and now tracks user login and displays information accordingly.
</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;footer&gt;
	&lt;? if(!session_is_registered('loggedin')) { ?&gt;&lt;a rel=&quot;login&quot; name=&quot;modal&quot;&gt;Admin login&lt;/a&gt; &lt;? } else { ?&gt;You are logged in. You can &lt;A  href=&quot;#addnews&quot; rel=&quot;addnews&quot; name=&quot;modal&quot;&gt;add news&lt;/a&gt; or &lt;a href=&quot;logout.php&quot; target=&quot;_self&quot;&gt;logout&lt;/a&gt;&lt;? } ?&gt;
&lt;/footer&gt;</pre></td></tr></table></div>



<p>
With this we've finished the HTML structure and content of the index page. Before moving on to the jQuery code which is used to add all the awesome functionalities let me remind you what the structure of an <em>article </em> is (referring to HTML article tag). Just below are presented two different structures, one used for showcasing news to end users while the second is used to display the news to admins that are logged in.
</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;article&gt;	
	&lt;h1&gt;news title&lt;/h1&gt;
	&lt;details&gt;Posted by &lt;span&gt;user&lt;/span&gt; in &lt;span class=&quot;cat&quot;&gt;category&lt;/span&gt; on &lt;span&gt;date&lt;/span&gt; at &lt;span&gt;time&lt;/span&gt;.&lt;/details&gt;
	&lt;p&gt;content of news&lt;/p&gt;
&lt;/article&gt;</pre></td></tr></table></div>





<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;article&gt;
	&lt;span class=&quot;controls&quot;&gt;&lt;a href=&quot;deletenews.php?id=ID&quot; target=&quot;_self&quot; class=&quot;delete&quot;&gt;&lt;/a&gt;&lt;a href=&quot;&quot; name=&quot;modal&quot; rel=&quot;editform&quot; title=&quot;ID&quot; target=&quot;_self&quot; class=&quot;edit&quot;&gt;&lt;/a&gt;&lt;/span&gt;	
	&lt;h1&gt;news title&lt;/h1&gt;
	&lt;details&gt;Posted by &lt;span&gt;user&lt;/span&gt; in &lt;span class=&quot;cat&quot;&gt;category&lt;/span&gt; on &lt;span&gt;date&lt;/span&gt; at &lt;span&gt;time&lt;/span&gt;.&lt;/details&gt;
	&lt;p&gt;content of news&lt;/p&gt;
&lt;/article&gt;</pre></td></tr></table></div>



<p>
As you can see the code used for admins contains an extra <em>span</em> with links to the edit and delete buttons for the news item. 
</p>
<h2>list_news()</h2>
<p>
Based on the average complexity of the functions presented up to this point, this function that spans across 100 lines of code is huge. Check out the code, try to understand it. Explanations will follow just after it.
</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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> list_news<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$categories_query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'categories'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// we have some registered categories, so our query will not select all news</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'categories'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$categorieskeys</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'categories'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$categorieskeys</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$categories_query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;AND (&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$categorieskeys</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">!=</span><span style="color: #009900; font-weight: bold;">null</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;">$count</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$categories_query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$categories_query</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; OR category = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$categorieskeys</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;' &quot;</span><span style="color: #339933;">;</span>
						<span style="color: #b1b100;">else</span> <span style="color: #000088;">$categories_query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$categories_query</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; category = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$categorieskeys</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;' &quot;</span><span style="color: #339933;">;</span>
						<span style="color: #000088;">$count</span><span style="color: #339933;">++;</span>
					<span style="color: #009900;">&#125;</span>
					<span style="color: #000088;">$categories_query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$categories_query</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; ) &quot;</span><span style="color: #339933;">;</span>				
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'start'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$end</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'end'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>	
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$categories_query</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">''</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: #009900;">&#40;</span><span style="color: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'start'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'end'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$start</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$end</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM News WHERE id&gt;0 &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$categories_query</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; AND  publishing_date &gt;= '<span style="color: #006699; font-weight: bold;">$start</span>' AND publishing_date &lt;='<span style="color: #006699; font-weight: bold;">$end</span>' ORDER BY publishing_date DESC, publishing_time DESC&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'start'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$start</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM News WHERE id&gt;0 &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$categories_query</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; AND publishing_date &gt;= '<span style="color: #006699; font-weight: bold;">$start</span>' ORDER BY publishing_date DESC, publishing_time DESC&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'end'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$end</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM News WHERE id&gt;0 &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$categories_query</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; AND publishing_date &lt;= '<span style="color: #006699; font-weight: bold;">$end</span>' ORDER BY publishing_date DESC, publishing_time DESC&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM News WHERE id&gt;0 &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$categories_query</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; ORDER BY publishing_date DESC, publishing_time DESC&quot;</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'start'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'end'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$start</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$end</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM News WHERE id&gt;0 AND  publishing_date &gt;= '<span style="color: #006699; font-weight: bold;">$start</span>' AND publishing_date &lt;= '<span style="color: #006699; font-weight: bold;">$end</span>' ORDER BY publishing_date DESC, publishing_time DESC&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'start'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$start</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM News WHERE id&gt;0 AND publishing_date &gt;= '<span style="color: #006699; font-weight: bold;">$start</span>' ORDER BY publishing_date DESC, publishing_time DESC&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'end'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$end</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM News WHERE id&gt;0 AND publishing_date &lt;= '<span style="color: #006699; font-weight: bold;">$end</span>' ORDER BY publishing_date DESC, publishing_time DESC&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM News WHERE id&gt;0 ORDER BY publishing_date DESC, publishing_time DESC&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000088;">$newlastID</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$rows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</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: #666666; font-style: italic;">// row contains ID title body owner publishing_date publishing_time category</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</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_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span><span style="color: #009900;">&#41;</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;">$newlastID</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> 
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$newlastID</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lastID'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span> 
					<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lastID'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newlastID</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$lastID</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$newlastID</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;">// no previous call</span>
					<span style="color: #990000;">session_register</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lastID'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lastID'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$lastID</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>				
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'loggedin'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$admincontrols</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;span class=<span style="color: #000099; font-weight: bold;">\&quot;</span>controls<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>deletenews.php?id=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span> target=<span style="color: #000099; font-weight: bold;">\&quot;</span>_self<span style="color: #000099; font-weight: bold;">\&quot;</span> class=<span style="color: #000099; font-weight: bold;">\&quot;</span>delete<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&lt;/a&gt;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\&quot;</span> name=<span style="color: #000099; font-weight: bold;">\&quot;</span>modal<span style="color: #000099; font-weight: bold;">\&quot;</span> rel=<span style="color: #000099; font-weight: bold;">\&quot;</span>editform<span style="color: #000099; font-weight: bold;">\&quot;</span> title=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span> target=<span style="color: #000099; font-weight: bold;">\&quot;</span>_self<span style="color: #000099; font-weight: bold;">\&quot;</span> class=<span style="color: #000099; font-weight: bold;">\&quot;</span>edit<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&lt;/a&gt;&lt;/span&gt;&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;
				&lt;article&gt;
					&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$admincontrols</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;
					&lt;h1&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</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: #0000ff;">&quot;&lt;/h1&gt;
					&lt;details&gt;Posted by &lt;span&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/span&gt; in &lt;span class=<span style="color: #000099; font-weight: bold;">\&quot;</span>cat<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/span&gt; on &lt;span&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/span&gt; at &lt;span&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/span&gt;.&lt;/details&gt;
					&lt;p&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/p&gt;
				&lt;/article&gt;
			&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;article class=<span style="color: #000099; font-weight: bold;">\&quot;</span>no-hover<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&lt;h1&gt;No news into the system&lt;/h1&gt;&lt;/article&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>



<p>
The first section of the function starting on the <em>third line and going up to line 22</em> checks if there are any categories selected. If at least one is selected the code builds a SQL statement which will be used in a SELECT / WHERE clause.
</p>
<p>
The code on <em>lines 24 and 25</em> is used to check whether the start and end dates used for news filtering are enabled. If they are enabled they must to be used in the SELECT statement against the database.
</p>
<p>
First batch of SELECT statements, starting on <em>line 27 up to 42</em>. This batch is used if the users have selected any categories to filter the results. There are multiple statements because each one of them is used based on start and end date filters contents. The second batch, <em>line 45 to 57</em>, does the same thing as the above batch, but doesn't limit the results based on categories because in this case none of them is selected.
</p>
<p>
The code between <em>lines 60 to 94</em> lists the results of the previous SQL queries. <em>$newlastID</em> variable is used to update the $lastID session described above. This is necessary because the ID of the last news added to the system changes as soon as applied filters change. The last news in one category doesn't have the same ID as the last news inside another category, or throughout the entire system. These checks and session updates are done between <em>lines 68 and 82</em> and are done once per each query.
</p>
<p>
Finally, the code from <em>line 83 to 90</em> is used to create the structure and content of each news item and display it.
</p>
<p>
Further more, there's another similar function as complex as this one. Its name is <em>list_new_news()</em> and the difference between this one and the one presented above is that it uses the <em>lastID</em> session in its SELECT statements, returning items which have an ID bigger than what <em>lastID</em> stores. This function is used to add to the DOM news items that were added to the system after the user has loaded the page. 
</p>
<p>
Basically, this function is used to achieve the functionality of the Twitter new items feature available in Twitter's search page. However, simply coding it won't do any good. This function has to be combined with some ajax requests, which are coded in the <em>sprinkle.js</em> jQuery file.
</p>
<h2>sprinkle.js</h2>
<p>
Thank God I've shown parts of this file in the previous five parts of the tutorial. Otherwise, the task for today would have been explaining 400 or more lines of code. Fortunately, some of them are already known so I am going to stick to showing the newly added lines only.
</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> updateNews<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'includes/check_new_items.php'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>newdata<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>newdata.<span style="color: #660066;">length</span><span style="color: #339933;">==</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#newdata'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">insertAfter</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section &gt; details'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #006600; font-style: italic;">// find out each new article, compute height, and set new heights for section and aside</span>
		<span style="color: #003366; font-weight: bold;">var</span> sectionHeight <span style="color: #339933;">=</span> <span style="color: #CC0000;">75</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// padding, margin, included elements</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section article'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			sectionHeight <span style="color: #339933;">=</span> sectionHeight <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">10</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 10 comes from padding t b, 25 from margin top</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		asideHeight <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
		asideHeight <span style="color: #339933;">=</span> asideHeight <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'aside div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'aside li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			asideHeight <span style="color: #339933;">=</span> asideHeight <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'title'</span><span style="color: #009900;">&#41;</span> asideHeight <span style="color: #339933;">=</span> asideHeight <span style="color: #339933;">+</span> <span style="color: #CC0000;">15</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>asideHeight <span style="color: #339933;">&lt;</span> sectionHeight<span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">//	alert (sectionHeight);</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'aside, section'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> height<span style="color: #339933;">:</span> sectionHeight <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>queue<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// update height	</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">else</span>
		<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">//	alert (asideHeight);</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'aside, section'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> height<span style="color: #339933;">:</span> asideHeight <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>queue<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// update height	</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #006600; font-style: italic;">// done with column heights</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section article'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>



<p>
The above function checks for new news added into the database. If new items are available, a message stating so is appended to the DOM. If the message box is clicked new news items are appended to the DOM and then faded in so that users can see them. This functions's corresponding php file is similar to the list_news() function. What is changed is the last part of the function which displays the news items, part that has been replaced with the code shown below:
</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$newLastID</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$nothere</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$rows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</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: #666666; font-style: italic;">// row contains ID title body owner publishing_date publishing_time category</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</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_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span><span style="color: #009900;">&#41;</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;">$newLastID</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$newLastID</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$oldLastID</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$newLastID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nothere</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</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;">&quot;new&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$nothere</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>



<p>
The following code is used to handle clicks performed on the categories listed in the sidebar. What the code does is it checks whether the clicked category is selected. If it is, the category is removed from the php categories session and its visual style changed. If the category is not selected it is appended to the php categories session and its visual appearance changed. The php file that's used to update the session is also shown below:
</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// change style for aside category selector and filters above content + ajax requests for updating selected categories</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;aside ul li a&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> classAttr <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>classAttr <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'selected-aside'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// update style class for category just clicked</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'selected-aside'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> category <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;span&gt;X&lt;/span&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		 <span style="color: #006600; font-style: italic;">// check filter legend status and update it if required</span>
		<span style="color: #003366; font-weight: bold;">var</span> classAttr <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.categories'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>classAttr <span style="color: #339933;">==</span> <span style="color: #3366CC;">'categories not-selected'</span><span style="color: #009900;">&#41;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.categories'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'categories selected'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #006600; font-style: italic;">// now we need to make an ajax call to add our category to our selected categories session</span>
		<span style="color: #003366; font-weight: bold;">var</span> jdata <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;function=add&amp;category=&quot;</span><span style="color: #339933;">+</span>category<span style="color: #339933;">;</span> 
		$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span>
			url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;includes/categories_sessions.php&quot;</span><span style="color: #339933;">,</span>
 				data<span style="color: #339933;">:</span> jdata 
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// update style class for category just clicked</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> span <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span>span<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> category <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #006600; font-style: italic;">// now we need to make an ajax call to remove our category to our selected categories session</span>
		<span style="color: #003366; font-weight: bold;">var</span> jdata <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;function=remove&amp;category=&quot;</span><span style="color: #339933;">+</span>category<span style="color: #339933;">;</span> 
		$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span>
			url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;includes/categories_sessions.php&quot;</span><span style="color: #339933;">,</span>
 				data<span style="color: #339933;">:</span> jdata 
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #006600; font-style: italic;">// check if there is any category selected</span>
		<span style="color: #006600; font-style: italic;">// if not, update filter legend class</span>
		<span style="color: #003366; font-weight: bold;">var</span> anyCatSelected <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.selected-aside'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>anyCatSelected<span style="color: #339933;">==</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span>	
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.categories'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'categories not-selected'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #006600; font-style: italic;">// from else</span></pre></td></tr></table></div>





<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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
	<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #000088;">$err</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'function'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$function</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'function'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #000088;">$err</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'category'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$category</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'category'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #000088;">$err</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$err</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;">$function</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'add'</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: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'categories'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span> 
				<span style="color: #000088;">$categories</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'categories'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$categories</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$category</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'categories'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$categories</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;">// no previous call</span>
				<span style="color: #990000;">session_register</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'categories'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$categories</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$categories</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$category</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'categories'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$categories</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$function</span>  <span style="color: #339933;">==</span> <span style="color: #0000ff;">'remove'</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$categories</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'categories'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$categories</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$category</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'categories'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$categories</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>



<p>
However, we're not done yet with the functionality. If someone changes the categories which are selected the news must be updated and the following code does that:
</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// ajax requests to reload content and resize section + aside</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section article'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
		<span style="color: #006600; font-style: italic;">// completion of fading current content out</span>
		<span style="color: #006600; font-style: italic;">//$('section').animate({ height: sectionHeight });  // reset height of section to original value</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section article'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">&gt;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section article'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> oldcontent <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'includes/load_news.php'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>newdata<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>oldcontent<span style="color: #339933;">+</span>newdata<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #006600; font-style: italic;">// find out each new article, compute height, and set new heights for section and aside</span>
			<span style="color: #003366; font-weight: bold;">var</span> sectionHeight <span style="color: #339933;">=</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// padding, margin, included elements</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section article'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				sectionHeight <span style="color: #339933;">=</span> sectionHeight <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">10</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 10 comes from padding t b, 25 from margin top</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			asideHeight <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
			asideHeight <span style="color: #339933;">=</span> asideHeight <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'aside div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'aside li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				asideHeight <span style="color: #339933;">=</span> asideHeight <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'title'</span><span style="color: #009900;">&#41;</span> asideHeight <span style="color: #339933;">=</span> asideHeight <span style="color: #339933;">+</span> <span style="color: #CC0000;">15</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>asideHeight <span style="color: #339933;">&lt;</span> sectionHeight<span style="color: #009900;">&#41;</span> 
			<span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">//	alert (sectionHeight);</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'aside, section'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> height<span style="color: #339933;">:</span> sectionHeight <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>queue<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// update height	</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">else</span>
			<span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">//	alert (asideHeight);</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'aside, section'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> height<span style="color: #339933;">:</span> asideHeight <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>queue<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// update height	</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #006600; font-style: italic;">// done with column heights</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section article'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			overlay<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>



<p>
Based on the request processed by the <em>load_news.php</em> file we'll probably have to change the content that's visible on the homepage. Therefore, current content is faded out, new content inserted and main content area and sidebar heights updated. Only after these processes are completed the new content is faded in and displayed in the page. The <em>load_news.php</em> file simply calls the <em>load_news()</em> function.
</p>
<p>
Same content reloading has to take place if users change the date range they want via the two datepickers located in the top of the right sidebar. The process is the same as the one described above in the case of categories being changed with a simple addition to the code. The new addition is the request processed by the <em>datepicker.php</em> file which does some session updating and nothing more. The differences are shown below:
</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#start_date'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// the start date of date picker has been changed</span>
	<span style="color: #006600; font-style: italic;">// ajax call to handle it</span>
	<span style="color: #003366; font-weight: bold;">var</span> startdate <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>startdate<span style="color: #339933;">!=</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> classAttr <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.daterange'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>classAttr <span style="color: #339933;">==</span> <span style="color: #3366CC;">'daterange not-selected'</span><span style="color: #009900;">&#41;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.daterange'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'daterange selected'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
			<span style="color: #003366; font-weight: bold;">var</span> jdata <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;function=start&amp;date=&quot;</span><span style="color: #339933;">+</span>startdate<span style="color: #339933;">;</span> 
			$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
				type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span>
				url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;includes/datepicker.php&quot;</span><span style="color: #339933;">,</span>
				data<span style="color: #339933;">:</span> jdata 
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// load_news.php request as presented above in the tutorial</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#end_date'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// the start date of date picker has been changed</span>
	<span style="color: #006600; font-style: italic;">// ajax call to handle it</span>
	<span style="color: #003366; font-weight: bold;">var</span> enddate <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>enddate<span style="color: #339933;">!=</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> classAttr <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.daterange'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>classAttr <span style="color: #339933;">==</span> <span style="color: #3366CC;">'daterange not-selected'</span><span style="color: #009900;">&#41;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.daterange'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'daterange selected'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
			<span style="color: #003366; font-weight: bold;">var</span> jdata <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;function=end&amp;date=&quot;</span><span style="color: #339933;">+</span>enddate<span style="color: #339933;">;</span> 
			$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
				type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span>
				url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;includes/datepicker.php&quot;</span><span style="color: #339933;">,</span>
				data<span style="color: #339933;">:</span> jdata 
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// load_news.php request as presented above in the tutorial</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>





<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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
	<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #000088;">$err</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'function'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$function</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'function'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #000088;">$err</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'date'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'date'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #000088;">$err</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$err</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;">$function</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'start'</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: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'start'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span> 
				<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'start'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$date</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'start'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$start</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;">// no previous call</span>
				<span style="color: #990000;">session_register</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'start'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'start'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$date</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'start'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$start</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$function</span>  <span style="color: #339933;">==</span> <span style="color: #0000ff;">'end'</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: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'end'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span> 
				<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'end'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$date</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;">// no previous call</span>
				<span style="color: #990000;">session_register</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'end'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'end'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$date</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$function</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'clear'</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'start'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'start'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$start</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$end</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'end'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$end</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'end'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$end</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>



<p>
How do we actually handle new news items. During the first part of the series a hidden div was added to the page structure, which was supposed to be used on a later time. That time has come; let me remind you that small piece of HTML:
</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;article class=&quot;no-hover hidden&quot; id=&quot;newdata&quot;&gt;
	&lt;h1&gt;New articles in the system. Click here to view them&lt;/h1&gt;  
&lt;/article&gt;</pre></td></tr></table></div>



<p>
The above div is hidden but it fades in on top of all the articles if ajax requests show that new items are available in the database and they correspond with user's filters. Checking is the task of the <em> updateNews()</em> function which uses the <em>check_new_items.php</em> file. If new items are available the #newdata div is added on top of the articles displayed in the page, just as described above when the updateNews() function was shown. Here's the code that handles the clicks performed on this div and that fades out current content and displays new news items:
</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#newdata'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#newdata'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">insertAfter</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'footer'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#newdata'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeAttr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'style'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// ajax requests to reload content and resize section + aside</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section article'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
				<span style="color: #006600; font-style: italic;">// completion of fading current content out</span>
				<span style="color: #006600; font-style: italic;">//$('section').animate({ height: sectionHeight });  // reset height of section to original value</span>
				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section article'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">&gt;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section article'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> oldcontent <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				$.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'includes/load_new_news.php'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>newdata<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>oldcontent<span style="color: #339933;">+</span>newdata<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #006600; font-style: italic;">// find out each new article, compute height, and set new heights for section and aside</span>
					<span style="color: #003366; font-weight: bold;">var</span> sectionHeight <span style="color: #339933;">=</span> <span style="color: #CC0000;">75</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// padding, margin, included elements</span>
					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section article'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
						sectionHeight <span style="color: #339933;">=</span> sectionHeight <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">10</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 10 comes from padding t b, 25 from margin top</span>
					<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					asideHeight <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
					asideHeight <span style="color: #339933;">=</span> asideHeight <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'aside div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'aside li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
						asideHeight <span style="color: #339933;">=</span> asideHeight <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'title'</span><span style="color: #009900;">&#41;</span> asideHeight <span style="color: #339933;">=</span> asideHeight <span style="color: #339933;">+</span> <span style="color: #CC0000;">15</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>asideHeight <span style="color: #339933;">&lt;</span> sectionHeight<span style="color: #009900;">&#41;</span> 
					<span style="color: #009900;">&#123;</span>
					<span style="color: #006600; font-style: italic;">//	alert (sectionHeight);</span>
						$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'aside, section'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> height<span style="color: #339933;">:</span> sectionHeight <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>queue<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// update height	</span>
					<span style="color: #009900;">&#125;</span>
					<span style="color: #000066; font-weight: bold;">else</span>
					<span style="color: #009900;">&#123;</span>
					<span style="color: #006600; font-style: italic;">//	alert (asideHeight);</span>
						$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'aside, section'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> height<span style="color: #339933;">:</span> asideHeight <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>queue<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// update height	</span>
					<span style="color: #009900;">&#125;</span>
					<span style="color: #006600; font-style: italic;">// done with column heights</span>
					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'section article'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					overlay<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>



<p>
You'd think that this is all the code we need. Unfortunately, it isn't. Just placing the updateNews() function won't make wonders. We need to call this function on a set interval, 5 or 10 seconds for example. This way the users do not have to reload the page to check for new news as the app periodically checks that for them and displays a notification message if new items are added. The users only have to sit back and enjoy the feed. The code that creates the 10 second loop call of the function is shown below:
</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">setInterval<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;updateNews()&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10000</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>



<h2>Conclusion</h2>
<p>
The webapp could be used to live blog different important events, such as WWDC, Oscars and so on. Moreover, due to the categories filters one could live blog different events simultaneously on the same app.
</p>
<h2>Source code and demo</h2>
<p>
As you know from the <a href="http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-demo/">demo part of the tutorial</a> a demo of the application is available <a href="http://demos.webia.info/news_system">over here</a>. I am working on packing the source codes and they should be available to download, use and extend soon!
</p>
<p>
If you liked this tutorial series, stay updated to new content via our <a href="http://feeds2.feedburner.com/WebInternationalAwards" target="_new">RSS feed</a> or by <a href="http://feedburner.google.com/fb/a/mailverify?uri=WebInternationalAwards&#038;loc=en_US" target="_new">email</a>, because a new series will start soon. This new series will be around coding the same webapp using JSP on Tomcat.
</p>]]></content:encoded>
			<wfw:commentRss>http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-end/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Building a live news blogging system in php. Spiced with HTML5, CSS3 and jQuery [part V]</title>
		<link>http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-part-v/</link>
		<comments>http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-part-v/#comments</comments>
		<pubDate>Tue, 11 May 2010 08:00:51 +0000</pubDate>
		<dc:creator>Bogdan Pop</dc:creator>
				<category><![CDATA[tutorials]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[live news system]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webia.info/?p=1616</guid>
		<description><![CDATA[

About a month ago I started a series of tutorials on building a live blogging system in php. In case you missed it, you should definitely start with the first part of the series, or at least check out the demo.


Until now I've covered building the database, login functionalities for administrators, adding and deleting news [...]]]></description>
			<content:encoded><![CDATA[<p class="imgpreview"><a href="http://www.webia.info/wp-content/uploads/2010/03/lns_preview_full.jpg" title="Tutorial app preview"><img src="http://www.webia.info/wp-content/uploads/2010/03/lnw_preview.jpg" alt="Tutorial app preview" /></a></p>
<p>
About a month ago I started a series of tutorials on building a live blogging system in php. In case you missed it, you should definitely start with the <a href="http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-part-i/" target="_self">first part</a> of the series, or at least check out the <a href="http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-demo/" target="_self">demo</a>.
</p>
<p>
Until now I've covered building the database, login functionalities for administrators, adding and deleting news from the database. Today I'll cover editing existing news. 
</p>
<h2>The edit form</h2>


<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
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;editform hidden&quot;&gt;
	&lt;form action=&quot;editnews.php&quot; method=&quot;post&quot; accept-charset=&quot;utf-8&quot; id=&quot;form-overlay&quot; class=&quot;insertcontent&quot;&gt;
		&lt;fieldset&gt;
&nbsp;
			&lt;label for=&quot;edittitle&quot;&gt;Title : &lt;/label&gt;
			&lt;input type=&quot;text&quot; name=&quot;edittitle&quot; value=&quot;&quot; id=&quot;edittitle&quot; class=&quot;edittitle all-rounded&quot; /&gt;
			&lt;label for=&quot;editformcategory&quot;&gt;Category :&lt;/label&gt;
			&lt;select name=&quot;editformcategory&quot; id=&quot;editformcategory&quot; size=&quot;1&quot; class=&quot;editcategory&quot;&gt;
				&lt;option selected=&quot;selected&quot; value=&quot;Economics&quot;&gt;Economics&lt;/option&gt;&lt;option value=&quot;Education&quot;&gt;Education&lt;/option&gt;&lt;option value=&quot;Health&quot;&gt;Health&lt;/option&gt;&lt;option value=&quot;IT&quot;&gt;IT&lt;/option&gt;&lt;option value=&quot;Politics&quot;&gt;Politics&lt;/option&gt;&lt;option value=&quot;Weather&quot;&gt;Weather&lt;/option&gt;					&lt;/select&gt;
&nbsp;
			&lt;label for=&quot;editbody&quot;&gt;Body text :&lt;/label&gt;
			&lt;textarea name=&quot;editbody&quot; rows=&quot;8&quot; cols=&quot;40&quot; class=&quot;editbody all-rounded&quot;&gt;&lt;/textarea&gt;
			&lt;input type=&quot;hidden&quot; name=&quot;editid&quot; value=&quot;&quot; id=&quot;editid&quot; class=&quot;editid hidden&quot; /&gt;
			&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Edit news&quot; id=&quot;submit&quot; class=&quot;submit all-rounded&quot; /&gt;
		&lt;/fieldset&gt;
	&lt;/form&gt;
&lt;/div&gt;</pre></td></tr></table></div>



<p>
I coded the edit form in the first article. However, this form needs be loaded with the proper content. If it shows up empty it isn't an edit form anymore. That's done by editing the html code and inserting some php snippets as follows.</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;div class=&quot;editform hidden&quot;&gt;
	&lt;form action=&quot;editnews.php&quot; method=&quot;post&quot; accept-charset=&quot;utf-8&quot; id=&quot;form-overlay&quot; class=&quot;insertcontent&quot;&gt;
		&lt;fieldset&gt;
			&lt;label for=&quot;edittitle&quot;&gt;Title : &lt;/label&gt;
			&lt;input type=&quot;text&quot; name=&quot;edittitle&quot; value=&quot;&quot; id=&quot;edittitle&quot; class=&quot;edittitle all-rounded&quot; /&gt;
			&lt;label for=&quot;editformcategory&quot;&gt;Category :&lt;/label&gt;
			&lt;select name=&quot;editformcategory&quot; id=&quot;editformcategory&quot; size=&quot;1&quot; class=&quot;editcategory&quot;&gt;
				<span style="color: #000000; font-weight: bold;">&lt;?</span> list_categories<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select-options&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
			&lt;/select&gt;
			&lt;label for=&quot;editbody&quot;&gt;Body text :&lt;/label&gt;
			&lt;textarea name=&quot;editbody&quot; rows=&quot;8&quot; cols=&quot;40&quot; class=&quot;editbody all-rounded&quot;&gt;&lt;/textarea&gt;
			&lt;input type=&quot;hidden&quot; name=&quot;editid&quot; value=&quot;&quot; id=&quot;editid&quot; class=&quot;editid hidden&quot; /&gt;
			&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Edit news&quot; id=&quot;submit&quot; class=&quot;submit all-rounded&quot; /&gt;
		&lt;/fieldset&gt;
	&lt;/form&gt;
&lt;/div&gt;</pre></td></tr></table></div>



<p>
The only part that's changed from the raw html form is the code inside the <em>select</em> tag. I've started coding that function and presented it in the <a href="http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-part-iii/" target="_self">third part</a> of the tutorial.
</p>
<p>
However, only with the php code above, I've fixed only the category issue. The rest of the form elements are still empty. Populating them with valid data is done via jQuery. Data is read directly from the DOM and appended accordingly into the form. I've started the overlay function in the <a href="http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-part-ii/" target="_self">second part</a> of the series, and now I am altering that function to add the required functionalities. Just after the 11th line (<em>var rel = $(this).attr('rel');</em>) in the overlay's function code we add the following:
</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>rel<span style="color: #339933;">==</span><span style="color: #3366CC;">'editform'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'title'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> article <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
	<span style="color: #003366; font-weight: bold;">var</span> details <span style="color: #339933;">=</span> article.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'details'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> category <span style="color: #339933;">=</span> details.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span.cat'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> body <span style="color: #339933;">=</span> article.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'p'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> title <span style="color: #339933;">=</span> article.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'h1'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> modal_content <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.'</span><span style="color: #339933;">+</span>rel<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#overlay-content div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>modal_content<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input.editid'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input.edittitle'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>title<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'textarea.editbody'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>body<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>



<p>
By adding the above code, when someone clicks the edit link of a news item, an overlay pops out displaying the contents of the news which are loaded from the DOM itself (directly from the <em>article</em> tags). Users can then edit the content and save it.
</p>
<h2>editnews.php</h2>
<p>
We need to somehow process the edit request and we do that with a script similar to the addnews.php file I talked about in <a href="http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-part-iii/" target="_self">part three</a> of the series.
</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;includes/functions.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">session_is_registered</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'logged in key'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$er</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'edittitle'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$er</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'1'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'edittitle'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'editformcategory'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$er</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'1'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #000088;">$formcategory</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'editformcategory'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'editbody'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$er</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'1'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #000088;">$body</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'editbody'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'editid'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$er</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'1'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'editid'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$er</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;location: index.php?error=Please input username and password&quot;</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: #009900;">&#123;</span>
		<span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> strip_all<span style="color: #009900;">&#40;</span><span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$formcategory</span> <span style="color: #339933;">=</span> strip_all<span style="color: #009900;">&#40;</span><span style="color: #000088;">$formcategory</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$body</span> <span style="color: #339933;">=</span> strip_all<span style="color: #009900;">&#40;</span><span style="color: #000088;">$body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UPDATE News SET `body` = '<span style="color: #006699; font-weight: bold;">$body</span>', `title` = '<span style="color: #006699; font-weight: bold;">$title</span>', `category` = '<span style="color: #006699; font-weight: bold;">$formcategory</span>' WHERE `id`='<span style="color: #006699; font-weight: bold;">$id</span>';&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$query</span><span style="color: #339933;">;</span>
		<span style="color: #339933;">@</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_query</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: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;location: index.php?msg=News edited successfully&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;location: index.php?error=You must be logged in to access this feature.&quot;</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>
The above code first checks for administrator status, then form data input errors. If everything is in order the news is edited and changes saved to the database.
</p>
<p>If you enjoyed this article you can stay updated to new content via our <a href="http://feeds2.feedburner.com/WebInternationalAwards" target="_new">RSS feed</a> or by <a href="http://feedburner.google.com/fb/a/mailverify?uri=WebInternationalAwards&#038;loc=en_US" target="_new">email</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.webia.info/articles/tutorials/building-a-live-news-blogging-system-in-php-spiced-with-html5-css3-and-jquery-part-v/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to display ratings as stars using CSS and a bit of jQuery</title>
		<link>http://www.webia.info/articles/css-articles/how-to-display-ratings-as-stars-using-css-and-a-bit-of-jquery/</link>
		<comments>http://www.webia.info/articles/css-articles/how-to-display-ratings-as-stars-using-css-and-a-bit-of-jquery/#comments</comments>
		<pubDate>Tue, 04 May 2010 10:00:11 +0000</pubDate>
		<dc:creator>Bogdan Pop</dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[techniques]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[maps]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rating]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[stars]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[ui]]></category>
		<category><![CDATA[ux]]></category>

		<guid isPermaLink="false">http://www.webia.info/?p=1598</guid>
		<description><![CDATA[
On a project I am working on I have different objects that can be rated by users. I need to display their rating which ranges from 0 to 5 with a 0.01 step as I am using floating numbers. I found a neat and simple way to do this, semantically correct with valid markup. 



In [...]]]></description>
			<content:encoded><![CDATA[<p>
On a project I am working on I have different objects that can be rated by users. I need to display their rating which ranges from 0 to 5 with a 0.01 step as I am using floating numbers. I found a neat and simple way to do this, semantically correct with valid markup. 
</p>
<p class="imgpreview"><a href="http://www.webia.info/wp-content/uploads/2010/05/stars.jpg" title="Preview of rating stars"><img src="http://www.webia.info/wp-content/uploads/2010/05/stars.jpg" alt="Preview of rating stars" /></a></p>
<p>
In the above screenshot you can check out what I'm trying to achieve. See those stars right under the name of the location? Well, that is done purely with CSS and two images. Let's see the HTML markup required.
</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;restaurant-stars&quot;&gt;
	&lt;div class=&quot;restaurant-stars-rating&quot; title=&quot;2.5&quot;&gt;              
		&amp;nbsp;
	&lt;/div&gt;
&lt;/div&gt;</pre></td></tr></table></div>



<p>
You may ask why am I using two different DIVs instead of one. Let me explain. We need the gray stars that are used to display the maximum rating achievable. We also need orange stars to display the actual rating. The orange stars must lay on top of the gray ones. 
</p>
<p>
To achieve this, the <em>restaurant-stars</em> div has a specific width. Mine is 76 pixels. Moreover, the background image of this div is an image containing all the 5 gray stars. Background image is set not to repeat. The <em>restaurant-stars-rating</em> div has a width that has to be between 0 and the <em>restaurant-stars</em> div's width, which is 76 pixels. The one I used in the example is 38 pixels. The background image of this second div is an image that contains 5 orange stars.
</p>
<p>
The trick is right here, in the length of the inner div. As its width is lower, the background image will not span all the way to cover up the outer div's background image, resulting in an awesome way to semantically display a star based rating value.
</p>
<h2>What's the CSS?</h2>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.restaurant-stars</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">block</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">76px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">20px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'images/rating_default.jpg'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.restaurant-stars-rating</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">block</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">38px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">20px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'images/rating_stared.jpg'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>



<p>
That's all that needs to be coded! 
</p>
<h2>Enhancing it with a bit of jQuery</h2>
<p>
I don't think that such a feature should be used on a static website, so how do you tackle dynamic content that has different values for ratings? I am using this rating feature on a Google Maps infoWindow. I am using jQuery to dynamically change it based on data I get from a JSON object passed along from a Rails application. Here's the code:
</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.restaurant-stars-rating'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span>foundRestaurant.<span style="color: #660066;">resrating</span><span style="color: #339933;">*</span><span style="color: #CC0000;">76</span><span style="color: #339933;">/</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>



<p>
As you can see I am simply computing the width value of my <em>restaurant-stars-rating</em> div as the product between the rating I have in the database with the outer div's width, divided by the maximum rating. Simple as that!
</p>
<p>If you enjoyed this article you can stay updated to new content via our <a href="http://feeds2.feedburner.com/WebInternationalAwards" target="_new">RSS feed</a> or by <a href="http://feedburner.google.com/fb/a/mailverify?uri=WebInternationalAwards&#038;loc=en_US" target="_new">email</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.webia.info/articles/css-articles/how-to-display-ratings-as-stars-using-css-and-a-bit-of-jquery/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

