[TYPO3-dev] Order result of query based on comma-seperated string

Sacha Vorbeck Vorbeck at moduleBox.com
Wed Oct 1 14:30:51 CEST 2008


Hi,

I have created a little extension with the two tables "authors" and 
"books". The author table contains a db-relation field "bookIDs" so that 
an editor can connect the author to several books.

I use the following query to read the book records belonging to an 
author. Beforehand the var $bookIDs was filled with  the comma-seperated 
string of book-uids entered by the author in the field "bookIDs".

$bookIDs = $row['bookIDs'];
$query = $GLOBALS['TYPO3_DB']->SELECTquery(
	'uid,title',
	'tx_myext_books',
	' uid IN (' . $bookIDs . ')'.$this->enableFields,
	'',
	'',
	''
);
$res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);

This works and delivers the selected books but it doesn`t take the sort 
order into account. So when the editor opens a record of the author 
table in the backend and changes the sort order of the values in the 
field bookIDs to:

3,1,4,2

The result array of the query will still be: 1,2,3,4

To make it work I could use PHP to sort the result-array according to 
the order of the comma-seperated uid-values in the $bookIDs-value. But 
I`m sure it is possible to solve this with a better query.

As this is a standard situation there even might be an API-method for 
it. Can someone give me a hint on how to create a query that takes the 
order of the uid-values into account and fills the result array accordingly?

-- 
thank you - all the best,
Sacha





More information about the TYPO3-dev mailing list