JTable/reorder: Difference between revisions
From Joomla! Documentation
m clean up |
m preparing for archive only |
||
| Line 4: | Line 4: | ||
===Syntax=== | ===Syntax=== | ||
| Line 89: | Line 89: | ||
===Examples=== | ===Examples=== | ||
=== Code Examples === | |||
<dpl> | <dpl> | ||
noresultsheader=\n | noresultsheader=\n | ||
category=reorder | category=reorder | ||
category=JTable | category=JTable | ||
namespace=CodeExample | |||
category=MethodExample | category=MethodExample | ||
include=* | include=* | ||
format= ,,, | format= ,,, | ||
</dpl> | </dpl> | ||
Latest revision as of 02:10, 25 March 2017
Description
Method to compact the ordering values of rows in a group of rows defined by an SQL WHERE clause.
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;
}
Examples
Code Examples