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

Re: Checking for missing images script

Post by tessthepup »

ecartz wrote: Sun Dec 04, 2022 12:24 pm

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");
@ecartz total superstar!!! :D :D :D

Works perfectly in identifying the missing images.

The only issue left is it shows the same product name for all missing images urgh


Join The Code Co-op to get access to your library in the Code Co-op Forum
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

        $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 AND language_id = " . (int)$_SESSION['languages_id'] . " WHERE p.products_id = " . (int)$id);
or just use

Code: Select all

Product::fetch_name($id)
to get the name, which might be easier.
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

Hi, I am trying to change the script above to check for missing product descriptions. I have it working but it only shows products with descriptions urgh

Code: Select all

    /* Our products_description array is now built, start checking data. */
    $missing_description = [];

    foreach ($db->fetch_all("SELECT products_id, products_description FROM products_description") as $product) {
      if ($product['products_description']) {
       Guarantor::guarantee_subarray($missing_description, $product['products_id']);
      $missing_description[$product['products_id']][] = $product['products_description'];
      }
    }

Code: Select all

<?php
        foreach ($missing_description as $id => $data) {
        $product_query = $db->query("SELECT p.products_id, pd.products_name, pd.products_description 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::fetch_name($id); ?></a></td>
                  <td>
                   <?php

                   if (count($missing_description) > 0) {
                     if (is_array($data) || is_object($data))
                     {
                     foreach ($data as $d) {
                      echo $d . '<br />';
                     }
                     }
                   ?>
                  </td>
                </tr>
                <?php } } ?>
I have tried changing line

Code: Select all

$missing_description = [];
to

Code: Select all

$missing_description = NULL;
Also tried '', FALSE and array() but not working

I have changed line

Code: Select all

if (count($missing_description) > 0) {
to

Code: Select all

if (empty($missing_description)) {
but still reporting products with description
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 descriptions script

Post by ecartz »

First code block:

Code: Select all

    $missing_description = [];

    foreach ($db->fetch_all("SELECT products_id, language_id, products_name, products_description FROM products_description") as $product) {
      if (empty($product['products_description'])) {
       Guarantor::guarantee_subarray($missing_description, $product['products_id']);
      $missing_description[$product['products_id']][$product['language_id']] = $product['products_name'] ?? 'Name missing';
      }
    }
Then for the second code block

Code: Select all

<?php
        foreach ($missing_description as $id => $data) {
          foreach ($data as $lid => $name) {
?>
                <tr>
                  <td><?= $id ?></td>
                  <td><?= $lid ?></td>
                  <td><?= $name ?></td>
                </tr>
<?php
          }
        }
?>
It will not display the description, but then, you don't want products that have descriptions. It will display the product ID, the language ID, and the product name (in the language that is missing the description).
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 descriptions script

Post by tessthepup »

ecartz wrote: Sat Dec 10, 2022 3:13 pm First code block:

Code: Select all

    $missing_description = [];

    foreach ($db->fetch_all("SELECT products_id, language_id, products_name, products_description FROM products_description") as $product) {
      if (empty($product['products_description'])) {
       Guarantor::guarantee_subarray($missing_description, $product['products_id']);
      $missing_description[$product['products_id']][$product['language_id']] = $product['products_name'] ?? 'Name missing';
      }
    }
Then for the second code block

Code: Select all

<?php
        foreach ($missing_description as $id => $data) {
          foreach ($data as $lid => $name) {
?>
                <tr>
                  <td><?= $id ?></td>
                  <td><?= $lid ?></td>
                  <td><?= $name ?></td>
                </tr>
<?php
          }
        }
?>
It will not display the description, but then, you don't want products that have descriptions. It will display the product ID, the language ID, and the product name (in the language that is missing the description).
@ecartz Fantastic thank you :D

I did change

Code: Select all

$missing_description[$product['products_id']][$product['language_id']] = $product['products_name'] ?? 'Name missing';
added a closing ] to [$product['language_id']

Now I would like to add an update button on each row which gives me an input box (maybe in a modal) that allows me to update the product description.

I do not expect the code written for me but if there is anywhere in core that would point me in the right direction to get started that would help.
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 descriptions script

Post by ecartz »

I wouldn't make a modal. Just a link with a blank target:

Code: Select all

<a href="<?= $Admin->link('catalog', ['action' => 'edit_product', 'products_id' => $id]) ?>" target="_blank">Add product description</a>
You may need the cPath too.

Code: Select all

        Guarantor::ensure_global('category_tree')->find_path($category_id)
will find the cPath given a category ID. You can get a valid category ID from the products_to_categories table.

Code: Select all

'SELECT categories_id FROM products_to_categories WHERE products_id = ' . (int)$id . ' LIMIT 1'
Code doing similar things: https://github.com/CE-PhoenixCart/Phoen ... efault.php
https://github.com/CE-PhoenixCart/Phoen ... y_path.php
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 descriptions script

Post by tessthepup »

ecartz wrote: Sat Dec 10, 2022 4:12 pm I wouldn't make a modal. Just a link with a blank target:

Code: Select all

<a href="<?= $Admin->link('catalog', ['action' => 'edit_product', 'products_id' => $id]) ?>" target="_blank">Add product description</a>
You may need the cPath too.

Code: Select all

        Guarantor::ensure_global('category_tree')->find_path($category_id)
will find the cPath given a category ID. You can get a valid category ID from the products_to_categories table.

Code: Select all

'SELECT categories_id FROM products_to_categories WHERE products_id = ' . (int)$id . ' LIMIT 1'
Code doing similar things: https://github.com/CE-PhoenixCart/Phoen ... efault.php
https://github.com/CE-PhoenixCart/Phoen ... y_path.php
Thank you, I will have a go and let you know how I get on
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

Ok not sure if I am barking up the right tree with this one but this line

Code: Select all

$description_input = (new Textarea("products_description[{$lid['id']}]", ['id' => 'Desc', 'cols' => '70', 'rows' => '15']))->require()->set_text($translations[$lid['id']]['products_description'] ?? '');
is giving me this warning just not sure why :?
PHP Warning: Trying to access array offset on value of type int
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 »

Maybe

Code: Select all

$description_input = (new Textarea("products_description[{$lid}]", ['id' => 'Desc', 'cols' => '70', 'rows' => '15']))->require()->set_text($translations[$lid]['products_description'] ?? '');
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

Well I am not going to tell a lie, I am totally stumped by this and have no idea how to move this forward.

All I have ended up with is the following which maybe completely wrong.

I have managed to break the foreach as my codes stops at product line one with no errors or warnings.

Also I have no clue about adding a save button to the code.

Code: Select all

<?php
        foreach ($missing_description as $id => $data) {
          foreach ($data as $lid => $name) {
?>
                <tr>
                  <td><?= $id ?></td>
                  <td><?= $name ?></td>

                  <td>
                   <?=
                    $description_input = (new Textarea("products_description[{$lid}]", ['id' => 'Desc', 'cols' => '10', 'rows' => '5']))->require()->set_text($translations[$lid]['products_description'] ?? '');

                    foreach ($db->fetch_all("SELECT products_id, language_id, products_name, products_description FROM products_description") as $description) {

	    $output = <<<EOD
{$description_input}
EOD;

	      return $output;

	      foreach (language::load_all() as $lid) {
	      $description = Text::prepare($_POST['products_description'][$lid['id']]);
	      $GLOBALS['db']->query("UPDATE products_description SET products_description = '" . $GLOBALS['db']->escape($description) . "' WHERE products_id = " . (int)$GLOBALS['products_id'] . " AND language_id = " . (int)$lid['id']);
          }
	?>

           </td>
          </tr>
<?php
    }
   }
  }
?>
         </tr>


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