Change list order to show a specific item first

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

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

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: [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

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