I'm pretty sure this is a simple question, which hopefully will have a simple answer.

I've created a block in Views 2 that lists all the directors a client of mine has worked for, which I can get to display fine, but I want to have these show up in a comma separated paragraph rather than rows. The only way I've found to get something like this to happen is to set the style to "Ordered List" and then in the CSS give the li tag for that block a value of "inline". Then, to get the commas in I've had to do the following cheat in my views-view-list--resume.tpl.php file:

foreach ($rows as $id => $row):

  • print $classes[$id]; "> print $row;
  •   echo ",";  
                       endforeach; 

    but I still get awkward spacing between the comma & the values, as well as a trailing comma after the last row is printed. I tried to play around with implodes, but I couldn't get that to work, and it seems like there is something in views already set up to have the values print out inline, but it appears to only work between two separate content fields, not the same.

    Basically I want to take:

    Director1
    Director2
    Director 3
    etc....

    and change it to:

    Director1, Director2, Director3

    Any and all help would be greatly appreciated.

    Comments

    WorldFallz’s picture

    Set the style to 'unformatted', row style to 'fields', then click on the little gear next to "fields" and select the inline fields and set the separator.

    ===
    "Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
    "God helps those who help themselves." - Ben Franklin
    "Search is your best friend." - Worldfallz

    dkane’s picture

    Unfortunately that doesn't work. I tried that first, which is what let me to my goofy solution. Setting the style to "unformatted" returns the values with line breaks between them, and the inline fields section doesn't seem to do anything whatsoever.

    Is there another setting hidden somewhere that might conflict with this?

    WorldFallz’s picture

    There must be something going on because it works fine when i use it (i just tried again to be sure). I'm using views 6.3.

    ===
    "Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
    "God helps those who help themselves." - Ben Franklin
    "Search is your best friend." - Worldfallz

    dkane’s picture

    hmmmm =/ I'm using views 2.3, but you're right there must be something else going on. When you tried it were you only printing one CCK field (containing multiple table rows)?

    {grumble}

    WorldFallz’s picture

    If you describe exactly what you want in the view, i'll try it out and see what happens.

    ===
    "Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
    "God helps those who help themselves." - Ben Franklin
    "Search is your best friend." - Worldfallz

    nevets’s picture

    The inline feature is part of an unformatted row, that is it only applies to the row.

    What you probably want to do is override the template for style output (it gathers the rows together). The default template is

    <?php if (!empty($title)): ?>
      <h3><?php print $title; ?></h3>
    <?php endif; ?>
    <?php foreach ($rows as $id => $row): ?>
      <div class="<?php print $classes[$id]; ?>">
        <?php print $row; ?>
      </div>
    <?php endforeach; ?>
    

    I would make a template override something like

    <?php if (!empty($title)): ?>
      <h3><?php print $title; ?></h3>
    <?php endif; ?>
    <div class="director-row">
    <?php
      $count = 0;
      foreach ($rows as $id => $row): ?>
      <?php if ( $count ) { print ", "; } $count++; ?>
      <span class="<?php print $classes[$id]; ?>">
        <?php print $row; ?>
      </span>
    <?php endforeach; ?>
    

    [Edited to change count++ to $count++]

    dkane’s picture

    Thanks nevets! That did the trick. I had a feeling that a counter had something to do with it, but couldn't quite get it to play nice. Just in case anyone out there is trying to do this too, there is one minor change that needs to happen if you want to copy and paste nevets' code, you need to make "count++" "$count++".

    Otherwise, it worked like a charm. Thanks for all of your help!

    sylvain_a’s picture

    This snippet is working, but just make sure you don't have any whitespace between the 'foreach' and 'if ( $count )', as the whitespace would appear between the items and the comma, creating a visual bug.

    In Drupal 6, you can add this to a template called 'views-view-unformatted--field-myfield.tpl.php'. To get the exact name, check within Views, under 'theme information', and then 'Style Output'.

    areikiera’s picture

    This looks like exactly the type of answer I'm looking for, but I'm not exactly sure how to implement it.

    I'm using View 6.x-2.8. I've created a content type (lodging) with a text field called 'features' which has multiple values enabled and populated. I call this field in views and trim it to 200 characters on a word boundary adding ellipses when there is more content. When I view it, it displays like this:

    Features:
    Hot tub
    Pool
    Hot Breakfast
    ...

    and I would like it to display like this:

    Features: Hot Tub, Pool, Hot Breakfast...

    I'm using Style: Grid, Row Style: Fields.

    I've attempted to play with a custom template views-view-fields.tpl.php, and views-view-field--field-lodging-features-value.tpl.php, but any edits I've made have been a shot in the dark, as I really am not comfortable with PHP quite yet.

    The original code from Views' views-view-fields.tpl.php is:

    <?php foreach ($fields as $id => $field): ?>
      <?php if (!empty($field->separator)): ?>
        <?php print $field->separator; ?>
      <?php endif; ?>
    
      <<?php print $field->inline_html;?> class="views-field-<?php print $field->class; ?>">
        <?php if ($field->label): ?>
          <label class="views-label-<?php print $field->class; ?>">
            <?php print $field->label; ?>:
          </label>
        <?php endif; ?>
          <?php
          // $field->element_type is either SPAN or DIV depending upon whether or not
          // the field is a 'block' element type or 'inline' element type.
          ?>
          <<?php print $field->element_type; ?> class="field-content"><?php print $field->content; ?></<?php print $field->element_type; ?>>
      </<?php print $field->inline_html;?>>
    <?php endforeach; ?>
    

    What edits would I make to this to achieve the inline, comma-separated results I'm looking for above in a views-view-fields--field-lodging-features-value.tpl.php file?

    Thanks so much for any help!

    dkane’s picture

    Seems like you're right on the verge. You should be able to do this right from within views without having to create a template override.

    Just change your style to "unformatted" and then click on the settings button next to "fields" and choose "features" (or whatever you want to be displayed as an inline field) as your inline field, then just enter a comma in the separator box.

    You could also do it with PHP, but this is a lot more versatile and saves you having to hard code anything.

    Hope that helps.

    Diegen’s picture

    thanks it worked just fine!

    liquidcms’s picture

    pretty sure that isn't answering the question above. that will display a set of fields as selected inline... what is being asked (and i'd love to know) is how to get the values for a single field to list in a csv string rather than 1 per line.

    ideally there would be a new formatter that would do this.

    liquidcms’s picture

    my solution so far was to use Views Customfield module

    and use something like this:

    <?php 
    
    foreach ($data->node_data_field_territory_field_territory_value as $item) {
      $values[] = $item['value'];
    }
    
    echo join(", ", $values);
    
    ?>
    
    theohawse’s picture

    This helps a lot actually! Thank-you, this snippet came in handy when working with views_data_export.

    dizarter’s picture

    Just in case someone else find this topic

    http://drupal.org/project/views_delimited_list

    That's what you are looking for.

    ilfelice’s picture

    FWIW, I have tried this module and it seems to work as advertised.