Display variable only once on a foreach and count total rows of an IF

Asked

Viewed 207 times

0

I have a script, which basically compares the files of a bank table with the files of a directory, and it shows the files that are NOT in the database and are in the directory.

I have 2 problems:

  1. Count the amount of files that nay are in the bank and are in the directory
  2. Display message with this amount, only once inside the foreach

Code I am using:

    <?php
    include('menu.php'); include('connect.php'); 

    $files = glob("teste-uploads/*.{pdf,jpg,png}", GLOB_BRACE);  // Encontra nomes dos arquivos de determinada pasta e salva no array $files

    $Busca_Imagens = mysqli_query($con,"SELECT fotos FROM teste");  // Busca nome das imagens no seu Banco de Dados

    while($Checa_Imagens = mysqli_fetch_assoc($Busca_Imagens)){

    $images[] = 'teste-uploads/' . $Checa_Imagens['fotos'];
          };

    foreach($files as $file){
    if(!in_array($file, $images)){

    echo "Cerca de <b>" .count($file). "</b> arquivo(s) que não estão no banco de dados <p></p>"; // Número total

    echo "<b>" . $file . "</b><br>"; // Exibe os arquivos
    }}?>

PRINT

The problem is that it is not counting correctly and the phrase "About ..." is repeating itself to each file.

1 answer

1


There are several problems in its implementation:

  1. It is not counting correctly, because each iteration has only 1 same;
  2. The phrase is repeating itself because it is inside the loop; You can solve this by putting the phrase into a variable to be concatenated, and initiating a counter out of the loop:
// Encontra nomes dos arquivos de determinada pasta e salva no array $files
$files = glob("teste-uploads/*.{pdf,jpg,png}", GLOB_BRACE);

// Busca nome das imagens no seu Banco de Dados
$Busca_Imagens = mysqli_query($con,"SELECT fotos FROM teste");  

while ($Checa_Imagens = mysqli_fetch_assoc($Busca_Imagens)) {
    $images[] = 'teste-uploads/' . $Checa_Imagens['fotos'];
}
$contador = 0;
$html = '';
foreach ($files as $file) {
  if (!in_array($file, $images)) {
    // Número total
    $contador++;
    $html .= "<b>" . $file . "</b><br>"; 
  }
}
echo 'Cerca de <b>' . $contador . '</b> arquivo(s) que não estão no banco de dados <p></p>';

// Exibe os arquivos
echo $html;
  • It worked, thank you very much!

  • Tiago, can you tell me if I can mention all the subfolders of a folder at once? in part $files = glob("teste-uploads/*.{pdf,jpg,png}", GLOB_BRACE);

  • 1

    There is: $iterator = new RecursiveTreeIterator(new RecursiveDirectoryIterator("teste-uploads/", RecursiveDirectoryIterator::SKIP_DOTS));&#xA;foreach($iterator as $path) {&#xA; echo $path."<br>";&#xA;}

  • I didn’t quite understand the huahuaha code, I will test it here. I have a folder called uploads, in it, has 12 sub-folders, with the months of the year... I can do what I mentioned in the post by selecting all the subfolders at once, so?

  • 1

    Yes, in case you want to filter can still improve with the RecursiveRegexIterator: $rd = new RecursiveDirectoryIterator("teste-uploads/", RecursiveDirectoryIterator::SKIP_DOTS);&#xA;$rr = new RecursiveRegexIterator($rd, '/\.(pdf|jpg|png)$/');&#xA;$it = new RecursiveTreeIterator($rr);&#xA;foreach($it as $path) {&#xA; echo $path."<br>";&#xA;}

  • Of course, just make sure the shape of the vailable $path is the same as in the bank.

  • But, how would I reference the fields? in that part of the code: {&#xA; $images[] = 'teste-uploads/' . $Checa_Imagens['fotos'];&#xA;} , are 12 fields in the table, the 12 have document path...

Show 2 more comments

Browser other questions tagged

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