Help with Where Clause Checking if an Upload Exists

2 posts by 2 authors in: Forums > CMS Builder
Last Post: June 24, 2013   (RSS)

By In-House-Logic - June 21, 2013

Hi there,

I have a two-stage query problem. The first part loads a category list. Then for each category loaded, we'd like to pull a random product which matches that category AND which has an image uploaded into a multi-upload field. I'm close but can't quite get the image check to fly. The current iteration looks like:

// load 'categories'
  list($categoriesRecords, $categoriesMetaData) = getRecords(array(
    'tableName'   => 'categories',
    'loadUploads' => false,
    'allowSearch' => false,
  ));

--- LATER ---
 
               <ul class="thumbnails">
                <?php foreach ($categoriesRecords as $record):
                      // load 1 random sample with an image from 'products' table for each category
                    $cat = trim($record['name']);

                  list($productsRecords, $productsMetaData) = getRecords(array(
                    'tableName'   => 'products',
                    'limit'       => '1',
                    'orderBy'     => 'RAND()',
                    'loadUploads' => true,
                    'allowSearch' => false,
                    'where'     => "CHAR_LENGTH(images) > 0 AND category LIKE '%\t".$cat."%\t'",
                    // 'where'         => "`category` LIKE '%\t".$cat."%\t'",
                     'debugSql'      => '1',
                  ));?>

                    <?php foreach ($productsRecords as $product): ?>
                    ---- DISPLAY SOME STUFF ----

                   <?php endforeach; // end single product sample load ?>
                  <?php endforeach; // end category selection loop ?>

At the moment I keep getting failure messages like the following after a single loop : 

    • MySQL Error: Unknown column 'images' in 'where clause'

Have also tried 'where' => "`images` != NULL",  also without success.

Any pointers on where I'm going off the rails. I can't see this code clearly anymore.

Thanks,

J.

By Jason - June 24, 2013

Hi,

In this case, the "images" column is a pseudo field, as it does not actually exist in the products table.  What we can do is figure out which products have uploads by querying the uploads table and then  using this list of numbers to retrieve a product record.

First, we can get all the products num with uploads like this:

// load 'categories'
  list($categoriesRecords, $categoriesMetaData) = getRecords(array(
    'tableName'   => 'categories',
    'loadUploads' => false,
    'allowSearch' => false,
  ));
  
  $productNumsWithUploads = array_pluck(mysql_select("uploads", "tableName = 'products' AND fieldName = 'images'"), 'recordNum');

Next, we can use this in our WHERE clause like this:

list($productsRecords, $productsMetaData) = getRecords(array(
        'tableName'   => 'products',
        'limit'       => '1',
        'orderBy'     => 'RAND()',
        'loadUploads' => true,
        'allowSearch' => false,
        'where'     => "num IN (".mysql_escapeCSV($productNumsWithUploads).")  AND category LIKE '%\t".$cat."%\t'",
         'debugSql'      => '1',
    ));

This should get you the results you are looking for. Let me know if you have any other questions.

Thanks

---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/