7

I keep on having different requirements and I always get stuck in Views. This time the requirement is to combine the two columns/fields of a views table but display it in different rows.

The eg: fields I currently have: (Below is the added Picture)

enter image description here

The output of the View in Table (Nickname Multivalue separated by comma)

enter image description here

While Putting the Multiple value field in different rows

enter image description here

In the table settings I did combine two fields like

enter image description here

i.e. Name and Nick Name but that only gives result like:

(Here Nick Name is displayed after Name)

enter image description here

But the requirements I need to display is: (Name and Nick Name in Separate Row but at the same column). The Column needs to be sortable like normal Views Table Sort function.

enter image description here

Is there any settings in Views which lets me perform this? Or How can I accomplish this ?

Edit: If this is not possible and anybody could guide me to build custom query for this then I'd be very thankful.

Edit 2: SQL Query from Views Log is: (This query outputs the 2nd Image from above)

SELECT node.title AS node_title, node.nid AS nid, field_data_field_nick_names.delta AS field_data_field_nick_names_delta, field_data_field_nick_names.language AS field_data_field_nick_names_language, field_data_field_nick_names.bundle AS field_data_field_nick_names_bundle, field_data_field_nick_names.field_nick_names_value AS field_data_field_nick_names_field_nick_names_value, field_data_field_nick_names.field_nick_names_format AS field_data_field_nick_names_field_nick_names_format, node.created AS node_created, 'node' AS field_data_field_nick_names_node_entity_type, 'node' AS field_data_field_ages_node_entity_type, 'girls:page' AS view_name
FROM 
{node} node
LEFT JOIN {field_data_field_nick_names} field_data_field_nick_names ON node.nid = field_data_field_nick_names.entity_id AND (field_data_field_nick_names.entity_type = 'node' AND field_data_field_nick_names.deleted = '0')
WHERE (( (node.status = '1') AND (node.type IN  ('girlss')) ))
ORDER BY node_created DESC
8
  • Sorry for misunderstanding. My answer doesn't solve your problem. And I removed it.
    – milkovsky
    Commented Nov 5, 2013 at 16:57
  • At first try to think what SQL query will give you such result. I tied to think but I didn't create the appropriate query. If you could show me SQL query that you want I can help you to do it with views.
    – milkovsky
    Commented Nov 5, 2013 at 16:58
  • Thank you for your effort. I couldn't write the query here, I will edit the question.
    – user12947
    Commented Nov 5, 2013 at 17:06
  • are you sure that this query does exactly what you want?
    – milkovsky
    Commented Nov 5, 2013 at 17:26
  • 1
    @neok, don't feel shy, who doesn't like girls? sometimes off-topic is what developers need to get the mood going. cheers, its friday :)
    – user12947
    Commented Nov 8, 2013 at 13:47

6 Answers 6

4
+50

You've structured your data in such a way that this can't be done with a single SQL query. You'll always be asking the database for two separate things: (1) a list of people based on their real names, and (2) a list based on their nick names.

But it doesn't have to be this way.

The easiest way to accomplish Picture 4, by far, is to always represent a person's "Name" as an additional "Nick Name".

So "Daina" should have 4 Nick Names: "Kristi", "Rita", "Sweetie", and "Daina". Then, all you need to ask the database for is a list of nick names.

This probably isn't the answer you're looking for. But I would argue that it's a more correct way to store your data. Think of the "Nick Name" field not as a list of alternate names, but rather as a list of all names that the person is known as. Based on the requirements of your situation, that's what it sounds like you're really after, anyway. It's a more complete model for your name data.

1
  • I give you the bounty as your opinion is right from the architectural point of view, but still it doesn't satisfy my requirements.
    – user12947
    Commented Nov 11, 2013 at 21:32
1

It would be possible to achieve this with overriding the views template file: views-view-table.tpl.php.

First you need to put multiple values in different rows.

Then in template file you should check for each row if Name is the same with the previous row. If not print the name and the age and add a new row in the code which includes the nickname and the age. If the name is the same with the previous one, then print only the nickname and the age.

I tried to explain it theoretically, but I can help with the implementation if you have difficulties.

Steps:

  1. Override the views template: You need to copy the views-view-table.tpl.php file under your theme folder. This file is located under the Views module views/theme/views-view-table.tpl.php.
  2. Rename the template file so that it only applies to that specific view: views-view-table--YOUR-VIEW.tpl.php. You should place the machine name of your view separated with 2 dashes.
  3. You need to modify the following code block in this template file:

Code

<?php foreach ($rows as $row_count => $row): ?>
  <?php // dpm($row) // You can use dpm() function to print out the contents of $row array. ?>
  <tr <?php if ($row_classes[$row_count]) { print 'class="' . implode(' ', $row_classes[$row_count]) .'"';  } ?>>
    <?php foreach ($row as $field => $content): ?>
      <td <?php if ($field_classes[$field][$row_count]) { print 'class="'. $field_classes[$field][$row_count] . '" '; } ?><?php print drupal_attributes($field_attributes[$field][$row_count]); ?>>
        <?php print $content; ?>
      </td>
    <?php endforeach; ?>
  </tr>
