Change list order to show a specific item first

4 posts by 2 authors in: Forums > CMS Builder
Last Post: September 14, 2012   (RSS)

By zaba - September 13, 2012

Hi, I am passing a variable in a url
listsomething.php?sector=<?php echo $record['sector'] ?>&num=<?php echo $record['num'] ?>
on the listsomething.php page I want to list all the results (usually about 5-6) but I want to display the record with variable $num passed to the page at the top of the list. The order of the others can remain as set in the editor.

so on the listsomething.php page I currently have to pick up the variables but i dont know how to change the order or if indeed it is possible.

<?php
#Pick up the variables
if (isset($_GET['sector'])) {
$sectorfromurl = $_GET['sector'];
}
else {
$sectorfromurl = 'Commercial';
}
if (isset($_GET['num'])) {
$numfromurl = $_GET['num'];
}
else {
}
?>


#this is as far as i have got

list($projectsRecords, $projectsMetaData) = getRecords(array(
'tableName' => 'projects',
'where' => "sector='$sectorfromurl'",

));

Re: [zaba] Change list order to show a specific item first

By gregThomas - September 13, 2012

Hi,

It is possible to do this. The code below should show you roughly what I think you need to do:

$sectorfromurl = (isset($_GET['sector']))? mysql_escape($_GET['sector']) : 'Commercial';


//Set $numfromurl from url to zero if there isn't a value for num in the array.

$numfromurl = (isset($_GET['num']))? intval($_GET['num']) : '0';


list($listingRecords, $listingDetails) = getRecords(array(
'where' => "sector='$sectorfromurl'",
'tableName' => 'listings'
));

//First we cycle through the records array to check if there is a related $numfromurl and display that if required.


foreach($listingRecords as $row){
if($row['num'] == $numfromurl){
echo $row['num'];
}
}

//Now we cycle through the records and display any records that arn't $numfromurl.

foreach($listingRecords as $row){
if($row['num'] != $numfromurl){
echo $row['num'];
}
}


Thanks
Greg Thomas







PHP Programmer - interactivetools.com

Re: [zaba] Change list order to show a specific item first

By gregThomas - September 14, 2012

I think the problem is related to the getRecords function I gave you. It needs the following added to it:
list($listingRecords, $listingDetails) = getRecords(array(
'where' => "sector='$sectorfromurl'",
'tableName' => 'listings',
'allowSearch' => false,
));


This should allow the original code to work. The allowSearch variable is automatically set to true in a getRecords function and I think it was seeing that there was a num variable in the URL and then only looking for that record in the table.

Glad you've found a work around, let me know if you have any more problems!

Thanks!
Greg Thomas







PHP Programmer - interactivetools.com