Including upload info fields in multisearch results

12 posts by 2 authors in: Forums > CMS Builder
Last Post: July 13   (RSS)

By daniel - July 13

Hi Jerry,

1) Showing the info fields for each image (test code below, full page code attached).

Unfortunately, the multi-search currently only has the ability to send back two configurable fields using titleField and summaryField. Adding extra arguments to the array won't do anything in particular, so you'll need to take some extra steps to get any additional data. First, you'll need to get the num of the upload record. Since it doesn't look like you're using the summaryField argument, we can change the uploads config array to something like this:

$searchTables['uploads'] = array(
'viewerUrl' => 'images.php',
'titleField' => 'filePath',
'summaryField' => 'num',
'searchFields' => array('filePath', 'info1','info2', 'info3', 'info4', 'info5'),

With this, $record['summaryField'] should now contain the upload record's num.

Next, you'll want to query the uploads table using that num and mysql_get():

<?php if ($record['tablename'] == 'uploads'): ?>
  <?php $uploadRecord = mysql_get('uploads', $record['summaryField']); ?>

From here, $uploadRecord should have access to the necessary fields - similar to on the upload detail page:

<?PHP $uploadRecord['_info1'] = preg_replace("/[\"]/", "''", $uploadRecord['_info1'] ); ?>
<?PHP $uploadRecord['_info2'] = preg_replace("/[\"]/", "''", $uploadRecord['_info2'] ); ?>
<?PHP $uploadRecord['_info3'] = preg_replace("/[\"]/", "''", $uploadRecord['_info3'] ); ?>
<?PHP $uploadRecord['_info4'] = preg_replace("/[\"]/", "''", $uploadRecord['_info4'] ); ?>
<?PHP $uploadRecord['_info5'] = preg_replace("/[\"]/", "''", $uploadRecord['_info5'] ); ?>

2) I’d also like to be able to show the name ('title') of the record that the image was originally associated with, and ultimately to create a link to that record for Admins. I tried tinkering around using the upload record ‘storage’ field but couldn’t get that to work, and don’t even know if that’s the correct field.

This data isn't going to be stored in the upload record itself, but luckily the upload record does contain a reference to the table (tableName) and record (recordNum) it's associated with, so it's relatively simple to pull that data once you have $uploadRecord. Since it has the table and record num you can again use mysql_get() to fetch the associated record's data:

  $originRecord = mysql_get($uploadRecord['tableName'], $uploadRecord['recordNum']);
  if (!empty( $originRecord['title'] )) {
    echo $originRecord['title'];

Note that while mysql_get() is a simple and easy method to fetch a single record when you know the table name and record num, it has fewer options and doesn't fetch all of the same info as getRecords() (things like list field labels, related records, etc.) - it just gets the raw data stored in the DB. If you come across a case where you do need the full getRecords() data, an equivalent usage would be:

  list($originRecords,) = getRecords(array(
    'tableName' => $record['tableName'],
    'where'     => mysql_escapef('num = ?', $record['recordNum']), 
  $originRecord = @$originRecords[0];

Also note that the above code isn't tested, so you may need to make some adjustments to get it to work in your script. 

Let me know if that does the trick, or if you have any more questions!


PHP Programmer