Everything PHP: MySQL ORDER BY

Published on Sep 5, 2009   //  Development

Everything PHP

When you perform a select query in MySQL, by default the result will be ordered by the primary key, in ascending order. This may not be ideal for some situations, as you may want to output or process the data in a certain order. You could achieve this in PHP, but MySQL offers a better way to have your query returns sorted.

Using an ORDER BY clause at the end of your query will allow you to specify the fields to order the results by, and how you would like those sorted (ascending [default] or descending). MySQL will then order the result appropriately based on your clause and sort based on your clause and the type of field in question. For example, a field containing numbers will sort 1-x (where x is basically infinite, higher than 1 at the least) if ascending and x-1 is descending. Let’s have a look at an example of this:

[sql]SELECT * FROM [table] ORDER BY [field][/sql]

This query will then return the results order by [field] in ascending order. To specify ascending specially, append ASC to the query, for descending, using DESC.

It is also possible to “sub-order” the queries. By specifying more than one field (comma separated), the results will first by order by the first query, then those results “sub-ordered” by the second query and so on. For example, if you’re storing country and city fields, you can order by country, then have those rows with the same country ordered by the city. You may even specify independent sorting values.

[sql]SELECT * FROM registrants ORDER BY country ASC, city ASC, age DESC[/sql]

There you have it, multiple ways to order and sort your data without using PHP to do so.

Web Development: CSS Floats

Published on Sep 2, 2009   //  Development

WordPress Development

In a previous article, we briefly went over CSS Floats. However, they deserve a more in-depth look, as they can be useful (and sometimes tricky).

CSS Floats allow you to position an object to the left or right, and have another object directly opposite (horizontally, speaking) it (on the same imaginary “top line”). We can use the floats using the float property in CSS. Let’s have a look at some example markup to apply this property to.

[html]<div id="left"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ac dui erat. Vivamus dui neque, laoreet volutpat tempor vel, facilisis eget tortor. Proin imperdiet mi non enim consequat tristique.</p></div>

<div id="right"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ac dui erat. Vivamus dui neque, laoreet volutpat tempor vel, facilisis eget tortor. Proin imperdiet mi non enim consequat tristique.</p></div>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ac dui erat. Vivamus dui neque, laoreet volutpat tempor vel, facilisis eget tortor. Proin imperdiet mi non enim consequat tristique.</p>[/html]

Now, say we want to position the first div to the left, the second to the right and the third underneath. We would do something like this (you’d have to play with the widths of the floated divs):

[css]#left {
float: left;
}
#right {
float: right;
}[/css]

Which is great, #left will be on the left and #right will be on the right. However, you may encounter a problem with the content thats below these float divs. Content below the floated objects may wrap onto the floated object’s line, creating an unsightly result.

To resolve this we will add the following markup after our floated divs:

[html]<div class="clear"></div>[/html]

Then, in our CSS:

[css].clear {
clear: both;
}[/css]

CSS floats are a useful feature when coding designs and wrapping text around images (just use one float, and no “clear”). While they can sometimes be a bit tricky, it’s usually a simple fix and the pros outweigh the cons.

WordPress Developing: Following Development

Published on Aug 30, 2009   //  Development

WordPress Development

If you’re interested in the development of WordPress, you may be interested in following along during its development cycles. If you plan on contributing code to WordPress, this will help you stay more current and allow you to make more-relevant patches.

One of the more obvious ways to keep up with development is to run a copy of the in-development version (trunk) of WordPress. You can do this by doing an SVN checkout on http://svn.automattic.com/wordpress/trunk/ and setting WordPress up normally. After that, you can do an SVN update to merge any updates with your local copy.

Along with running the current development version, you may want to be notified whenever something is checked in to the WordPress repository. There are multiple methods of getting these notifications. You can receive them by email, an RSS feed or Twitter.

There are also multiple mailing lists for the discussion of the development of WordPress, all of which are listed here.

Everything PHP: Counting Rows

Published on Aug 27, 2009   //  Development

Everything PHP

At some point in time, you’ll need to know either how many rows were returned in your result resource or how many rows match your query. While these two may seem the same, they use different methods to get the result and how different use cases. As such, we’ll take a look at these separately.

Number of Rows Returned

