1

I want to list the states from the state/province field (handled by the Location module) in a content type. I used views, but it shows the same state name more than once.

I enabled distinct in the Views module, but it doesn't works. How can I avoid the same state name is shown more than once?

Current Result: Illinois, Georgia, Alabama, Illinois, Alabama, Alabama, Georgia.

I attached the screen shot of my view (I used table format and views group by module).

Expected Result : Illinois, Georgia, Alabama.

This is my views set up.

screenshot

This is what I'm getting from my view.

screenshot

I'm expecting this result no matter how many nodes have the same state; I want to display the state name only once.

screenshot

This is the table style I used, and views group by module.

screenshot

0

2 Answers 2

1

If Views' distinct parameter doesn't work for your case, you can always use a views_pre_render() hook to do anything with the result before it is rendered. Something along the lines of...

YOURMODULE_views_pre_render(&$view) {

  if ($view->name=='NAMEOFVIEW') {

    // drupal_set_message('<pre>' . print_r($view->result, TRUE) . '</pre>');

    $states_found=array();

    foreach($view->result as $delta => $row) {

      if (in_array($row->LOCATION_FIELD_NAME_VALUE, $states_found)) {
        unset($view->result[$delta]);
      } else {
        $states_found[]=$row->LOCATION_FIELD_NAME_VALUE;
      }

    }

  }

  // any other pre_render()ing here...

}

What this does is loop through the rows of your view result keeping track of what states have been found and unsetting any duplicates. The commented out dsm at the beginning will help you figure out what LOCATION_FIELD_NAME_VALUE you need to use in your specific case.

4
  • @Jimajamma.I used this in my views.module but the $views doesn't retrive any values and its not working.Anyother solutions?
    – Ranjani
    Aug 17, 2012 at 9:25
  • 3
    This code is meant to go in a module of your own creation. Just replace "YOURMODULE" with (for example) "statereducer", save the file as "statereducer.module", make a file called "statereducer.info" and put them both in a folder called "statereducer" in your modules folder. Copy another modules .info file, replacing where needed. Go to admin/build/modules and enable it.
    – griz
    Aug 17, 2012 at 10:04
  • 1
    Also, uncomment line 5 and change "NAMEOFVIEW" to the machine name of your view and "LOCATION_FIELD_NAME_VALUE" to that of the field as it appears in the output of drupal_set_message().
    – griz
    Aug 17, 2012 at 10:05
  • @Jimajamma & griz. Hi,I apologize to both of you.I used your code but it doesn't give me the result exactly what I expected.I repeats the state names because it gets the state name for each and every node.If I might not understood and followed your instructions correctly I apologize for that.Any way thanks for your post and support!.
    – Ranjani
    Aug 20, 2012 at 14:20
0

I used the following code to get the state names(major condition is no matter how many nodes have the same location but it should retrive the state name only once)

<?php 
$var = db_query("SELECT DISTINCT province FROM  location");
          while($variable = db_fetch_array($var)){

            print $variable['province'].'<br/>';

          }

?>

Just used the select query in my page.tpl it displays the statename only once even one or more than one node has chosen the same state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.