By joshua.howell on
I'm using a PHP snippet inside a page node to display a list of all the corresponding database records but what i would like to do is to add a combo box to the page that would change the SQL ORDER BY value so that the records could be order by price, recency, etc. when the page is first displayed if would sort by the default value, but if the user chose a different option then it would reorder them according to the value chosen. I'm really not sure where to start with this so any help would be great.
<?php
$output = "";
$SQL = "SELECT L.nid
FROM {rlisting_listing} L
WHERE (L.building_type_id = 1 OR building_type_id = 2 OR building_type_id = 3 OR building_type_id = 4 OR building_type_id = 5 OR building_type_id = 6)
AND L.listing_type_id = 1
AND (L.listing_status = ".AVAILABLE_LISTING." OR L.listing_status = ".AVAILABLE_SOON.")
ORDER BY L.nid desc
";
$result = pager_query($SQL,10,0);
if (db_num_rows($result) > 0){
while ($record = db_fetch_object($result)){
$nodes .= node_view(node_load($record->nid),true);
};
}else{
$output = "There were no results found";
};
$output .= theme('pager',NULL,10,0)."<br />";
$output .= $nodes;
$output .= theme('pager',NULL,10,0);
return $output;
?>
Comments
Well if this node based content
Well if this node based content use the views module, add filters for the selected fields and expose them.
I had a look at using views
I had a look at using views but there doesn't seem to be any options for filtering on custom fields, only the defaults.
If you are developing your
If you are developing your onw module then,
This is a example i have given.
http://drupal.org/node/227103
for you in place of this code, you need do give some thing like this.
array('data' => t('username'), 'field' => 'username'),
array('data' => t('First Name'), 'field' => 'firstname', 'sort' => 'desc'),
array('data' => t('Operations'))
// header with 'field' declared will have the option to be sorted.
Hope this will help.
Cheers.
i'm not actually developing
i'm not actually developing a module, i was just trying to figure out how to sort the output from the rListing module. I'm still new to Drupal, PHP and mySQL so i still don't really know how the code in the modules links to the actual pages that are generated but i can better understand the PHP snippets. All i wanted to do was add a combobox which would sort the node teasers.
Would it be possible to do
Would it be possible to do this sort of thing with views + cck? i don't know anything about cck so would it allow me to add custom fields to filter a view on? Or is there a way to add custom fields to views with another module?
I tried writing some code as a PHP snippet for what i want but i'm not sure how to get it to work.
Basically all i want to do is allow visitors to sort the results using a combo box. Anyone have any ideas?
I would personally use CCK
I would personally use CCK plus views to do this unless I had some overriding reason the content type needed to be implemented as a module.
I'm using the rListing
I'm using the rListing module (a real estate listings module) which creates the content type 'Listing' via the module. Would that stop CCK from working with the 'Listing' content type?
You should be able to add fields with CCK
You should be able to add fields with CCK to the 'Listing' content type (I am assuming it is node based and shows on the "Administer" > "Content management" > "Content types" page). Unless the rListing module makes it's fields available to a view though you will not be able to see those fields in any views.
I'm still trying to write
I'm still trying to write some code that will enable a user to order / sort a list of teaser nodes but i can't figure out how to get it to work. This is what i have so far;
can anyone see what is wrong with this code? When i try to preview this as a PHP snippet it crashes apache (on xampp).
You are doing in Wrong way..!
Hey..
[code]
// upon changing the dropdown box, reorder the SQL results with the new ORDER BY
'#attributes' => array('onChange' => sort_results()),
[/code]
U can Call A PHP function using ajax.Not directly.If u want u can use xajax library for using ajax.
Moreever:-
[code]
function sort_results()
{
//set sort by variable as the value chosen in the dropdown box
$sortby = $form['sortby']['#value'];
[/code]
You cant access $form variable from here in this way..!! You need to focus on PHP variables basics.
---
Inder Singh
http://www.Indiapoly.com
Inder Singh
http://indersingh.com