Checking for missing images script

Open to all! Ask other shopowners for help.
User avatar
tessthepup
Certified Developer
Posts: 383
Joined: Mon Mar 01, 2021 5:55 pm
Phoenix Version:
Has thanked: 56 times
Been thanked: 62 times

Checking for missing images script

Post 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';
?>


Join The Code Co-op to get access to your library in the Code Co-op Forum
raiwa
Certified Developer
Posts: 1641
Joined: Sat Dec 21, 2019 8:08 am
Phoenix Version: 1.1.0.6
Has thanked: 70 times
Been thanked: 152 times

Re: Checking for missing images script

Post by raiwa »

As far as I see:

Code: Select all

 $image_columns = '';
Is defined empty and nowhere filled in something.
Public Phoenix Change Log Cheat Set on Google Sheets
https://docs.google.com/spreadsheets/d/ ... sp=sharing

Need Help?viewtopic.php?f=10&t=27
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Checking for missing images script

Post 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.
User avatar
tessthepup
Certified Developer
Posts: 383
Joined: Mon Mar 01, 2021 5:55 pm
Phoenix Version:
Has thanked: 56 times
Been thanked: 62 times

Re: Checking for missing images script

Post 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] != '') {
Dan Cole
Senior Contributor
Posts: 499
Joined: Fri Oct 25, 2019 2:14 pm
Phoenix Version: 1.0.8.21
Has thanked: 67 times
Been thanked: 61 times

Re: Checking for missing images script

Post 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
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Checking for missing images script

Post 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.
User avatar
tessthepup
Certified Developer
Posts: 383
Joined: Mon Mar 01, 2021 5:55 pm
Phoenix Version:
Has thanked: 56 times
Been thanked: 62 times

Re: Checking for missing images script

Post 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 :?
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Checking for missing images script

Post 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.
User avatar
tessthepup
Certified Developer
Posts: 383
Joined: Mon Mar 01, 2021 5:55 pm
Phoenix Version:
Has thanked: 56 times
Been thanked: 62 times

Re: Checking for missing images script

Post 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
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Checking for missing images script

Post 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");


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply