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: [greg] Change list order to show a specific item first

By zaba - September 14, 2012

Thanks Greg,
It kind of worked...

I had to change
mysql_escape to mysql_real_escape_string for it to work and also make sure it came after the call to the database, thanks for the shorter more secure code though, i'm just an entry level php'r.

Also, when the results page is linked to with the variables passed to it (sector and num) the page only displays the row that has been passed to it (and not all the others which it should).

When the page is accessed without any variables passed to it it lists all projects in that sector (which is correct).

However, this is not to bad as I have added a link when it displays the selected one from the previous page to show all projects in this sector, which is kind of better way to do it from a usability point of view.

Just a little intrigued why it did not work though.

Thanks for your help on this Greg, it has certainly moved the project on for me. This level of support is outstanding.

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