Ordered List of Records

6 posts by 2 authors in: Forums > CMS Builder
Last Post: April 23, 2013   (RSS)

By Perchpole - April 22, 2013

This is a two-stage question. I'll get the main bit ot of the way first.

I have three nested categories:

Home
- News
-- Current

Each category editor has a textfield called "selectItems". In this field I want to input a numeric sequence, e.g:

4,5,6,1,2,3

The numbers correspond to records in a table called "items".

When a category page is displayed, I want to list each of these items in the order of the sequence.

That's part one...!

:0)

Perch

Hi Perch, 

I've done some research and you need to use the FIELD function in your order by string to order the items:

  // load records from 'blog'
  list($blogRecords, $blogMetaData) = getRecords(array(
    'tableName'   => 'items',
    'allowSearch' => false,
    'orderBy'     => "FIELD(num,4,5,6,1,2,3) DESC"
  ));

So the FIELD function orders records by the num field, and will cycle through each value in the array after it. 

You'd have to ensure that the items are entered in comma separated string, or you'll get MySQL errors. You could do this by limiting the values allowed in selectedITems field to numbers and commas.

Let me know if you have any questions

Cheers

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Perchpole - April 22, 2013

Hi, Greg -

Sounds interesting - but how could I apply this approach to different [category] pages with different number strings?

The idea is that each page would have it's own number string.

:0/

Perch

Hi Perch,

So each category record will have a detail page, and the Items will be in a different order on it?

If you had a getRecords function that was retrieving a specific category, you could use its selectItems field to order the data in the items section like this:

  // load record from 'category'
  list($categories, $categoryMetaData) = getRecords(array(
    'tableName'   => 'category',
    'where'       => whereRecordNumberInUrl(0),
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $category= @$categories[0]; // get first record
  if (!$category) { dieWith404("Record not found!"); } // show error message if no record found


 $itemOrder = $category['selectItems'];
  // load records from 'items'
  list($items, $itemsMetaData) = getRecords(array(
    'tableName'   => 'items',
    'loadUploads' => true,
    'allowSearch' => false,
    'orderBy'     => "FIELD(num,$itemOrder) DESC"
  ));

This is just example code, so you'll have to make a few changes to get it working with your site.

So a category is being loaded from a category section and the record is being set against the $category variable. Then the selectItems field variable is set to the $itemOrder variable, and this is used in the orderBy key in the getRecords for the items section. 

Cheers!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Perch, 

Here is a way you can get MySQL to do the work for you:

  $itemOrder = $category['selectItems'];

  // load records from 'items'
  list($items, $itemsMetaData) = getRecords(array(
    'tableName'   => 'items',
    'where'       => "num IN($itemOrder)",
    'loadUploads' => true,
    'allowSearch' => false,
    'orderBy'     => "FIELD(num,$itemOrder) DESC",
    'limit'    => $limit,

  ));

So the num in the MySQL field function is the num field in the items section, which is the key value that is automatically created when you add a record. It attempts to sort the returned items in the order of the values in it. You can read more about it here:

http://www.electrictoolbox.com/mysql-order-specific-field-values/

I've added a where statement which will only return values in the num field that are in the IN function.

Let me know if you have any questions.

Cheers

Greg

Greg Thomas







PHP Programmer - interactivetools.com