<?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>Printed Matters &#187; php</title>
	<atom:link href="http://burden.ca/blog/category/development/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://burden.ca/blog</link>
	<description>Newspapers, their websites, and their future</description>
	<lastBuildDate>Sun, 02 Oct 2011 21:28:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>array_multisort is your friend</title>
		<link>http://burden.ca/blog/2008/03/array_multisort-is-your-friend/</link>
		<comments>http://burden.ca/blog/2008/03/array_multisort-is-your-friend/#comments</comments>
		<pubDate>Fri, 28 Mar 2008 00:23:10 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://burden.ca/blog/?p=36</guid>
		<description><![CDATA[More and more these days I&#8217;ve been using Wordpress as the base for web projects. The code base is fantastic, elegant, and simple, and they care about web standards and proper XHTML output. Plugins and themes are easy to write, and the documentation is excellent.
So, for sites that lend themselves to a certain blogginess &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>More and more these days I&#8217;ve been using Wordpress as the base for web projects. The code base is fantastic, elegant, and simple, and they care about web standards and proper XHTML output. Plugins and themes are easy to write, and the documentation is excellent.</p>
<p>So, for sites that lend themselves to a certain blogginess &#8211; sites that are mainly content, updated regularly, and where the content falls within a few different categories &#8211; it&#8217;s a good fit. You can update a core code base from the maintainers, while your plugins and themes for that site remain in separate folders and that&#8217;s the only stuff you have to maintain.</p>
<p>But, sometimes you need to do non-bloggy stuff. For example, on a site for media lawyers I&#8217;m working on, we want a list of all the member lawyers. These lawyers will be able to log on and post things in a contributor or author role, and so they all have accounts on the site. It seemed natural, then, to expand the information stored for each user and use the accounts to generate the member list.</p>
<p><span id="more-36"></span></p>
<p>The first thing I did was download or create a plugin which allowed me to store more information about each author than I could with the stock registration form. I found <a href="http://www.dealsway.net/2007/11/05/wp-user-manager/">WP User Manager</a>, so instead of writing my own I plugged that in.</p>
<p>Next, I created a page (not a post) called &#8220;Media Lawyers&#8221; and assigned to that a template of my own creation. I called mine &#8220;author-list.php&#8221; and stuck it in the folder for the theme I created especially for the site. This file needs to have these magic lines somewhere near the top:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #666666; font-style: italic;">/*
Template Name: Member List
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>That tells Wordpress that this is a template available for use with pages.</p>
<p>Now in this file you write the code for displaying the member list. Wordpress provides a handy function for this: <a href="http://codex.wordpress.org/Template_Tags/wp_list_authors">wp_list_authors()</a>. But there&#8217;s a rub. No, two rubs. The first is that it won&#8217;t list any authors who have not yet written anything. That would make it useless as a member directory, since not even half of them have written stuff for the site yet. The second problem is that we wanted the lawyers to sort by province. A custom function was required.</p>
<p>WP User Manager stores all extra information about authors in the wp_usermeta table, which consists of four columns: id, user id, meta_key, and meta_ value. That&#8217;s logical, because that way the info gets pulled into the author queries that the Wordpress core does, and also your work won&#8217;t be changed or erased the next time you update Wordpress.</p>
<p>Each user might have any number of entries in that table, like <code>131, 5, compname, My Big Lawyer Firm</code>. So you know right away we&#8217;re going to be in to a loop of database calls and some array manipulation. Messy, but no way around it. So let&#8217;s get started:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM <span style="color: #006699; font-weight: bold;">{$wpdb-&gt;users}</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</p>
<p>That&#8217;s going to pull all the user data out of the main user table, but there&#8217;s no good way to join to the user meta table and get the associated data for each user. So we just have to do another table query as we loop through that list.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ID'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * 
	FROM <span style="color: #006699; font-weight: bold;">{$wpdb-&gt;usermeta}</span> 
	WHERE <span style="color: #006699; font-weight: bold;">{$wpdb-&gt;usermeta}</span>.user_id = <span style="color: #006699; font-weight: bold;">{$user['ID']}</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$res2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$datum</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$users</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ID'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$datum</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'meta_key'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> 
						<span style="color: #339933;">=</span> <span style="color: #000088;">$datum</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'meta_value'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we have all our user data in one place, one entry per user in our <code>$users</code> array.</p>
<p>But wait, how do we sort it? We want the lawyers to sort first by province, then by last name. <code>array_multisort</code> to the rescue. But first, because we have an array of rows, and <code>array_multisort</code> needs an array of columns, we have to create a couple of arrays for <code>array_multisort</code> to sort against. Here comes another loop:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$users</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$prov</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'wpum_compprov'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$last_name</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'last_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And now we&#8217;re ready for the magic of multisorting:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">array_multisort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$prov</span><span style="color: #339933;">,</span> SORT_ASC<span style="color: #339933;">,</span> <span style="color: #000088;">$last_name</span><span style="color: #339933;">,</span> SORT_ASC<span style="color: #339933;">,</span> <span style="color: #000088;">$users</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And bingo, I&#8217;ve got a nice sorted array of users with all their data. Now I can loop through that and display it any way I want.</p>
]]></content:encoded>
			<wfw:commentRss>http://burden.ca/blog/2008/03/array_multisort-is-your-friend/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

