I've posted this question here: http://drupal.org/node/258188 and haven't gotten a response. I'd rather pay a fast ransom ($50?) for a quick fix what I'd need to sort CCK fields. Here is the example:

if ($_GET['sort']== "title") {
  $view->sort[0]['position'] = 0;
  $view->sort[0]['field'] = 'node.title';
  $view->sort[0]['sortorder'] = $_GET['order'];
  $view->sort[0]['options'] = '';
  $view->sort[0]['tablename'] = '';
  $view->sort[0]['id'] = 'node.title';
}
else if ($_GET['sort']== "date") {
  $view->sort[0]['position'] = 0;
  $view->sort[0]['field'] = 'node.created';
  $view->sort[0]['sortorder'] = $_GET['order'];
  $view->sort[0]['options'] = '';
  $view->sort[0]['tablename'] = '';
  $view->sort[0]['id'] = 'node.created';
}
else if ($_GET['sort']== "price") {
  $view->sort[0]['position'] = 0;
  $view->sort[0]['field'] = 'field_price';
  $view->sort[0]['sortorder'] = $_GET['order'];
  $view->sort[0]['options'] = '';
  $view->sort[0]['tablename'] = '';
  $view->sort[0]['id'] = 'field_price';
}
return $args;

I am sure ppl have done this, you see the last clause in this if statement is a CCK field but this code will break. I need to know how to fill out the params "tablename", etc.

I'm looking for someone who's already done this and can give me a quick fix for a quick ransom and then I can move on bc I don't want to have to spend a lot of time trying to research this. Please message me if interested, thanks!

Jay

Comments

nevets’s picture

Here is the way I would solve this. I would start with the view you have and under sort criteria add a sort by 'price' criteria. Now export the view and look at the generated sort (repeat as needed for other sort criteria)

drupalninja99’s picture

<?php
$view = views_get_view('property_search');

if ($_GET['sort']== "title") {
  $view->sort[0]['position'] = 0;
  $view->sort[0]['field'] = 'node.title';
  $view->sort[0]['sortorder'] = $_GET['order'] ? $_GET['order'] : 'ASC';
  $view->sort[0]['options'] = '';
  $view->sort[0]['tablename'] = '';
  $view->sort[0]['id'] = 'node.title';
}
elseif ($_GET['sort']== "date") {
  $view->sort[0]['position'] = 0;
  $view->sort[0]['field'] = 'node.created';
  $view->sort[0]['sortorder'] = $_GET['order'] ? $_GET['order'] : 'ASC';
  $view->sort[0]['options'] = '';
  $view->sort[0]['tablename'] = '';
  $view->sort[0]['id'] = 'node.created';
}
elseif ($_GET['sort']== "price") {
  $view->sort[0]['position'] = 0;
  $view->sort[0]['field'] = '';
  $view->sort[0]['sortorder'] = $_GET['order'] ? $_GET['order'] : 'ASC';
  $view->sort[0]['options'] = '';
  $view->sort[0]['tablename'] = 'node_data_field_price';
  $view->sort[0]['id'] = '';
}

print views_build_view('page', $view, array(0 => 3), false, false);
?>

And it's still not working. I copied exactly what the view was doing when I set the sort field in the edit view page. All I did was set it by the query string and yet it's not working? The listings show up fine they just don't sort no matter what I do. Help!

nevets’s picture

Where does this code exist (what is the context)? And the use of $_GET['sort'] suggests you have a form method set to GET while drupal forms use method=POST by default so how are you producing the form?

drupalninja99’s picture

I'm going to make a dropdown or something that will populate the query string. That's not the issue. The crazy view sorting that is so difficult is my problem =)

BengkelWeb’s picture

pls try this, hope it work :

if ($_GET['sort']== "title") {
$view->sort[0]['position'] = 0;
$view->sort[0]['field'] = 'node.title';
$view->sort[0]['sortorder'] = $_GET['order'];
$view->sort[0]['options'] = '';
$view->sort[0]['tablename'] = '';
$view->sort[0]['id'] = 'node.title';
}
else if ($_GET['sort']== "date") {
$view->sort[0]['position'] = 0;
$view->sort[0]['field'] = 'node.created';
$view->sort[0]['sortorder'] = $_GET['order'];
$view->sort[0]['options'] = '';
$view->sort[0]['tablename'] = '';
$view->sort[0]['id'] = 'node.created';
}
else if ($_GET['sort']== "price") {
$view->sort[0]['position'] = 0;
$view->sort[0]['field'] = 'node_data_field_price.field_price';
$view->sort[0]['sortorder'] = $_GET['order'];
$view->sort[0]['options'] = 0;
$view->sort[0]['tablename'] = 'node_data_field_price';
$view->sort[0]['id'] = 'node_data_field_price.field_price';
}
return $args;

Best Regards,
Dhara
YM, MSN, Skype : dhara_ok
Paypal : dhara_ok@yahoo.com
www.bengkel.web.id - total web solution

drupalninja99’s picture

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#039;LIMIT 0, 10&#039; at line 1 query: SELECT node.nid, field_price FROM node node WHERE (node.type IN (&#039;featured_property&#039;)) ORDER BY LIMIT 0, 10 in C:\xampp\htdocs\drupal5\web\includes\database.mysql.inc on line 172.

