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

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.