In your code, you may want to check how many rows where returned to you from your query. Commonly, you’ll check to see if you have more than 0 (or not 0) rows to work with (otherwise you may encounter errors while trying to process non-existing rows). We will accomplish this by using the mysql_num_rows() function, which takes a resource as a parameter. You cannot give this function the result of mysql_fetch_assoc() (or similar), the original resource is required. The function will return the number of rows. Let’s have a look at an example of this function:

[php]$query = mysql_query("SELECT * FROM posts");
if (mysql_num_rows($query) > 0) {
// Do something with the data
}[/php]

Number of Matching Rows

This method allows you to retrieve the number of rows that match your query, without actually retrieving the data. This is preferable if you will not need this data, as you will not need to waste processing power to download the data. With this method, MySQL will do all the heavy lifting and give you the result as a single “row”. This method utilizes a select query, where you wrap the field in COUNT(). For example:

[sql]SELECT COUNT(*) FROM posts WHERE type=’page’[/sql]

On the PHP side, you will still get the standard resource returned to you. If you use mysql_fetch_assoc() on it, the array key of the number will be what you used in the query (in this case, the key would be COUNT(*)). Because that will look messy in your code ($array['COUNT(*)'] is a pain to type out), you may choose to use mysql_fetch_row(), which will return a numerical array to you (so you would use $array[0] instead to get the number of rows). Let’s have a look at an example:

[php]$query = mysql_query("SELECT COUNT(*) FROM posts WHERE type=’page’");
$array = mysql_fetch_row($query);
echo ‘Number of pages: ‘ . $array[0];[/php]

The Main Difference

The main difference between these two methods is the fact that with the former, you have all the data that you’re counting available to you. With the latter method, you don’t have the data available. If you just need the number of rows (without needing the data) use the second method, which will be much quicker and less resource-intensive.

Web Development: Lists

Published on Aug 26, 2009   //  Development
Off

Web Development

Lists are commonly used on the web, through the use of special HTML tags which allow you to semantically create them. You’ll recognize these in the form of a list with bullet points, letters or numbers. Using CSS, we can manipulate them to even remove the list markers or replace them with a custom image.

There are three tags that we will be talking about: ul, ol and li. The first two are used to define what type of list you are creating (unordered or ordered, respectively). The third tag is wrapped around each item on the list. All the items are contained in one of the first two tags.

Let’s look at an example of how to format a list:

[html]<ul>
<li>Point</li>
<li>Point</li>
<li>Point</li>
</ul>[/html]

Which will product a bulleted-list. To produce a numbered list, you can use the ol tag instead of ul.

Styling Lists

We can use the list-style-type and list-style-image properties in CSS to modify the appearance of lists. The former will allow you to choose the type of list markers (bullets, numbers, letters) to use in your list (see here for potential values and browser compatibility). The latter allows you to specify an image to use as a list marker (see here for the proper syntax).

WordPress Development: Threaded Comments API

Published on Aug 22, 2009   //  Development
Off

WordPress Development

Beginning with WordPress 2.7, a threaded comments API has been included. This API allows theme developers to add the functionality of “threads” (or “nesting”) to the comments sections of their theme. Threaded comments allow users to reply to another person’s comment, which will then appear beneath the original comment, creating a thread (think of it like a mini-forum). Having threaded comments on your blog can increase conversations, as they’ll be much easier to follow along with.

To implement threaded comments in your theme, you’ll need to modify your comments.php file. Instead of going over the exact code you will need to use for threaded comments (which would be unnecessarly lengthy), we’re going to talk about the key differences with the threaded comments code.

Along with the threaded comments functionality, came a couple of functions that made the threaded comments (as well as regular comments) easier to implement. The primary new function is wp_list_comments(). This function replaces the what would have been a loop to display the comments. This function will handle looping through the comments, ordering them and adding classes to them (so you can easily display them properly with CSS).

While we could go over exactly how to implement threaded comments here. Instead, it may be better if you do some research and implement it from what you’ve learned. You may just have to follow the trail to figure it out, but it should leave you feeling more self-sufficient.

To help you out, you may find the wp_list_comments() documentation, WordPress 2.7 theme migration article and the default theme’s comments.php to be useful.

Page 4 of 42« First...23456...102030...Last »