API16

JTable/reorder

From Joomla! Documentation

< API16:JTable

The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

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



Syntax[edit]

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

Returns[edit]

mixed Boolean true on success.

Defined in[edit]

libraries/joomla/database/table.php

Importing[edit]

jimport( 'joomla.database.table' );

Source Body[edit]

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;
}



Examples[edit]

Code Examples[edit]