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

nevets’s picture

Well if this node based content use the views module, add filters for the selected fields and expose them.

joshua.howell’s picture

I had a look at using views but there doesn't seem to be any options for filtering on custom fields, only the defaults.

naveenpl’s picture

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.

joshua.howell’s picture

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.

joshua.howell’s picture

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.

<?php

	$output = "";
	if ($sort == ""){
		$sort = t('L.nid desc');
	};
	$SQL = "SELECT L.nid
			FROM {rlisting_listing} L
			WHERE (L.building_type_id = 1 OR L.building_type_id = 3 OR L.building_type_id = 4)
					AND L.listing_type_id = 2
					AND (L.listing_status = ".AVAILABLE_LISTING." OR L.listing_status = ".AVAILABLE_SOON.")
					ORDER BY $sort
	";
	$result = pager_query($SQL,40,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 = drupal_get_form('sortby_dropdown_form', $form);
	$output .= $nodes;
	$output .= theme('pager',NULL,40,0);

	return $output;

function sortby_dropdown_form() {
$formname="sortby";
$sortby_array = array (
	t('L.nid desc')	=> t('newest'),
	t('L.nid ')	=> t('oldest'),
);
 $form['sortby'] = array(
      '#type' => 'select',
      '#name' => $formname,
      '#id' => $formname,
      '#title' => '',
      '#default_value' => '',
      '#options' => $sortby_array,
      '#description' => '',
      '#multiple' => $multiple = FALSE,
      '#required' => $required = FALSE,
      // '#attributes' => array('onChange' => $sort = $form['sortby']['#value'], then run the SQL select statement again and page the results???),
    );
        return $form;
}
?>

Basically all i want to do is allow visitors to sort the results using a combo box. Anyone have any ideas?

nevets’s picture

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.

joshua.howell’s picture

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?

nevets’s picture

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.

joshua.howell’s picture

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;

<?php

	// default sort value
	$sortby = "L.nid desc";

	$output = "";
	$SQL = "SELECT L.nid
			FROM {rlisting_listing} L
			WHERE (L.building_type_id = 1 OR L.building_type_id = 3 OR L.building_type_id = 4)
					AND L.listing_type_id = 2
					AND (L.listing_status = ".AVAILABLE_LISTING." OR L.listing_status = 

".AVAILABLE_SOON.")
					ORDER BY $sortby
	";
	$result = pager_query($SQL,40,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 = drupal_get_form('sortby_dropdown_form', $form);
	$output .= $nodes;
	$output .= theme('pager',NULL,40,0);

	return $output;


function sortby_dropdown_form() {

$formname="sortby";
$sortby_array = array (
	t('L.nid desc')	=> t('newest'),
	t('L.city_name ')	=> t('suburb'),
	t('L.price desc')	=> t('price'),
);
 $form['sortby'] = array(
      '#type' => 'select',
      '#name' => $formname,
      '#id' => $formname,
      '#title' => '',
      '#default_value' => '',
      '#options' => $sortby_array,
      '#description' => '',
      '#multiple' => $multiple = FALSE,
      '#required' => $required = FALSE,
	// upon changing the dropdown box, reorder the SQL results with the new ORDER BY
	'#attributes' => array('onChange' => sort_results()),
    );

        return $form;
}

function sort_results(){

//set sort by variable as the value chosen in the dropdown box
$sortby = $form['sortby']['#value'];

	$output = "";
	$SQL = "SELECT L.nid
			FROM {rlisting_listing} L
			WHERE (L.building_type_id = 1 OR L.building_type_id = 3 OR L.building_type_id = 4)
					AND L.listing_type_id = 2
					AND (L.listing_status = ".AVAILABLE_LISTING." OR L.listing_status = 

".AVAILABLE_SOON.")
					ORDER BY $sortby
	";
	$result = pager_query($SQL,40,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 = drupal_get_form('sortby_dropdown_form', $form);
	$output .= $nodes;
	$output .= theme('pager',NULL,40,0);

	return $output;
}

can anyone see what is wrong with this code? When i try to preview this as a PHP snippet it crashes apache (on xampp).

inders’s picture

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