Who’s up for a little challenge? Everyone is invited to join in, even if you’re a beginning php developer. We’re going to tackle a simple problem. The winner gets eternal Joomla stardom!

The Problem

Imagine a simple e-commerce system. The shop owner can manage products, and show them on the front page. The owner wants to able to give some products more prominence, by ordering the list. After a while, because of all the products being added, removed and reordered, the database table becomes messy.

The Challenge

This is the table:

CREATE TABLE `jos_products` (
`id` SERIAL,
`name` VARCHAR( 100 ) NOT NULL ,
`ordering` INT NOT NULL
);

The table might look a little like this:

+----+------------+----------+
| id | name       | ordering |
+----+------------+----------+
|  6 | Chocolate  |       25 |
|  8 | Beer       |        2 |
| 19 | Cookies    |       19 |
| 21 | Lemonade   |        8 |
+----+------------+----------+

We want the ‘ordering’ column to be sequential, like this:

+----+------------+----------+
| id | name       | ordering |
+----+------------+----------+
|  6 | Chocolate  |        4 |
|  8 | Beer       |        1 |
| 19 | Cookies    |        3 |
| 21 | Lemonade   |        2 |
+----+------------+----------+

Your job is to write a bit of code to clean up the mess.

The rules

To win, your code should be short, elegant, and performant, with as little queries as possible. Obviously, your code should use the Joomla framework. PHP 5.2 and MySQL 5 are assumed. All we need is the actual code to fix the table, not the complete component. So your submission should look like this:

function cleanup_ordering() {
$db = JFactory::getDBO();
/* your code here */
}

Please don’t go hunting the net for ready made solutions, that would kill the fun. The code should be your own work. Don’t post your submissions here, but mail them to mathias {AT} joomlatools {DOT} eu.

I will post my own solution when the contest is over. This will serve as the proof:

echo md5(file_get_contents('solution.php'));
// Hash of my solution: d5dc5903a8d57ea28af5c618ec92418e
  • http://torkiljohnsen.com Torkil Johnsen

    Using the Joomla framework would mean just using JTable->reorder in my opinion, since this function does exactly what you are describing.

    Do you want a rewrite of the reorder-function maybe?

  • http://torkiljohnsen.com Torkil Johnsen

    Using the Joomla framework would mean just using JTable->reorder in my opinion, since this function does exactly what you are describing.

    Do you want a rewrite of the reorder-function maybe?

  • http://mathiasverraes.com Mathias Verraes

    JTable::reorder() allows to reorder a specific set of rows, which is outside of scope for this challenge. It also has a query inside a loop, which is generally considered bad practice.

    For clarity, I’m not asking for something like this:
    function cleanup_ordering($table_name, $where).
    The only goal is to compact all rows in one specific table, namely jos_contents.
    You need to use the framework’s db object, not php’s mysql_* functions.

  • http://torkiljohnsen.com Torkil Johnsen

    I understand… And I might just be very very tired… but JTables reorder function seems to reorder all rows in a table, doesn’t it?

    If so, this challenge is alot like trying to rewrite that function, but then again; My 4 month old son has given me some sleep deprivation lately so please excuse any stupidities :)

  • http://torkiljohnsen.com Torkil Johnsen

    I understand… And I might just be very very tired… but JTables reorder function seems to reorder all rows in a table, doesn’t it?

    If so, this challenge is alot like trying to rewrite that function, but then again; My 4 month old son has given me some sleep deprivation lately so please excuse any stupidities :)

  • http://mathiasverraes.com Mathias Verraes

    @Richard Greset: The solution you posted is indeed the one. I removed your comment for now, so others can have a go. I’ll put it back in a week or so.

    @All: please _email_ your solution, as stated in the post, don’t post it here.

  • severdia

    The solution is also one that was recently added to Joomla 1.5.6 (I believe) to fix the same problem with menu item ordering, so the answer is there for those who look in the right place.

  • http://www.thomas-kahl.net Thomas

    Hi, when do you want to show the solutions?

    Thomas

  • http://mathiasverraes.com Mathias Verraes
  • http://mathiasverraes.com Mathias Verraes
  • Pingback: Solution for the Developers Challenge | Joomlatools Blog