<?php endforeach; ?>
4
  • Thank you for your answer. I am still newbie in Drupal and Views is my biggest headache till now. 2 days ago I bought the Views Cookbook and have been trying to grasp how it works properly but it doesn't explain anything in my case. So, a sample implementation would be very very helpful.
    – user12947
    Commented Nov 5, 2013 at 17:51
  • I've updated my answer. I recommend you to install Devel module. And use dpm() function to print out the variables/arrays in a user-friendly way.
    – Elin Y.
    Commented Nov 5, 2013 at 19:05
  • Thank you for giving the hint, it was helpful. However the result is not the one that I was looking for, I got some idea how to work with views template file. I will look if I can add Name and Nick Name in same column but different rows with this approach
    – user12947
    Commented Nov 6, 2013 at 14:28
  • The code I posted is not an implementation. You should do some logic, something like I described: if ($content == $content_previous){}. This is only a hint.
    – Elin Y.
    Commented Nov 6, 2013 at 14:43
0

OK, it looks like you are having the three columns combine into one, and then be split into the two you require, and as such are getting arbitrary data. Try going to "Style Settings" with the options you have now, alter the format to "unformatted", and save. Now go back in and reset it to Table. There should be the option to combine columns, and just choose the two that you wish.

I hope this helps.

3
  • No, its just two columns combined into One, I have already done the settings you said. There is an option to combine columns (which I have already tried and explained in the question) and I have also posted the results when those combine happens. Is there anything I am missing here?
    – user12947
    Commented Nov 5, 2013 at 7:51
  • Ah, sorry I wasn't more help. Try hiding the nicknames column and then re-attempting to combine the remaining two?
    – KyleM
    Commented Nov 5, 2013 at 7:55
  • I am sorry if you understood the question correctly. I want to display the nickname field in Name's row... I have edited the question too, please have a look.
    – user12947
    Commented Nov 5, 2013 at 9:09
0

In Structure -> Views -> settings -> Check on "Show the SQL query". Go to your view, it displays query.

FORMAT - Show:fields, and then use "Global: PHP" for writing custom query. If you not installed use this link, https://drupal.org/project/views_php .

After selecting Global: PHP - You can use the query already shown and then we can do as we like. All above fields should be checked with "Exclude from display" (i.e., Name, age, etc..).

3
  • I am stuck with this step you provided. I did try to add Global PHP as field but I can only see place to use PHP like value code,output code, etc. Where exactly do I use these queries? Can you give me hint how to use it?
    – user12947
    Commented Nov 5, 2013 at 11:50
  • You have to write code in Output code <?php ... ?> Click on Available variables Link.. for example, <?php echo $row->name; ?> check the output of available variables.
    – J Sinha
    Commented Nov 5, 2013 at 12:13
  • I have edited my question with pictures, if you could provide me sample how could I do that then I'd be very thankful.
    – user12947
    Commented Nov 5, 2013 at 15:51
0

Sorry I don't have a Drupal 7 testbed handy, but if you don't mind the age being grouped together, you can do something like this (screenshots here are in Drupal 6) that I assume is pretty much the same in Drupal 7.

First, off, grab the fields you need, in this case I created a content type called name_nick_age along with three fields nna_name, nna_nick and nna_age and then created a View:

enter image description here

And then inside the Style Settings for the Table, you by default have something like this:

enter image description here

That I changed to this:

enter image description here

The only change is placing the Name in the Nick column. This then created this table:

enter image description here

You gain access to the style settings by clicking on the little gear to the right of Style: Table and while the screen shots for Drupal 7 will be different, I am betting the functionality is in there for this.

5
  • Hi, thanks for visual info. I have already tried those steps as you said, and the result is in the 3rd Picture. I have achieved as you said, but my requirement is exact like picture 4 (I made that photo with editor) because they want the row to be sortable.
    – user12947
    Commented Nov 6, 2013 at 0:37
  • AH! New information! You need to be able to sort on the combination of names and nicknames into one big alphabetical list? (I assume you mean column not row re: sort. If wrong, let me know what you mean.)
    – Jimajamma
    Commented Nov 6, 2013 at 0:45
  • Yes, I mean column (got mixed up). I mean the column which will contain both Names and Nick names in separate rows should be sortable (like normal column sort in Views).
    – user12947
    Commented Nov 6, 2013 at 13:45
  • Please add this new information to your question as it seriously adds to the complexity as it is no longer only a display issue now.
    – Jimajamma
    Commented Nov 6, 2013 at 13:48
  • Yes I felt so, I am stuck in this since last week, really.
    – user12947
    Commented Nov 6, 2013 at 14:06
0
   <?php $result = db_query("SELECT node.title AS node_title, node.nid AS nid, field_data_field_nick_names.delta AS field_data_field_nick_names_delta, field_data_field_nick_names.language AS field_data_field_nick_names_language, field_data_field_nick_names.bundle AS field_data_field_nick_names_bundle, field_data_field_nick_names.field_nick_names_value AS field_data_field_nick_names_field_nick_names_value, field_data_field_nick_names.field_nick_names_format AS field_data_field_nick_names_field_nick_names_format, node.created AS node_created, 'node' AS field_data_field_nick_names_node_entity_type, 'node' AS field_data_field_ages_node_entity_type, 'girls:page' AS view_name
    FROM 
    {node} node
    LEFT JOIN {field_data_field_nick_names} field_data_field_nick_names ON node.nid = field_data_field_nick_names.entity_id AND (field_data_field_nick_names.entity_type = 'node' AND field_data_field_nick_names.deleted = '0')
    WHERE (( (node.status = '1') AND (node.type IN  ('girlss')) ))
    ORDER BY node_created DESC");

    $fullname = "";
    $nickname = "";
    foreach($result as $rec)
    {   
        $fullname .= $rec->node_title.",";
        $nickname .= $rec->field_data_field_nick_names_delta.",";
    }
    echo $fullname;
    echo "<br>";
    echo $nickname;
?>

Check if results are displaying or not?

Your Answer

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