Actions

API16

JTable/reorder

From Joomla! Documentation

< API16:JTable
Revision as of 17:41, 22 March 2010 by Doxiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Method to compact the ordering values of rows in a group of rows defined by an SQL WHERE clause.

[Edit Descripton]

Description:JTable/reorder

Syntax

reorder($where= '')
Parameter Name Default Value Description
$where WHERE clause to use for limiting the selection of rows to compact the ordering values.

Returns

mixed Boolean true on success.

Defined in

libraries/joomla/database/table.php

Importing

jimport( 'joomla.database.table' );

Source Body

public function reorder($where = '')
{
        // If there is no ordering field set an error and return false.
        if (!property_exists($this, 'ordering')) {
                $this->setError(get_class($this).' does not support ordering');
                return false;
        }
 
        // Initialise variables.
        $k = $this->_tbl_key;
 
        // Setup the extra where and ordering clause data.
        $where = ($where) ? ' AND '.$where : '';
        $ordering = ($this->_tbl == '#__content_frontpage') ? ', `content_id` DESC' : '';
 
        // Get the primary keys and ordering values for the selection.
        $this->_db->setQuery(
                'SELECT `'.$this->_tbl_key.'`, `ordering`' .
                ' FROM `'.$this->_tbl.'`' .
                ' WHERE `ordering` >= 0' .
                $where .
                ' ORDER BY `ordering`'. $ordering
        );
        $rows = $this->_db->loadObjectList();
 
        // Check for a database error.
        if ($this->_db->getErrorNum()) {
                $this->setError($this->_db->getErrorMsg());
                return false;
        }
 
        // Compact the ordering values.
        for ($i=0, $n=count($rows); $i < $n; $i++) {
                // Make sure the ordering is a positive integer.
                if ($rows[$i]->ordering >= 0) {
                        // Only update rows that are necessary.
                        if ($rows[$i]->ordering != $i+1) {
                                // Update the row ordering field.
                                $this->_db->setQuery(
                                        'UPDATE `'.$this->_tbl.'`' .
                                        ' SET `ordering` = '.($i+1) .
                                        ' WHERE `'.$this->_tbl_key.'` = '.$this->_db->quote($rows[$i]->$k)
                                );
                                $this->_db->query();
 
                                // Check for a database error.
                                if ($this->_db->getErrorNum()) {
                                        $this->setError($this->_db->getErrorMsg());
                                        return false;
                                }
                        }
                }
        }
 
        return true;
}

[Edit See Also] SeeAlso:JTable/reorder

Examples

<CodeExamplesForm />