How to insert an Else to display message in case you don’t have files to upload

Asked

Viewed 66 times

2

Code

<?php
$files = $obj->field('upload');

foreach ($files as $file)
  {
    $file_url = get_attached_file($file['id']);
    echo '<a href="' . $file["guid"] . '">' . $file["post_title"] . '</a>';
    echo '<br>';
  }
?>

Error message when not attached:

Warning: Invalid argument supplied for foreach() in /var/www/html/wp-content/plugins/pods/Components/Templates/Templates.php(500) : Eval()’d code on line 8

  • The variable $obj is an instance of which class? Post a little more code.

  • Check whether $files has some element, can use the count() ;)

  • This is the part of the code I call inside the pods template

  • <tr> <td> <? php $files = $obj->field('upload'); foreach ( $files as $file ) { $file_url = attach_ed_file($file['id']); echo '<a href="' . $file["guid"] . '">' . $file["post_title"] . '</a>'; echo '<br>'; } ? > </td> <td>{@object}</td> <td>{@aperture}</td> <td>{@valor_da_gru}</td> <td>{@fase}</td> </tr>

  • I don’t know much so I have a hard time explaining.

  • @Rodrigo, what the emtpy function does is to check if a variable is empty, literally - Empty

Show 1 more comment

3 answers

2

In your case, I believe it’s quite simple:

<?php

$files = $obj->field('upload');
if (!empty($files)) {
    foreach ( $files as $file ) {
             $file_url = get_attached_file($file['id']);
             echo '<a href="' . $file["guid"] . '">'
                  . $file["post_title"]
                  . '</a>';
                  . '<br>';
    }
} else {
   echo "Não há arquivos enviados!";  die();
}

But I suggest doing something better worked, using exception:

function listFiles()
{
  $file = func_get_args(0);
  try {

      if (empty($file)) {
         throw new Exception("Não há arquivo(s) enviado(s)!");
      }
       $content = '';
      foreach ( $files as $file ) {
         $file_url = get_attached_file($file['id']);
         $content.= '<a href="' . $file["guid"] . '">'
                    . $file["post_title"]
                    . '</a>';
                    . '<br>';
      }
      return $content;

  } catch (Exception $e)  {
     return $e->getMessage();
  } 
} 

echo listFiles($obj->field('upload'));
  • Thanks for your Code I got to Empty.

1

Use a isset or empty to verify that this variable/array has values.

<?php
$files = $obj->field('upload');

if(!empty($files)){
foreach ($files as $file)
  {
    $file_url = get_attached_file($file['id']);
    echo '<a href="' . $file["guid"] . '">' . $file["post_title"] . '</a>';
    echo '<br>';
  }
} else {
 print "Nenhum ficheiro selecionado";
}
?>

0

You can do a simple check on $files:

<?php

$files = $obj->field('upload');
if (count($files)) {
    foreach ( $files as $file ) {
    $file_url = get_attached_file($file['id']);
    echo '<a href="' . $file["guid"] . '">' . $file["post_title"] . '</a>';
       echo '<br>';
    }
}
  • I tested and presented an error: Parse error: syntax error, Unexpected '<' in /var/www/html/wp-content/plugins/pods/Components/Templates/Templates.php(500) Eval()’d code on line 15

  • Just make it clear to Rodrigo, that using count(), it will not display the files, but will list all the files when you have at least one in the collection.

  • 1

    I was just talking about that, an isset/Empty would be more fruitful.

  • Well guys I solved with ( Empty ) It serves to know if a variable exists <? php $files = $obj->field('upload'); if(!Empty($files): foreach ( $files as $file ) { $file_url = get_attached_file($file['id']); echo '<a href="' . $file["guid"] . '">' . $file["post_title"] . '</a>'; echo '<br>'; } Else: echo "No content found." ; endif; endif;; ?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.