Listing Records in a Specific Sequence
5 posts by 2 authors in: Forums > CMS Builder
Last Post: July 21, 2011 (RSS)
By Perchpole - July 20, 2011
I have a bit of a puzzler which I hope someone will be able to help me with!
I've generated a sequence of numbers (which can be either comma or tab delimited). For example:
5, 7, 3, 1
Each number corresponds to a $record['num'].
I want to use the sequence to set the order in which records are returned.
Using the example above the records would be returned as follows:
Record 5
Record 7
Record 3
Record 1
How can I do this?
:o/
Perch
Re: [Perchpole] Listing Records in a Specific Sequence
By Jason - July 20, 2011
What you can do is convert your sequence of numbers into an array. Then you can loop through your array one at a time, and select a single record.
For example, if you had a sequence like this:
$recordNumbers = "5,7,3,1";
First you can create your array like this:
$numberArray = explode(",", trim($recordNumbers, ","));
Then loop through like this:
<?php foreach ($numberArray as $num):?>
<?php
$query = "SELECT * FROM `{$TABLE_PREFIX}records` WHERE num = '".intval($num)."'";
$record = mysql_query_fetch_row_assoc($query);
?>
<?php endforeach?>
One thing to note is that this approach will not give you any of the uploads or the pseudo fields in record. If you need those, you can replace this:
<?php
$query = "SELECT * FROM `{$TABLE_PREFIX}records` WHERE num = '".intval($num)."'";
$record = mysql_query_fetch_row_assoc($query);
?>
With a call to getRecords()
Hope this helps get you started.
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Listing Records in a Specific Sequence
By Perchpole - July 20, 2011
This is great. I tried your first suggest - which worked but (as you said) had limited output.
I then tried the getRecords() method which was perfect.
Am I right in saying that you have to put an extra foreach() loop around the output to make it work?
Thanks again,
:0)
Perch
Re: [Perchpole] Listing Records in a Specific Sequence
By Jason - July 21, 2011
You could use a foreach loop, but since we are only ever returning 1 record, we could do this instead:
<?php foreach ($numberArray as $num):?>
<?php
list($Record, $RecordMetaData) = getRecords(array(
'tableName' => 'records',
'allowSearch' => false,
'where' => "num = '".intval($num)."'",
'limit' => 1,
));
if ($Record) {
$record = $Record[0];
}
else {
continue;
}
?>
<?php endforeach?>
So you can use the variable $record the same as if you were in a foreach loop.
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Listing Records in a Specific Sequence
By Perchpole - July 21, 2011
Nice one, Jason!
:0)
Perch