hi

in my custom content type i've a CCK filefield and i would to print it in custom node.tpl
i know basically do that with :

for single field :

 <?php if ($node->field_txt_dossier[0]['view']): ?>
        <div id="txt-dossier-bts">
                <?php  print $node->field_txt_dossier[0]['view'] ?>
        </div>
   <?php endif; ?>

defined number of fields :

<?php
       if ($node->field_docs_bts_alternance[0]['view']
        OR $node->field_docs_bts_alternance[1]['view']
        OR $node->field_docs_bts_alternance[2]['view']
        OR $node->field_docs_bts_alternance[3]['view']
        ): ?>
        <ul id="docs-alternance-bts">
           <?php if ($node->field_docs_bts_alternance[0]['view']): ?>
              <li>      <?php  print $node->field_docs_bts_alternance[0]['view'] ?></li>
              <?php endif; ?>
              <?php if ($node->field_docs_bts_alternance[1]['view']): ?>
              <li>      <?php  print $node->field_docs_bts_alternance[1]['view'] ?></li>
              <?php endif; ?>
                 <?php if ($node->field_docs_bts_alternance[2]['view']): ?>
              <li>      <?php  print $node->field_docs_bts_alternance[2]['view'] ?></li>
              <?php endif; ?>
              <?php if ($node->field_docs_bts_alternance[3]['view']): ?>
              <li>      <?php  print $node->field_docs_bts_alternance[3]['view'] ?></li>
              <?php endif; ?>
        </ul>
   <?php endif; ?>

but for undefined number of field i need more advanced php ,i think this could be fine but i'm not programer so if someone could correct me :

<?php
  foreach($node->field_docs_bts_alternance[i] as $file) {
<?php if ($node->field_docs_bts_alternance[i]['view']): ?>
    print $node->field_docs_bts_alternance[i++]['view'];
<?php endif; ?>
  }
?>

thanks

Comments

pbarnett’s picture

foreach($node->field_docs_bts_alternance as $file) {
  if ($file['view']) {
      print $file['view'];
  }
}
aiphes’s picture

thanks for your quick replay :) can i format this in a table like this ? :

<table>
    <?php
foreach($node->field_objectif_bts as $file) {
    ?>
<tr>
    <?php
  if ($file['view']) {
      ?>
  <td>
      <?php
      print $file['view'];
      ?>
</td>
<?php
  }
  ?>
</tr>
  <?php
}?>
</table>

but whith the conditionnal display on the table too...

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

pbarnett’s picture

Yes, but not like that!

If you want the fields in a table, do this -

$rows = array();
foreach($node->field_objectif_bts as $file) {
  if ($file['view']) {
      $rows[] = $file['view'];
  }
}
if (count($rows)) {
  print theme_table(array(), $rows);
}

See http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_table/6

aiphes’s picture

in the same idea, i need t do that but for 2 different field type, here is the actual code that it could be optimized ...

<?php  if ($node->field_fichier_joint_rp[0]['view']
OR $node->field_fichier_joint_rp[1]['view']
OR $node->field_fichier_joint_rp[2]['view']
OR $node->field_fichier_audio_rp[0]['view']
): ?>
<table id="table_infos_plus" border="0">
    <tbody>
        <tr><td><h3>Infos en plus</h3> </td></tr>
        <?php if ($node->field_fichier_joint_rp[0]['view']): ?>
        <tr>
            <td>
              <?php  print $node->field_fichier_joint_rp[0]['view'] /*Fichier joint 1*/ ?>
                <hr class="ombrage_bas"/>
            </td>
        </tr>
                        <?php endif; ?>

        <?php if ($node->field_fichier_joint_rp[1]['view']): ?>
        <tr><td>
                    <?php  print $node->field_fichier_joint_rp[1]['view'] /*Fichier joint 2*/ ?>
                <hr class="ombrage_bas"/>
            </td></tr>
        <?php endif; ?>
        <?php if ($node->field_fichier_joint_rp[2]['view']): ?>
        <tr><td>
                    <?php  print $node->field_fichier_joint_rp[2]['view'] /*Fichier joint 3*/ ?>
                <hr class="ombrage_bas"/>
            </td></tr>
        <?php endif; ?>

        <?php if ($node->field_fichier_audio_rp[0]['view']): ?>
        <tr><td>
                    <?php  print $node->field_fichier_audio_rp[0]['view'] /*Fichier son*/ ?>
                <hr class="ombrage_bas"/>
            </td></tr>
        <?php endif; ?>

    </tbody>
