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

<channel>
	<title>Geek Notes &#187; PHP</title>
	<atom:link href="http://www.geek-notes.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.geek-notes.com</link>
	<description>A web developer's blog: languages, search engines, servers.</description>
	<lastBuildDate>Thu, 12 Nov 2009 01:14:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/2.5/</creativeCommons:license>
		<item>
		<title>How to save search queries on Google Custom Search Engine</title>
		<link>http://www.geek-notes.com/mysql/33/how-to-save-search-queries-on-google-custom-search-engine/</link>
		<comments>http://www.geek-notes.com/mysql/33/how-to-save-search-queries-on-google-custom-search-engine/#comments</comments>
		<pubDate>Fri, 17 Nov 2006 18:07:50 +0000</pubDate>
		<dc:creator>Maurizio Petrone</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Search Engines]]></category>

		<guid isPermaLink="false">http://www.geek-notes.com/mysql/33/how-to-save-search-queries-on-google-custom-search-engines/</guid>
		<description><![CDATA[
Does anybody out there run a Google Custom Search Engine?
Still wondering how to save your user&#8217;s search queries?
If you host yourself your own CSE, and your server can run PHP/MySQL, here is the solution!
First of all, you have to set up a MySQL table where the search terms searched on your Google Custom Search Engines [...]]]></description>
			<content:encoded><![CDATA[<img src="/wp-content/icons/icon_mysql.gif" width="80" height="80" class="icon" alt="MySQL" />
<p>Does anybody out there run a <a href="http://googleblog.blogspot.com/2006/10/eureka-your-own-search-engine-has.html" title="CSE" >Google Custom Search Engine</a>?</p>
<p>Still wondering <strong>how to save your user&#8217;s search queries</strong>?</p>
<p>If you host yourself your own CSE, and your server can run PHP/MySQL, here is the solution!</p>
<p><span id="more-33"></span>First of all, you have to set up a MySQL table where the search terms searched on your Google Custom Search Engines will be saved: to do so, open your favourite MySQL frontend (like PhpMyAdmin), and run this query:</p>
<p><code>CREATE TABLE `query` (<br />
  `id` int(11) NOT NULL auto_increment,<br />
  `query` varchar(255) NOT NULL default '',<br />
  `datetime` int(11) NOT NULL default '0',<br />
  PRIMARY KEY  (`id`)<br />
) ENGINE=MyISAM<br />
</code></p>
<p>Done? Well. Now, open the page in which you pasted the Google&#8217;s code that displays search results, and add to that page the code that will save data: it grabs search term from the URL:</p>
<p><code>< ?php<br />
/*-----------------------<br />
First part: db connection<br />
-------------------------*/<br />
$dbhost = "localhost";<br />
$dbname = "mylittledb";<br />
$dbuser = "root";<br />
$dbpass = "password";<br />
$db=mysql_connect($dbhost, $dbuser, $dbpass);<br />
if ($db==FALSE)<br />
die("Error while connecting to MYSQL ".mysql_error());<br />
mysql_select_db($dbname ,$db);<br />
/*------------------------------<br />
Read and save the search query<br />
-------------------------------*/<br />
$querystat = mysql_real_escape_string($_GET['q']);<br />
$datetime = time();<br />
if( ($_SERVER['HTTP_REFERER'] == '') AND ( ($querystat != $_SESSION['prev_search']) OR ( ($datetime - $_SESSION['datetime']) > 60) ) ) {<br />
	$insertquery = "INSERT INTO `query` ( `query` , `datetime`) VALUES ( '$querystat' , '$datetime');";<br />
	mysql_query($insertquery, $db);<br />
}<br />
$_SESSION['datetime'] = $datetime;<br />
$_SESSION['prev_search'] = $querystat;<br />
?></p>
<p></code></p>
<p>That&#8217;s all.</p>
<p>This code saves also the time/date of each search, so you can may later analyze searches e.g. per day, per month, or per time period.</p>
<p>Example&#8217;d code <strong>saves queries only if the referer is blank</strong> (i.e. the user did not request the search results page clicking on a link to it, but he has to manually submit the form), and it <strong>prevents multiple page refreshes</strong> (a minute at least has to pass, or the search query has to be different, before the term is saved into database).</p>
<p>If you don&#8217;t want filters, and you want to save right every query, keep just the database connection and the two lines inside the IF statement for the second part.</p>
<div class="sociable"><span class="sociable_tagline"><strong>Bookmark:</strong><span>These icons link to social bookmarking sites where readers can share and discover new web pages.</span></span><ul>
	<li><a href="http://digg.com/submit?phase=2&amp;url=http://www.geek-notes.com/mysql/33/how-to-save-search-queries-on-google-custom-search-engine/&amp;title=How+to+save+search+queries+on+Google+Custom+Search+Engine" title="digg"><img src="http://www.geek-notes.com/wp-content/plugins/sociable/images/digg.png" alt="digg" /></a></li>
	<li><a href="http://del.icio.us/post?url=http://www.geek-notes.com/mysql/33/how-to-save-search-queries-on-google-custom-search-engine/&amp;title=How+to+save+search+queries+on+Google+Custom+Search+Engine" title="del.icio.us"><img src="http://www.geek-notes.com/wp-content/plugins/sociable/images/delicious.png" alt="del.icio.us" /></a></li>
	<li><a href="http://blogmarks.net/my/new.php?mini=1&amp;simple=1&amp;url=http://www.geek-notes.com/mysql/33/how-to-save-search-queries-on-google-custom-search-engine/&amp;title=How+to+save+search+queries+on+Google+Custom+Search+Engine" title="blogmarks"><img src="http://www.geek-notes.com/wp-content/plugins/sociable/images/blogmarks.png" alt="blogmarks" /></a></li>
	<li><a href="http://co.mments.com/track?url=http://www.geek-notes.com/mysql/33/how-to-save-search-queries-on-google-custom-search-engine/&amp;title=How+to+save+search+queries+on+Google+Custom+Search+Engine" title="co.mments"><img src="http://www.geek-notes.com/wp-content/plugins/sociable/images/co.mments.gif" alt="co.mments" /></a></li>
	<li><a href="http://www.newsvine.com/_tools/seed&amp;save?u=http://www.geek-notes.com/mysql/33/how-to-save-search-queries-on-google-custom-search-engine/&amp;h=How+to+save+search+queries+on+Google+Custom+Search+Engine" title="NewsVine"><img src="http://www.geek-notes.com/wp-content/plugins/sociable/images/newsvine.png" alt="NewsVine" /></a></li>
	<li><a href="http://www.furl.net/storeIt.jsp?u=http://www.geek-notes.com/mysql/33/how-to-save-search-queries-on-google-custom-search-engine/&amp;t=How+to+save+search+queries+on+Google+Custom+Search+Engine" title="Furl"><img src="http://www.geek-notes.com/wp-content/plugins/sociable/images/furl.png" alt="Furl" /></a></li>
</ul></div>
]]></content:encoded>
			<wfw:commentRss>http://www.geek-notes.com/mysql/33/how-to-save-search-queries-on-google-custom-search-engine/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/2.5/</creativeCommons:license>
	</item>
		<item>
		<title>Cascading Style Sheet Switcher</title>
		<link>http://www.geek-notes.com/css/23/cascading-style-sheet-switcher/</link>
		<comments>http://www.geek-notes.com/css/23/cascading-style-sheet-switcher/#comments</comments>
		<pubDate>Tue, 23 May 2006 11:30:55 +0000</pubDate>
		<dc:creator>Maurizio Petrone</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.geek-notes.com/css/23/cascading-style-sheet-switcher/</guid>
		<description><![CDATA[
This code snippet will allow your users to choose between different styles to apply to your website.
Yes, it&#8217;s a PHP skin selector for websites!
You can view a live example of it in this weblog itself: it&#8217;s the switch style icon on the top right corner (Click it right now!).
Due to an heavy layout restyling, this [...]]]></description>
			<content:encoded><![CDATA[<img src="/wp-content/icons/icon_php.gif" width="80" height="80" class="icon" alt="PHP" />
<p>This code snippet will allow your users to choose between different styles to apply to your website.<br />
Yes, it&#8217;s a <strong>PHP skin selector for websites</strong>!</p>
<p><strike>You can view a live example of it in this weblog itself: it&#8217;s the <strong>switch style</strong> icon on the top right corner (<a href="/switch.php?style=geek" rel="nofollow" title="Change the stylesheet to 'Geek' for this website">Click it right now!</a>).</strike><br />
<em>Due to an heavy layout restyling, this function is no longer available on my blog. Sorry for the inconvenience</em></p>
<p>Here is a little presentation of the features that this little code provides:</p>
<ul>
<li>Have a default style (in absence of previous choices)</li>
<li>Unlimited choices (depending on your fantasy and ability)</li>
<li>Style choose take effect immediately, and site-wide</li>
<li>Switcher link brings you back to the page you were seeing, but restyled</li>
<li>The preference is saved for each user, for successive visits</li>
</ul>
<p>And now, let&#8217;s explain the technique.<br />
<span id="more-23"></span></p>
<p>Before diving directly into the code, I like to spend some words on the idea behind the switcher.</p>
<p>The main thing we do is <strong>printing links to different <acronym title="Cascading Style Sheet">CSS</acronym> files</strong> into our page HTML &lt;head&gt; section, depending on what we read from a <strong>cookie</strong>.</p>
<h4><strong>Why CSS files?</strong></h4>
<p>There are several advantages with having your site presentation and style informations stored into a separate <acronym title="Cascading Style Sheet">CSS</acronym> file, instead of having them embedded directly into the markup.</p>
<p>Without listing all of them, I just say that you can vary the <em>look &#038; feel</em> of a whole site by changing only a single line of code &#8211; that one that tells us the name and the path of <acronym title="Cascading Style Sheet">CSS</acronym> file.</p>
<h4><strong>What I need?</strong></h4>
<p>An (X)HTML output for your website (ok, you already got it); two or more CSS files applicable to that markup output; a server that can run PHP; and a client that can save cookies!</p>
<p>For multiple pages, it would be useful to have a template-driven output, so you don&#8217;t have to change every single page &lt;head&gt; section, but only the template one.</p>
<p>Please note: the more standard, well-formed and separated from presentation your markup code is, the more effective and powerful this technique will be.<br />
(<em>XHTML Strict</em>, without any embedded style information, is the best.)</p>
<h4><strong>How it works?</strong></h4>
<p>Ok, do you have at least two CSS files for your markup? <strong>Let&#8217;s switch &#8216;em!</strong></p>
<p>First of all, change the line that display the link to your CSS with something like this:</p>
<div class="codebox">&lt;link&nbsp;href=&#8221;/path/<span style="color: red">&lt;?php echo $style ?&gt;</span>.css&#8221; rel=&#8221;stylesheet&#8221; type=&#8221;text/css&#8221; /&gt;</div>
<p>Substantially, you have to change the CSS filename (without its extension) with the <em>echo</em> of <span class="codespan">$style</span> variable on PHP.</p>
<p>To fill this variable with a valid value (that is an existing CSS file name in the specified path), put this code immediately before the previous line:</p>
<div class="codebox">&lt;php<br />
if(isset($_COOKIE['style'])){<br />
$style=$_COOKIE['style'];<br />
} else {<br />
$style=&#8217;default&#8217;; //the name of default style file<br />
}<br />
?&gt;</div>
<p>With these lines, we control if there&#8217;s a cookie named STYLE, then assign its value to <span class="codespan">$style</span>, else we assign a default value to that variable.<br />
<em>I guess that you already know what comes next&#8230;</em></p>
<p>The last thing to do is to write a script that will write a custom value (that is CSS name) in our cookie, and then send the client back to the page it was on before calling that script.</p>
<p>Create an empty file called switch.php and fill it with the following:</p>
<div class="codebox">&lt;?php setcookie(&#8221;style&#8221;, $_GET["style"], time()+31536000);<br />
header(&#8221;Location:&#8221;.$HTTP_SERVER_VARS["HTTP_REFERER"]);<br />
?></div>
<p>You can call it from any page with a link like this:</p>
<div class="codebox">&lt;a&nbsp;href=&#8221;/switch.php?style=default&#8221;&gt;View page with Default style!&lt;/a&gt;<br />
&lt;a&nbsp;href=&#8221;/switch.php?style=alternate&#8221;&gt;View page with Alternate style!&lt;/a&gt;</div>
<p>When you click the link, the page reloads: the cookie will be read and the new style link will be shown, and the page from where you clicked on the switcher link will be styled accordingly!</p>
<div class="sociable"><span class="sociable_tagline"><strong>Bookmark:</strong><span>These icons link to social bookmarking sites where readers can share and discover new web pages.</span></span><ul>
	<li><a href="http://digg.com/submit?phase=2&amp;url=http://www.geek-notes.com/css/23/cascading-style-sheet-switcher/&amp;title=Cascading+Style+Sheet+Switcher" title="digg"><img src="http://www.geek-notes.com/wp-content/plugins/sociable/images/digg.png" alt="digg" /></a></li>
	<li><a href="http://del.icio.us/post?url=http://www.geek-notes.com/css/23/cascading-style-sheet-switcher/&amp;title=Cascading+Style+Sheet+Switcher" title="del.icio.us"><img src="http://www.geek-notes.com/wp-content/plugins/sociable/images/delicious.png" alt="del.icio.us" /></a></li>
	<li><a href="http://blogmarks.net/my/new.php?mini=1&amp;simple=1&amp;url=http://www.geek-notes.com/css/23/cascading-style-sheet-switcher/&amp;title=Cascading+Style+Sheet+Switcher" title="blogmarks"><img src="http://www.geek-notes.com/wp-content/plugins/sociable/images/blogmarks.png" alt="blogmarks" /></a></li>
	<li><a href="http://co.mments.com/track?url=http://www.geek-notes.com/css/23/cascading-style-sheet-switcher/&amp;title=Cascading+Style+Sheet+Switcher" title="co.mments"><img src="http://www.geek-notes.com/wp-content/plugins/sociable/images/co.mments.gif" alt="co.mments" /></a></li>
	<li><a href="http://www.newsvine.com/_tools/seed&amp;save?u=http://www.geek-notes.com/css/23/cascading-style-sheet-switcher/&amp;h=Cascading+Style+Sheet+Switcher" title="NewsVine"><img src="http://www.geek-notes.com/wp-content/plugins/sociable/images/newsvine.png" alt="NewsVine" /></a></li>
	<li><a href="http://www.furl.net/storeIt.jsp?u=http://www.geek-notes.com/css/23/cascading-style-sheet-switcher/&amp;t=Cascading+Style+Sheet+Switcher" title="Furl"><img src="http://www.geek-notes.com/wp-content/plugins/sociable/images/furl.png" alt="Furl" /></a></li>
</ul></div>
]]></content:encoded>
			<wfw:commentRss>http://www.geek-notes.com/css/23/cascading-style-sheet-switcher/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/2.5/</creativeCommons:license>
	</item>
	</channel>
</rss>
