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

<channel>
	<title>scriptygoddess &#187; PHP</title>
	<atom:link href="http://www.scriptygoddess.com/archives/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scriptygoddess.com</link>
	<description></description>
	<lastBuildDate>Thu, 08 Dec 2011 18:23:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>CSS for iPhone&#039;s eyes only (watch out for the cache)</title>
		<link>http://www.scriptygoddess.com/archives/2010/04/14/css-for-iphones-eyes-only-watch-out-for-the-cache/</link>
		<comments>http://www.scriptygoddess.com/archives/2010/04/14/css-for-iphones-eyes-only-watch-out-for-the-cache/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 04:21:07 +0000</pubDate>
		<dc:creator>Jennifer</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS related]]></category>
		<category><![CDATA[Lessons learned]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.scriptygoddess.com/?p=1636</guid>
		<description><![CDATA[On a project I was working on recently, I was pulling in some css for iPhone users using PHP with the following code: &#60;php if (strpos($_SERVER['HTTP_USER_AGENT'],"iPhone")) { ?&#62; ... CUSTOM CSS FOR IPHONE... &#60;php } ?&#62; This will also work for iPhones and iPod Touch: &#60;php if (strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') &#124;&#124; strstr($_SERVER['HTTP_USER_AGENT'],'iPod')) { ?&#62; ... CUSTOM CSS... [...]
No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>On a project I was working on recently, I was pulling in some css for iPhone users using PHP with the following code:</p>
<p><code>&lt;php if (strpos($_SERVER['HTTP_USER_AGENT'],"iPhone")) { ?&gt;<br />
... CUSTOM CSS FOR IPHONE...<br />
&lt;php } ?&gt;</code></p>
<p>This will also work for iPhones and iPod Touch:</p>
<p><code>&lt;php if (strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod')) { ?&gt;<br />
... CUSTOM CSS...<br />
&lt;php } ?&gt;</code></p>
<p>That&#039;s great and all &#8211; but then suddenly one day it stopped working. Going back through what changed on the site, I realized one thing we added was a cache plugin. DOH! So the page gets cached without the custom CSS for the iPhone &#8211; the iPhone calls up the site but gets served the cached page (non-iPhone version)&#8230; Yeah, that would do it.</p>
<p>So I found <a href="http://www.boutell.com/newfaq/creating/iphone.html">this solution</a>:</p>
<p><code>&lt;!--[if !IE]&gt;--&gt;<br />
    &lt;style type="text/css"&gt;<br />
	@media only screen and (max-device-width: 480px) {<br />
.... CUSTOM CSS HERE....<br />
	}<br />
	&lt;/style&gt;<br />
&lt;!--&lt;![endif]--&gt;</code></p>
<p>or of course you can pull in a whole stylesheet for the iphone:</p>
<p><code>&lt;!--[if !IE]&gt;--&gt;<br />
&lt;link media="only screen and (max-device-width: 480px)"  rel="stylesheet" type="text/css" href="iphone.css" /&gt;<br />
&lt;!--&lt;![endif]--&gt;</code></p>
<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.scriptygoddess.com/archives/2010/04/14/css-for-iphones-eyes-only-watch-out-for-the-cache/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Showing multiple random quotes</title>
		<link>http://www.scriptygoddess.com/archives/2010/04/01/showing-multiple-random-quotes/</link>
		<comments>http://www.scriptygoddess.com/archives/2010/04/01/showing-multiple-random-quotes/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 05:03:39 +0000</pubDate>
		<dc:creator>Jennifer</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Script snippet]]></category>

		<guid isPermaLink="false">http://www.scriptygoddess.com/?p=1629</guid>
		<description><![CDATA[Showing random quotes isn&#039;t anything new. When I needed to do something similar on a site recently, there were plenty of pre-written scripts to choose from. The trick however, was that I wanted to show more than one quote at a time (and of course I didn&#039;t want to show the same quote twice. That [...]
No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Showing random quotes isn&#039;t anything new. When I needed to do something similar on a site recently, there were plenty of pre-written scripts to choose from. The trick however, was that I wanted to show more than one quote at a time (and of course I didn&#039;t want to show the same quote twice. That wasn&#039;t as easy to find.  (I&#039;m sure there&#039;s variations out there, but now I&#039;ll always have a place to find mine) <img src='http://www.scriptygoddess.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>First step is to load all the quotes you want into an array:</p>
<p><code>$quotes[] = "This is line 1";<br />
$quotes[] = "This is line 2";<br />
$quotes[] = "this is line 3";<br />
$quotes[] = "This is another line";<br />
$quotes[] = "This is yet another line";<br />
$quotes[] = "Line 6";</code></p>
<p>Now get a handful of keys using <a href="http://php.net/manual/en/function.array-rand.php">array_rand</a>:</p>
<p><code>$rand_keys = array_rand($quotes, 2);</code></p>
<p>We specified to get two random keys from the $quotes array &#8211; (You can grab more if you want, just change the &#034;2&#034;)</p>
<p>Now that we have our random keys &#8211; you can either list them out one by one like this:</p>
<p><code>//show the first quote...<br />
echo $quotes[$rand_keys[0]];<br />
//show the second quote<br />
echo $quotes[$rand_keys[1]];</code></p>
<p>Or you can loop through the keys like this:</p>
<p><code>foreach ($rand_keys as $value) {<br />
	echo $quotes[$value]."&lt;br /&gt;";<br />
}</code></p>
<p>That&#039;s all there is to it.</p>
<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.scriptygoddess.com/archives/2010/04/01/showing-multiple-random-quotes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Never Update the Copyright Year Again (on a PHP page)</title>
		<link>http://www.scriptygoddess.com/archives/2010/01/13/never-update-the-copyright-year-again-on-a-php-page/</link>
		<comments>http://www.scriptygoddess.com/archives/2010/01/13/never-update-the-copyright-year-again-on-a-php-page/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 17:57:55 +0000</pubDate>
		<dc:creator>Jennifer</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.scriptygoddess.com/?p=1628</guid>
		<description><![CDATA[This is a really silly little trick &#8211; but if you have a PHP page that has a &#169; copyright year at the bottom &#8211; there is no reason you should be updating that every year (unless it&#039;s just something you overlooked initially) As you get requests from clients now that we&#039;re in a new [...]
No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>This is a really silly little trick &#8211; but if you have a PHP page that has a &copy; copyright year at the bottom &#8211; there is no reason you should be updating that every year (unless it&#039;s just something you overlooked initially) <img src='http://www.scriptygoddess.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  As you get requests from clients now that we&#039;re in a new year to change the copyright year in the footer &#8211; do yourself a favor and use this instead so you never have to do it again. <img src='http://www.scriptygoddess.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><code>&lt;?php echo date("Y"); ?&gt;</code></p>
<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.scriptygoddess.com/archives/2010/01/13/never-update-the-copyright-year-again-on-a-php-page/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to make a progress/goal (thermometer-like) bar graph with PHP</title>
		<link>http://www.scriptygoddess.com/archives/2008/02/23/how-to-make-a-progressgoal-thermometer-like-bar-graph-with-php/</link>
		<comments>http://www.scriptygoddess.com/archives/2008/02/23/how-to-make-a-progressgoal-thermometer-like-bar-graph-with-php/#comments</comments>
		<pubDate>Sat, 23 Feb 2008 20:09:39 +0000</pubDate>
		<dc:creator>Jennifer</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2008/02/23/how-to-make-a-progressgoal-thermometer-like-bar-graph-with-php/</guid>
		<description><![CDATA[On one of my projects recently, they needed a dynamic bar graph that would show the progress towards a goal of donations. I&#039;ve never done something like that before, and it turns out it&#039;s actually pretty simple to do. I&#039;ll explain how the code works and then include everything at the end. What we&#039;ll be [...]
No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>On one of my projects recently, they needed a dynamic bar graph that would show the progress towards a goal of donations. I&#039;ve never done something like that before, and it turns out it&#039;s actually pretty simple to do. I&#039;ll explain how the code works and then include everything at the end.<br />
<span id="more-1512"></span><br />
What we&#039;ll be doing is calling a php script into the src of an img tag in HTML. The php processes how to draw that image. We pass values to the PHP script to tell it what to draw. I&#039;m going to show this with those values hardcoded in that src path &#8211; but in practice these values would probably be pulled from a database that totals up those numbers into a &#034;total donation&#034; value.</p>
<p>So first things first &#8211; since we&#039;ll be passing values in the src path &#8211; we need to check for them and then pull them into our script using $_GET: (also we&#039;ll say we&#039;re making a image with <a href="http://us3.php.net/manual/en/function.header.php">header</a>)</p>
<p><code>if (isset($_GET['cur']) &#038;&#038; isset($_GET['goal']) &#038;&#038; isset($_GET['height']) &#038;&#038; isset($_GET['width']) ) {<br />
header("Content-type: image/png");<br />
//setting the values we'll use in our script pulled in from GET<br />
$height = trim($_GET['height']);<br />
$width = trim($_GET['width']);<br />
$goal = trim($_GET['goal']);<br />
$current = trim($_GET['cur']);</code></p>
<p>Next &#8211; for the text to be displayed on the bar graph &#8211; we&#039;ll want to say what percent towards the goal we are. (Or if the goal is reached &#8211; just say 100%)</p>
<p><code>if ($current &gt; $goal) {<br />
//if we've reached the goal - just max out current value and say 100% achieved<br />
//if you don't mind text saying over 100% then you can remove the next two lines<br />
$current = $goal;<br />
$percent= "100%!";<br />
} else if ($current == "" || $current == "0") {<br />
//if no donations - just say 0%<br />
$percent = "0%";<br />
} else {<br />
//otherwise calculate the percent to goal text<br />
$percent = round(($current/$goal)*100) . "%";<br />
}</code></p>
<p>Now lets start building our bar graph. First we <a href="http://us3.php.net/manual/en/function.imagecreate.php">create an image</a> and assign a variable name so we can apply stuff to it. Then, to create colors we can use on our image, we use the <a href="http://us3.php.net/manual/en/function.imagecolorallocate.php">ImageColorAllocate </a>function in PHP &#8211; so that it understands it&#039;s a color.</p>
<p><code>$im = @imagecreate($width,$height)<br />
or die("Cannot Initialize new GD image stream");<br />
$bg = imagecolorallocate($im,200,200,200); // grey<br />
$fg = imagecolorallocate($im,255,0,0); //red<br />
$text_color = imagecolorallocate($im, 0, 0, 0); //black</code></p>
<p>(<em>Side note: </em>The first color that is called with imagecolorallocate&#8230; this is the color which is used as the default background of the image even though it isn&#039;t SPECIFICALLY &#034;applied&#034; to the image as such in any particular function.)</p>
<p>Now we need to calculate the &#034;fill&#034; box. The box I&#039;m creating in this example grows UP. This part for some reason I had the toughest time trying to understand what the values were and how to calculate it. What &#034;x&#034; and &#034;y&#034; were in relation to my graphic. So now that I have a basic understanding of how it works, I&#039;ll try to explain it in the way that I understand it.</p>
<p>We&#039;re going to use the <a href="http://us3.php.net/manual/en/function.imagefilledrectangle.php">imagefilledrectangle</a> function to create the box in our graphic that shows how many donations we have. PHP.net shows the function like this:</p>
<p>imagefilledrectangle ($image, $x1, $y1, $x2, $y2, $color)</p>
<p>x1 and y1 are coordinates on our image. It starts at 0 at the top and then works <strong>across our width</strong> for our <em>x value</em>, and <strong>down our height</strong> for <em>y value</em>. x1,y1 are coordinates of the left point of our rectangle, and x2,y2 are the coordinates of the right point which is on the opposite/diagonal side of our rectangle. If we wanted to fill in our image completely and our image was 20pixels WIDE and 100pixels HIGH: the top left of the image x,y coordinate value is 0,0. Then we tell it the second x,y coordinate &#8211; again we go across the full width of the image for the x value (20pixels) and go down the height of the image to get the y value (100pixels). And if that didn&#039;t confuse you, I made a diagram that should make all that clear as mud:</p>
<p><img src='http://www.scriptygoddess.com/wp-content/uploads/2008/02/goalgraphexplanation.jpg' alt='X Y coordinate diagram' /></p>
<p>In our thermometer/goal graph &#8211; that box is dynamic &#8211; so we need to calcuate that first x,y coordinate based on our donations. Specifically &#8211; the y coordinate &#8211; because that tells us how filled up our thermometer is. Here is how I calculate it. (Because you actually are still here even after my convoluted coordinate explanation I&#039;ll spare you the explanation of how the calculation works and you can take my word for it that it will figure out the correct height based on the goal and current achieved donation values.)</p>
<p><code>$fill = $height-(($current/$goal)*$height);</code></p>
<p>And now we make our rectangle:</p>
<p><code>imagefilledrectangle($im, 0, $fill, $width, $height, $fg);</code></p>
<p>Now lets write on our picture what the percentage is. <a href="http://us3.php.net/manual/en/function.imagestring.php">imagstring</a> has a few parameters that get passed &#8211; first is of course the image variable, then a &#034;font&#034; number (default fonts are 1-5), then another x , y coordinate. This will be the top left corner of where the text will show up. For simplicity sake I just have it a little off the corner at the top., then our text (we defined this earlier), and then our text color &#8211; (also defined earlier)</p>
<p><code>imagestring($im, 3, 2, 2,  $percent, $text_color);</code></p>
<p>Then we output our image to the browser:</p>
<p><code>imagepng($im);</code></p>
<p>Clean up our mess and close our &#034;if&#034; statement:</p>
<p><code>imagedestroy($im);<br />
}</code></p>
<p><a href="http://scriptygoddess.com/resources/1512/goalgraph.php?cur=1650&#038;goal=3000&#038;width=30&#038;height=100">Here&#039;s a demo</a>. </p>
<p><a href="http://scriptygoddess.com/downloads/goalgraph.txt">Here&#039;s the whole thing in one file</a> (rename it with a .php extension). </p>
<p>Call it in your html like this:</p>
<p><code>&lt;img src="goalgraph.php?cur=1650&#038;goal=3000&#038;width=30&#038;height=100" alt="Help us get to our goal" /&gt;</code></p>
<p><strong>Bonus Tip</strong><br />
<a href="http://scriptygoddess.com/resources/1512/goaldemo.htm">Now position your bar graph over another image with css.</a><br />
(I removed the text in that last example because it was too thin&#8230;)</p>
<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.scriptygoddess.com/archives/2008/02/23/how-to-make-a-progressgoal-thermometer-like-bar-graph-with-php/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Use PHP to get the current page/file name</title>
		<link>http://www.scriptygoddess.com/archives/2007/07/13/use-php-to-get-the-current-pagefile-name/</link>
		<comments>http://www.scriptygoddess.com/archives/2007/07/13/use-php-to-get-the-current-pagefile-name/#comments</comments>
		<pubDate>Fri, 13 Jul 2007 17:45:26 +0000</pubDate>
		<dc:creator>Jennifer</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2007/07/13/use-php-to-get-the-current-pagefile-name/</guid>
		<description><![CDATA[I probably already have this posted somewhere, I know I use it a ton but always have to look it up for the exact syntax. Here&#039;s how you can get the name of the current file (ie. if your file is &#034;aboutus.php&#034; this will echo &#034;aboutus.php&#034;) &#60;?php $currentFile = $_SERVER["PHP_SELF"]; $parts = Explode('/', $currentFile); echo [...]
No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>I probably already have this posted somewhere, I know I use it a ton but always have to look it up for the exact syntax. Here&#039;s how you can get the name of the current file (ie. if your file is &#034;aboutus.php&#034; this will echo &#034;aboutus.php&#034;)<br />
<code>&lt;?php<br />
$currentFile = $_SERVER["PHP_SELF"];<br />
$parts = Explode('/', $currentFile);<br />
echo  $parts[count($parts) - 1];<br />
?&gt;</code></p>
<p>I&#039;ve seen variations on it, as I said, I always have to look it up for the exact syntax &#8211; but today I found that from <a href="http://www.programmingtalk.com/archive/index.php/t-915.html">here</a>. Also <a href="http://www.roscripts.com/snippets/show/83">here </a>- which seems to list a bunch of other useful snippets.</p>
<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.scriptygoddess.com/archives/2007/07/13/use-php-to-get-the-current-pagefile-name/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>How to use Session Cookies in PHP</title>
		<link>http://www.scriptygoddess.com/archives/2007/05/28/how-to-use-session-cookies-in-php/</link>
		<comments>http://www.scriptygoddess.com/archives/2007/05/28/how-to-use-session-cookies-in-php/#comments</comments>
		<pubDate>Tue, 29 May 2007 04:03:47 +0000</pubDate>
		<dc:creator>Jennifer</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2007/05/28/how-to-use-session-cookies-in-php/</guid>
		<description><![CDATA[I had written this tutorial sometime ago with the intention of cleaning it up and posting it here. Finally getting around to this. The original purpose of the tutorial was to explain how to use session cookies to a friend of mine who was working on a form that was a number of pages long. [...]
No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>I had written this tutorial sometime ago with the intention of cleaning it up and posting it here. Finally getting around to this. The original purpose of the tutorial was to explain how to use session cookies to a friend of mine who was working on a form that was a number of pages long. I apologize if it seems like I&#039;m starting in the middle of an explanation (I am a bit). The tutorial assumes you&#039;ve created a few form pages that are connected to each other and that the end result will be taking the data from all the pages and dumping it into a database. This tutorial really focuses on just the session cookie piece and does not go into how to create the forms, or do the inserts for the database.<br />
<span id="more-1482"></span><br />
Since the form spans across a number of pages, the best way to handle this &#034;in between&#034; stage is to store all the values in a session cookie. The reason being, that $_POST array disappears after the first page. Just using the $_POST array, you could spit back all the information to the page &#8211; however, in order to <em>submit the data again</em> to be processed and inserted into a database, you&#039;d need to put it into yet another form &#8211; specifically hidden values. You can do it this way &#8211; there&#039;s nothing wrong with it really &#8211; it&#039;s just more of a pain. By putting all the values into a session cookie, if the user clicked the &#034;back&#034; button to make changes, their info is still remembered, where as with $_POST &#8211; it wouldn&#039;t be there any more and they&#039;d have to start from scratch! (I know that firefox tends to remember the data &#8211; but IE won&#039;t)</p>
<p>side note: difference between a session cookie and a regular cookie &#8211; session cookies only last as long as your browser is up and running. They will &#034;timeout&#034; after a while too &#8211; how long before it times out is a setting in PHP itself (that php.ini file) on the server. Usually it&#039;s a few minutes. If it&#039;s not long enough &#8211; then session cookies probably aren&#039;t appropriate for what you need to do! Regular cookies are stored on the users machine. You can set the &#034;lifespan&#034; of this cookie to be several minutes or even several years. So obviously it doesn&#039;t go away if the user closes their browser, etc. Since we&#039;re only using this information for a moment, we probably wouldn&#039;t want the browser to remember it forever and ever &#8211; which is why session cookies are perfect.</p>
<p>Ok, so here&#039;s how you put all the post values into a session cookie.</p>
<p>First thing &#8211; you need to tell the page you&#039;re using sessions. Start the session by putting this snippet of code above ANY html tags (including doctype declarations)<code>&lt;?php<br />
session_start();<br />
?&gt;</code></p>
<p>In this next step we&#039;ll want to iterate through that $_POST array and store each piece of information in a similar session cookie array.</p>
<p>To create the session cookies &#8211; very easy:<code>&lt;?php<br />
foreach($_POST as $k=&gt;$v) {<br />
            $_SESSION['post'][$k]=$v;<br />
}<br />
?&gt;</code><br />
(I&#039;ve actually <a href="http://www.scriptygoddess.com/archives/2007/02/07/store-all-post-variablesvalues-in-a-session/">mentioned this method before</a>)</p>
<p>What that says is &#8211; for each piece of information we have in the $_POST array &#8211; $k will be the index name and $v will be the value &#8211; So if you have a field with the name &#034;First_Name&#034; it&#039;s value is now stored in this session cookie: $_SESSION['post']['First_Name'].</p>
<p>Another side note &#8211; the reason why I&#039;m storing them in a sub-array $_SESSION['post']['FIELDNAME'] as opposed to just $_SESSION['FIELDNAME'] is in the event that the field name overlaps with something else that might be used. In fact, you can rename &#039;post&#039; to be specific to the form you&#039;re working on to ensure it&#039;s not overlapping with anything else&#8230; ie. $_SESSION['myContactForm']['FIELDNAME']</p>
<p>Ok &#8211; once you do all the above (start the session &#8211; get all the data into a session cookie, etc.) you can then display each piece by using the session cookies. For example if you want to do a &#034;confirm&#034; page that spits back all the data that was entered before it gets submitted. You just echo all the session cookies like this: <code>echo $_SESSION['post']['First_name']</code></p>
<p>On this &#034;confirm&#034; page &#8211; you can have a simple form with just a &#034;confirm&#034; and &#034;go back&#034; button. If you want people to be able to go back and edit their entries, you can make the fields in the form pages be &#034;pre-populated&#034; using the session cookie values like this:<br />
<code>First Name: &lt;input type="text" name="First_Name" value="&lt;?= $_SESSION['post']['First_Name']; ?&gt;"&gt;</code></p>
<p>Unfortunately &#8211; checkboxes and dropdowns need to be handled differently. Because we&#039;re not displaying the value &#8211; we need to check each option &#8211; and if that&#039;s what had been selected, then we echo the text <em>selected=&#034;selected&#034;</em></p>
<p>To do the check you could do it &#034;long hand&#034; like this:<code>&lt;option value="NEW YORK" &lt;?php if ($_SESSSION['post']['state'] == "NEW YORK") {<br />
            echo 'selected="selected"';<br />
} ?&gt; &gt;NEW YORK&lt;/option&gt;</code></p>
<p>and do that for each item &#8211; or you can do it the &#034;shorthand&#034; way &#8211; which I prefer for this much repetition &#8211; makes the code cleaner too:<code>&lt;option value="NEW YORK" &lt;?php echo $_SESSION['post']['state']=='NEW YORK'?'selected="selected"':''; ?&gt; &gt;NEW YORK&lt;/option&gt;</code></p>
<p>Another side note: the way that shorthand works is this:</p>
<p><strong>CONDITIONAL_STATEMENT ? Do_this_if_true : Do_this_if_false</strong></p>
<p>Once we&#039;re done updating the database, we&#039;ll want to clear out our session cookie:</p>
<p><code>&lt;?php<br />
unset($_SESSION['post']);<br />
?&gt;</code></p>
<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.scriptygoddess.com/archives/2007/05/28/how-to-use-session-cookies-in-php/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Insert text/image block in the middle of a WordPress category page</title>
		<link>http://www.scriptygoddess.com/archives/2007/05/05/insert-textimage-block-in-the-middle-of-a-wordpress-category-page/</link>
		<comments>http://www.scriptygoddess.com/archives/2007/05/05/insert-textimage-block-in-the-middle-of-a-wordpress-category-page/#comments</comments>
		<pubDate>Sat, 05 May 2007 22:57:30 +0000</pubDate>
		<dc:creator>Jennifer</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2007/05/05/insert-textimage-block-in-the-middle-of-a-wordpress-category-page/</guid>
		<description><![CDATA[For a long time I resisted the urge to put ads on the site. I&#039;m sure you&#039;ve noticed I finally caved in this past year. I&#039;ve tried to keep it relatively unobtrusive. In one case I was contacted directly to put an ad up on one of my category pages in the content area. I [...]
No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>For a long time I resisted the urge to put ads on the site. I&#039;m sure you&#039;ve noticed I finally caved in this past year. I&#039;ve tried to keep it relatively unobtrusive. In one case I was contacted directly to put an ad up on one of my category pages in the content area. I didn&#039;t want to put it at the top above the content. Ideally &#8211; I wanted it put in the middle in a separate div block, maybe after 3 or so posts, but only on the first category page. Here is the code I used to get that to work&#8230;</p>
<p>First, above the loop I added this:<br />
<code>&lt;?php<br />
$count = 0;<br />
?&gt;</code><br />
This is to count the number of posts we&#039;re on.</p>
<p>Then just after the end of the loop call (&lt;?php while (have_posts()) : the_post(); ?&gt;) but before you start the code for the posts, I added this:<br />
<code>&lt;?php<br />
if ($wp_query-&gt;get("page") || $wp_query-&gt;get("paged")) {<br />
$onFirstPage = false;<br />
} else {<br />
$onFirstPage = true;<br />
}<br />
?&gt;</code><br />
That bit of code I got basically from <a href="http://wordpress.org/support/topic/72466">this post on the support boards</a>.</p>
<p>Then to determine if we should call the text for the ad (in this case I wanted it displayed only on category id #29), and to then display the ad&#8230;<br />
<code>&lt;?php<br />
if (is_category('29') &#038;&#038; $count == 2 &#038;&#038; $onFirstPage) {<br />
?&gt;<br />
...ADVERTISEMENT TEXT OR IMAGE GOES HERE...<br />
&lt;?<br />
}<br />
$count++;<br />
?&gt;</code></p>
<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.scriptygoddess.com/archives/2007/05/05/insert-textimage-block-in-the-middle-of-a-wordpress-category-page/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MySQL Cheatsheet</title>
		<link>http://www.scriptygoddess.com/archives/2007/03/24/mysql-cheatsheet/</link>
		<comments>http://www.scriptygoddess.com/archives/2007/03/24/mysql-cheatsheet/#comments</comments>
		<pubDate>Sat, 24 Mar 2007 19:27:36 +0000</pubDate>
		<dc:creator>Jennifer</dc:creator>
				<category><![CDATA[mySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2007/03/24/mysql-cheatsheet/</guid>
		<description><![CDATA[Another mini-cheatsheet&#8230; I know this stuff, but I&#039;m always looking it up for the exact syntax. Connect to MySQL and to the database &#8211; mysql_connect $connect = mysql_connect("localhost", "mysqluser", "userpassword") or die(mysql_error()); Select the database &#8211; mysql_select_db mysql_select_db("databasename", $connect) or die(mysql_error()); Close connection &#8211; mysql_close mysql_close($connect) or die(mysql_error()); Select data from the database &#8211; mysql_query [...]
No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Another mini-cheatsheet&#8230; I know this stuff, but I&#039;m always looking it up for the exact syntax.</p>
<p>Connect to MySQL and to the database &#8211; <a href="http://us2.php.net/manual/en/function.mysql-connect.php">mysql_connect</a><br />
<code>$connect = mysql_connect("localhost", "mysqluser", "userpassword") or die(mysql_error());</code></p>
<p>Select the database &#8211; <a href="http://us2.php.net/manual/en/function.mysql-select-db.php">mysql_select_db</a><br />
<code>mysql_select_db("databasename", $connect) or die(mysql_error());</code></p>
<p>Close connection &#8211; <a href="http://us2.php.net/manual/en/function.mysql-close.php">mysql_close</a><br />
<code>mysql_close($connect) or die(mysql_error());</code></p>
<p>Select data from the database &#8211; <a href="http://us2.php.net/manual/en/function.mysql-query.php">mysql_query</a><br />
<code>$myquery = "SELECT tablefield, tablefield2 FROM tablename WHERE tablefield = '" .$variable. "' AND tablefield2 = '" .$variable2. "'";<br />
(or)<br />
$myquery  = "SELECT * FROM tablename WHERE tablefield = '" .$variable. "' AND tablefield2 = '" .$variable2. "'";<br />
$myresult = mysql_query($myquery ) or die(mysql_error());</code></p>
<p><a href="http://sqlcourse2.com/select2.html">Selecting</a>, <a href="http://sqlcourse2.com/groupby.html">Grouping</a>, and <a href="http://sqlcourse2.com/orderby.html">Ordering</a> the data<br />
<code>SELECT [ALL | DISTINCT] column1[,column2] FROM table1[,table2] [WHERE "conditions"] [GROUP BY "column-list"] [HAVING "conditions] [ORDER BY "column-list" [ASC | DESC] ]</code></p>
<p>Display Data &#8211; <a href="http://us2.php.net/manual/en/function.mysql-fetch-array.php">mysql_fetch_array</a><br />
<code>while ($row = mysql_fetch_array($myresult)) {<br />
echo $row["tablefield"] . " and " . $row["tablefield2"] . "&lt;br /&gt;";<br />
}</code></p>
<p><a href="http://sqlcourse.com/insert.html">Insert data</a><br />
<code>$query_insert = "INSERT INTO tablename(tablefield, tablefield2) VALUES('" .$variable. "', '" .$variable2. "')";<br />
$result_insert = mysql_query($query_insert) or die(mysql_error());</code></p>
<p>Insert data, multiple rows<br />
<code>$query_insert = "INSERT INTO tablename(tablefield, tablefield2) VALUES ('" .$variable. "', '" .$variable2. "'),<br />
('" .$variable. "', '" .$variable2. "'),<br />
('" .$variable. "', '" .$variable2. "')";<br />
$result_insert = mysql_query($query_insert) or die(mysql_error());</code></p>
<p>Update data<br />
<code>$query_update = "UPDATE tablename SET tablefield = '" .$variable. "', tablefield2 = '" .$variable. "' WHERE tablefield3 = '" .$variable3. "'";<br />
$result_update = mysql_query($query_update) or die(mysql_error());</code></p>
<p>Delete a row from a table<br />
<code>$query_delete = "DELETE FROM tablename WHERE tablefield = '" .$variable. "'";<br />
$result_delete = mysql_query($query_delete) or die(mysql_error());</code></p>
<p>Some good references:<br />
<a href="http://sqlcourse.com/">http://sqlcourse.com</a><br />
<a href="http://www.w3schools.com/sql/">http://www.w3schools.com/sql/</a></p>
<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.scriptygoddess.com/archives/2007/03/24/mysql-cheatsheet/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Store Locator using PHP and AJAX</title>
		<link>http://www.scriptygoddess.com/archives/2007/02/13/store-locator-using-php-and-ajax/</link>
		<comments>http://www.scriptygoddess.com/archives/2007/02/13/store-locator-using-php-and-ajax/#comments</comments>
		<pubDate>Wed, 14 Feb 2007 03:45:49 +0000</pubDate>
		<dc:creator>Jennifer</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Scripts]]></category>

		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2007/02/13/store-locator-using-php-and-ajax/</guid>
		<description><![CDATA[Well, this was fun! I just made a store locator for a client using PHP and AJAX! I was basically following the instructions from this page: SomeCoders.com (Retrieving database information with AJAX, PHP and MySQL) Take a look&#8230; Here&#039;s what the heart of the &#034;user&#034; page looks like. At the top &#8211; before the HTML [...]
No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Well, this was fun! I just made a store locator for a client using PHP and AJAX! <img src='http://www.scriptygoddess.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>I was basically following the instructions from this page: <a href="http://www.somecoders.com/2006/04/retrieving-database-information-with-ajax-php-and-mysql/">SomeCoders.com</a> (Retrieving database information with AJAX, PHP and MySQL)</p>
<p>Take a look&#8230;<br />
<span id="more-1456"></span><br />
Here&#039;s what the heart of the &#034;user&#034; page looks like. </p>
<p>At the top &#8211; before the HTML tag &#8211; I open a connection with the database:</p>
<p><code>&lt;?php<br />
$dbconnection = mysql_connect ("localhost", "mysqlusername", "mysqlpassword") or die ('I cannot connect to the database because: ' . mysql_error());<br />
mysql_select_db ("mydatabase", $dbconnection) or die("Couldn't open database: ".mysql_error());<br />
?&gt;</code></p>
<p>Then inside the page&#8230;</p>
<p><code>&lt;h1&gt;Store Locator&lt;/h1&gt;<br />
&lt;p&gt;To locate a store, select a state from the list below.&lt;/p&gt;<br />
&lt;form name="selectastore"&gt;<br />
&lt;?php<br />
$result = mysql_query("SELECT DISTINCT state from storelocator order by state");<br />
?&gt;<br />
&lt;div class="row"&gt;<br />
&lt;div class="label"&gt;State:&lt;/div&gt;&lt;div class="field"&gt;&lt;select name="state" onchange="getcities(this.value)"&gt;<br />
&lt;option value="" selected="selected"&gt;Select a State...&lt;/option&gt;<br />
&lt;?php<br />
while ($row = mysql_fetch_array($result)) {<br />
?&gt;<br />
&lt;option value="&lt;?=$row['state']; ?&gt;"&gt;&lt;?=$row['state']; ?&gt;&lt;/option&gt;<br />
&lt;?php } ?&gt;<br />
&lt;/select&gt;&lt;/div&gt;<br />
&lt;/div&gt;<br />
&lt;div id="cities" class="row"&gt;<br />
&lt;/div&gt;<br />
&lt;div id="stores" class="row"&gt;<br />
&lt;/div&gt;<br />
&lt;/form&gt;</code></p>
<p>I used the javascript from the example. There&#039;s probably a way to combine it into one function, but because I&#039;m still sorting my way through this, I just duplicated the function to pull cities, and then pull store names&#8230;</p>
<p>This is the function to grab the list of cities&#8230; (I&#039;ve removed the comments, <a href="http://www.somecoders.com/2006/04/retrieving-database-information-with-ajax-php-and-mysql/">please see their post</a> for a more detailed explanation on how this function works.)</p>
<p><code>function getcities(state){<br />
document.getElementById('stores').innerHTML = "";<br />
/*<br />
doing this in case someone changes their mind and wants to select a different state. When they do - any stores that are already listed will be cleared.<br />
*/<br />
var xmlhttp=false;<br />
        try {<br />
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');<br />
        } catch (e) {<br />
                try {<br />
                        xmlhttp = new<br />
                        ActiveXObject('Microsoft.XMLHTTP');<br />
            } catch (E) {<br />
                xmlhttp = false;<br />
                        }<br />
        }<br />
        if (!xmlhttp &#038;&#038; typeof XMLHttpRequest!='undefined') {<br />
                xmlhttp = new XMLHttpRequest();<br />
        }<br />
        var file = 'getcities.php?state=';<br />
    xmlhttp.open('GET', file + state, true);<br />
    xmlhttp.onreadystatechange=function() {<br />
        if (xmlhttp.readyState==4) {<br />
                var content = xmlhttp.responseText;<br />
                if( content ){<br />
                      document.getElementById('cities').innerHTML = content;<br />
                }<br />
        }<br />
        }<br />
        xmlhttp.send(null)<br />
return;<br />
;<br />
}</code></p>
<p>This is the function (almost exact duplicate of the above) to grab the list of stores&#8230; This time, I&#039;m passing in the selected city, and the selected state.</p>
<p><code>function getstores(city, state){<br />
var xmlhttp=false;<br />
        try {<br />
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');<br />
        } catch (e) {<br />
                try {<br />
                        xmlhttp = new<br />
                        ActiveXObject('Microsoft.XMLHTTP');<br />
            } catch (E) {<br />
                xmlhttp = false;<br />
                        }<br />
        }<br />
        if (!xmlhttp &#038;&#038; typeof XMLHttpRequest!='undefined') {<br />
                xmlhttp = new XMLHttpRequest();<br />
        }<br />
    xmlhttp.open('GET', 'getstores.php?state=' + state + '&#038;city=' + city, true);<br />
    xmlhttp.onreadystatechange=function() {<br />
        if (xmlhttp.readyState==4) {<br />
                var content = xmlhttp.responseText;<br />
                if( content ){<br />
                      document.getElementById('stores').innerHTML = content;<br />
                }<br />
        }<br />
        }<br />
        xmlhttp.send(null)<br />
return;<br />
}</code></p>
<p>Now onto the PHP pages I&#039;m calling with the javascript&#8230;</p>
<p>&#034;getcities.php&#034; will populate the storelocator page with another dropdown box of cities it pulls from the database. Here&#039;s what that looks like:</p>
<p><code>&lt;?php<br />
$dbconnection = mysql_connect ("localhost", "mysqlusername", "mysqlpassword") or die ('I cannot connect to the database because: ' . mysql_error());<br />
mysql_select_db ("mydatabase", $dbconnection) or die("Couldn't open database: ".mysql_error());<br />
$result = mysql_query("SELECT DISTINCT city from storelocator where `state`= '".$_GET['state']."' order by city");<br />
echo "&lt;div class=\"label\"&gt;City:&lt;/div&gt;&lt;div class=\"field\"&gt;&lt;select name=\"city\" onchange=\"getstores(this.value, document.selectastore.state.options[ document.selectastore.state.selectedIndex ].value)\"&gt;";<br />
echo "&lt;option selected=\"selected\" value=\"\" &gt;Select a City...&lt;/option&gt;";<br />
while ($row = mysql_fetch_array($result)) {<br />
		echo "&lt;option value=\"".$row['city'] ."\"&gt;" . $row['city'] ."&lt;/option&gt;";<br />
	}<br />
echo"&lt;/select&gt;&lt;/div&gt;";<br />
?&gt;</code></p>
<p>When you select a city, a second php script gets called&#8230; &#034;getstores.php&#034;. This one grabs all the store data for the selected city and state and updates the page with the listing of stores&#8230;</p>
<p><code>&lt;?php<br />
$dbconnection = mysql_connect ("localhost", "mysqlusername", "mysqlpassword") or die ('I cannot connect to the database because: ' . mysql_error());<br />
mysql_select_db ("mydatabase", $dbconnection) or die("Couldn't open database: ".mysql_error());<br />
$result = mysql_query("SELECT * from storelocator where `city`='".urldecode($_GET['city'])."' AND `state`='".$_GET['state']."';");<br />
while ($row = mysql_fetch_array($result)) {<br />
		echo "&lt;p&gt;";<br />
		if ($row['url'] != "") {<br />
			echo "&lt;a href='".$row['url']."' target='_blank'&gt;";<br />
		}<br />
		echo $row['store'];<br />
		if ($row['url'] != "") {<br />
			echo "&lt;/a&gt;";<br />
		}<br />
		echo "&lt;br&gt;";<br />
		echo $row['address']."&lt;br&gt;";<br />
		if ($row['address2'] != "") {<br />
			echo $row['address2']."&lt;br&gt;";<br />
		}<br />
		echo $row['city'].", ".$row['state']." ".$row['zip'];<br />
		if ($row['phone'] != "") {<br />
			echo "&lt;br&gt;".$row['phone'];<br />
		}<br />
		echo "&lt;/p&gt;";<br />
	}<br />
?&gt;</code></p>
<p>I haven&#039;t done much with AJAX, so if you see any issues, feel free to let me know and I&#039;ll update the post with the corrections&#8230;</p>
<p>If I get some free time (*cough*HA!*cough*) I&#039;ll try to package it all up into a downloadable .zip file &#8211; including the &#034;admin&#034; side which has the adding/editing/deleting store functionality.</p>
<p><strong>5/26/2008 Update</strong> Since there&#039;s still some interest in this, <a href="http://www.scriptygoddess.com/downloads/storelocator.zip">here is a download</a> for the store locator. It includes the sql to create the store locator table, the admin pages, etc. There&#039;s no &#034;design&#034; to the pages &#8211; and the &#034;login&#034; for the admin pages is pretty bare-bones.</p>
<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.scriptygoddess.com/archives/2007/02/13/store-locator-using-php-and-ajax/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

