<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.2.3" -->
<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/"
	>

<channel>
	<title>PHP-Scripts Blog &#187; How To</title>
	<link>http://www.php-scripts.com</link>
	<description>Writing about PHP scripting since 12/99. Learn something new every day.</description>
	<pubDate>Thu, 09 Oct 2008 14:17:07 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.3</generator>
	<language>en</language>
			<item>
		<title>Date and time page last updated</title>
		<link>http://www.php-scripts.com/20070329/104/</link>
		<comments>http://www.php-scripts.com/20070329/104/#comments</comments>
		<pubDate>Thu, 29 Mar 2007 16:22:05 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[one liners]]></category>

		<category><![CDATA[How To]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/20070329/104/</guid>
		<description><![CDATA[A PHP one-liner to keep the date/time the page was last updated:

page last updated &#60;?php echo date&#40;&#8220;F d Y H:i:s&#8221;, getlastmod&#40;&#41; &#41;; ?&#62;

Note: uses the server timezone by default. getlastmod() returns a timestamp and if you are on a shared server (virtual hosting) and cannot change the timezone, let&#8217;s say the timezone is off by [...]]]></description>
			<content:encoded><![CDATA[<p>A PHP one-liner to keep the date/time the page was last updated:</p>
<div class="codesnip-container" >
<div class="codesnip">page last updated &lt;?php <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#8220;F d Y H:i:s&#8221;</span>, <a href="http://www.php.net/getlastmod"><span class="kw3">getlastmod</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>; <span class="kw2">?&gt;</span></div>
</div>
<p>Note: uses the server timezone by default. getlastmod() returns a timestamp and if you are on a shared server (virtual hosting) and cannot change the timezone, let&#8217;s say the timezone is off by two hours just use the following:</p>
<div class="codesnip-container" >
<div class="codesnip">page last updated &lt;?php <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#8220;F d Y H:i:s&#8221;</span>, <span class="br0">&#40;</span><a href="http://www.php.net/getlastmod"><span class="kw3">getlastmod</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span> - <span class="nu0">7200</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>; <span class="kw2">?&gt;</span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20070329/104/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to use MySQL REPLACE function</title>
		<link>http://www.php-scripts.com/20070314/101/</link>
		<comments>http://www.php-scripts.com/20070314/101/#comments</comments>
		<pubDate>Wed, 14 Mar 2007 17:02:43 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[How To]]></category>

		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[Functions]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/20070314/101/</guid>
		<description><![CDATA[Here&#8217;s a MySQL query fuction that&#8217;s easy to forget about: REPLACE. Let&#8217;s say I have a bunch of records in a field with http://www and want to quickly change all to http://. Here&#8217;s the syntax to update:

UPDATE tdurl_1 SET URL = REPLACE(URL, &#8216;http://www.tdurl.com/&#8217;,'http://tdurl.com/&#8217;);

]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a MySQL query fuction that&#8217;s easy to forget about: REPLACE. Let&#8217;s say I have a bunch of records in a field with http://www and want to quickly change all to http://. Here&#8217;s the syntax to update:</p>
<div class="codesnip-container" >
<div class="codesnip">UPDATE tdurl_1 SET URL = REPLACE(URL, &#8216;http://www.tdurl.com/&#8217;,'http://tdurl.com/&#8217;);</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20070314/101/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to build a how many days in the future script with timezone offset</title>
		<link>http://www.php-scripts.com/20061221/98/</link>
		<comments>http://www.php-scripts.com/20061221/98/#comments</comments>
		<pubDate>Thu, 21 Dec 2006 18:31:36 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Date and Time]]></category>

		<category><![CDATA[How To]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/20061221/98/</guid>
		<description><![CDATA[This morning I was wondering what the date would be an arbitrary number of days in the future, like if somebody says: get back to me in a few months. I decided to search Google to see if there was a simple, web-based form that I could enter in the number of days in the [...]]]></description>
			<content:encoded><![CDATA[<p>This morning I was wondering what the date would be an arbitrary number of days in the future, like if somebody says: get back to me in a few months. I decided to search Google to see if there was a simple, web-based form that I could enter in the number of days in the future and my timezone and get returned the date/time in the future.</p>
<p>A couple variations of queries didn&#8217;t turn up anything that fit what I was looking for, so I decided to build my own and share the process here. For this project we&#8217;ll be using the following built-in PHP functions: </p>
<p>count - get the size of the timezonelist array<br />
time - get timestamp<br />
strtotime - add form submitted amount of time<br />
date - format the date<br />
putenv - change timezone</p>
<p><b>Data needed</b><br />
List of timezones inserted into a sorted array. I&#8217;ve done the work here and provide in an array called $timezonelist. This will be used in the dropdown input form so the person using can set to their timezone.</p>
<p><b>Security concerns</b><br />
To prevent users from entering in bogus timezones we&#8217;ll tie to a valid array index and check before using putenv() to change.</p>
<p>STEP 1. Add a comment header to explain the code and licensing terms:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="coMULTI">/* Created: 12/21/2006<br />
daysinfuture2.php -&gt; original code by TDavid @ tdscripts.com <br />
License: Creative Commons 2.5 Attribution<br />
Desscription: Enter in days in the future and find out the date<br />
*/</span></div>
</div>
<p>STEP 2. Add the sorted time zone list array and determine the array size using the count function. Due to the size of the array, I&#8217;m including only a couple entries in the code below. You&#8217;ll be able to view the entire array at the end of this post.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$timezonelist</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;Africa/Abidjan&#8217;</span>,<span class="st0">&#8216;Africa/Accra&#8217;</span><span class="br0">&#41;</span>; <span class="co1">// incomplete timezone list, example only</span><br />
<span class="re0">$sizetz</span> = <a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$timezonelist</span><span class="br0">&#41;</span>;</div>
</div>
<p>STEP 3. Handle the input from the form submit, if any and validate the input as integers. The maximum number of days allowed into the future is 10,000. </p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$fdays</span> = <span class="br0">&#40;</span>int<span class="br0">&#41;</span><span class="re0">$_POST</span><span class="br0">&#91;</span><span class="st0">&#8216;d&#8217;</span><span class="br0">&#93;</span>; <span class="co1">// number of days entered from form</span><br />
<span class="re0">$ftz</span> = <span class="br0">&#40;</span>int<span class="br0">&#41;</span><span class="re0">$_POST</span><span class="br0">&#91;</span><span class="st0">&#8216;tz&#8217;</span><span class="br0">&#93;</span>; <span class="co1">// timezone chosen from</span></p>
<p><span class="co1">// 10,000 days max</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$fdays</span> &amp;lt;<span class="nu0">0</span> || <span class="re0">$fdays</span> &gt;<span class="nu0">10000</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="re0">$fdays</span> = <span class="nu0">7</span>; <span class="co1">// week from now is default</span><br />
<span class="br0">&#125;</span></p>
<p><span class="co1">// default timezone is Los Angeles / PST</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$ftz</span> &gt; <span class="re0">$sizetz</span> || <span class="re0">$ftz</span> &lt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="re0">$ftz</span> = <span class="nu0">107</span>; <span class="co1">// default timezone PST</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>STEP 4. Set the timezone using the index in the timezonelist array chosen by the person submitting the form or the default if an invalid or no selection was made.</p>
<div class="codesnip-container" >
<div class="codesnip"><a href="http://www.php.net/putenv"><span class="kw3">putenv</span></a><span class="br0">&#40;</span><span class="st0">&#8220;TZ=$timezonelist[$ftz]&#8221;</span><span class="br0">&#41;</span>;</div>
</div>
<p>STEP 5. Get the current time and calculate the number of days into the future.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$today</span> = <a href="http://www.php.net/time"><span class="kw3">time</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$future_tense</span> = <a href="http://www.php.net/strtotime"><span class="kw3">strtotime</span></a><span class="br0">&#40;</span><span class="st0">&#8220;+$fdays days&#8221;</span>, <span class="re0">$today</span><span class="br0">&#41;</span>;</div>
</div>
<p>STEP 6. Output to the browser the current formatted date/time and future date/time. Then close the PHP script.</p>
<div class="codesnip-container" >
<div class="codesnip"><a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;Now: &#8220;</span> . <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#8220;l M d, Y H:i:s&#8221;</span>, <span class="re0">$today</span><span class="br0">&#41;</span> . <span class="st0">&#8221; &lt;i&gt;$timezonelist[$ftz]&lt;br /&gt;&lt;font color=<span class="es0">\&#8221;</span>green<span class="es0">\&#8221;</span>&gt;&lt;b&gt;+ $fdays days =&lt;/b&gt;&lt;/font&gt; &lt;b&gt;&#8221;</span> . <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#8220;l M d, Y H:i:s&#8221;</span>, <span class="re0">$future_tense</span><span class="br0">&#41;</span> . <span class="st0">&#8216;&lt;/b&gt;&#8217;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p>STEP 7.  Create the first part of the form for the person to submit to the script the days into  the future and timezone. At the top of the list select the USA Los Angeles time (PST) as default. The values for the options are the index position in the timezone array.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="sc2"><a href="http://december.com/html/4/element/form.html"><span class="kw2">&lt;form</span></a> <span class="kw3">method</span>=<span class="st0">&#8220;POST&#8221;</span> <span class="kw3">action</span>=<span class="st0">&#8220;daysinfuture2.php&#8221;</span><span class="kw2">&gt;</span></a></span><br />
Number of days in the future? <span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> <span class="kw3">type</span>=<span class="st0">&#8220;text&#8221;</span> <span class="kw3">size</span>=<span class="st0">&#8220;3&#8243;</span> <span class="kw3">name</span>=<span class="st0">&#8220;d&#8221;</span> <span class="kw3">value</span>=<span class="st0">&#8220;&lt;?php echo($fdays);?/&gt;</span>&quot;&gt; Timezone? (pending) <br />
<span class="sc2">&lt;select name=&#8221;</span>tz<span class="st0">&#8220;&gt;</span><br />
<span class="sc2">&lt;option SELECTED value=&#8221;</span>107<span class="st0">&#8220;&gt;</span>USA - Pacific (GMT -8)<span class="sc2">&lt;/option&gt;</span><br />
<span class="sc2">&lt;option value=&#8221;</span>70<span class="st0">&#8220;&gt;</span>USA - Midwest (GMT -6)<span class="sc2">&lt;/option&gt;</span><br />
<span class="sc2">&lt;option value=&#8221;</span>122<span class="st0">&#8220;&gt;</span>USA - Eastern (GMT -5)<span class="sc2">&lt;/option&gt;</span> </span></div>
</div>
<p>STEP 8. Dynamically generate all sorted timezones from the array.</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="re0">$y</span>=<span class="nu0">0</span>;<span class="re0">$y</span>&lt;<span class="re0">$sizetz</span>;<span class="re0">$y</span>++<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a><span class="br0">&#40;</span><span class="st0">&#8220;&lt;option value=<span class="es0">\&#8221;</span>$y<span class="es0">\&#8221;</span>&gt;$timezonelist[$y]<span class="es0">\n</span>&#8220;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p>STEP 9. Now close the HTML form code and we&#8217;re done.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="sc2"><span class="kw2">&lt;/select&gt;</span></span> <span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> <span class="kw3">type</span>=<span class="st0">&#8220;submit&#8221;</span> <span class="kw3">value</span>=<span class="st0">&#8220;See future date&#8221;</span>/<span class="kw2">&gt;</span></a></span><br />
<span class="sc2"><span class="kw2">&lt;/form&gt;</span></span></div>
</div>
<p>Test execution of <a href="http://www.php-scripts.com/examples/daysinfuture2.php">daysinfuture2.php</a><br />
Complete <a href="http://www.php-scripts.com/examples/daysinfuture2.phps">source code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20061221/98/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to randomize, return and remove numbers from a pool</title>
		<link>http://www.php-scripts.com/20061211/97/</link>
		<comments>http://www.php-scripts.com/20061211/97/#comments</comments>
		<pubDate>Mon, 11 Dec 2006 17:30:17 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Random]]></category>

		<category><![CDATA[How To]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/20061211/97/</guid>
		<description><![CDATA[This morning in the IRC chat (irc.scriptschool.com #scriptschool) a random visitor named Rnd-Amarion stopped by with the following how-to PHP question:
I am about to learn PHP, and want to program a simple web page with a long string of random numbers. I have the random numbers allready. When someone queries the web site, it retrieves [...]]]></description>
			<content:encoded><![CDATA[<p>This morning in the IRC chat (irc.scriptschool.com #scriptschool) a random visitor named Rnd-Amarion stopped by with the following how-to PHP question:<br />
<blockquote>I am about to learn PHP, and want to program a simple web page with a long string of random numbers. I have the random numbers allready. When someone queries the web site, it retrieves four of those numbers from the front of this list and gives them to the querier and then removes those numbers form the front fo the list.</p></blockquote>
<p>I asked him what were the range of numbers he wanted randomized. While this information wasn&#8217;t critical to writing the code to solve this how-to, it would better demonstrate to him (her? I didn&#8217;t ask). Didn&#8217;t get a response but I decided to help this unknown person anyway.</p>
<p>Let&#8217;s consult the PHP manual for the function array_rand (PHP 4, 5) and the definition:<br />
<blockquote>array_rand &#8212; Pick one or more random entries out of an array</p></blockquote>
<p>That sounds like the right function, but with most programming languages there are many ways to approach the same problem. Let&#8217;s create some code to do this using the PHP functions range, shuffle and array_shift.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$random_numbers</span> = <a href="http://www.php.net/range"><span class="kw3">range</span></a><span class="br0">&#40;</span><span class="nu0">1</span>,<span class="nu0">100</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/shuffle"><span class="kw3">shuffle</span></a><span class="br0">&#40;</span><span class="re0">$random_numbers</span><span class="br0">&#41;</span>; <span class="co1">// mix up the order</span><br />
<span class="co1">// return the first four numbers when queried and remove from array</span><br />
<span class="re0">$numbers_chosen</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="re0">$random_numbers</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, <span class="re0">$random_numbers</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>, <span class="re0">$random_numbers</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>, <span class="re0">$random_numbers</span><span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;</p>
<p><a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;Before: &lt;br /&gt;&#8221;</span>;<br />
<a href="http://www.php.net/print_r"><span class="kw3">print_r</span></a><span class="br0">&#40;</span><span class="re0">$random_numbers</span><span class="br0">&#41;</span>; <span class="co1">// show all random numbers</span></p>
<p><span class="co1">// remove first four numbers</span><br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="re0">$i</span>=<span class="nu0">0</span>;<span class="re0">$i</span>&amp;lt;<span class="nu0">4</span>;<span class="re0">$i</span>++<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<a href="http://www.php.net/array_shift"><span class="kw3">array_shift</span></a><span class="br0">&#40;</span><span class="re0">$random_numbers</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><a href="http://www.php.net/print"><span class="kw3">print</span></a><span class="st0">&#8220;&lt;hr /&gt;After: &#8220;</span>;<br />
<a href="http://www.php.net/print_r"><span class="kw3">print_r</span></a><span class="br0">&#40;</span><span class="re0">$random_numbers</span><span class="br0">&#41;</span>; <span class="co1">// show all remaining random numbers</span><br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a><span class="st0">&#8220;&lt;hr /&gt;Chosen: &#8220;</span>;<br />
<a href="http://www.php.net/print_r"><span class="kw3">print_r</span></a><span class="br0">&#40;</span><span class="re0">$numbers_chosen</span><span class="br0">&#41;</span>; <span class="co1">// first four numbers chosen </span><br />
&nbsp;</div>
</div>
<p>Example: <a href="http://www.php-scripts.com/examples/array_shift.php">code execution</a>, <a href="http://www.php-scripts.com/examples/array_shift.phps">code source</a></p>
<p>Notes: The array_shift function removes the first item in an array. The converse array_pop removes the last item in an array. Another useful array function is array_splice.</p>
<p><b>Update 9:55am PST</b>: You can see a more efficient version using <a href="http://www.php-scripts.com/examples/array_splice.phps">array_splice here</a> that eliminates the use of the for loop in the code above and the use of array_shift. As for which is more efficient for removing only one item in the array, that&#8217;s a test for you to run :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20061211/97/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Tips for speeding up your PHP scripts</title>
		<link>http://www.php-scripts.com/20061207/96/</link>
		<comments>http://www.php-scripts.com/20061207/96/#comments</comments>
		<pubDate>Thu, 07 Dec 2006 14:49:15 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[How To]]></category>

		<category><![CDATA[Optimization]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/20061207/96/</guid>
		<description><![CDATA[Jaslabs shares six key points on improving the speed of your dynamic PHP scripts including: object code caching, template system, distributed object caching system, turning off some global PHP variable settings, output compression and database query optimization.
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.whenpenguinsattack.com/2006/08/14/using-php-in-large-websites-redone/?artid=134">Jaslabs shares</a> six key points on improving the speed of your dynamic PHP scripts including: object code caching, template system, distributed object caching system, turning off some global PHP variable settings, output compression and database query optimization.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20061207/96/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to fix reBlog 2.0b2 URL sanitizing bug</title>
		<link>http://www.php-scripts.com/20060617/86/</link>
		<comments>http://www.php-scripts.com/20060617/86/#comments</comments>
		<pubDate>Sat, 17 Jun 2006 18:39:14 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Bugs]]></category>

		<category><![CDATA[How To]]></category>

		<category><![CDATA[Information]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/20060617/86/</guid>
		<description><![CDATA[Behavior: Links in Reblog cannot be &#8216;archived&#8217; or &#8216;published&#8217;
Problem: Reblog wasn&#8217;t sanitizing the &#8216;link&#8217; field in the MySQL table properly, allowing injection of unescaped single quotes (&#8217;) from RSS feeds into the database. The query would fail and thus would never process properly.
File: Controller.class.php in /refeed/library/RF
Version: $Revision: 1.40 (reBlog version 2.0b2)
Status: Bug reported with code [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Behavior</strong>: Links in Reblog cannot be &#8216;archived&#8217; or &#8216;published&#8217;<br />
<strong>Problem</strong>: Reblog wasn&#8217;t sanitizing the &#8216;link&#8217; field in the MySQL table properly, allowing injection of unescaped single quotes (&#8217;) from RSS feeds into the database. The query would fail and thus would never process properly.<br />
<strong>File</strong>: Controller.class.php in /refeed/library/RF<br />
<strong>Version: $Revision: 1.40 (reBlog version 2.0b2)<br />
</strong><strong>Status</strong>: Bug reported with code fix implemented below. I didn&#8217;t see a solution posted in the official <a href="http://sourceforge.net/forum/forum.php?forum_id=363574">Reblog help forum</a>. I have posted about the issue there and linked here.</p>
<p><strong>Example database call</strong> (http part is stripped so WP doesn&#8217;t make a link, error emphasized)<br />
mysql> select link from items where link like &#8216;%\&#8217;%';</p>
<p>| businessweek.com/technology/content/jun2006/tc20060615_290127.htm?<br />
chan=technology_technology+index+page_more+of+today<font color="red"><strong>&#8216;</strong></font>s+top+stories |</p>
<p>1 row in set (1.81 sec)</p>
<p>Note: this link isn&#8217;t being sent properly by del.icio.us either. It should have been converted to a safe URL by them and then transported through the RSS feed. This leaves the problem to the aggregator to sanitize before entering the database &#8212; which should always be done anyway.</p>
<p>This URL bug ran through three places: businessweek RSS feed (source), delicious popular (tagged) and reblog (aggregator, and into the database).</p>
<p>The proper code to sanitize query strings from URLs like this:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$the_link</span> = <span class="st0">&#8220;?chan=technology_technology+index+page_more+of+today‘s+top+stories&#8221;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;before: $the_link</p>
<p>&lt;hr /&gt;After: &#8220;</span> . <a href="http://www.php.net/urlencode"><span class="kw3">urlencode</span></a><span class="br0">&#40;</span><span class="re0">$the_link</span><span class="br0">&#41;</span>;</div>
</div>
<p>If this step had been taken before transferring the URL to the source RSS feed (businessweek) or in the tagging (delicious popular) then it wouldn&#8217;t be needed in the RSS Aggregator (Reblog). Since that&#8217;s not the case, I decided to modify the Reblog code to deal with unsanitized URLs before they reach the database and render future database writes in Reblog disfunctional. Because Reblog can use multiple input sources it makes more sense to alter the point just before the save routine rather than alter the code of each input source (like magepieRSS).</p>
<p><strong>Reblog</strong><br />
1. locate Controller.class.php in /refeed/library/RF<br />
2. line to patch is in saveItem() function starting around line 1679 as follows:</p>
<p>Locate this:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">function</span> saveItem<span class="br0">&#40;</span>&amp;<span class="re0">$item</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$dbhw</span> =&amp; <span class="re0">$this</span>-&gt;<span class="me1">getWriteHandle</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$data</span> = <span class="re0">$item</span>-&gt;<span class="me1">columnNamesValues</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</div>
<p>And <strong>CHANGE</strong> to this:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">function</span> saveItem<span class="br0">&#40;</span>&amp;<span class="re0">$item</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$dbhw</span> =&amp; <span class="re0">$this</span>-&gt;<span class="me1">getWriteHandle</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$data</span> = <span class="re0">$item</span>-&gt;<span class="me1">columnNamesValues</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">// sanitize all links being saved with single quote</span><br />
<span class="re0">$data</span><span class="br0">&#91;</span><span class="st0">&#8216;link&#8217;</span><span class="br0">&#93;</span> = <a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st0">&#8220;&#8216;&#8221;</span>,<span class="st0">&#8220;%27&#8243;</span>,<span class="re0">$data</span><span class="br0">&#91;</span><span class="st0">&#8216;link&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;</div>
</div>
<p>Notice this patch string replaces any instance of the single quote character in the link to the %27 sanitized version. A more comprehensive fix would be to urlencode the query string portion of the URL shown earlier in this post. My concern was fixing that specific character which kept causing problems.</p>
<p>You can test this issue in your version of Reblog with the broken del.icio.us feed cached here (not linked):<br />
php-scripts.com/examples/popular-delicious_06172006.xml</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20060617/86/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Dealing with the browser buttons in AJAX</title>
		<link>http://www.php-scripts.com/20060609/85/</link>
		<comments>http://www.php-scripts.com/20060609/85/#comments</comments>
		<pubDate>Fri, 09 Jun 2006 10:44:15 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[How To]]></category>

		<category><![CDATA[AJAX]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/20060609/85/</guid>
		<description><![CDATA[Developing PHP the AJAX Way [article]:
A major challenge of Asynchronous JavaScript and XML (Ajax)-driven Web sites is the lack of a Back button. We will use JavaScript to create a history stack for the Ajax photo gallery
Good article on how to deal with the browser Back, Forward and Reload button.
]]></description>
			<content:encoded><![CDATA[<p>Developing PHP the AJAX Way [<a href="http://www-128.ibm.com/developerworks/opensource/library/os-php-rad2/?ca=dgr-btw01AjaxAndPHP">article</a>]:<br />
<blockquote>A major challenge of Asynchronous JavaScript and XML (Ajax)-driven Web sites is the lack of a Back button. We will use JavaScript to create a history stack for the Ajax photo gallery</p></blockquote>
<p>Good article on how to deal with the browser Back, Forward and Reload button.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20060609/85/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Showing a user&#8217;s profile picture one time on a page</title>
		<link>http://www.php-scripts.com/20060106/76/</link>
		<comments>http://www.php-scripts.com/20060106/76/#comments</comments>
		<pubDate>Fri, 06 Jan 2006 16:40:52 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[How To]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/?p=76</guid>
		<description><![CDATA[This is more of a proof of concept, but is in response to a post at Performancing where the concern is the same profile picture showing up more than one time on the front page:
What bothers me most, is how it looks when one author (me) might publish like 5 times in a row&#8230;
Let&#8217;s say [...]]]></description>
			<content:encoded><![CDATA[<p>This is more of a proof of concept, but is in response to a <a href="http://performancing.com/node/769">post at Performancing</a> where the concern is the same profile picture showing up more than one time on the front page:<br />
<blockquote>What bothers me most, is how it looks when one author (me) might publish like 5 times in a row&#8230;</p></blockquote>
<p>Let&#8217;s say you have a script which outputs articles by different authors on the front page and rolls off after that. How do you only show the author&#8217;s profile picture one time?</p>
<p><b>Solution</b><br />
First, let&#8217;s make some assumptions. In the data you&#8217;ll have the author name as part of the byline and author&#8217;s ID. It&#8217;s better to use an ID than the author&#8217;s name because of name collision (there might be two authors with the name John Smith).</p>
<p>The database is pulling out each article and author ID. Let&#8217;s create some bogus data as an example and separate (delimit) the data by the pipe | symbol:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="re0">$article_data</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
<span class="st0">&#8220;1|John Smith|this is my first article&#8221;</span>,<br />
<span class="st0">&#8220;2|Harry Smith|I am John&#8217;s brother&#8221;</span>,<br />
<span class="st0">&#8220;1|John Smith|Their relation to me&#8221;</span>,<br />
<span class="st0">&#8220;1|John Smith|I&#8217;m hung up talking about their relation to me&#8221;</span>,<br />
<span class="st0">&#8220;4|John Smith|I&#8217;m a different John Smith with the same name&#8221;</span>,<br />
<span class="st0">&#8220;3|Mary Smith|I&#8217;m John&#8217;s wife&#8221;</span>,<br />
<span class="st0">&#8220;1|John Smith|Ok, I&#8217;m married to Mary and Harry&#8217;s my brother&#8221;</span><br />
<span class="br0">&#41;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Now, as you can see above, the author John Smith would dominate the front page, but I threw in some name collision with the John Smith and author ID 4. That&#8217;s a different John Smith so if we matched only on the name, our code that follows wouldn&#8217;t work. So we use the unique author ID field to determine if we&#8217;ve previously shown an article by author ID on the page as follows:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="re0">$article_data</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
<span class="st0">&#8220;1|John Smith|this is my first article&#8221;</span>,<br />
<span class="st0">&#8220;2|Harry Smith|I am John&#8217;s brother&#8221;</span>,<br />
<span class="st0">&#8220;1|John Smith|Their relation to me&#8221;</span>,<br />
<span class="st0">&#8220;1|John Smith|I&#8217;m hung up talking about their relation to me&#8221;</span>,<br />
<span class="st0">&#8220;4|John Smith|I&#8217;m a different John Smith with the same name&#8221;</span>,<br />
<span class="st0">&#8220;3|Mary Smith|I&#8217;m John&#8217;s wife&#8221;</span>,<br />
<span class="st0">&#8220;1|John Smith|Ok, I&#8217;m married to Mary and Harry&#8217;s my brother&#8221;</span><br />
<span class="br0">&#41;</span>;<br />
<span class="re0">$picture_shown</span> = <span class="st0">&#8221;</span>; <span class="co1">// empty array</span><br />
<span class="re0">$number_articles</span> = <a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$article_data</span><span class="br0">&#41;</span>;<br />
<span class="co1">// start article loop</span><br />
&nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="re0">$i</span>=<span class="nu0">0</span>;<span class="re0">$i</span>&lt;<span class="re0">$number_articles</span>;<span class="re0">$i</span>++<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="co1">// split the delimited data into the $parts array</span><br />
&nbsp; &nbsp; &nbsp; <span class="re0">$parts</span> = <a href="http://www.php.net/explode"><span class="kw3">explode</span></a><span class="br0">&#40;</span><span class="st0">&#8220;|&#8221;</span>,<span class="re0">$article_data</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span><span class="br0">&#41;</span>; <br />
&nbsp; &nbsp; &nbsp; <span class="re0">$author_ID</span> = <span class="re0">$parts</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>; <span class="co1">// first item in $parts array is author ID</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>!<span class="re0">$picture_shown</span><span class="br0">&#91;</span><span class="re0">$author_id</span><span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// new article by author on page, show picture</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$picture_shown</span><span class="br0">&#91;</span><span class="re0">$author_id</span><span class="br0">&#93;</span> = <span class="nu0">1</span>; <span class="co1">// set times shown </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;[I would show $author_ID.gif] &#8220;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// already shown article by this author on page, do NOT show picture</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$picture_shown</span><span class="br0">&#91;</span><span class="re0">$author_id</span><span class="br0">&#93;</span>++;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;&lt;i&gt;&#8217;</span> . <a href="http://www.php.net/ucwords"><span class="kw3">ucwords</span></a><span class="br0">&#40;</span><span class="re0">$parts</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span><span class="br0">&#41;</span> . <span class="st0">&#8221;</span> . <span class="st0">&#8221; by $parts[1] (ID#$author_ID)&lt;br /&gt;&#8221;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Notes: The use of the ucwords($string) which makes sure to capitalize the first word of the article title. The line marked &#8220;I would show $author_ID.gif&#8221; would be replaced with the HTML img src code to insert the author&#8217;s image. This script assumes the image for the author is stored with the format: author_ID.gif. It could be .jpg, .png or whatever of course.</p>
<p>Happy coding to you!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20060106/76/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Debugging numerical sorting with strings</title>
		<link>http://www.php-scripts.com/20060103/75/</link>
		<comments>http://www.php-scripts.com/20060103/75/#comments</comments>
		<pubDate>Wed, 04 Jan 2006 01:32:41 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[How To]]></category>

		<category><![CDATA[Functions]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/?p=75</guid>
		<description><![CDATA[Ahh, good to be back after the holidays. Took a little time off from most the blogs including this one for those who are new readers. 
In the meantime a good question came in on the sorting post back in October from Beregszászi Mihály, who wrote: 

I have got a problem:
rsort($numbers);
Print:
9
8
7
70
6
69
68
67
This is my problem! My [...]]]></description>
			<content:encoded><![CDATA[<p>Ahh, good to be back after the holidays. Took a little time off from most the blogs including this one for those who are new readers. </p>
<p>In the meantime a good question came in on the <a href="http://www.php-scripts.com/20051014/40/">sorting post back in October</a> from Beregszászi Mihály, who wrote: </p>
<blockquote><p>
I have got a problem:<br />
rsort($numbers);</p>
<p>Print:<br />
9<br />
8<br />
7<br />
70<br />
6<br />
69<br />
68<br />
67</p>
<p>This is my problem! My dream:<br />
70<br />
69<br />
68<br />
67<br />
66<br />
65<br />
9<br />
8<br />
7<br />
6
</p></blockquote>
<p>The first thing I did was whip up some example code to test the rsort() and asort() functions. I didn&#8217;t find the situation that he was talking about so I tried to work it backwards and treat the numbers as strings with spaces and possibly returns and other hidden characters in them.</p>
<p>This created the following sort bug that Beregszászi was talking about (and yet he didn&#8217;t offer the code for):</p>
<p>[code=&#8221;html&#8221;]<br />
Reverse sort -> rsort()<br />
9,8 ,70 ,7 ,69 ,68,67,6<br />
[/code]</p>
<p><b>Solution</b><br />
What Beregszászi is looking for is a numerical sort using data that isn&#8217;t in numerical type. To change the type this is called <i>casting</i>. PHP isn&#8217;t as anal as <strike>some</strike> most languages about using the right type but it is critical in <strike>some</strike> most other languages. It&#8217;s best to get in the habit as a programmer as always ensuring the data type matches or you&#8217;ll end up with type mismatch errors (in PHP you&#8217;ll often end up with unexpected results). </p>
<p>First we need to type the data as a number so we can sort it properly. We can do that as follows:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="re0">$numbers2</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
<span class="st0">&#8216;9&#8242;</span>,<br />
<span class="st0">&#8216;8&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8216;</span>,<br />
<span class="st0">&#8216;7&nbsp; &#8216;</span>,<br />
<span class="st0">&#8216;70<br />
&#8216;</span>,<br />
<span class="st0">&#8216;6&#8242;</span>,<br />
<span class="st0">&#8216;69</p>
<p>&#8216;</span>,<br />
<span class="st0">&#8216;68&#8242;</span>,<br />
<span class="st0">&#8216;67&#8242;</span><span class="br0">&#41;</span>;</p>
<p><a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;&lt;hr&gt;Jumbled order (STRING TEST)&lt;br /&gt;&#8221;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a><span class="br0">&#40;</span><a href="http://www.php.net/join"><span class="kw3">join</span></a><span class="br0">&#40;</span><span class="st0">&#8220;,&#8221;</span>,<span class="re0">$numbers2</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">// begin code to cast array contents from string to numeric</span><br />
<span class="re0">$sizeof</span> = <a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$numbers2</span><span class="br0">&#41;</span>;<br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="re0">$i</span>=<span class="nu0">0</span>;<span class="re0">$i</span>&lt; <span class="re0">$sizeof</span>;<span class="re0">$i</span>++<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$numbers2</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span> = <span class="br0">&#40;</span>int<span class="br0">&#41;</span><span class="re0">$numbers2</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span>;&nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span><br />
<span class="co1">// end code to cast array contents from string to numeric</span></p>
<p><a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;&lt;br /&gt;Jumbled order (STRING TEST after casting)&lt;br /&gt;&#8221;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a><span class="br0">&#40;</span><a href="http://www.php.net/join"><span class="kw3">join</span></a><span class="br0">&#40;</span><span class="st0">&#8220;,&#8221;</span>,<span class="re0">$numbers2</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p><a href="http://www.php.net/rsort"><span class="kw3">rsort</span></a><span class="br0">&#40;</span><span class="re0">$numbers2</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;&lt;br /&gt;Reverse sort -&gt; rsort()&lt;br /&gt; &#8220;</span> . <span class="br0">&#40;</span><a href="http://www.php.net/join"><span class="kw3">join</span></a><span class="br0">&#40;</span><span class="st0">&#8220;,&#8221;</span>,<span class="re0">$numbers2</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;&lt;br /&gt;Regular order -&gt; asort()&lt;br /&gt;&#8221;</span>;<br />
<a href="http://www.php.net/asort"><span class="kw3">asort</span></a><span class="br0">&#40;</span><span class="re0">$numbers2</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a><span class="br0">&#40;</span><a href="http://www.php.net/join"><span class="kw3">join</span></a><span class="br0">&#40;</span><span class="st0">&#8220;,&#8221;</span>,<span class="re0">$numbers2</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Now it will sort as the &#8220;dream&#8221; option he requested.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20060103/75/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to not show the same thing twice when randomizing</title>
		<link>http://www.php-scripts.com/20051212/72/</link>
		<comments>http://www.php-scripts.com/20051212/72/#comments</comments>
		<pubDate>Tue, 13 Dec 2005 04:04:20 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Random]]></category>

		<category><![CDATA[How To]]></category>

		<category><![CDATA[Sessions]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/?p=72</guid>
		<description><![CDATA[Last night I was reviewing a new site that randomizes pages and I kept seeing the same pages coming up repeatedly. This is a rather common problem with many basic randomizers that do not keep track of what has already been shown. 

This begs the question: how do you setup even distribution of a randomized [...]]]></description>
			<content:encoded><![CDATA[<p>Last night I was reviewing a new site that randomizes pages and I kept seeing the same pages coming up repeatedly. This is a rather common problem with many basic randomizers that do not keep track of what has already been shown. </p>
<p><img src="http://www.php-scripts.com/images/2005/evendistribution.jpg" border="0" ALT="How to evenly distribute a pool of randomized content screenshot"/></p>
<p>This begs the question: how do you setup even distribution of a randomized pool of content? There are a couple different ways to do this, some of which are extremely efficient, but today we&#8217;ll review one that is very efficient and scales nicely.</p>
<p>First we need to pre-randomize the results and save an index of where we are at in the randomized pool. For example let&#8217;s say you have 10 numbers you want to randomize like this:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="re0">$numbers</span> = <a href="http://www.php.net/range"><span class="kw3">range</span></a><span class="br0">&#40;</span><span class="nu0">1</span>,<span class="nu0">10</span><span class="br0">&#41;</span>; <span class="co1">// fill with numbers 1-10</span><br />
<a href="http://www.php.net/shuffle"><span class="kw3">shuffle</span></a><span class="br0">&#40;</span><span class="re0">$numbers</span><span class="br0">&#41;</span>; <span class="co1">// as of PHP 4.2.0 no need to seed with srand()</span><br />
<span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$numbers</span> <span class="kw1">as</span> <span class="re0">$each</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;#1: $each&lt;br /&gt;&#8221;</span>; <span class="co1">// display all random numbers </span><br />
<span class="br0">&#125;</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Now you would just need to remember the position of the randomized array from page to page so that the next randomized item in the array is displayed.  To do this within the same browsing session we coud use sessions. To do this across browser sessions, we could use cookies. Here is some code illustrating how to do this using a session:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<a href="http://www.php.net/session_start"><span class="kw3">session_start</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$_SESSION</span><span class="br0">&#91;</span><span class="st0">&#8216;index&#8217;</span><span class="br0">&#93;</span> == <span class="nu0">10</span> or <span class="re0">$_SESSION</span><span class="br0">&#91;</span><span class="st0">&#8216;index&#8217;</span><span class="br0">&#93;</span> == <span class="st0">&#8221;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// first or LAST time, generate numbers</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$_SESSION</span><span class="br0">&#91;</span><span class="st0">&#8216;index&#8217;</span><span class="br0">&#93;</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rnumbers</span> = <a href="http://www.php.net/range"><span class="kw3">range</span></a><span class="br0">&#40;</span><span class="nu0">1</span>,<span class="nu0">10</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/shuffle"><span class="kw3">shuffle</span></a><span class="br0">&#40;</span><span class="re0">$rnumbers</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$_SESSION</span><span class="br0">&#91;</span><span class="st0">&#8216;numbers&#8217;</span><span class="br0">&#93;</span> = <span class="re0">$rnumbers</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="re0">$curindex</span> = <span class="re0">$_SESSION</span><span class="br0">&#91;</span><span class="st0">&#8216;index&#8217;</span><span class="br0">&#93;</span>;<br />
<span class="re0">$rnumbers</span> = <span class="re0">$_SESSION</span><span class="br0">&#91;</span><span class="st0">&#8216;numbers&#8217;</span><span class="br0">&#93;</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;Current randomize (#&#8217;</span> . <span class="br0">&#40;</span><span class="re0">$curindex</span>+<span class="nu0">1</span><span class="br0">&#41;</span> . <span class="st0">&#8216;): &lt;b&gt;&#8217;</span> .&nbsp; <span class="re0">$rnumbers</span><span class="br0">&#91;</span><span class="re0">$curindex</span><span class="br0">&#93;</span>&nbsp; . <span class="st0">&#8216;&lt;hr /&gt;&#8217;</span>;<br />
<span class="re0">$x</span> = <span class="nu0">1</span>;<br />
<span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$rnumbers</span> <span class="kw1">as</span> <span class="re0">$each</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="br0">&#40;</span><span class="re0">$x</span>-<span class="nu0">1</span><span class="br0">&#41;</span> == <span class="re0">$curindex</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;&lt;font color=&#8217;green&#8217;&gt;&lt;b&gt;#$x: $each&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;&#8221;</span>; <span class="co1">// display all random numbers</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;#$x: $each&lt;br /&gt;&#8221;</span>; <span class="co1">// display all random numbers</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$x</span>++;<br />
<span class="br0">&#125;</span><br />
<span class="re0">$_SESSION</span><span class="br0">&#91;</span><span class="st0">&#8216;index&#8217;</span><span class="br0">&#93;</span> = <span class="re0">$curindex</span>+<span class="nu0">1</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;&lt;hr /&gt;Refresh &lt;a href=&quot;even_distribution.php&quot;&gt;or click&lt;/a&gt; for next randomized number. When all 10 numbers have cycled, a new shuffle of numbers in the pool will occur.&#8217;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p><b>Notes:</b> start the code with session_start() to initialize the session. Then we check to see if the $_SESSION[&#8217;index&#8217;] is set or has reached the ceiling of the array. If the ceiling has been reached (10 numbers) then we reshuffle the array and reset the index to the first position.</p>
<p>This code also illustrates how to highlight the current item selected in a foreach loop in a bold green font. Since arrays begin numbering at zero but lists start numbering at 1, it is necessary to add and subtract one where applicable in the code. The $x variable is a list counter variable.</p>
<p><b>What if you want to add new content to the array?</b><br />
Say you are currently at item #6 and there has been two new numbers added? You could wait until the current cycle is complete and re-randomize the numbers for that user or you could re-randomize the remaining indices with the new content added pushed to the end. You could then fill a new array with the remaining numbers, shuffle that and then <i>replace</i> the existing $numbers array starting at the next index number. This way the user would not see the content they&#8217;d already seen and would see the new content randomized into the list.</p>
<p><b>Above and beyond</b><br />
This technique could be used for anything you could put into an array. If the array size is very big then you might want to work in stages, for example only load in 50 or 100 items into the session variables at a time as opposed to loading in thousands of possible pointers. Once the user has reached the random point, just grab a new randomized set of numbers from set #2 then set #3, etc all the way until all the pool has been exhausted.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20051212/72/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