It's not tried to join the CCK table, driving me nuts!

drupalninja99’s picture

  $view = new stdClass();
  $view->name = 'property_search';
  $view->description = 'Property Search';
  $view->access = array (
);
  $view->view_args_php = 'if ($_GET[\'sort\']== "title") {
$view->sort[0][\'position\'] = 0;
$view->sort[0][\'field\'] = \'node.title\';
$view->sort[0][\'sortorder\'] = $_GET[\'order\'];
$view->sort[0][\'options\'] = \'\';
$view->sort[0][\'tablename\'] = \'\';
$view->sort[0][\'id\'] = \'node.title\';
}
else if ($_GET[\'sort\']== "date") {
$view->sort[0][\'position\'] = 0;
$view->sort[0][\'field\'] = \'node.created\';
$view->sort[0][\'sortorder\'] = $_GET[\'order\'];
$view->sort[0][\'options\'] = \'\';
$view->sort[0][\'tablename\'] = \'\';
$view->sort[0][\'id\'] = \'node.created\';
}
else if ($_GET[\'sort\']== "price") {
$view->sort[0][\'position\'] = 0;
$view->sort[0][\'field\'] = \'node_data_field_price.field_price\';
$view->sort[0][\'sortorder\'] = $_GET[\'order\'];
$view->sort[0][\'options\'] = 0;
$view->sort[0][\'tablename\'] = \'node_data_field_price\';
$view->sort[0][\'id\'] =\'node_data_field_price.field_price\';
}
return $args;';
  $view->page = TRUE;
  $view->page_title = 'Featured Properties';
  $view->page_header = '';
  $view->page_header_format = '1';
  $view->page_footer = '';
  $view->page_footer_format = '1';
  $view->page_empty = 'Sorry, no properties found.';
  $view->page_empty_format = '1';
  $view->page_type = 'teaser';
  $view->url = 'properties';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '10';
  $view->sort = array (
  );
  $view->argument = array (
    array (
      'type' => 'content: field_price',
      'argdefault' => '2',
      'title' => '',
      'options' => '',
      'wildcard' => '',
      'wildcard_substitution' => '',
    ),
  );
  $view->field = array (
    array (
      'tablename' => 'node',
      'field' => 'title',
      'label' => '',
      'handler' => 'views_handler_field_nodelink',
      'options' => 'link',
    ),
    array (
      'tablename' => 'node',
      'field' => 'body',
      'label' => '',
      'handler' => 'views_handler_field_body',
    ),
    array (
      'tablename' => 'node_data_field_listing_gallery',
      'field' => 'field_listing_gallery_fid',
      'label' => '',
      'handler' => 'content_views_field_handler_group',
      'options' => 'big_thumb',
    ),
    array (
      'tablename' => 'node_data_field_price',
      'field' => 'field_price_value',
      'label' => '',
      'handler' => 'content_views_field_handler_group',
      'options' => 'default',
    ),
  );
  $view->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => array (
  0 => 'featured_property',
),
    ),
  );
  $view->exposed_filter = array (
  );
  $view->requires = array(node, node_data_field_listing_gallery, node_data_field_price);
  $views[$view->name] = $view;

drupalninja99’s picture

I've tried the last day or so to create a php page and call the view, override the sort logic then display the view and that doesn't work either.

drupalninja99’s picture

I swear I did this yesterday and yet yesterday it wasn't working. It must have been just subtlety different. Anyways all I do is call the view and then override the sort. I added the 3 fields I'm sorting to the sort box in the edit view screen. Then I did a print_r on the $view object to see what it was generating. I grabbed that and then made it configurable by the query string and threw all that in a php page. I ran some tests and finally it looks like it's working. I tried all 3 fields and asc and desc order. I guess I will just make this a page and drop a javascript dropdown that will let you sort by maybe 4-5 different fields and be done with it. Hallelujah!

<?php

//load the view by name
$view = views_get_view('property_search');

if ($_GET['sort'] == 'price') {
$view->sort = array( array
                (
                    'vid' => 3,
                    'position' => 0,
                    'field' => 'node_data_field_price.field_price_value',
                    'sortorder' => $_GET['order'] ? $_GET['order'] : 'ASC',
                    'options' => '',
                    'tablename' => '',
                    'id' => 'node_data_field_price.field_price_value'
                ));
}
elseif ($_GET['sort'] == 'title') {
$view->sort = array(array(
                    'vid' => 3,
                    'position' => 0,
                    'field' => 'node.title',
                    'sortorder' => $_GET['order'] ? $_GET['order'] : 'ASC',
                    'options' => '',
                    'tablename' => '',
                    'id' => 'node.title'
));
}
elseif ($_GET['sort'] == 'date'){

$view->sort = array(array(
                    'vid' => 3,
                    'position' => 1,
                    'field' => 'node_data_field_price.field_price_value',
                    'sortorder' => $_GET['order'] ? $_GET['order'] : 'ASC',
                    'options' => '',
                    'tablename' => '',
                    'id' => 'node_data_field_price.field_price_value'
));
}

//output the view
print views_build_view('embed', $view);
doublejosh’s picture

I realize that you are embedding the view. But can't you just put this in the argument handling and use arg(n)?