<?xml version="1.0" encoding="iso-8859-1"?>
<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>CrankBerry Blog &#187; Scripts and Programming</title>
	<atom:link href="http://www.crankberryblog.com/category/scripts-and-programming/feed" rel="self" type="application/rss+xml" />
	<link>http://www.crankberryblog.com</link>
	<description>Sharing ideas on web design, development and the internet economy</description>
	<lastBuildDate>Thu, 24 Nov 2011 19:37:08 +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>crankSlider the Jquery DIV and Image Slider Script</title>
		<link>http://www.crankberryblog.com/2011/crankslider-the-jquery-div-and-image-slider-script</link>
		<comments>http://www.crankberryblog.com/2011/crankslider-the-jquery-div-and-image-slider-script#comments</comments>
		<pubDate>Mon, 30 May 2011 22:10:36 +0000</pubDate>
		<dc:creator>Jerry Low</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.crankberryblog.com/?p=501</guid>
		<description><![CDATA[I was looking for a simple Jquery DIV slider the other day and took a while before I found anything but they were too heavy and clunky. I just decided the best way was to write my own. Sometimes you may want a slider for not just images but for Divs as well, that way you can include other elements inside the div or create more animations inside.

Without further ado head over here to grab the <a href="http://www.crankberryblog.com/tool/crank-slider.php">crankSlider Jquery Div and Image Slider</a>.]]></description>
			<content:encoded><![CDATA[<p>I was looking for a simple Jquery DIV slider the other day and took a while before I found anything but they were too heavy and clunky. I just decided the best way was to write my own. Sometimes you may want a slider for not just images but for Divs as well, that way you can include other elements inside the div or create more animations inside.</p>
<p>Without further ado head over here to grab the <a href="http://www.crankberryblog.com/tool/crank-slider.php">crankSlider Jquery Div and Image Slider</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crankberryblog.com/2011/crankslider-the-jquery-div-and-image-slider-script/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Installing Sphinx on CentOS/Linux through SSH</title>
		<link>http://www.crankberryblog.com/2011/installing-sphinx-on-centoslinux-through-ssh</link>
		<comments>http://www.crankberryblog.com/2011/installing-sphinx-on-centoslinux-through-ssh#comments</comments>
		<pubDate>Fri, 22 Apr 2011 17:29:00 +0000</pubDate>
		<dc:creator>Jerry Low</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.crankberryblog.com/?p=479</guid>
		<description><![CDATA[Here are the steps into setting up Sphinx for your Plesk sever through SSH. I'm running on CentOS 5.2 but I'm pretty sure these instructions work on most Linux environment. If you're looking to <a href="http://www.crankberryblog.com/2011/intalling-sphinx-on-wamp-localhost-windows">install Sphinx on WAMP</a> (Windows machine) there's another article for that.
]]></description>
			<content:encoded><![CDATA[<p>Here are the steps into setting up Sphinx for your Plesk sever through SSH. I&#039;m running on CentOS 5.2 but I&#039;m pretty sure these instructions work on most Linux environment. If you&#039;re looking to <a href="http://www.crankberryblog.com/2011/intalling-sphinx-on-wamp-localhost-windows">install Sphinx on WAMP</a> (Windows machine) there&#039;s another article for that.</p>
<p>Here is my environment:</p>
<ul>
<li>CentOS 5.2</li>
<li>MySQL 5.1.36</li>
<li>PHP 5.3.6</li>
<li>Sphinx 1.10</li>
</ul>
<p>To do all this, login to your server via SSH with your favourite Telnet/SSH client. I prefer to use <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/" target="_blank">PuTTY</a>. Oh, you must login as root so have root access!</p>
<h3>How Does Sphinx Works</h3>
<p>In brief this is how Sphinx works. Sphinx creates a separate index of the table you want to search. Your PHP scripts calls the Sphinx API to search connecting to a specific port where Sphinx is listening to. Sphinx then searches its own index and does the search returning the result. Yes, the index has to be updated every time your update your original table. On my live environment I have a <a href="http://www.crankberryblog.com/2010/php-cron-job-without-using-cron-tab-cron-alternative-with-mysql-or-just-txt">cron job setup</a> to reindex with Sphinx every so often.</p>
<h3>Step 1 Extract and Prepare Conf File</h3>
<p>First of all you have to download the Sphinx package and extract it on your server. Here are the command lines for that. Note that the package I&#039;m downloading is the newest (1.10 Beta at this moment). If you&#039;re downloading the older stable version or newer ones since this article is written then you have to change the URL. BTW don&#039;t type in the ($).</p>
<pre>$ wget http://sphinxsearch.com/files/sphinx-1.10-beta.tar.gz
$ tar xzvf sphinx-0.9.8.tar.gz
$ cd sphinx</pre>
<p>To Summarize, the first line downloaded the Sphinx package to your server. Second line untarred it and lastly you went into the directory of your new download.</p>
<p>You can do the configuration if you&#039;ll like to edit the installation process. Here&#039;s an excerpt from Sphinx&#039;s site:</p>
<pre>$ ./configure

There's a number of options to configure. The complete listing may be
obtained by using --help switch. The most important ones are:

--prefix, which specifies where to install Sphinx; such as
--prefix=/usr/local/sphinx (all of the examples use this prefix)
--with-mysql, which specifies where to look for MySQL include and
library files, if auto-detection fails;
--with-pgsql, which specifies where to look for PostgreSQL include
and library files.</pre>
<p>Once you&#039;re done then all you have to do is make the binaries and install the software:</p>
<pre>$ make
$ make install</pre>
<p>Once you have installed Sphinx you&#039;ll need to prepare the configuration file. This is the most important part as Sphinx actually acts based on the settings and parameters defined in this single document. Let&#039;s say we&#039;ve installed Sphinx into /usr/local/sphinx. Copy the sample configuration file from the downloaded package into the Sphinx installation folder:</p>
<pre>mv /sphinx/sphinx-min.conf.in /usr/local/sphinx/sphinx.conf </pre>
<p>I named it sphinx.conf for ease, but you can call it anything you want. Now you want to edit sphinx.conf with your favourite editor. I like to use VI editor. Here&#039;s what the initial open of the file looks like and I&#039;ll run through each section of this script.</p>
<pre>#
# Minimal Sphinx configuration sample (clean, simple, functional)
#

source src1
{
	type			= mysql

	sql_host		= localhost
	sql_user		= test
	sql_pass		=
	sql_db			= test
	sql_port		= 3306	# optional, default is 3306

	sql_query		= \
		SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS
		date_added, title, content \
		FROM table

	sql_attr_uint		= group_id
	sql_attr_timestamp	= date_added

	sql_query_info		= SELECT * FROM table WHERE id=$id
}

index test1
{
	source			= src1
	path			= @CONFDIR@/data/test1
	docinfo			= extern
	charset_type		= sbcs
}

index testrt
{
	type			= rt
	rt_mem_limit		= 32M

	path			= @CONFDIR@/data/testrt
	charset_type		= utf-8

	rt_field		= title
	rt_field		= content
	rt_attr_uint		= gid
}

indexer
{
	mem_limit		= 32M
}

searchd
{
	listen			= 9312
	listen			= 9306:mysql41
	log			= @CONFDIR@/log/searchd.log
	query_log		= @CONFDIR@/log/query.log
	read_timeout		= 5
	max_children		= 30
	pid_file		= @CONFDIR@/log/searchd.pid
	max_matches		= 1000
	seamless_rotate		= 1
	preopen_indexes		= 0
	unlink_old		= 1
	workers			= threads # for RT to work
}</pre>
<p>These settings should cover most of what you&#039;re looking for in terms of setting up Sphinx. I&#039;ll try to run through everything as much as possible. There are more parameters and customization you can do by reading their documentation here.</p>
<p><strong>source src1</strong></p>
<p>This section defines your SQL settings and what is needed to be index. So the first few lines sql_host, sql_user, sql_pass, sql_db is exactly your local host MySQL settings. The sql_port is defaulted to 3306. I haven&#039;t changed this setting for Wamp so that&#039;s where my port sits.</p>
<p>The sql_query is actually crucial. This is the query that you want Sphinx to do and build an index on. Its just like an SQL query you would do. Say my table people had columns: id, name, address, city, age, and joined (stored as time stamp) as follow:</p>
<table width="520" border="1" cellspacing="2" cellpadding="2">
<tr>
<td>id</td>
<td>name</td>
<td>address</td>
<td>city</td>
<td>age</td>
<td>joined</td>
</tr>
<tr>
<td>50</td>
<td>john doe</td>
<td>546 random st.</td>
<td>Vancouver</td>
<td>42</td>
<td>124568794562</td>
</tr>
<tr>
<td>51</td>
<td>amanda smith</td>
<td>
<p>3425 great ave</p>
</td>
<td>Vancouver</td>
<td>18</td>
<td>124568923423</td>
</tr>
<tr>
<td>52</td>
<td>justin heiber</td>
<td>352 awesome dr</td>
<td>
<p>Burnaby</p>
</td>
<td>56</td>
<td>124569000231</td>
</tr>
<tr>
<td>53</td>
<td>macy store</td>
<td>355 loser pl</td>
<td>Richmond</td>
<td>26</td>
<td>124569000352</td>
</tr>
</table>
<p>I want to be able to search for the people&#039;s name and address only so thats all I care about, but I have to include the id and the timestamp. You can also defined unsigned integer attributes. So my sql_query will be:</p>
<pre>sql_query		= \
		SELECT id, id AS id_attr, name, address, joined  \
		FROM people

sql_attr_uint		= id_attr
sql_attr_timestamp	= joined

sql_query_info		= SELECT * FROM people WHERE id=$id</pre>
<p>I&#039;ve defined the sql_attr_uint, sql_attr_timestamp, and sql_query_info. The sql_attr_uint is the unsigned integer attribute and it can&#039;t be the first column which is to define your id. The sql_attr_timestamp is the UNIX timestamp in your column. You can convert if you want but I usually store my dates in timestamp anyways. The sql_query_info is for testing purpose only. It is the query that retrieves the actual row after searching. You have to do this yourself in your PHP Script.</p>
<p><strong>index test1</strong></p>
<p>You can change the name of test1 to anything you want but for the sake of it I&#039;ll just leave it. The source is just the name of the section we did above. If you changed the name then you have to change it here. The path is where you want to store the indexed file on your site. So I&#039;ll store it in the site directory in a folder called sphinx: /var/www/vhosts/site.com/sphinx/test1. The remainder I left the same. I&#039;ve taken out the real time indexing because I&#039;ll be doing my own thing and I heard there were lots of limitations to it at this point, so its not my thing. If it floats your boat you can do some more research and look into it. Here&#039;s my index:</p>
<pre>index test1
{
	source			= src1
	path			= /var/sphinx/test1
	docinfo			= extern
	charset_type		= sbcs
}
indexer</pre>
<p>This section defines the indexer. My database is big so I increased the mem_limit to 64M. Don&#039;t just go for a high or low number. Too low can hurt your searching and too high can hurt your server. Find the balance based on the size of your database.</p>
<p><strong>searchd</strong></p>
<p>This part defined the searchd which is the search application from Sphinx. Besides the log paths I didn&#039;t change anything else. The listen port is where you want searchd to listen in on for search queries. I left it at 9312 which is where I&#039;ll point my PHP script to later on.</p>
<p>Here is my sphinx.conf file:</p>
<pre>#
# Minimal Sphinx configuration sample (clean, simple, functional)
#

source src1
{
	type			= mysql

	sql_host		= localhost
	sql_user		= test
	sql_pass		=
	sql_db			= test
	sql_port		= 3306	# optional, default is 3306

	sql_query		= \
		SELECT id, id AS id_attr, name, address, joined  \
		FROM people

	sql_attr_uint		= id_attr
	sql_attr_timestamp	= joined

	sql_query_info		= SELECT * FROM people WHERE id=$id
}

index test1
{
	source			= src1
	path			= /var/sphinx/test1
	docinfo			= extern
	charset_type		= sbcs
}

indexer
{
	mem_limit		= 64M
}

searchd
{
	listen			= 9312
	listen			= 9306:mysql41
	log			= /var/log/sphinx/test1searchd.log
	query_log		= /var/log/sphinx/test1query.log
	read_timeout		= 5
	max_children		= 30
	pid_file		= /var/log/sphinx/test1searchd.pid
	max_matches		= 1000
	seamless_rotate		= 1
	preopen_indexes		= 0
	unlink_old		= 1
}</pre>
<h3>Getting Sphinx Ready</h3>
<p>Now that you have the conf file setted up, save and close the file. Now we&#039;re ready to install.</p>
<p>First, we&#039;re going to build the index by typing in <em>indexer -config /usr/local/sphinxsphinx.conf test1</em>. Replace test1 if you changed the name of your index. If you don&#039;t get any errors and see some numbers run then you&#039;re fine.</p>
<p>Once the index is build you want to prepare Sphinx&#039;s service on your machine. Type in searchd -install -config /usr/local/sphinx/sphinx.conf. It&#039;ll prompt you that the installation was successful.</p>
<h3>Testing Sphinx Out</h3>
<p>Let us try out the new nifty search. So say I want to search my table for john doe I&#039;ll type search /usr/local/sphinx/sphinx.conf &#034;john doe&#034;. That should return something for me. I can define the matching modes and which column to search as well such as search -ext2 -config /usr/local/sphinx/sphinx.conf &#034;@name john @address awesome&#034;. If you are getting the results you want then you are ready to move forward into your PHP script.</p>
<h3>Sphinx and Your PHP Script</h3>
<p>You need the Sphinx API in your script for it to work so go to c:\sphinx\api and copy sphinxapi.php to your site directory. Include this file in your script and call on the class:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st0">&#039;sphinxapi.php&#039;</span><span class="br0">&#41;</span>;<br />
<span class="co1">//Sphinx</span><br />
<span class="re0">$s</span> = <span class="kw2">new</span> SphinxClient;<br />
<span class="re0">$s</span>-&gt;<span class="me1">setServer</span><span class="br0">&#40;</span><span class="st0">&quot;localhost&quot;</span>, <span class="nu0">9312</span><span class="br0">&#41;</span>;<br />
<span class="re0">$s</span>-&gt;<span class="me1">setMatchMode</span><span class="br0">&#40;</span>SPH_MATCH_EXTENDED2<span class="br0">&#41;</span>;</div>
<p>Noticed I have setServer pointing to the port I told Sphinx to listen to. The setMatchMode is just the matching mode you&#039;ll like to use. In my case I&#039;m using extended 2.</p>
<p>Now you can query search your Sphinx index with:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">//Search Query</span><br />
<span class="re0">$result</span> = <span class="re0">$s</span>-&gt;<span class="me1">query</span><span class="br0">&#40;</span><span class="st0">&quot;@name $searchName @address $searchAddress&quot;</span><span class="br0">&#41;</span>;</div>
<p>The $result returned will not include the rows and columns of your actual table. It&#039;ll return the follow parameters (taken from <a href="http://www.php.net" target="_blank">php.net</a>):</p>
<table width="520" border="1" cellpadding="1" cellspacing="1">
<thead valign="middle">
<tr valign="middle">
<th>Key</th>
<th>Value description</th>
</tr>
</thead>
<tbody valign="middle">
<tr valign="middle">
<td align="left">&quot;matches&quot;</td>
<td align="left">An array with found document IDs as keys and their weight and attributes values as values</td>
</tr>
<tr valign="middle">
<td align="left">&quot;total&quot;</td>
<td align="left">Total number of matches found and retrieved (depends on your settings)</td>
</tr>
<tr valign="middle">
<td align="left">&quot;total_found&quot;</td>
<td align="left">Total number of found documents matching the query</td>
</tr>
<tr valign="middle">
<td align="left">&quot;words&quot;</td>
<td align="left">An array with words (case-folded and stemmed) as keys and per-word statistics as values</td>
</tr>
<tr valign="middle">
<td align="left">&quot;error&quot;</td>
<td align="left">Query error message reported by searchd</td>
</tr>
<tr valign="middle">
<td align="left">&quot;warning&quot;</td>
<td align="left">Query warning reported by searchd</td>
</tr>
</tbody>
</table>
<p>This means that $result['total'] will tell me how many results were found and $result['matches'] is where the id of the search results are stored. So here&#039;s a simple line to return all of the results:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$result</span><span class="br0">&#91;</span><span class="st0">&#039;total&#039;</span><span class="br0">&#93;</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$result</span><span class="br0">&#91;</span><span class="st0">&#039;matches&#039;</span><span class="br0">&#93;</span> <span class="kw1">as</span> <span class="re0">$id</span> =&gt; <span class="re0">$otherStuff</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Get Column</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$searchColumn</span> = <a href="http://www.php.net/mysql_fetch_array"><span class="kw3">mysql_fetch_array</span></a><span class="br0">&#40;</span> <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st0">&quot;SELECT * FROM people WHERE id=$id&quot;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Dump</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/var_dump"><span class="kw3">var_dump</span></a><span class="br0">&#40;</span><span class="re0">$searchColumn</span><span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#039;No results found&#039;</span>; &nbsp; &nbsp; &nbsp; &nbsp;<br />
<span class="br0">&#125;</span></div>
<p>Notice that the mysql_query is the same as sql_query_info in sphinx.conf. What a coincidence eh? LOL. That is all and now you have a working Sphinx search system setup on your local machine. If you need to reindex your Sphinx you can do so without restarting the service. In command prompt just run <em>indexer -config /usr/local/sphinx/sphinx.conf -rotate test1</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crankberryblog.com/2011/installing-sphinx-on-centoslinux-through-ssh/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Intalling Sphinx on Wamp localhost (Windows)</title>
		<link>http://www.crankberryblog.com/2011/intalling-sphinx-on-wamp-localhost-windows</link>
		<comments>http://www.crankberryblog.com/2011/intalling-sphinx-on-wamp-localhost-windows#comments</comments>
		<pubDate>Sun, 03 Apr 2011 20:42:39 +0000</pubDate>
		<dc:creator>Jerry Low</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Hosting]]></category>

		<guid isPermaLink="false">http://www.crankberryblog.com/?p=458</guid>
		<description><![CDATA[I recall the first time trying to setup Sphinx search on my Windows machine with Wamp it wasn't so obvious of how it was to be setup. Here's a guide in how to get it setup on you local machine. If you want to <a href="http://www.crankberryblog.com/2011/installing-sphinx-on-centoslinux-through-ssh">install Sphinx on Linux / Plesk</a> there's an article for that too.]]></description>
			<content:encoded><![CDATA[<p>I recall the first time trying to setup Sphinx search on my Windows machine with Wamp it wasn&#039;t so obvious of how it was to be setup. Here&#039;s a guide in how to get it setup on you local machine. If you want to <a href="http://www.crankberryblog.com/2011/installing-sphinx-on-centoslinux-through-ssh">install Sphinx on Linux / Plesk</a> there&#039;s an article for that too.</p>
<p>Here is my environment:</p>
<ul>
<li>Windows Vista</li>
<li>Wamp 2.0</li>
<li>Apache 2.2.11</li>
<li>MySQL 5.1.36</li>
<li>PHP 5.3.0</li>
<li>Sphinx 1.10</li>
</ul>
<p>First of all download Sphinx for Windows which can be found (<a href="http://sphinxsearch.com/downloads/beta/">http://sphinxsearch.com/downloads/beta/</a>). I downloaded Win32 binaries w/MySQL support version because I only work with MySQL. Once you got this you&#039;re ready to begin.</p>
<h3>How Does Sphinx Works</h3>
<p>In brief this is how Sphinx works. Sphinx creates a separate index of the table you want to search. Your PHP scripts calls the Sphinx API to search connecting to a specific port where Sphinx is listening to. Sphinx then searches its own index and does the search returning the result. Yes, the index has to be updated every time your update your original table. On my live environment I have a <a href="http://www.crankberryblog.com/2009/setting-php-cron-job-with-crontab">cron job</a> setup to reindex with Sphinx every so often.</p>
<h3>Step 1 Extract and Prepare Conf File</h3>
<p>Extract the files into any folder you want, I&#039;ve selected c:\sphinx\. The next thing you need to do is create a document in c:\sphinx\bin called sphinx.conf. This is the most important part as Sphinx actually acts based on the settings and parameters defined in this single document. Once you have created sphinx.conf in the bin folder copy the content from c:\sphinx\sphinx-min.conf.in (this is their provided sample file). It looks like:</p>
<pre>#
# Minimal Sphinx configuration sample (clean, simple, functional)
#

source src1
{
	type			= mysql

	sql_host		= localhost
	sql_user		= test
	sql_pass		=
	sql_db			= test
	sql_port		= 3306	# optional, default is 3306

	sql_query		= \
		SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS
		date_added, title, content \
		FROM table

	sql_attr_uint		= group_id
	sql_attr_timestamp	= date_added

	sql_query_info		= SELECT * FROM table WHERE id=$id
}

index test1
{
	source			= src1
	path			= @CONFDIR@/data/test1
	docinfo			= extern
	charset_type		= sbcs
}

index testrt
{
	type			= rt
	rt_mem_limit		= 32M

	path			= @CONFDIR@/data/testrt
	charset_type		= utf-8

	rt_field		= title
	rt_field		= content
	rt_attr_uint		= gid
}

indexer
{
	mem_limit		= 32M
}

searchd
{
	listen			= 9312
	listen			= 9306:mysql41
	log			= @CONFDIR@/log/searchd.log
	query_log		= @CONFDIR@/log/query.log
	read_timeout		= 5
	max_children		= 30
	pid_file		= @CONFDIR@/log/searchd.pid
	max_matches		= 1000
	seamless_rotate		= 1
	preopen_indexes		= 0
	unlink_old		= 1
	workers			= threads # for RT to work
}</pre>
<p>These settings should cover most of what you&#039;re looking for in terms of setting up Sphinx. I&#039;ll try to run through everything as much as possible. There are more parameters and customization you can do by reading their documentation <a href="http://sphinxsearch.com/docs/1.10/">here</a>.</p>
<p><strong>source src1</strong></p>
<p>This section defines your SQL settings and what is needed to be index. So the first few lines sql_host, sql_user, sql_pass, sql_db is exactly your local host MySQL settings. The sql_port is defaulted to 3306. I haven&#039;t changed this setting for Wamp so that&#039;s where my port sits.</p>
<p>The sql_query is actually crucial. This is the query that you want Sphinx to do and build an index on. Its just like an SQL query you would do. Say my table people had columns: id, name, address, city, age, and joined (stored as time stamp) as follow: </p>
<table width="520" border="1" cellspacing="2" cellpadding="2">
<tr>
<td>id</td>
<td>name</td>
<td>address</td>
<td>city</td>
<td>age</td>
<td>joined</td>
</tr>
<tr>
<td>50</td>
<td>john doe</td>
<td>546 random st.</td>
<td>Vancouver</td>
<td>42</td>
<td>124568794562</td>
</tr>
<tr>
<td>51</td>
<td>amanda smith</td>
<td>
<p>3425 great ave</p>
</td>
<td>Vancouver</td>
<td>18</td>
<td>124568923423</td>
</tr>
<tr>
<td>52</td>
<td>justin heiber</td>
<td>352 awesome dr</td>
<td>
<p>Burnaby</p>
</td>
<td>56</td>
<td>124569000231</td>
</tr>
<tr>
<td>53</td>
<td>macy store</td>
<td>355 loser pl</td>
<td>Richmond</td>
<td>26</td>
<td>124569000352</td>
</tr>
</table>
<p>I want to be able to search for the people&#039;s name and address only so thats all I care about, but I have to include the id and the timestamp. You can also defined unsigned integer attributes. So my sql_query will be:</p>
<pre>sql_query		= \
		SELECT id, id AS id_attr, name, address, joined  \
		FROM people

sql_attr_uint		= id_attr
sql_attr_timestamp	= joined

sql_query_info		= SELECT * FROM people WHERE id=$id</pre>
<p>I&#039;ve defined the sql_attr_uint, sql_attr_timestamp, and sql_query_info. The sql_attr_uint is the unsigned integer attribute and it <strong>can&#039;t</strong> be the first column which is to define your id. The sql_attr_timestamp is the UNIX timestamp in your column. You can convert if you want but I usually store my dates in timestamp anyways. The sql_query_info is for testing purpose only. It is the query that retrieves the actual row after searching. You have to do this yourself in your PHP Script.</p>
<p><strong>index test1</strong></p>
<p>You can change the name of test1 to anything you want but for the sake of it I&#039;ll just leave it. The source is just the name of the section we did above. If you changed the name then you have to change it here. The path is where you want to store the indexed file on your site. So I&#039;ll store it in the site directory in a folder called sphinx: C:\Wamp\www\site\sphinx\test1. The remainder I left the same. I&#039;ve taken out the real time indexing because I&#039;ll be doing my own thing and I heard there were lots of limitations to it at this point, so its not my thing. If it floats your boat you can do some more research and look into it. Here&#039;s my index:</p>
<pre>index test1
{
	source			= src1
	path			= C:\Wamp\www\site\sphinx\test1
	docinfo			= extern
	charset_type		= sbcs
}</pre>
<p><strong>indexer</strong></p>
<p>This section defines the indexer. My database is big so I increased the mem_limit to 64M. Don&#039;t just go for a high or low number. Too low can hurt your searching and too high can hurt your server. Find the balance based on the size of your database.</p>
<p><strong>searchd</strong></p>
<p>This part defined the searchd which is the search application from Sphinx. Besides the log paths I didn&#039;t change anything else. The listen port is where you want searchd to listen in on for search queries. I left it at 9312 which is where I&#039;ll point my PHP script to later on.</p>
<p>Here is my sphinx.conf file:</p>
<pre>#
# Minimal Sphinx configuration sample (clean, simple, functional)
#

source src1
{
	type			= mysql

	sql_host		= localhost
	sql_user		= test
	sql_pass		=
	sql_db			= test
	sql_port		= 3306	# optional, default is 3306

	sql_query		= \
		SELECT id, id AS id_attr, name, address, joined  \
		FROM people

	sql_attr_uint		= id_attr
	sql_attr_timestamp	= joined

	sql_query_info		= SELECT * FROM people WHERE id=$id
}

index test1
{
	source			= src1
	path			= C:\Wamp\www\s\sphinx\test1
	docinfo			= extern
	charset_type		= sbcs
}

indexer
{
	mem_limit		= 64M
}

searchd
{
	listen			= 9312
	listen			= 9306:mysql41
	log			= C:\Wamp\www\s\sphinx\searchd.log
	query_log		= C:\Wamp\www\s\sphinx\query.log
	read_timeout		= 5
	max_children		= 30
	pid_file		= C:\Wamp\www\s\sphinx\searchd.pid
	max_matches		= 1000
	seamless_rotate		= 1
	preopen_indexes		= 0
	unlink_old		= 1
}</pre>
<h3>Getting Sphinx Ready</h3>
<p>Now that you have prepared the Configuration file which is the hardest part of this then you&#039;re ready to begin. The first part you want to do is prepare the Sphinx index. Open up your command promt (Start > Search [cmd] > CMD.</p>
<p>In command promt navigate to your Sphinx bin folder by typing in <em>cd c:\sphinx\bin</em>. </p>
<p>Once there launched the indexer to build the index by typing in <em>indexer.exe &#8211;config c:\sphinx\bin\sphinx.conf test1</em>. Replace test1 if you changed the name of your index. If you don&#039;t get any errors and see some numbers run then you&#039;re fine. If you get a &#034;failed to write&#034; message it&#039;s because you don&#039;t have admin access to your machine.</p>
<p>Once the index is build you want to install Sphinx on your Windows machine as a service by typing <em>searchd.exe &#8211;install &#8211;config c:\sphinx\bin\sphinx.conf &#8211;servicename Sphinx</em>. It&#039;ll prompt you that the installation was successful.</p>
<p>Now you have to start the service you just installed. Go to control panel > administrative tools > services. Find Sphinx in the list. Click on it and on the left side you should see Start (on my machine that&#039;s where it is). Click on it and it should start.</p>
<h3>Testing Sphinx Out</h3>
<p>Go back to command prompt and test it out using search. So say I want to search my table for john doe I&#039;ll type <em>search sphinx.conf &#034;john doe&#034;</em>. That should return something for me. I can define the <a href="http://sphinxsearch.com/docs/1.10/matching-modes.html">matching modes</a> and which column to search as well such as <em>search &#8211;ext2 &#8211;config sphinx.conf &#034;@name john @address awesome&#034;</em>. If you are getting the results you want then you are ready to move forward into your PHP script.</p>
<h3>Sphinx and Your PHP Script</h3>
<p>You need the Sphinx API in your script for it to work so go to c:\sphinx\api and copy sphinxapi.php to your site directory. Include this file in your script and call on the class:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st0">&#039;sphinxapi.php&#039;</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//Sphinx</span><br />
<span class="re0">$s</span> = <span class="kw2">new</span> SphinxClient;<br />
<span class="re0">$s</span>-&gt;<span class="me1">setServer</span><span class="br0">&#40;</span><span class="st0">&quot;localhost&quot;</span>, <span class="nu0">9312</span><span class="br0">&#41;</span>;<br />
<span class="re0">$s</span>-&gt;<span class="me1">setMatchMode</span><span class="br0">&#40;</span>SPH_MATCH_EXTENDED2<span class="br0">&#41;</span>;</div>
<p>Noticed I have setServer pointing to the port I told Sphinx to listen to. The setMatchMode is just the matching mode you&#039;ll like to use. In my case I&#039;m using extended 2.</p>
<p>Now you can query search your Sphinx index with:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">//Search Query</span><br />
<span class="re0">$result</span> = <span class="re0">$s</span>-&gt;<span class="me1">query</span><span class="br0">&#40;</span><span class="st0">&quot;@name $searchName @address $searchAddress&quot;</span><span class="br0">&#41;</span>;</div>
<p>The $result returned will not include the rows and columns of your actual table. It&#039;ll return the follow parameters (taken from <a href="http://php.net/manual/en/sphinxclient.query.php">php.net</a>):</p>
<table width="520" border="1" cellpadding="1" cellspacing="1">
<thead valign="middle">
<tr valign="middle">
<th>Key</th>
<th>Value description</th>
</tr>
</thead>
<tbody valign="middle">
<tr valign="middle">
<td align="left">&quot;matches&quot;</td>
<td align="left">An array with found document IDs as keys and their weight and attributes values as values</td>
</tr>
<tr valign="middle">
<td align="left">&quot;total&quot;</td>
<td align="left">Total number of matches found and retrieved (depends on your settings)</td>
</tr>
<tr valign="middle">
<td align="left">&quot;total_found&quot;</td>
<td align="left">Total number of found documents matching the query</td>
</tr>
<tr valign="middle">
<td align="left">&quot;words&quot;</td>
<td align="left">An array with words (case-folded and stemmed) as keys and per-word statistics as values</td>
</tr>
<tr valign="middle">
<td align="left">&quot;error&quot;</td>
<td align="left">Query error message reported by searchd</td>
</tr>
<tr valign="middle">
<td align="left">&quot;warning&quot;</td>
<td align="left">Query warning reported by searchd</td>
</tr>
</tbody>
</table>
<p>This means that $result['total'] will tell me how many results were found and $result['matches'] is where the id of the search results are stored. So here&#039;s a simple line to return all of the results:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$result</span><span class="br0">&#91;</span><span class="st0">&#039;total&#039;</span><span class="br0">&#93;</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$result</span><span class="br0">&#91;</span><span class="st0">&#039;matches&#039;</span><span class="br0">&#93;</span> <span class="kw1">as</span> <span class="re0">$id</span> =&gt; <span class="re0">$otherStuff</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Get Column</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$searchColumn</span> = <a href="http://www.php.net/mysql_fetch_array"><span class="kw3">mysql_fetch_array</span></a><span class="br0">&#40;</span> <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st0">&quot;SELECT * FROM people WHERE id=$id&quot;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Dump</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/var_dump"><span class="kw3">var_dump</span></a><span class="br0">&#40;</span><span class="re0">$searchColumn</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#039;No results found&#039;</span>;&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span class="br0">&#125;</span></div>
<p>Notice that the mysql_query is the same as sql_query_info in sphinx.conf. What a coincidence eh? LOL. That is all and now you have a working Sphinx search system setup on your local machine. If you need to reindex your Sphinx you can do so without restarting the service. In command prompt just run <em>indexer.exe &#8211;config c:\sphinx\bin\sphinx.conf &#8211;rotate test1.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crankberryblog.com/2011/intalling-sphinx-on-wamp-localhost-windows/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Why do I get &quot;page includes resources which are not secure&quot;</title>
		<link>http://www.crankberryblog.com/2011/why-do-i-get-page-includes-resources-which-are-not-secure</link>
		<comments>http://www.crankberryblog.com/2011/why-do-i-get-page-includes-resources-which-are-not-secure#comments</comments>
		<pubDate>Thu, 03 Mar 2011 23:51:11 +0000</pubDate>
		<dc:creator>Jerry Low</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Security]]></category>

		<guid isPermaLink="false">http://www.crankberryblog.com/?p=454</guid>
		<description><![CDATA[You just built a site, and have implemented <a href="http://www.crankberryblog.com/2009/what-is-ssl-and-do-i-need-ssl">SSL</a>, but when you go to test it you get "page includes resources which are not secure". This is annoying but you need to fix it.]]></description>
			<content:encoded><![CDATA[<p>You just built a site, and have implemented <a href="http://www.crankberryblog.com/2009/what-is-ssl-and-do-i-need-ssl">SSL</a>, but when you go to test it you get &#034;page includes resources which are not secure&#034;. This is annoying but you need to fix it.</p>
<p>You can choose to ignore it as for most browser this message is not obtrusive but for some this may generate constant popups that will bug the hell out of your users.</p>
<p>There is only one reason why this message shows up but maybe multiple elements that contributes to this. The reason why this message appears is that on the page you&#039;re trying to load through SSL you have links to elements that are not secured through the layer. The warning is letting the user know that although the page maybe secure some elements could put the user at risk. It is essential to have this message. Image you&#039;re on a payment website about to type in your credit card and &#034;BAM&#034; this message pops up. What if it is the credit card module that is not secure? Wouldn&#039;t you worry? Let&#039;s see some of the elements that causes this.</p>
<h3>Your Own Content</h3>
<p>Depending on the way you apply SSL to your user there maybe a leakage in the elements that are secure. You can use HTACCESS to force SSL on a folder by placing this in .htaccess within that folder:</p>
<div class="dean_ch" style="white-space: wrap;"># Turn On for This Folder<br />
RewriteCond %{SERVER_PORT} 80 <br />
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]</div>
<p>This is assuming you have the rewrite engine and everything enabled. If you have a single file within the directory put into SSL this might be trickier. Look at the page and the elements it links to. Although the page maybe forced into SSL the images, scripts and CSS may not be (don&#039;t worry about included PHP scripts they don&#039;t count for this). The method that could work is to force those directory into SSL as well. Now this code is not perfect but it has worked:</p>
<div class="dean_ch" style="white-space: wrap;">RewriteCond %{SERVER_PORT} ^443$<br />
RewriteCond %{REQUEST_URI} ^/(image|css|javascripts)/?<br />
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/$2 [R,L]</div>
<p>Basically image, css and javascripts are locations (folders) where those files are located. Change these based on your files. Add more directories with | in between each name.</p>
<h3>Analytics</h3>
<p>If you use a third party analytics tracker you may have pasted some sort of code at the bottom of your page. Depending on who you use they may not support SSL. If that&#039;s the case you can code it into your file to exclude the script from running. Here&#039;s a PHP example:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span> <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st0">&#039;HTTPS&#039;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> || <span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st0">&#039;HTTPS&#039;</span><span class="br0">&#93;</span> != <span class="st0">&quot;on&quot;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw2">?&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Analytics Script</span><br />
<span class="kw2">&lt;?php</span> <span class="br0">&#125;</span> <span class="kw2">?&gt;</span></div>
<h3>Advertisement and Third Party Codes</h3>
<p>Just like the analytics code above, third party codes may not have an SSL option for you. If you use a third party code on your site such as advertisements, widgets and feeds you may have to turn it off with the same code as above:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span> <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st0">&#039;HTTPS&#039;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> || <span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st0">&#039;HTTPS&#039;</span><span class="br0">&#93;</span> != <span class="st0">&quot;on&quot;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw2">?&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Ads, Widgets and Feeds</span><br />
<span class="kw2">&lt;?php</span> <span class="br0">&#125;</span> <span class="kw2">?&gt;</span></div>
<p>The downside is that on these pages you can&#039;t use the third party codes. Depending on the page, you may not want to anyways. For example, if you were using analytics on a checkout page, you may just want to check your database for actual analytics rather than hits and bounce rates.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crankberryblog.com/2011/why-do-i-get-page-includes-resources-which-are-not-secure/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>PHP text based counter &#8211; script specific no MySQL counter</title>
		<link>http://www.crankberryblog.com/2010/php-text-based-counter-script-specific</link>
		<comments>http://www.crankberryblog.com/2010/php-text-based-counter-script-specific#comments</comments>
		<pubDate>Wed, 29 Sep 2010 17:49:03 +0000</pubDate>
		<dc:creator>Jerry Low</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[counter]]></category>
		<category><![CDATA[PHP Script]]></category>

		<guid isPermaLink="false">http://www.crankberryblog.com/?p=428</guid>
		<description><![CDATA[Here's a counter script that can help you count downloads, specific pages, or overall general count. This is a simple function that works by storing text rather than using a MySQL database. Just for quick usage.]]></description>
			<content:encoded><![CDATA[<p>Here&#039;s a counter script that can help you count downloads, specific pages, or overall general count. This is a simple function that works by storing text rather than using a MySQL database. Just for quick usage.</p>
<p>A sample of the results page:</p>
<p><img src="http://www.crankberryblog.com/images/counter.jpg" /></p>
<p>Here&#039;s the function that you&#039;ll need for this to work.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">function</span> counter<span class="br0">&#40;</span><span class="re0">$counterName</span>, <span class="re0">$counterStage</span> = <span class="st0">&#039;store&#039;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Name</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$counter_name</span> = <a href="http://www.php.net/preg_replace"><span class="kw3">preg_replace</span></a><span class="br0">&#40;</span><span class="st0">&quot;/[^a-zA-Z0-9-]/&quot;</span>, <span class="st0">&quot;&quot;</span>, <span class="re0">$counterName</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//File</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$file</span> = <span class="st0">&#039;counter/counter_&#039;</span> . <span class="re0">$counter_name</span> . <span class="st0">&#039;.csv&#039;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Get Time</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$now</span> = <a href="http://www.php.net/strtotime"><span class="kw3">strtotime</span></a><span class="br0">&#40;</span><span class="st0">&quot;now&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//IP</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$ip</span> = <span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st0">&#039;REMOTE_ADDR&#039;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Open File</span><br />
&nbsp; &nbsp; <span class="re0">$handle</span> = <a href="http://www.php.net/fopen"><span class="kw3">fopen</span></a><span class="br0">&#40;</span><span class="re0">$file</span>, <span class="st0">&quot;a+&quot;</span><span class="br0">&#41;</span>; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Store</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$counterStage</span> == <span class="st0">&#039;store&#039;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Store Array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$storeArray</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="re0">$ip</span>, <span class="re0">$now</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Store</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fputcsv<span class="br0">&#40;</span><span class="re0">$handle</span>, <span class="re0">$storeArray</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Read</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">elseif</span> <span class="br0">&#40;</span><span class="re0">$counterStage</span> == <span class="st0">&#039;read&#039;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Row Counter</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$row</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$totalRow</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$dispArray</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span><span class="br0">&#40;</span><span class="re0">$data</span> = <a href="http://www.php.net/fgetcsv"><span class="kw3">fgetcsv</span></a><span class="br0">&#40;</span><span class="re0">$handle</span>, <span class="nu0">0</span>, <span class="st0">&quot;,&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> !== <span class="kw2">FALSE</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Counter</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$num</span> = <a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$data</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Store To Array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$totalRow</span> &lt; <span class="nu0">100</span><span class="br0">&#41;</span> <span class="re0">$dispArray</span><span class="br0">&#91;</span><span class="re0">$row</span><span class="br0">&#93;</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="re0">$data</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, <span class="re0">$data</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Increase Row</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$row</span>++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$totalRow</span>++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Display</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#039;&lt;h2&gt;Counter Total For &#039;</span> . <span class="re0">$counterName</span> . <span class="st0">&#039;: &#039;</span> . <span class="re0">$row</span> . <span class="st0">&#039;&lt;/h2&gt;&#039;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#039;Recent 100 Counts (&lt;a href=&quot;&#039;</span> . <span class="re0">$file</span> .<span class="st0">&#039;&quot; target=&quot;_blank&quot;&gt;Download full Stats&lt;/a&gt;):</p>
<p>&#039;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#039;&lt;table border=&quot;1&quot; cellpadding=&quot;2&quot; cellspacing=&quot;0&quot;&gt;&#039;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#039;&lt;tr&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td width=&quot;20&quot; style=&quot;font-size: 10px;&quot;&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td width=&quot;100&quot; style=&quot;font-size: 10px;&quot;&gt;&lt;strong&gt;IP&lt;/strong&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td width=&quot;160&quot; style=&quot;font-size: 10px;&quot;&gt;&lt;strong&gt;Date&lt;/strong&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/tr&gt;&#039;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Sort</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/krsort"><span class="kw3">krsort</span></a><span class="br0">&#40;</span><span class="re0">$dispArray</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rowCount</span> = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Displaying</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$dispArray</span> <span class="kw1">as</span> <span class="re0">$row</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#039;&lt;tr&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td style=&quot;font-size: 10px;&quot;&gt;&#039;</span> . <span class="re0">$rowCount</span> . <span class="st0">&#039;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td style=&quot;font-size: 10px;&quot;&gt;&#039;</span> . <span class="re0">$row</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> . <span class="st0">&#039;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td style=&quot;font-size: 10px;&quot;&gt;&#039;</span> . <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#039;M d, Y h:i a&#039;</span>, <span class="re0">$row</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span> . <span class="st0">&#039;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/td&gt;&#039;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Count</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rowCount</span>++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#039;&lt;/table&gt;&#039;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.php.net/fclose"><span class="kw3">fclose</span></a><span class="br0">&#40;</span><span class="re0">$handle</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>To call the script to store this is the function to call:</p>
<div class="dean_ch" style="white-space: wrap;">counter<span class="br0">&#40;</span><span class="st0">&#039;test&#039;</span><span class="br0">&#41;</span>;</div>
<p>The above case would store in a script called &#034;test&#034;. If you&#039;re trying to do specific pages you can name your script home, about, or contact, whichever you please. To view results you&#039;ll call it with.</p>
<div class="dean_ch" style="white-space: wrap;">counter<span class="br0">&#40;</span><span class="st0">&#039;test&#039;</span>, <span class="st0">&#039;read&#039;</span><span class="br0">&#41;</span>;</div>
<p>Same thing with the store function just change &#034;test&#034; to whichever counter you wish to retrieve.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crankberryblog.com/2010/php-text-based-counter-script-specific/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP Cron job without using Cron tab (cron alternative with MySQL or just txt)</title>
		<link>http://www.crankberryblog.com/2010/php-cron-job-without-using-cron-tab-cron-alternative-with-mysql-or-just-txt</link>
		<comments>http://www.crankberryblog.com/2010/php-cron-job-without-using-cron-tab-cron-alternative-with-mysql-or-just-txt#comments</comments>
		<pubDate>Tue, 28 Sep 2010 00:21:49 +0000</pubDate>
		<dc:creator>Jerry Low</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[cron alternative]]></category>
		<category><![CDATA[cron job]]></category>
		<category><![CDATA[cron tab]]></category>
		<category><![CDATA[PHP Scripts]]></category>

		<guid isPermaLink="false">http://www.crankberryblog.com/?p=420</guid>
		<description><![CDATA[You may have the need to schedule a PHP script to run but don't know and don't want to bother to learn how to use <a href="http://www.crankberryblog.com/2009/setting-php-cron-job-with-crontab">CRON job</a>. That is ok because when I develop for smaller clients with shared hosting I tend to use a different technique anyways. Here's my alternative to running a  CRON tab.]]></description>
			<content:encoded><![CDATA[<p>You may have the need to schedule a PHP script to run but don&#039;t know and don&#039;t want to bother to learn how to use <a href="http://www.crankberryblog.com/2009/setting-php-cron-job-with-crontab">CRON job</a>. That is ok because when I develop for smaller clients with shared hosting I tend to use a different technique anyways. Here&#039;s my alternative to running a  CRON tab.</p>
<p>Its simple and many developers have mentioned it, just include the script in your scripts to run. Here&#039;s how to set this up. First of all you&#039;ll need to setup a MySQL table to store the date. If you don&#039;t have access to a MySQL database or choose not to use one, then you are being too picky. Doesn&#039;t matter, I&#039;ll work with you here. </p>
<h3>MySQL Version &#8211; Create this Table</h3>
<div class="dean_ch" style="white-space: wrap;">CREATE TABLE <span class="kw1">IF</span> NOT EXISTS cronjob <span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; job_id bigint<span class="br0">&#40;</span><span class="nu0">20</span><span class="br0">&#41;</span> NOT <span class="kw2">NULL</span> AUTO_INCREMENT,<br />
&nbsp; &nbsp; job_name varchar<span class="br0">&#40;</span><span class="nu0">50</span><span class="br0">&#41;</span> NOT <span class="kw2">NULL</span>,<br />
&nbsp; &nbsp; job_date bigint<span class="br0">&#40;</span><span class="nu0">12</span><span class="br0">&#41;</span> NOT <span class="kw2">NULL</span>,<br />
&nbsp; &nbsp; PRIMARY <a href="http://www.php.net/key"><span class="kw3">KEY</span></a> <span class="br0">&#40;</span>job_id<span class="br0">&#41;</span>,<br />
&nbsp; &nbsp; INDEX <span class="br0">&#40;</span>job_name<span class="br0">&#41;</span>,<br />
&nbsp; &nbsp; INDEX <span class="br0">&#40;</span>job_date<span class="br0">&#41;</span><br />
<span class="br0">&#41;</span></div>
<h3>Non MySQL Version &#8211; Do This</h3>
<p>If you are not using MySQL database then create a file called cronjob.txt and just leave the content of it blank.</p>
<h2>The CRON function</h2>
<p>You&#039;ll need to have this function either in your script or in the class/object that you&#039;re using. </p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">//Cronjob Function</span><br />
<span class="kw2">function</span> cronjob <span class="br0">&#40;</span><span class="re0">$runScript</span>, <span class="re0">$runHours</span> = <span class="nu0">1</span>, <span class="re0">$database</span> = <span class="kw2">TRUE</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Get Time</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$now</span> = <a href="http://www.php.net/strtotime"><span class="kw3">strtotime</span></a><span class="br0">&#40;</span><span class="st0">&quot;now&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Name</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$runScript_name</span> = <a href="http://www.php.net/preg_replace"><span class="kw3">preg_replace</span></a><span class="br0">&#40;</span><span class="st0">&quot;/[^a-zA-Z0-9-]/&quot;</span>, <span class="st0">&quot;&quot;</span>, <span class="re0">$runScript</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Convert Time</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$timePass</span> = <span class="br0">&#40;</span> <span class="nu0">60</span> * <span class="nu0">60</span> <span class="br0">&#41;</span> * <span class="re0">$runHours</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$timeLast</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Get Passed Time</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$database</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$get_time</span> = <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span> <span class="st0">&quot;SELECT job_date FROM cronjob WHERE job_name=&#039;$runScript_name&#039; ORDER BY job_date DESC LIMIT 0, 1&quot;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$get_time_num</span> = <a href="http://www.php.net/mysql_num_rows"><span class="kw3">mysql_num_rows</span></a><span class="br0">&#40;</span><span class="re0">$get_time</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Reading</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$get_time_num</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="re0">$timeLast</span> = <a href="http://www.php.net/mysql_result"><span class="kw3">mysql_result</span></a><span class="br0">&#40;</span> <span class="re0">$get_time</span>, <span class="nu0">0</span>, <span class="st0">&#039;job_date&#039;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//File</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$file</span> = <span class="st0">&#039;cronjob/cronjob_&#039;</span> . <span class="re0">$runScript_name</span> . <span class="st0">&#039;.txt&#039;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Open File</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$handle</span> = <a href="http://www.php.net/fopen"><span class="kw3">fopen</span></a><span class="br0">&#40;</span><span class="re0">$file</span>, <span class="st0">&quot;a+&quot;</span><span class="br0">&#41;</span>;&nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Time</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$get_time</span> = <a href="http://www.php.net/fread"><span class="kw3">fread</span></a><span class="br0">&#40;</span><span class="re0">$handle</span>, <span class="nu0">12</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$get_time</span> != <span class="st0">&#034;</span><span class="br0">&#41;</span> <span class="re0">$timeLast</span> = <span class="re0">$get_time</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Close</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<a href="http://www.php.net/fclose"><span class="kw3">fclose</span></a><span class="br0">&#40;</span><span class="re0">$handle</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; </p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Run Script</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="br0">&#40;</span><span class="re0">$timeLast</span> + <span class="re0">$timePass</span><span class="br0">&#41;</span> &lt; <span class="re0">$now</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Run Script</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">include</span> <span class="br0">&#40;</span> <span class="re0">$runScript</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Ending</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$database</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Insert Time</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$insert_time</span> = @<a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span> <span class="st0">&quot;INSERT INTO cronjob(job_name, job_date) VALUES(&#039;$runScript_name&#039;, &#039;$now&#039;)&quot;</span> <span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Open File</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$handle</span> = <a href="http://www.php.net/fopen"><span class="kw3">fopen</span></a><span class="br0">&#40;</span><span class="re0">$file</span>, <span class="st0">&quot;w&quot;</span><span class="br0">&#41;</span>;&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Write</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/fwrite"><span class="kw3">fwrite</span></a><span class="br0">&#40;</span><span class="re0">$handle</span>, <span class="re0">$now</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Close File</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<a href="http://www.php.net/fclose"><span class="kw3">fclose</span></a><span class="br0">&#40;</span><span class="re0">$handle</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw2">?&gt;</span></div>
<h2>Using the Function</h2>
<p>To call the cron job simple include the following line in one of the scripts that&#039;s often loaded in your site.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">//Run Job &#8211; MySQL Database Version</span><br />
cronjob <span class="br0">&#40;</span><span class="st0">&#039;function/delete_logistics.php&#039;</span>, <span class="nu0">1</span>, <span class="kw2">TRUE</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//Run Job &#8211; Txt Version</span><br />
cronjob <span class="br0">&#40;</span><span class="st0">&#039;function/delete_logistics.php&#039;</span>, <span class="nu0">1</span>, <span class="kw2">TRUE</span><span class="br0">&#41;</span>;</div>
<p>Basically you call the function cronjob and the arguements are: script name, hours to run script, and using MySQL database (TRUE for yes and FALSE for no). Say I want to run the script crankberry_awesome.php every two days using txt format. This is what I would have.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">//Run Job</span><br />
cronjob <span class="br0">&#40;</span><span class="st0">&#039;crankberry_awesome.php&#039;</span>, <span class="nu0">48</span>, <span class="kw2">FALSE</span><span class="br0">&#41;</span>;</div>
<p>That&#039;s all there is to it. So have fun with running your crons aka scheduled PHP tasks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crankberryblog.com/2010/php-cron-job-without-using-cron-tab-cron-alternative-with-mysql-or-just-txt/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The ultimate newb&#039;s guide to learning web development (Pt. 1: The Basic)</title>
		<link>http://www.crankberryblog.com/2010/the-ultimate-newbs-guide-to-learning-web-development-pt-1-the-basic</link>
		<comments>http://www.crankberryblog.com/2010/the-ultimate-newbs-guide-to-learning-web-development-pt-1-the-basic#comments</comments>
		<pubDate>Fri, 24 Sep 2010 22:22:21 +0000</pubDate>
		<dc:creator>Jerry Low</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.crankberryblog.com/?p=406</guid>
		<description><![CDATA[If you are just discovering the world of web development but know nothing about HTML code, css and mumble-jumble, here's a good place to start. In this article I will outline in general where to find resources and some good skills to pick up on to becoming a minor expert in the field.]]></description>
			<content:encoded><![CDATA[<p>If you are just discovering the world of web development but know nothing about HTML code, css and mumble-jumble, here&#039;s a good place to start. In this article I will outline in general where to find resources and some good skills to pick up on to becoming a minor expert in the field.</p>
<p>To be honest, web development is all about practice and understanding, of course creativity as well (I&#039;ll get to this later). Here&#039;s a little scope about how I picked up on web development. I started making websites through FrontPage when I was 13, using only a bit of HTML and mainly the WYSIWYG (what you see is what you get) editor. It wasn&#039;t until I was 15 that I started developing with HTML and using CSS. When I was 18 I felt the need for more power in my websites which is when I picked up PHP and MySQL. About a year ago was finally when I started picking up on Javascript.</p>
<p>Basically, I learned everything through every resource except taking classes. I would search through the web, read books from the library or just pure trial and error. It wasn&#039;t insanely hard to gather all the information but the only times when I really learn was through projects and experiments. Web development is definitely a hands-on type of thing. Point is, if you want to learn web development you&#039;ll have to spend 30% reading and 70% doing.</p>
<h2>The Program/Software</h2>
<p>Web development can be accomplished through a few languages and most of them don&#039;t require any special software to write. The basic notepad that came with your Windows is capable of completing the task. I prefer to use a software that color codes coding to mitigate headaches. Here&#039;s a list of software that you could choose from.</p>
<ul>
<li><a href="http://notepad-plus-plus.org/" target="_blank">Notepad++ (Free)</a></li>
<li><a href="http://kompozer.net/" target="_blank">KompoZer (Free)</a></li>
<li><a href="http://www.w3.org/Amaya/" target="_blank">Amaya (Free)</a></li>
<li><a href="http://www.seamonkey-project.org/" target="_blank">SeaMonkey (Free)</a></li>
<li><a href="http://www.microsoft.com/expression/" target="_blank">Microsoft Expression Studio ($149)</a></li>
<li><a href="http://www.adobe.com/products/dreamweaver/" target="_blank">Adobe Dreamweaver ($399)</a></li>
</ul>
<p>I personally use Dreamweaver on my computer as I have been using it since I was young so I have a nostalgic feeling to it. When I&#039;m on the go I have notepad++ on my USB.</p>
<h2>Learning HTML</h2>
<p>Before you learn anything else, you have to be really good at HTML (hyper text markup language). The basic of websites all boils down to this language. HTML is the markup language containing markup tags that creates the webpage document. Every website requires HTML to make it work, yes even Flash websites.</p>
<p>Your basic website is wrapped in an opening and closing HTML tag. Opening tags are surrounded by arrow brackets while closing tags are the same but there&#039;s a front slash immediately after the first arrow bracket like such</p>
<div class="dean_ch" style="white-space: wrap;">&lt;html&gt;&lt;/html&gt;</div>
<p>Within the HTML tags there will be a HEAD and BODY tag. The head tags will describe items related to the website but will not be produced in the content. Items such as site title, meta tags, CSS, and Javascripts can be defined in the HEAD tag. The BODY tag will describe content that will be outputted to the user. A code like this:</p>
<div class="dean_ch" style="white-space: wrap;">&lt;html&gt;<br />
&lt;head&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;title&gt;Test Page&lt;/title&gt;<br />
&lt;/head&gt;</p>
<p>&lt;body&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Hello world!<br />
&lt;/body&gt;<br />
&lt;/html&gt;</div>
<p>Will output &#034;Hello world!&#034; on the browser with the title Test Page in the browser title bar. Once you get this down you&#039;re ready to start building the website. The goal following this is to output the content you wish to show, it could be paragraphed text, images or tables. Everything you wished to use requires a markup tag to make it work. It&#039;ll be redundant for me to give you a tutorial on all the tags but here are two resources that are great.</p>
<ul>
<li><a href="http://www.w3schools.com/html/default.asp" target="_blank">w3school &#8211; HTML</a></li>
<li><a href="http://www.html.net/tutorials/html/" target="_blank">HTML.net</a></li>
</ul>
<p>w3school is great, as they are short writeups but concise so you can digest all of the content quickly. HTML.net is more detailed and a great read too but they incorporate a bit of CSS so its better to read it after w3school to cross-reference some of the ideas and undestanding. Once you&#039;ve got this down you&#039;re ready for some CSS.</p>
<h2>Learning CSS</h2>
<p>CSS (Cascading Style Sheet) is a language that focuses on the style and layout of your markup language. Its a common standard now generate the accurate aesthetic results of many websites. There are a few ways to include CSS in your website.</p>
<div class="dean_ch" style="white-space: wrap;">//Link to a CSS file &#8211; Goes in the &lt;head&gt;&lt;/head&gt; tag<br />
&lt;link href=&quot;css/styles.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;</p>
<p>//Have the CSS in the &lt;head&gt;&lt;/head&gt; tag<br />
&lt;style type=&quot;text/css&quot;&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; .css { font-weight: bold; }<br />
&lt;/style&gt;</p>
<p>//Inline with content markup tags &#8211; in &lt;body&gt;&lt;/body&gt; tag<br />
&lt;p style=&quot;font-size: 22px;&quot;&gt;this is a paragraph&lt;/p&gt;<br />
&nbsp;</div>
<p>Once you have your website linked with some CSS you can start controlling your website. Here are some example of CSS controlling a webpage.</p>
<div class="dean_ch" style="white-space: wrap;">//All paragraph content will be bold and have a line height of 22 pixels<br />
p { font-weight: bold; line-height: 22px; }</p>
<p>//The following code will modify all markup tags with the class &quot;change&quot; to have a background color of black<br />
.change { background: #000; }</p>
<p>//The following code will modify the element with ID &quot;element_1&quot; to have a background color of white<br />
#element_1 { background: #FFF; }</div>
<p>Here are the resources to read to learn all about CSS.</p>
<ul>
<li><a href="http://www.w3schools.com/css/default.asp" target="_blank">w3school &#8211; CSS</a></li>
<li><a href="http://www.html.net/tutorials/css/" target="_blank">HTML.net</a></li>
</ul>
<p>Again its good to go over the basics with w3school first then head to HTML.net. </p>
<p>At this point you should practice making a few websites with HTML and CSS. Try to envision something and then build it. The best practice is to try to design something in Photoshop then try to build a web site from it. As I always tell developers, just design it and I&#039;ll make it into a template, because I don&#039;t believe there&#039;s many design limitations that can&#039;t be built with HTML and CSS. By doing this exercise it&#039;ll help you learn different methods of building different elements.</p>
<h2>Notes and Reminders</h2>
<p>There are some things you should keep in mind when building with HTML and CSS though.</p>
<h3>Using tables for design</h3>
<p>This is a no and not recommended. A lot of people argue over this topic. Some believe that tables are good for design because its easier to control and it holds the shape of the original design better. The problem is tables are clunky, heavy in coding and not very flexible. Tables have specific standards to them so if you have two rows and two columns, the columns have to be equal width while rows have to be equal height. In order to work around that you&#039;ll have to nest your tables, imagine dealing with that. Its also less flexible for future expansions. You should use a table for tabular data though. Extreme non-table believers will argue that you don&#039;t, they can produce the same effect with other elements. They sure can but when it comes down to the user extracting your data (copying to word or excel), copying for a table is much easier than anything else. It just makes sense.</p>
<h3>Avoid absolute positioning of everything</h3>
<p>Many developers when they start will notice the amount of control with absolute positioning. It is not until their design gets more complicated that it all falls apart. Absolute position provides more flexibility but will sometimes diminish your control, use wisely.</p>
<h3>Test in multiple browsers</h3>
<p>There are various browsers on the web. It sometimes depends on who your website caters to but mind as well cover the major users. Test on desktop machines with Internet Explorer 6, 7, 8, 9, Chrome, Firefox, Safari. On mobile devices with Android, Opera Mini, iOS. Just make sure everything&#039;s consistent. This is a good chance to test how good your skills are too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crankberryblog.com/2010/the-ultimate-newbs-guide-to-learning-web-development-pt-1-the-basic/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Function to replace variable value in URL</title>
		<link>http://www.crankberryblog.com/2010/function-to-replace-variable-value-in-url</link>
		<comments>http://www.crankberryblog.com/2010/function-to-replace-variable-value-in-url#comments</comments>
		<pubDate>Thu, 19 Aug 2010 08:00:05 +0000</pubDate>
		<dc:creator>Jerry Low</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[PHP Script]]></category>
		<category><![CDATA[Variable]]></category>

		<guid isPermaLink="false">http://www.crankberryblog.com/?p=386</guid>
		<description><![CDATA[Passing variables through the URL is awesome but how do you update, say, the id or the page when there's twenty different variables (index.php?id=2325213&#038;page=30&#038;view=210&#038;sort=213&#038;user=231221). Here's a quick function to create a new URL string that will replace values of specific variables. You can also remove variables which are not necessary.]]></description>
			<content:encoded><![CDATA[<p>Passing variables through the URL is awesome but how do you update, say, the id or the page when there&#039;s twenty different variables (index.php?id=2325213&#038;page=30&#038;view=210&#038;sort=213&#038;user=231221). Here&#039;s a quick function to create a new URL string that will replace values of specific variables. You can also remove variables which are not necessary.</p>
<p>First of all though you&#039;ll need this function here. This function will spit out the page name for you. If you are using htaccess rewrite for .htm then replace index.php with index.htm:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">function</span> getPage<span class="br0">&#40;</span><span class="re0">$page_url</span>, <span class="re0">$ex_qs</span>=<span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Filtering Page</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$get_page</span> = <a href="http://www.php.net/substr"><span class="kw3">substr</span></a><span class="br0">&#40;</span><span class="re0">$page_url</span>, strripos<span class="br0">&#40;</span><span class="re0">$page_url</span>, <span class="st0">&#039;/&#039;</span><span class="br0">&#41;</span><span class="nu0">+1</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Position of The ?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/strpos"><span class="kw3">strpos</span></a><span class="br0">&#40;</span><span class="st0">&#039;.&#039;</span> . <span class="re0">$get_page</span>, <span class="st0">&#039;?&#039;</span><span class="br0">&#41;</span> == <span class="nu0">1</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$get_page</span> = <span class="st0">&#039;index.htm&#039;</span> . <span class="re0">$get_page</span>; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Exempt Query String</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$ex_qs</span>==<span class="nu0">1</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>@<a href="http://www.php.net/strpos"><span class="kw3">strpos</span></a><span class="br0">&#40;</span><span class="re0">$get_page</span>, <span class="st0">&#039;?&#039;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="re0">$get_page</span> = <a href="http://www.php.net/substr"><span class="kw3">substr</span></a><span class="br0">&#40;</span><span class="re0">$get_page</span>, <span class="nu0">0</span>, <a href="http://www.php.net/strpos"><span class="kw3">strpos</span></a><span class="br0">&#40;</span><span class="re0">$get_page</span>, <span class="st0">&#039;?&#039;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//They Want Only Query String</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">elseif</span> <span class="br0">&#40;</span><span class="re0">$ex_qs</span>==<span class="nu0">2</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//There is A Query String</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>@<a href="http://www.php.net/strpos"><span class="kw3">strpos</span></a><span class="br0">&#40;</span><span class="re0">$get_page</span>, <span class="st0">&#039;?&#039;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$currentQS</span> = <a href="http://www.php.net/substr"><span class="kw3">substr</span></a><span class="br0">&#40;</span><span class="re0">$get_page</span>, <a href="http://www.php.net/strpos"><span class="kw3">strpos</span></a><span class="br0">&#40;</span><span class="re0">$get_page</span>, <span class="st0">&#039;?&#039;</span><span class="br0">&#41;</span><span class="nu0">+1</span>, <a href="http://www.php.net/strlen"><span class="kw3">strlen</span></a><span class="br0">&#40;</span><span class="re0">$get_page</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$currentQS</span> = <span class="st0">&#034;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$get_page</span> = <span class="re0">$currentQS</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$get_page</span>;<br />
<span class="br0">&#125;</span></div>
<p>Now for the function to replace variables:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">function</span> varURL<span class="br0">&#40;</span><span class="re0">$variables</span>, <span class="re0">$subtract_variables</span> = <span class="kw2">NULL</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Get Current Page</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$gl_current_page</span> = getPage<span class="br0">&#40;</span><span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st0">&#039;REQUEST_URI&#039;</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$gl_current_page_nqs</span> = getPage<span class="br0">&#40;</span><span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st0">&#039;REQUEST_URI&#039;</span><span class="br0">&#93;</span>, <span class="nu0">1</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$gl_current_ext</span> = getPage<span class="br0">&#40;</span><span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st0">&#039;REQUEST_URI&#039;</span><span class="br0">&#93;</span>, <span class="nu0">2</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//If There&#039;s Extention in the Current Page</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$gl_current_ext</span>!=<span class="st0">&#034;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">//Return Extension</span><br />
&nbsp; &nbsp; <span class="re0">$returnExt</span> = <span class="re0">$gl_current_page</span>;<br />
&nbsp; &nbsp; <span class="re0">$currentExt</span> = <span class="st0">&#039;?&#039;</span> . <span class="re0">$gl_current_ext</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">//ADDING VAR *****************************************</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$variables</span>!=<span class="st0">&#034;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//If There Are Multiple Variables</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>@<a href="http://www.php.net/strpos"><span class="kw3">strpos</span></a><span class="br0">&#40;</span><span class="re0">$variables</span>, <span class="st0">&quot;&amp;&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Explode The String</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$explodeVar</span> = <a href="http://www.php.net/explode"><span class="kw3">explode</span></a><span class="br0">&#40;</span><span class="st0">&quot;&amp;&quot;</span>, <span class="re0">$variables</span><span class="br0">&#41;</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; <span class="re0">$explodeVar</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="re0">$variables</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Check if Variables Changed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$explodeVar</span> <span class="kw1">as</span> <span class="re0">$currentVar</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Not Empty</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$currentVar</span>!=<span class="st0">&#034;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Explode The Var</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$currentExplode</span> = <a href="http://www.php.net/explode"><span class="kw3">explode</span></a><span class="br0">&#40;</span><span class="st0">&quot;=&quot;</span>, <span class="re0">$currentVar</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Find In Current String</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/preg_match"><span class="kw3">preg_match</span></a><span class="br0">&#40;</span><span class="st0">&#039;/[<span class="es0">\?</span>|<span class="es0">\&#038;</span>]&#039;</span>.<span class="re0">$currentExplode</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>.<span class="st0">&#039;=/&#039;</span>, <span class="re0">$currentExt</span><span class="br0">&#41;</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Replace</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$currentExt</span> = <a href="http://www.php.net/preg_replace"><span class="kw3">preg_replace</span></a><span class="br0">&#40;</span><span class="st0">&#039;/&#039;</span>.<span class="re0">$currentExplode</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>.<span class="st0">&#039;=[^<span class="es0">\&#038;</span>]*/&#039;</span>, <span class="re0">$currentVar</span>, <span class="re0">$currentExt</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Not In String Add to it</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; <span class="re0">$currentExt</span> .= <span class="st0">&#039;&amp;&#039;</span> . <span class="re0">$currentVar</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="co1">//Subtracting Var *****************************************</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$subtract_variables</span> != <span class="kw2">NULL</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//If There Are Multiple Variables</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>@<a href="http://www.php.net/strpos"><span class="kw3">strpos</span></a><span class="br0">&#40;</span><span class="re0">$subtract_variables</span>, <span class="st0">&quot;&amp;&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Explode The String</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$explodeVar</span> = <a href="http://www.php.net/explode"><span class="kw3">explode</span></a><span class="br0">&#40;</span><span class="st0">&quot;&amp;&quot;</span>, <span class="re0">$subtract_variables</span><span class="br0">&#41;</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; <span class="re0">$explodeVar</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="re0">$subtract_variables</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Subtracting Them</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$explodeVar</span> <span class="kw1">as</span> <span class="re0">$currentVar</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Explode The Var</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$currentExplode</span> = <a href="http://www.php.net/explode"><span class="kw3">explode</span></a><span class="br0">&#40;</span><span class="st0">&quot;=&quot;</span>, <span class="re0">$currentVar</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Find In Current String</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/preg_match"><span class="kw3">preg_match</span></a><span class="br0">&#40;</span><span class="st0">&#039;/(&amp;|<span class="es0">\?</span>)&#039;</span>.<span class="re0">$currentExplode</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>.<span class="st0">&#039;=/&#039;</span>, <span class="re0">$currentExt</span><span class="br0">&#41;</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="re0">$currentExt</span> = <a href="http://www.php.net/preg_replace"><span class="kw3">preg_replace</span></a><span class="br0">&#40;</span><span class="st0">&#039;/(&amp;|<span class="es0">\?</span>)&#039;</span>.<span class="re0">$currentExplode</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>.<span class="st0">&#039;=([^&amp;|$]+)/&#039;</span>, <span class="st0">&quot;$1&quot;</span>, <span class="re0">$currentExt</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Removing Duplicate</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$currentExt</span> = <a href="http://www.php.net/preg_replace"><span class="kw3">preg_replace</span></a><span class="br0">&#40;</span><span class="st0">&#039;/<span class="es0">\?</span>&amp;/&#039;</span>, <span class="st0">&#039;?&#039;</span>, <a href="http://www.php.net/preg_replace"><span class="kw3">preg_replace</span></a><span class="br0">&#40;</span><span class="st0">&#039;/&amp;&amp;/&#039;</span>, <span class="st0">&#039;&amp;&#039;</span>, <span class="re0">$currentExt</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">//Build Return Extention</span><br />
&nbsp; &nbsp; <span class="re0">$returnExt</span> = <span class="re0">$gl_current_page_nqs</span> . <span class="re0">$currentExt</span>;<br />
&nbsp; &nbsp; <br />
<span class="co1">//No Extention</span><br />
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re0">$returnExt</span> = <span class="re0">$gl_current_page_nqs</span> . <span class="st0">&#039;?&#039;</span>. <span class="re0">$variables</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="co1">//Returning</span><br />
<span class="kw1">return</span> <span class="re0">$returnExt</span>;</p>
<p><span class="br0">&#125;</span></div>
<p>To use the function just use the following examples:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">//Replace the id with 5 and name with Berry</span><br />
&lt;a href=<span class="st0">&quot;&lt;?php varURL(&#039;id=5&amp;name=Berry&#039;); ?&gt;&quot;</span>&gt;Link&lt;/a&gt;</p>
<p><span class="co1">//Remove the id variables &#8211; just set the =1 the one will automatically be replaced anything</span><br />
&lt;a href=<span class="st0">&quot;&lt;?php varURL(&#034;, id=1); ?&gt;&quot;</span>&gt;Link&lt;/a&gt;</p>
<p><span class="co1">//Replace the id with 5 and remove the name variable</span><br />
&lt;a href=<span class="st0">&quot;&lt;?php varURL(&#039;id=5&#039;, &#039;name=Berry&#039;); ?&gt;&quot;</span>&gt;Link&lt;/a&gt;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.crankberryblog.com/2010/function-to-replace-variable-value-in-url/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Random row from your MySQL table with PHP, alternative to ORDER BY rand()</title>
		<link>http://www.crankberryblog.com/2010/random-row-from-your-mysql-table-with-php-alternative-to-order-by-rand</link>
		<comments>http://www.crankberryblog.com/2010/random-row-from-your-mysql-table-with-php-alternative-to-order-by-rand#comments</comments>
		<pubDate>Thu, 22 Apr 2010 21:28:04 +0000</pubDate>
		<dc:creator>Jerry Low</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Script]]></category>
		<category><![CDATA[Random]]></category>

		<guid isPermaLink="false">http://www.crankberryblog.com/?p=379</guid>
		<description><![CDATA[Working with MySQL and PHP is awesome. Its one of the best way to manage your data, but once in a while you may want to do some randomizing. One problem I came across recently was how to select a random row from your MySQL table with PHP. Using the ORDER BY rand() method in your query maybe a bit slow so here's what we've got.]]></description>
			<content:encoded><![CDATA[<p>Working with MySQL and PHP is awesome. Its one of the best way to manage your data, but once in a while you may want to do some randomizing. One problem I came across recently was how to select a random row from your MySQL table with PHP. Using the ORDER BY rand() method in your query maybe a bit slow so here&#039;s what we&#039;ve got.</p>
<h3>The Premade Method</h3>
<p>As I mentioned in the beginning. MySQL actually has a premade method that allows you to select a random row by querying for it.</p>
<div class="dean_ch" style="white-space: wrap;"><a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st0">&quot;SELECT * FROM table ORDER BY rand() LIMIT 0,1&quot;</span><span class="br0">&#41;</span>;</div>
<p>This method is great but if you are randomizing from a large table this method is not very efficient and with a high volume of queries it really puts a strain on your DB.</p>
<h3>The New Method &#8211; Randomizing 1 Entry</h3>
<p>Now here&#039;s the new solution by selecting a random row with the offset. This method will query exactly to the random entry; thus, making this process much faster.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="re0">$query</span> = <span class="st0">&quot;SELECT * FROM table&quot;</span><br />
<span class="re0">$num_rows</span> = <a href="http://www.php.net/mysql_num_rows"><span class="kw3">mysql_num_rows</span></a><span class="br0">&#40;</span> <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span> <span class="re0">$query</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span>;</p>
<p><span class="co1">//Random Number</span><br />
<span class="re0">$rand_row</span> = <a href="http://www.php.net/rand"><span class="kw3">rand</span></a><span class="br0">&#40;</span><span class="nu0">0</span>, <span class="re0">$num_rows</span> &#8211; <span class="nu0">1</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//Selecting the Random Row</span><br />
<span class="re0">$rand_row</span> = <span class="re0">$query</span> .= <span class="st0">&quot; LIMIT $rand_row, 1&quot;</span>;</div>
<p>Now you can modify the $query line to include your filters and sorting stuff, whatever you need.</p>
<h3>The New Method &#8211; Randomizing More than 1 Entry</h3>
<p>The above method right now can only select a single row. What if you want to randomized more than one item. Here&#039;s a method to select multiple items without overlap.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="co1">//Set the Amount of Random Rows you want</span><br />
<span class="re0">$rand_amount</span> = <span class="nu0">5</span>;</p>
<p><span class="re0">$query</span> = <span class="st0">&quot;SELECT * FROM table&quot;</span><br />
<span class="re0">$num_rows</span> = <a href="http://www.php.net/mysql_num_rows"><span class="kw3">mysql_num_rows</span></a><span class="br0">&#40;</span> <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span> <span class="re0">$query</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span>;</p>
<p><span class="co1">//Make sure you have more than the amount of random count you want</span><br />
<span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$num_rows</span> &lt;= <span class="re0">$rand_amount</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//An Array of Numbers Choosen</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rand_array</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Do IT!!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="re0">$i</span> = <span class="nu0">1</span>; <span class="re0">$i</span> &gt;= <span class="re0">$rand_amount</span>; <span class="re0">$i</span>++ <span class="br0">&#41;</span> <span class="br0">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">do</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rand_added</span> = <span class="kw2">FALSE</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rand_num</span> = <a href="http://www.php.net/rand"><span class="kw3">rand</span></a><span class="br0">&#40;</span><span class="nu0">0</span>, <span class="re0">$num_rows</span> &#8211; <span class="nu0">1</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Should We Add to Array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/in_array"><span class="kw3">in_array</span></a><span class="br0">&#40;</span><span class="re0">$rand_num</span>, <span class="re0">$rand_array</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rand_array</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$rand_num</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$rand_added</span> = <span class="kw2">TRUE</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">while</span> <span class="br0">&#40;</span>!<span class="re0">$rand_added</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//You now have a set of random row numbers to choose from</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Display The Result of What you have</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$rand_array</span> <span class="kw1">as</span> <span class="re0">$rand_num</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_query</span> = <span class="re0">$query</span> . <span class="st0">&quot; LIMIT $rand_num, 1&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Displaying What you have</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$current_row</span> = <a href="http://www.php.net/mysql_fetch_array"><span class="kw3">mysql_fetch_array</span></a><span class="br0">&#40;</span> <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span> <span class="re0">$new_query</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>To be honest you can use the multiple randomizer and just set the random amount to 1 but the other method saves you like 20 lines of code. Imagine how much faster that could be!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crankberryblog.com/2010/random-row-from-your-mysql-table-with-php-alternative-to-order-by-rand/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Automated MySQL database backup with PHP and cronjob</title>
		<link>http://www.crankberryblog.com/2010/automated-mysql-database-backup-with-php-and-cronjob</link>
		<comments>http://www.crankberryblog.com/2010/automated-mysql-database-backup-with-php-and-cronjob#comments</comments>
		<pubDate>Fri, 16 Apr 2010 17:04:36 +0000</pubDate>
		<dc:creator>Jerry Low</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Web Owners]]></category>
		<category><![CDATA[Cronjob]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP Script]]></category>

		<guid isPermaLink="false">http://www.crankberryblog.com/?p=373</guid>
		<description><![CDATA[At some point of working on your website you'll want to back up you MySQL database. This is they key for all of you building dynamic sites. If one day your server goes down or something you may have the files backed up but without the database you'll have to start from scratch again. If you operate a very slow-paced site then manual back up is fine but if you want to backup your database daily then it becomes tedious. I, myself is lazy so I came up with a PHP and Cronjob solution to automatically backup MySQL database.]]></description>
			<content:encoded><![CDATA[<p>At some point of working on your website you&#039;ll want to back up you MySQL database. This is they key for all of you building dynamic sites. If one day your server goes down or something you may have the files backed up but without the database you&#039;ll have to start from scratch again. If you operate a very slow-paced site then manual back up is fine but if you want to backup your database daily then it becomes tedious. I, myself is lazy so I came up with a PHP and Cronjob solution to automatically backup MySQL database.</p>
<h3>Automating It</h3>
<p><strong>What you need?</strong> The first thing you need to know how to do it set cronjobs. If you don&#039;t know how to do that you can <a href="http://www.crankberryblog.com/2009/setting-php-cron-job-with-crontab" title="Learn to set cronjobs">learn how to set cronjobs here</a>. The cronjob is what will automate your server to run the script and how often to run it.</p>
<h3>The Files</h3>
<p>All you&#039;ll need is two files to make this work. The first file, <strong>mysqldump.php</strong> can be grabbed from <a href="http://www.creativefactory.it/lab/">CreativeFactory.it</a>. The next thing you need is my backup.php file. Just create a file called <strong>backup.php</strong> and dump the following script inside.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span></p>
<p><span class="co1">//The File Name You Want To Use</span><br />
<span class="re0">$fileName</span> = <span class="st0">&#039;Filename&#039;</span>;</p>
<p><span class="co1">//Global</span><br />
<span class="re0">$sqlhost</span> = <span class="st0">&#039;localhost&#039;</span>;<br />
<span class="re0">$sqluser</span> = <span class="st0">&#039;db_user&#039;</span>;<br />
<span class="re0">$sqlpass</span> = <span class="st0">&#039;db_password&#039;</span>;<br />
<span class="re0">$sqldb</span> = <span class="st0">&#039;db_name&#039;</span>;</p>
<p><span class="co1">//**********************************</span><br />
<span class="co1">//You Don&#039;t Need to Edit This</span><br />
<span class="co1">//**********************************</span></p>
<p><span class="co1">//Today&#039;s Timestamp</span><br />
<span class="re0">$today_ts</span> = <a href="http://www.php.net/strtotime"><span class="kw3">strtotime</span></a><span class="br0">&#40;</span><span class="st0">&quot;now&quot;</span><span class="br0">&#41;</span>;</p>
<p>
<span class="re0">$backupDate</span> = <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#039;y.m.d.h.i&#039;</span>, <span class="re0">$today_ts</span><span class="br0">&#41;</span>;<br />
<span class="re0">$backupFile</span> = &nbsp;<span class="re0">$backupDate</span>.<span class="st0">&#039;- &#039;</span>.<span class="re0">$fileName</span>.<span class="st0">&#039;.txt&#039;</span>;</p>
<p><span class="co1">//Connect to mysql server</span><br />
<span class="re0">$connessione</span> = @<a href="http://www.php.net/mysql_connect"><span class="kw3">mysql_connect</span></a><span class="br0">&#40;</span><span class="re0">$sqlhost</span>,<span class="re0">$sqluser</span>,<span class="re0">$sqlpass</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//Include class</span><br />
<span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st0">&#039;mysqldump.php&#039;</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//Create new instance of MySQLDump</span><br />
<span class="re0">$dumper</span> = <span class="kw2">new</span> MySQLDump<span class="br0">&#40;</span><span class="re0">$sqldb</span><span class="br0">&#41;</span>;</p>
<p><span class="co1">//If you want to write the MySQL dump to file</span><br />
<span class="re0">$dumper</span>-&gt;<span class="me1">writeDump</span><span class="br0">&#40;</span><span class="re0">$backupFile</span><span class="br0">&#41;</span>;</p>
<p><span class="kw2">?&gt;</span></div>
<h3>The Setup</h3>
<p>Configure the first file variables of the script and then just leave the rest. Put the backup.php and mysqldump.php in the same folder where you want the back up files to be dumped. Set the cronjob to run backup.php you don&#039;t need to worry about mysqldump.php. That&#039;s it, it&#039;ll automatically backup.</p>
<h3>Possible Problems</h3>
<p>The problems that you may experience is the MySQL privileges. If your account doesn&#039;t have enough privilege then you may not be able to properly query the database. Another problem maybe the CHMOD of the folder you&#039;re backing up to. You need to have write access into that folder.</p>
<h3>Recovering from Backup</h3>
<p>To recover you database just log into your phpMyAdmin, open the database you want to recover. Select the import tab and just browse for the file. If I recall the file itself should overwrite tables that are already in place if not then you may have to drop all the tables first.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crankberryblog.com/2010/automated-mysql-database-backup-with-php-and-cronjob/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