</table>
<?php endif; ?>

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

aiphes’s picture

i need some help to use tips from pbarnett because my table doesnt display content with theme_table, i dont understand how to use it in my case..

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

aiphes’s picture

still there...i don't know what is the variable to put in array()...if someone knows..

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

pbarnett’s picture

The array() is an empty array as we don't want a header for the table in this use case.

See the documentation for theme_table().

aiphes’s picture

i had read doc but not understand all..then i try:

$rows = array();
foreach($node->field_fiche_programme as $file) {
  if ($file['view']) {
      $rows[] = $file['view'];
  }
}
if (count($rows)) {
  print theme_table(array('Cell 1', 'Cell 2', 'Cell 3'), $rows);
}

but the table is empty while i've a pdf in the filefield...so $rows seem empty..what is miss ?

EDIT : i understand how works the theme_table..but still no data for my filefield..arghh

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

pbarnett’s picture

Try this :-

<?php
$rows = array();
foreach($node->field_fiche_programme as $file) {
  if ($file['view']) {
      $rows[] = $file['view'];
  }
  else {
      $rows[] = "file['view'] is empty!";
  }
}
if (count($rows)) {
  print theme_table(array('Cell 1', 'Cell 2', 'Cell 3'), $rows);
}
?>

I suspect there's nothing in $file['view']...

aiphes’s picture

but if i do

$rows[] = print $file['view'];

my files are displayed inline...out of the table;
my actual code:

$rows = array();
foreach($node->field_lien_utile_ficheform as $file) {
  if ($file['view']) {
      $rows[] = $file['view'];
    }
    else {
      $rows[] = "file['view'] is empty!";
  }
}
if (count($rows)) {
    $rows = array(
    //ligne de cellules
      array('Lien :'),
    // attributs des cellules et de la table
    array(
   'data' => array('Cell 1'), 'class' => 'ma_classe2')
    );
    $output = theme_table(array(), $rows);
    print $output;
       drupal_set_message('Contenu de file : '.$rows,'status');
}

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

pbarnett’s picture

From the theme_table documentation :-

$rows An array of table rows. Every row is an array of cells, or an associative array with the following keys:

* "data": an array of cells
* Any HTML attributes, such as "class", to apply to the table row.

i.e. each row has to be an array. Try -

$rows = array();
$row = array();
foreach($node->field_lien_utile_ficheform as $file) {
  if ($file['view']) {
      $row[] = $file['view'];
    }
}
$rows[] = $row;
$output = theme_table(array(), $rows);
print $output;
aiphes’s picture

it's really better, it give 2 cells with the two links inside,look :

<table>
<tbody>
<tr class="odd">
<td>
<a target="_blank" href="http://www.google.com">Google</a>
</td>
<td>
<a href="http://www.bip.com">Lien 2</a>
</td>
</tr>
</tbody>
</table>

problems to solve:
- have two TR whith one TD to have links under ones
- can add a class to the table to style it

thanks a lot for help, this is not easier for not developper man

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

pbarnett’s picture

$rows = array();
foreach($node->field_lien_utile_ficheform as $file) {
  if ($file['view']) {
      $rows[] = array($file['view']);
    }
}
$output = theme_table(array(), $rows, array('class' => 'mytableclass'));
print $output;
aiphes’s picture

you rocks pete !

all work as desire...with css it's that i would..

great thanks to you

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

aiphes’s picture

i would to use this way but , this time i need to give field content over columns , how can i add this functionnality to my code ?

thanks

EDIt : i find this http://www.group42.ca/theming_101_%E2%80%93_theme_table_function , i try to help me with but example are for static data, not for dynamics ones...
another examples but too complex for me instead of it look like what i need : http://internetdevels.com/en/blog/examples-themetable-and-themefieldset-...

my idea is "telling to drupal if it fill in 4 cells in column 1, then put item 5 to 9 in a second column, etc...each 4 items" , is it possible in php ?

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data