Page 1 of 3

Checking for missing images script

Posted: Tue Nov 29, 2022 4:29 pm
by tessthepup
Hi Guys,

Please can some one have a look at the code below and hazard a guess as to why it 1. It does not work and 2. It returns no errors :? :? :?

The code is supposed to check the product image name in the and then check the /images folder for a corresponding image and report back if the image is missing in the folder.

I have either totally mucked it up or I am missing something simple

Code: Select all

<?php
  error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
  ini_set("display_errors", 1);

  require 'includes/application_top.php';
  require 'includes/template_top.php';

  //Change this line if you keep your images stored in a strange directory.
  //Don't touch it unless you're having problems.
  $image_directory = DIR_FS_CATALOG . 'images/';

  $column_query_string = '';
  $image_count = 0;
  $image_columns = '';

if (is_iterable($image_columns)) {
  foreach ($image_columns as $column) {
    if ($column_query_string != '') $column_query_string .= ', ';
    $column_query_string .= $column;
  }
}

  $image_array = array();
  $images_query = $db->query("SELECT products_id, ' . $column_query_string . ' from products");

  while ($row = $images_query->fetch_assoc()) {

  $image_array[$row['products_id']] = array();

if (is_iterable($image_columns)) {
    foreach ($image_columns as $column) {
      if ($row[$column] != '') {
        $image_array[$row['products_id']][] = array('products_image' => $row[$column], 'column' => $column);
        $image_count++;
      }
    }
  }
}
  /* Our image array is now built, start checking files. */
  $missing_images = array();

  foreach ($image_array as $id => $product) {
    foreach ($product as $image) {
      if (!is_file($image_directory . $image['products_image'])) {
        if (!is_array($missing_images[$id])) $missing_images[$id] = array();

        $missing_images[$id][] = $image['products_image'];
      }
    }
  }
?>
          <table class="table w-75 mt-4 mx-auto">
           <thead class="thead-light">
            <tr>
             <th><?php echo 'Product id'; ?></td>
             <th><?php echo 'Product name'; ?></td>
             <th><?php echo 'Product image'; ?></td>
            </tr>
           </thead>
<?php
        foreach ($missing_images as $id => $files) {
        $product_query = $db->query("SELECT products_id, products_name, products_image FROM products_description pd INNER JOIN products p ON p.products_id = pd.products_id ORDER BY p.products_id");
        $product = $product_query->fetch_assoc();
?>
                <tr>
                  <td><?php echo $id; ?></td>
                  <td><?php echo $product['products_name']; ?></a></td>
                  <td>
                   <?php

                   if (count($missing_images) > 0) {
                     if (is_array($files) || is_object($files))
                     {
                     foreach ($files as $f) {
                      echo $f . '<br />';
                     }
                     }
                   ?>
                  </td>
                </tr>
                <?php } } ?>
         </tr>
       </table>

<?php
require 'includes/template_bottom.php';
require 'includes/application_bottom.php';
?>

Re: Checking for missing images script

Posted: Tue Nov 29, 2022 7:16 pm
by raiwa
As far as I see:

Code: Select all

 $image_columns = '';
Is defined empty and nowhere filled in something.

Re: Checking for missing images script

Posted: Thu Dec 01, 2022 11:36 pm
by ecartz
You might change that line to

Code: Select all

  $image_columns = ['products_image'];
Of course, it would still miss all the images from the products_images table.

Re: Checking for missing images script

Posted: Fri Dec 02, 2022 7:07 pm
by tessthepup
ecartz wrote: Thu Dec 01, 2022 11:36 pm You might change that line to

Code: Select all

  $image_columns = ['products_image'];
Of course, it would still miss all the images from the products_images table.
@raiwa @ecartz

Thanks guys and sorry for taking so long to get back to you.

I now get this error about 4.5k times lol

Warning: Undefined array key "products_image"

on this line

Code: Select all

if ($row[$column] != '') {

Re: Checking for missing images script

Posted: Sat Dec 03, 2022 10:20 pm
by Dan Cole
I monitor my images by adding a small bit of code with the missing file name to a database table when KISS Images generates a image not available response. If you use KISS images and that is the sort of thing you want, just let me know. I'm not a coder, just a cut and paste shop owner so you'll need to be able to adjust the code to suit.

Dan

Re: Checking for missing images script

Posted: Sun Dec 04, 2022 8:40 am
by ecartz
Old code:

Code: Select all

  $image_columns = '';

if (is_iterable($image_columns)) {
  foreach ($image_columns as $column) {
Proposed new code:

Code: Select all

  $image_columns = ['products_image'];

if (is_iterable($image_columns)) {
  foreach ($image_columns as $column) {
I would expect the result you get if you didn't set the image_columns until after the first foreach.

Re: Checking for missing images script

Posted: Sun Dec 04, 2022 10:23 am
by tessthepup
ecartz wrote: Sun Dec 04, 2022 8:40 am Old code:

Code: Select all

  $image_columns = '';

if (is_iterable($image_columns)) {
  foreach ($image_columns as $column) {
Proposed new code:

Code: Select all

  $image_columns = ['products_image'];

if (is_iterable($image_columns)) {
  foreach ($image_columns as $column) {
I would expect the result you get if you didn't set the image_columns until after the first foreach.
You would have thought that but it has me totally stumped :?

Re: Checking for missing images script

Posted: Sun Dec 04, 2022 11:54 am
by ecartz

Code: Select all

  $image_directory = DIR_FS_CATALOG . 'images/';

  /* Our image array is now built, start checking files. */
  $missing_images = [];

  foreach ($db->fetch_all("SELECT products_id, products_image FROM products") as $product) {
    if (!is_file("$image_directory{$product['products_image']}")) {
      Guarantor::guarantee_subarray($missing_images, $product['id']);

      $missing_images[$product['id']][] = $product['products_image'];
    }
  }
?>
The first and last lines of that are from the original file to show where this goes. Note that this is considerably shorter than the original code.

Re: Checking for missing images script

Posted: Sun Dec 04, 2022 12:19 pm
by tessthepup
ecartz wrote: Sun Dec 04, 2022 11:54 am

Code: Select all

  $image_directory = DIR_FS_CATALOG . 'images/';

  /* Our image array is now built, start checking files. */
  $missing_images = [];

  foreach ($db->fetch_all("SELECT products_id, products_image FROM products") as $product) {
    if (!is_file("$image_directory{$product['products_image']}")) {
      Guarantor::guarantee_subarray($missing_images, $product['id']);

      $missing_images[$product['id']][] = $product['products_image'];
    }
  }
?>
The first and last lines of that are from the original file to show where this goes. Note that this is considerably shorter than the original code.
@ecartz
Thanks for the help I do appreciate it.

The code you posted generates this warning
Warning: Undefined array key "id"
on these 2 lines

Code: Select all

Guarantor::guarantee_subarray($missing_images, $product['id']);
$missing_images[$product['id']][] = $product['products_image'];
And also this warning at the end
Warning: DB: [1052] Column 'products_id' in field list is ambiguous from

Re: Checking for missing images script

Posted: Sun Dec 04, 2022 12:24 pm
by ecartz

Code: Select all

      Guarantor::guarantee_subarray($missing_images, $product['products_id']);

      $missing_images[$product['products_id']][] = $product['products_image'];
Sorry.

Later, for the other message

Code: Select all

        $product_query = $db->query("SELECT p.products_id, pd.products_name, p.products_image FROM products_description pd INNER JOIN products p ON p.products_id = pd.products_id ORDER BY p.products_id");