<?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>DJS Consulting Tech Blog &#187; PHP</title>
	<atom:link href="http://djs-consulting.com/linux/blog/category/programming/php/feed" rel="self" type="application/rss+xml" />
	<link>http://djs-consulting.com/linux/blog</link>
	<description>Technical Information You Can Use (or not - but hey, at least it&#039;s free!)</description>
	<lastBuildDate>Tue, 02 Mar 2010 19:11:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Handy PHP Backup Script</title>
		<link>http://djs-consulting.com/linux/blog/2008/a-handy-php-backup-script</link>
		<comments>http://djs-consulting.com/linux/blog/2008/a-handy-php-backup-script#comments</comments>
		<pubDate>Sat, 29 Mar 2008 05:06:03 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[mysqldump]]></category>
		<category><![CDATA[pg_dump]]></category>

		<guid isPermaLink="false">http://www.djs-consulting.com/linux/blog/2008/a-handy-php-backup-script</guid>
		<description><![CDATA[I found a script over on the Lunarpages Forums about using PHP to back up your site.  I have taken it, modified it a little, beefed up the documentation a lot, and am now posting it here.  You can download the source code for it, and it is also displayed below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
&#60;?php
/**
 * Generic [...]]]></description>
			<content:encoded><![CDATA[<p>I found a script over on the Lunarpages Forums about <a href="http://www.lunarforums.com/lunarpages_how_tos/site_and_mysql_backups_via_cron-t22118.0.html" title="Site and MySQL backups via cron - Lunarforums">using PHP to back up your site</a>.  I have taken it, modified it a little, beefed up the documentation a lot, and am now posting it here.  You can <a href="/linux/generic_backup.txt" title="Generic Backup Script">download the source code</a> for it, and it is also displayed below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
</pre></td><td class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #0000ff; font-style: italic;">/**
 * Generic Backup Script.
 * 
 * To configure this script for your purposes, just edit the parameters below.
 * Once you have the parameters set properly, when the script executes, it will
 * create an archive file, gzip it, and e-mail it to the address specified.  It
 * can be executed through cron with the command
 * 
 * php -q [name of script]
 * 
 * You are free to use this, modify it, copy it, etc.  However, neither DJS
 * Consulting nor Daniel J. Summers assume any responsibility for good or bad
 * things that happen when modifications of this script are run.
 * 
 * @author Daniel J. Summers &lt;daniel@djs-consulting.com&gt;
 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// --- SCRIPT PARAMETERS ---</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*  -- File Name --
	This is the name of the file that you're backing up, and should contain no
	slashes.  For example, if you're backing up a database, this might look
	something like...
$sFilename = &quot;backup-my_database_name-&quot; . date(&quot;Y-m-d&quot;) . &quot;.sql&quot;; */</span>
<span style="color: #000088;">$sFilename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;backup-[whatever-it-is]-&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.[extension]&quot;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">/*  -- E-mail Address --
	This is the e-mail address to which the message will be sent. */</span>
<span style="color: #000088;">$sEmailAddress</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;[your e-mail address]&quot;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">/*  -- E-mail Subject --
	This is the subject that will be on the e-mail you receive. */</span>
<span style="color: #000088;">$sEmailSubject</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;[something meaningful]&quot;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">/*  -- E-mail Message --
	This is the text of the message that will be sent. */</span>
<span style="color: #000088;">$sMessage</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Compressed database backup file $sFilename.gz attached.&quot;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">/*  -- Backup Command --
	This is the command that does the work.
&nbsp;
	A note on the database commands - your setup likely requires a password
	for these commands, and they each allow you to pass a password on the
	command line.  However, this is very insecure, as anyone who runs &quot;ps&quot; can
	see your password!  For MySQL, you can create a ~/.my.cnf file - it is
	detailed at http://dev.mysql.com/doc/refman/4.1/en/password-security.html .
	For PostgreSQL, the file is ~/.pgpass, and it is detailed at
	http://www.postgresql.org/docs/8.0/interactive/libpq-pgpass.html .  Both of
	these files should be chmod-ded to 600, so that they can only be viewed by
	you, the creator.
&nbsp;
	That being said, some common commands are...
&nbsp;
- Backing Up a MySQL Database
$sBackupCommand = &quot;mysqldump -u [user_name] [db_name] &gt; $sFilename&quot;;
&nbsp;
- Backing Up a PostgreSQL Database
$sBackupCommand = &quot;pg_dump [db_name] -h localhost -U [user_name] -d -O &gt; $sFilename&quot;;
&nbsp;
- Backing Up a set of files (tar and gzip)
$sBackupCommand = &quot;tar cvf $sFilename [directory]/*&quot;;
&nbsp;
Whatever command you use, this script appends .gz to the filename after the command is executed.  */</span>
<span style="color: #000088;">$sBackupCommand</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;[a backup command]&quot;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// --- END OF SCRIPT PARAMETERS ---</span>
<span style="color: #666666; font-style: italic;">//</span>
<span style="color: #666666; font-style: italic;">// Edit below at your own risk.  :)</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Do the backup.</span>
<span style="color: #000088;">$sResult</span> <span style="color: #339933;">=</span> <span style="color: #990000;">passthru</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sBackupCommand</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;; gzip $sFilename&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$sFilename</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;.gz&quot;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// Create the message.</span>
<span style="color: #000088;">$sMessage</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Compressed database backup file $sFilename attached.&quot;</span>;
<span style="color: #000088;">$sMimeBoundary</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;&lt;&lt;:&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$sData</span> <span style="color: #339933;">=</span> <span style="color: #990000;">chunk_split</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">base64_encode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sFilename</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #000088;">$sHeaders</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;From: $sEmailAddress<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;MIME-Version: 1.0<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;Content-type: multipart/mixed;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; boundary=<span style="color: #000099; font-weight: bold;">\&quot;</span>$sMimeBoundary<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #000088;">$sContent</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;This is a multi-part message in MIME format.<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;--$sMimeBoundary<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;Content-Type: text/plain; charset=<span style="color: #000099; font-weight: bold;">\&quot;</span>iso-8859-1<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;Content-Transfer-Encoding: 7bit<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #339933;">.</span> <span style="color: #000088;">$sMessage</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;--$sMimeBoundary<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;Content-Disposition: attachment;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;Content-Type: Application/Octet-Stream; name=<span style="color: #000099; font-weight: bold;">\&quot;</span>$sFilename<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;Content-Transfer-Encoding: base64<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #339933;">.</span> <span style="color: #000088;">$sData</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;--$sMimeBoundary<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// Send the message.</span>
<span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sEmailAddress</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sEmailSubject</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sContent</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sHeaders</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// Delete the file - we don't need it any more.</span>
<span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sFilename</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://djs-consulting.com/linux/blog/2008/a-handy-php-backup-script/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Algorithm for One-to-Many Child Table Updates</title>
		<link>http://djs-consulting.com/linux/blog/2008/algorithm-for-one-to-many-child-table-updates</link>
		<comments>http://djs-consulting.com/linux/blog/2008/algorithm-for-one-to-many-child-table-updates#comments</comments>
		<pubDate>Sat, 29 Mar 2008 03:13:21 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[many-to-one]]></category>
		<category><![CDATA[one-to-many]]></category>
		<category><![CDATA[pdo]]></category>

		<guid isPermaLink="false">http://www.djs-consulting.com/linux/blog/2008/algorithm-for-one-to-many-child-table-updates</guid>
		<description><![CDATA[While working on the Not So Extreme Makeover: Community Edition site, I came up with an algorithm that simplifies anything else I&#8217;ve ever written to deal with this condition.  I&#8217;ll set the scenario, explain the algorithm, share how I implemented it in PHP, and provide a modification if the scenario is a bit more [...]]]></description>
			<content:encoded><![CDATA[<p>While working on the <a href="http://www.notsoextreme.org" title="Not So Extreme Makeover: Community Edition">Not So Extreme Makeover: Community Edition</a> site, I came up with an algorithm that simplifies anything else I&#8217;ve ever written to deal with this condition.  I&#8217;ll set the scenario, explain the algorithm, share how I implemented it in PHP, and provide a modification if the scenario is a bit more complicated.</p>
<p><strong>Scenario</strong> &#8211; You have two parent tables, and a child table with a many-to-one relationship with both parent tables, used to map entries in the two parent tables to each other.  For this example, we&#8217;ll use these three tables&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> volunteer <span style="color: #66cc66;">&#40;</span>
    vol_id  integer  <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
    vol_last_name  varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>  <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
    <span style="color: #66cc66;">...</span>etc<span style="color: #66cc66;">...</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>vol_id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> r_volunteer_area <span style="color: #66cc66;">&#40;</span>
    rva_id  integer  <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
    rva_description  varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>  <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>rva_id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> volunteer_area <span style="color: #66cc66;">&#40;</span>
    va_volunteer_id  integer  <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
    va_area_id  integer  <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>va_volunteer_id<span style="color: #66cc66;">,</span> va_area_id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>va_volunteer_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> volunteer <span style="color: #66cc66;">&#40;</span>vol_id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>va_area_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> r_volunteer_area <span style="color: #66cc66;">&#40;</span>rva_id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p><strong>Algorithm</strong> &#8211; The three-step algorithm is as follows&#8230;</p>
<ol>
<li>Create a comma-delimited string of IDs for the child table.</li>
<li>Delete the IDs from the child table that are not in the list.</li>
<li>Insert the IDs into the child table that are not there already.</li>
</ol>
<p><strong>Implementation</strong> &#8211; In PHP, if you have an array, it&#8217;s easy to come up with comma-delimited list.  To get an array of values back in a post, define your fields with &#8220;[]&#8221; after the name&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;checkbox&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;area[]&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;chkArea1&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;chkArea1&quot;</span>&gt;</span>Do Something<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;checkbox&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;area[]&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;chkArea7&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;7&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;chkArea7&quot;</span>&gt;</span>Do Something Else<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span></pre></td></tr></table></div>

<p>Here&#8217;s the PHP code, using <a href="http://us.php.net/pdo" title="PHP Data Objects (PDO)">PHP Data Objects (PDO)</a> as the database interface, behind a helper class that creates the statement, appends the parameters, and executes it.  <em>(The &#8220;quoting&#8221; escapes the statement to avoid potential SQL injection attacks &#8211; putting it in its own class would make the implementation here much cleaner.)</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #0000ff; font-style: italic;">/**
 * STEP 1
 *    Create a comma-delimited list of IDs.
 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Quote will return the string as '2,3,4' - since we're using this</span>
<span style="color: #666666; font-style: italic;">// as an IN clause of integers, we'll strip the quotes off.</span>
<span style="color: #000088;">$sAreas</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pdo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">join</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;area&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$sAreas</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sAreas</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sAreas</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// Quote the volunteer ID.</span>
<span style="color: #000088;">$iVol</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pdo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;vol&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">PARAM_INT</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #0000ff; font-style: italic;">/**
 * STEP 2
 *    Delete the IDs that are no longer in the list.
 */</span>
<span style="color: #000088;">$dbService</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">executeCommand</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">&quot;DELETE FROM volunteer_area
    WHERE   va_volunteer_id = ?
        AND va_area_id NOT IN ($sAreas)&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iVol</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #0000ff; font-style: italic;">/**
 * STEP 3
 *    Insert the IDs that are not yet in the list.
 */</span>
<span style="color: #000088;">$dbService</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">executeCommand</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">&quot;INSERT INTO volunteer_area
        SELECT $iVol, rva_id
        FROM r_volunteer_area
        WHERE   rva_id IN ($sAreas)
            AND rva_id NOT IN
            (SELECT va_area_id
            FROM volunteer_area
            WHERE va_volunteer_id = ?)&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iVol</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;</pre></td></tr></table></div>

<p><strong>Modification</strong> &#8211; Suppose that now you accepted comments along with each of the checkboxes, so a simple two-integer insert/delete is no longer sufficient.  You would still only need to break step 3 into two steps.</p>
<ol>
<li>Get a list of IDs to update.</li>
<li>For each ID in the posted list
<ol>
<li>If the ID exists in the update list, update it.</li>
<li>Otherwise, insert it.</li>
</ol>
</li>
</ol>
<p>The implementation would then be able to use this list to make the decision without hitting the database every time.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Assume this returns an associative array of IDs.</span>
<span style="color: #000088;">$aUpdates</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dbService</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">performSelect</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">&quot;SELECT va_area_id
    FROM volunteer_area
    WHERE   va_volunteer_id = ?
        AND va_area_id IN ($sAreas)&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iVol</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;area&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$iArea</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iArea</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aUpdates</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Update the table</span>
        <span style="color: #339933;">...</span>etc<span style="color: #339933;">...</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Insert into the table</span>
        <span style="color: #339933;">...</span>etc<span style="color: #339933;">...</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I think you&#8217;ll agree that this is much better than spinning through a loop, doing a count on each ID to see if it exists, then either doing an update or an insert based on the count.  And, while the implementation here is PHP, it could easily be implemented in any language that supports arrays and database access.</p>
]]></content:encoded>
			<wfw:commentRss>http://djs-consulting.com/linux/blog/2008/algorithm-for-one-to-many-child-table-updates/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Incorporating an Akismet Counter Into a WordPress Theme</title>
		<link>http://djs-consulting.com/linux/blog/2007/incorporating-an-akismet-counter-into-a-wordpress-theme</link>
		<comments>http://djs-consulting.com/linux/blog/2007/incorporating-an-akismet-counter-into-a-wordpress-theme#comments</comments>
		<pubDate>Mon, 06 Aug 2007 16:14:57 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.djs-consulting.com/linux/blog/2007/incorporating-an-akismet-counter-into-a-wordpress-theme</guid>
		<description><![CDATA[Akismet is, by far, the most popular anti-spam plug-in for WordPress.  (It comes bundled with the download, so that gets it market share.  But, it&#8217;s also very, very good.)  It comes with a counter that can be put into a WordPress theme.  It&#8217;s attractive, but its light blue color may not [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://akismet.com" title="Akismet">Akismet</a> is, by far, the most popular anti-spam plug-in for WordPress.  (It comes bundled with the download, so that gets it market share.  But, it&#8217;s also very, very good.)  It comes with a counter that can be put into a WordPress theme.  It&#8217;s attractive, but its light blue color may not integrate well into a given theme.</p>
<p>I went digging around in the source code, and found the line that actually pulls the count from the database.  Using this parameter, I was able to integrate a spam count into the sidebar that has a look consistent with the rest of the site.</p>
<p>Here&#8217;s the code that&#8217;s in use on the theme on this site.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php php" style="font-family:monospace;">&lt;li id=&quot;spamstats&quot;&gt;
	&lt;h2&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> _e<span style="color: #009900;">&#40;</span><span style="">'Akismet-Eaten Spam:'</span><span style="color: #009900;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/h2&gt;
	&lt;ul&gt;
		&lt;li&gt;&lt;a href=&quot;http://akismet.com&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span>
			<span style="color: #990000;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">number_format</span><span style="color: #009900;">&#40;</span>get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;akismet_spam_count&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>
			and counting...&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;
&lt;/li&gt;</pre></td></tr></table></div>

<p>Of course, line 5 is the important one &#8211; that&#8217;s how to get the number, formatted for whatever locale the server is set up for.  (On my <a href="http://daniel.summershome.org" title="Daniel J. Summers">personal blog</a>, the number is up over 1,400!)</p>
]]></content:encoded>
			<wfw:commentRss>http://djs-consulting.com/linux/blog/2007/incorporating-an-akismet-counter-into-a-wordpress-theme/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Posting Source Code in WordPress, Take 2</title>
		<link>http://djs-consulting.com/linux/blog/2007/posting-source-code-in-wordpress-take-2</link>
		<comments>http://djs-consulting.com/linux/blog/2007/posting-source-code-in-wordpress-take-2#comments</comments>
		<pubDate>Wed, 13 Jun 2007 17:33:51 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.djs-consulting.com/linux/blog/2007/posting-source-code-in-wordpress-take-2</guid>
		<description><![CDATA[In my searching, I have found another WordPress source code plugin, called wp-syntax.  This one uses GeSHi, the Generic Syntax Highlighter.  It features many languages, and is extensible to even more.  (If I ever post a COBOL snippet, I&#8217;ll probably add COBOL support to it, and contribute it to the codebase.)
To use [...]]]></description>
			<content:encoded><![CDATA[<p>In my searching, I have found another WordPress source code plugin, called <a href="http://wordpress.org/extend/plugins/wp-syntax/" title="WP Syntax Plugin">wp-syntax</a>.  This one uses <a href="http://qbnz.com/highlighter/" title="GeSHi">GeSHi</a>, the Generic Syntax Highlighter.  It features many languages, and is extensible to even more.  (If I ever post a COBOL snippet, I&#8217;ll probably add COBOL support to it, and contribute it to the codebase.)</p>
<p>To use it, you simply put a pre tag, followed by &#8220;lang=[language]&#8220;.  It will also do line numbering.  Of course, with a single &#8220;language&#8221; parameter, the embedded language will not be highlighted as well.  In this case, the previous plug-in works better; although the syntax highlighting has to be done manually, it can handle multiple languages.</p>
<p>Here is what the example from the &#8220;Category DropDowns in WordPress&#8221; post looks like with <code class="inline" style="padding:0;">lang="php"</code>&hellip;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #0000ff; font-style: italic;">/**
* This creates a list of category links that can be used with a category dropdown
*/</span>
<span style="color: #000088;">$aCategories</span> <span style="color: #339933;">=</span> get_all_category_ids<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$iMaxCat</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0</span>;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aCategories</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$iThisCat</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iMaxCat</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$iThisCat</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$iMaxCat</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$iThisCat</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$iMaxCat</span>++;
<span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;div style=&quot;text-align:center;&quot;&gt;
    &lt;form name=&quot;categoryform&quot; action=&quot;&quot; style=&quot;text-align:center;&quot;&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
        var aLink = new Array(<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iMaxCat</span><span style="color: #009900;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>);
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aCategories</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$iThisCat</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;aLink[$iThisCat] = <span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">.</span> get_category_link<span style="color: #339933;">&lt;/</span>span<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iThisCat</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
        function goCat() {
            window.location =
                aLink[document.getElementById('cat')[document.getElementById('cat').selectedIndex].value];
        }
    &lt;/script&gt;
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> wp_dropdown_categories<span style="color: #009900;">&#40;</span><span style="">'class=sidebardropdown&amp;orderby=name&amp;show_count=1&amp;hierarchical=1'</span><span style="color: #009900;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;br /&gt;
    &lt;button class=&quot;sidebarbutton&quot; type=&quot;button&quot; style=&quot;margin-top:5px;&quot; onclick=&quot;goCat();&quot;&gt;View Category&lt;/button&gt;
    &lt;/form&gt;
&lt;/div&gt;</pre></td></tr></table></div>

<p>And, here&#8217;s what it looks like with <code class="inline" style="padding:0;">lang="javascript"</code>&hellip;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;">&lt;?php
/**
* This creates a list of category links that can be used with a category dropdown
*/
$aCategories = get_all_category_ids();
$iMaxCat = 0;
foreach($aCategories as $iThisCat) {
    if ($iMaxCat &lt; $iThisCat) {
        $iMaxCat = $iThisCat;
    }
}
$iMaxCat++;
?&gt;&lt;div style=&quot;text-align:center;&quot;&gt;
    &lt;form name=&quot;categoryform&quot; action=&quot;&quot; style=&quot;text-align:center;&quot;&gt;
    <span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
        <span style="color: #003366; font-weight: bold;">var</span> aLink <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;?</span>php echo<span style="color: #009900;">&#40;</span>$iMaxCat<span style="color: #009900;">&#41;</span>; <span style="color: #339933;">?&gt;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #339933;">&lt;?</span>php
foreach<span style="color: #009900;">&#40;</span>$aCategories <span style="color: #000066; font-weight: bold;">as</span> $iThisCat<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    echo<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;aLink[$iThisCat] = <span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> . <span style="color: #660066;">get_category_link</span><span style="color: #339933;">&lt;/</span>span<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>$iThisCat<span style="color: #009900;">&#41;</span> . <span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span> <span style="color: #339933;">?&gt;</span>
        <span style="color: #003366; font-weight: bold;">function</span> goCat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            window.<span style="color: #660066;">location</span> <span style="color: #339933;">=</span>
                aLink<span style="color: #009900;">&#91;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cat'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cat'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">selectedIndex</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#93;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
    &lt;?php wp_dropdown_categories('class=sidebardropdown&amp;orderby=name&amp;show_count=1&amp;hierarchical=1'); ?&gt;
    &lt;br /&gt;
    &lt;button class=&quot;sidebarbutton&quot; type=&quot;button&quot; style=&quot;margin-top:5px;&quot; onclick=&quot;goCat();&quot;&gt;View Category&lt;/button&gt;
    &lt;/form&gt;
&lt;/div&gt;</pre></td></tr></table></div>

<p>This is another option, and is probably what I&#8217;ll use for single-language posts, or posts where the embedded language may not be crucial.</p>
]]></content:encoded>
			<wfw:commentRss>http://djs-consulting.com/linux/blog/2007/posting-source-code-in-wordpress-take-2/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Category Drop-Down in WordPress</title>
		<link>http://djs-consulting.com/linux/blog/2007/category-drop-down-in-wordpress</link>
		<comments>http://djs-consulting.com/linux/blog/2007/category-drop-down-in-wordpress#comments</comments>
		<pubDate>Thu, 24 May 2007 15:00:47 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.djs-consulting.com/linux/blog/2007/category-drop-down-in-wordpress</guid>
		<description><![CDATA[WordPress provides a template tag, wp_dropdown_categories, that inserts a drop-down list (the HTML &#60;select&#62; element) of categories, where the value of each item is the ID from the database.  This works fine if you are not using rewrite rules (AKA &#8220;pretty links&#8221;) &#8211; you can construct a URL using the value (?cat=[number]).  However, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://wordpress.org" title="WordPress">WordPress</a> provides a <a href="http://codex.wordpress.org/Template_Tags" title="Template Tags - WordPress Codex">template tag</a>, <a href="http://codex.wordpress.org/Template_Tags/wp_dropdown_categories" title="wp_dropdown_categories - WordPress Codex">wp_dropdown_categories</a>, that inserts a drop-down list (the HTML &lt;select&gt; element) of categories, where the value of each item is the ID from the database.  This works fine if you are not using rewrite rules (AKA &#8220;pretty links&#8221;) &#8211; you can construct a URL using the value (?cat=[number]).  However, if you use any sort of rewrite rules, this does not work.  I recently converted my <a href="http://www.djs-consulting.com/personal" title="Daniel J. Summers">personal site</a>, which uses the Pool theme, to utilize a JavaScript array to assist with displaying category pages.</p>
<p>Here&#8217;s the code&#8230;</p>
<pre>
<code><span class="embed">&lt;?php
<span class="com">/**
* This creates a list of category links that can be used with a category dropdown
*/</span>
$aCategories = <span class="func">get_all_category_ids</span>();
$iMaxCat = 0;
<span class="key">foreach</span>($aCategories <span class="key">as</span> $iThisCat) {
    <span class="key">if</span> ($iMaxCat &lt; $iThisCat) {
        $iMaxCat = $iThisCat;
    }
}
$iMaxCat++;
?&gt;</span>&lt;<span class="key">div</span> style=<span class="attr">"text-align:center;"</span>&gt;
    &lt;<span class="key">form</span> name=<span class="attr">"categoryform"</span> action=<span class="attr">""</span> style=<span class="attr">"text-align:center;"</span>&gt;
    &lt;<span class="key">script</span> type=<span class="attr">"text/javascript"</span>&gt;
        <span class="key">var</span> aLink = <span class="key">new Array</span>(<span class="embed" style="display:inline;">&lt;?php <span class="key">echo</span>($iMaxCat); ?&gt;</span>);
<span class="embed">&lt;?php
<span class="key">foreach</span>($aCategories <span class="key">as</span> $iThisCat) {
    <span class="func">echo</span>("aLink[$iThisCat] = "" . <span class="func">get_category_link</span>($iThisCat) . "";\n");
} ?&gt;</span>        <span class="key">function</span> <span class="func">goCat</span>() {
            window.location =
                aLink[document.<span class="func">getElementById</span>('cat')[document.<span class="func">getElementById</span>('cat').selectedIndex].value];
        }
    &lt;/<span class="key">script</span>&gt;
    <span class="embed inline">&lt;?php <span class="func">wp_dropdown_categories</span>('class=sidebardropdown&amp;orderby=name&amp;show_count=1&amp;hierarchical=1'); ?&gt;</span>
    &lt;<span class="key">br</span> /&gt;
    &lt;<span class="key">button</span> class=<span class="attr">"sidebarbutton"</span> type=<span class="attr">"button"</span> style=<span class="attr">"margin-top:5px;"</span> onclick=<span class="attr">"goCat();"</span>&gt;View Category&lt;/<span class="key">button</span>&gt;
    &lt;/<span class="key">form</span>&gt;
&lt;/<span class="key">div</span>&gt;</code></pre>
<p>This works for both &#8220;pretty&#8221; and standard links, as it uses the template tag <a href="http://codex.wordpress.org/Function_Reference/get_category_link" title="get_category_link - WordPress Codex">get_category_link</a> to specify the link.</p>
]]></content:encoded>
			<wfw:commentRss>http://djs-consulting.com/linux/blog/2007/category-drop-down-in-wordpress/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wow</title>
		<link>http://djs-consulting.com/linux/blog/2004/wow</link>
		<comments>http://djs-consulting.com/linux/blog/2004/wow#comments</comments>
		<pubDate>Sun, 01 Aug 2004 06:00:02 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[Internet Apps]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Red Hat / Fedora]]></category>
		<category><![CDATA[Wireless Support]]></category>

		<guid isPermaLink="false">http://www.djs-consulting.com/linux/blog/2007/wow</guid>
		<description><![CDATA[Today I installed Fedora Core 2.  This thing is slick!  WBEL looked a lot like RH8, which I had seen before my renewed Linux learning began.  FC2 has a graphical loader that hides a lot of the background stuff (unless an error occurs) &#8211; that&#8217;s cool.  During the install, I skipped [...]]]></description>
			<content:encoded><![CDATA[<p>Today I installed Fedora Core 2.  This thing is slick!  WBEL looked a lot like RH8, which I had seen before my renewed Linux learning began.  FC2 has a graphical loader that hides a lot of the background stuff (unless an error occurs) &#8211; that&#8217;s cool.  During the install, I skipped OpenOffice.org and MySQL, although I installed PHP with MySQL support.  The reason for that is that I wanted to get the latest and greatest versions of those two products.  We&#8217;ll see if this proves to be a good decision or not.</p>
<p>The wireless network card still wasn&#8217;t recognized (phooey).  I did some more searching, armed with the knowledge that I have an adm8211 chipset.  One of the first hits under Google&#8217;s Linux search for &#8220;adm8211&#8243; pointed me to a project called <a href="http://ndiswrapper.sourceforge.net/">NDISwrapper</a>.  This is a &#8220;wrapper&#8221; that uses the vendor&#8217;s Windows DLL file, and converts the hooks from Windows to Linux.  Doing this, this driver can (in theory) support most any network card, especially those that aren&#8217;t in the Linux Hardware Compatibility List (HCL).  I downloaded it, compiled it, and followed the directions to install my driver under it.  I still wasn&#8217;t able to create a connection, but on a hunch, I restarted the computer.  NDISwrapper is also a kernel module, and I know that often those are only read at startup.  Once the computer was restarted, I was able to create a connection, and now my network card works!  YEA!!!  (And it was only one night&#8217;s worth of work &#8211; much better.)</p>
<p>Now that I have networking working under both operating systems, I plan to try to get four things working the same, whether I&#8217;m booted to WXP or Linux &#8211; E-mail (using Mozilla Thunderbird), PHP (using Apache on Linux, IIS on WXP), MySQL (using the exact same version on both), and a web server that uses the same html root directory (again, Apache on Linux, IIS on WXP).  If I didn&#8217;t already have IIS up, running, and configured under WXP, I&#8217;d probably just do Apache on both, but this will be interesting &#8211; it should work, as I don&#8217;t have many creative permission rules.</p>
]]></content:encoded>
			<wfw:commentRss>http://djs-consulting.com/linux/blog/2004/wow/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Like a Banshee</title>
		<link>http://djs-consulting.com/linux/blog/2004/programming-like-a-banshee</link>
		<comments>http://djs-consulting.com/linux/blog/2004/programming-like-a-banshee#comments</comments>
		<pubDate>Sun, 27 Jun 2004 07:00:20 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.djs-consulting.com/linux/blog/2004/programming-like-a-banshee</guid>
		<description><![CDATA[Despite being ill early in the day (which caused me to miss Sunday School and church), the day on the computer went pretty well.  I made a lot more progress on TMTS, although I ran into a problem &#8211; it appears that the PHP that came with WBEL may not have MySQL support enabled. [...]]]></description>
			<content:encoded><![CDATA[<p>Despite being ill early in the day (which caused me to miss Sunday School and church), the day on the computer went pretty well.  I made a lot more progress on TMTS, although I ran into a problem &#8211; it appears that the <a href="http://www.php.net/">PHP</a> that came with WBEL may not have <a href="http://www.mysql.com/">MySQL</a> support enabled.  Rather than get sidelined with this right now, I&#8217;m going to continue converting pages on the application, and work this issue as part of unit testing.</p>
<p>I also managed to catch up on comp.lang.cobol and comp.sys.unisys, two newsgroups in which I participate.  I was able to play a file off a DVD (although I still can&#8217;t play the disc itself).</p>
]]></content:encoded>
			<wfw:commentRss>http://djs-consulting.com/linux/blog/2004/programming-like-a-banshee/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running Out of Browsers&#8230;</title>
		<link>http://djs-consulting.com/linux/blog/2004/running-out-of-browsers</link>
		<comments>http://djs-consulting.com/linux/blog/2004/running-out-of-browsers#comments</comments>
		<pubDate>Sat, 26 Jun 2004 07:00:57 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Internet Apps]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.djs-consulting.com/linux/blog/2004/running-out-of-browsers</guid>
		<description><![CDATA[When I moved the mouse to try to get the screen to unblank this morning, nothing happened.  It was locked up once again.  I decided to only run one process, to see if I could isolate which one was causing me problems.  I started with the F@H client.  I started it [...]]]></description>
			<content:encoded><![CDATA[<p>When I moved the mouse to try to get the screen to unblank this morning, nothing happened.  It was locked up once again.  I decided to only run one process, to see if I could isolate which one was causing me problems.  I started with the F@H client.  I started it before we left for breakfast, and when we got back, the computer was still running okay.  I started using the computer actively, and found another problem &#8211; my profile for Firefox now thinks it&#8217;s still in use, because I was using it when the machine crashed.</p>
<p>Now, anyone who has ever used Linux will know that one browser is much less severe than, for example, IE becoming unusable in a Windows evironment.  I switched to using Mozilla, and was doing some research on Linux crashes when the machine locked up again.  This time, when I restarted, both Firefox <em>and</em> Mozilla thought they were still in use.  I fired up the only other currently-installed browser, Konqueror, and surfed out to <a href="http://www.linuxquestions.org/">LinuxQuestions.org</a> and posted a message asking how to tell these browsers that they&#8217;re not in use.</p>
<p>A few hours later, an answer appeared.  For Firefox, the file is <nobr>~/.mozilla/firefox/default.lz7/lock</nobr>, and for Mozilla, the file is <nobr>~/.mozilla/default/{something}.slt/lock.</nobr>  Both these are symbolic links to a process PID &#8211; deleting them freed up the default profiles so these browsers could be used again.</p>
<p>In the mean time, I have not restarted F@H, but I&#8217;ve had Evolution running in the background without incident.  It seems that it may be the F@H client.  That bugs me, because I was really looking forward to using this machine to help with the project.  I may try to run the Windows version under <a href="http://www.winehq.com/">wine</a>, a Windows emulator for Linux.</p>
<p>Some folks have also expressed interest in the Tournament and Membership Tracking System (TMTS), which is a web application I coded to track membership and golf tournaments for a local golfing organization.  They&#8217;re interested in the PHP version, for which I no longer have the source code (it was on a laptop that was stolen).  So, much of my computing effort over the next few days will be trying to get this recreated.  Today, I was able to get the database rebuilt, and the first few pages converted.</p>
]]></content:encoded>
			<wfw:commentRss>http://djs-consulting.com/linux/blog/2004/running-out-of-browsers/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
