Including upload info fields in multisearch results
12 posts by 2 authors in: Forums > CMS Builder
Last Post: July 13, 2020 (RSS)
By daniel - July 13, 2020
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:
<?php
$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!
Cheers,
Technical Lead
interactivetools.com
By gkornbluth - July 13, 2020 - edited: July 13, 2020
Wow!
Thanks again Daniel, for a very complete, and complex response.
A few small changes and your suggestions worked perfectly...
For the info Fields:
<?php if ($record['tablename'] == 'uploads'): ?>
<?php $uploadRecord = mysql_get('uploads', $record['_summary']); ?>
<?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'] ); ?>
<br> <div align='center' style='font-size:1.4em; vertical-align:top; text-align:left;' class='text_font' ><?php echo($uploadRecord['info1']) ?> <?php echo($uploadRecord['info2']) ?> <?php echo($uploadRecord['info3']) ?> <?php echo($uploadRecord['info4']) ?> <?php echo($uploadRecord['info5']) ?></div>
And for the title link:
<?php
$originRecord = mysql_get($uploadRecord['tableName'], $uploadRecord['recordNum']);
if (!empty( $originRecord['title'] )) {
$title = $originRecord['title'];
}
if (empty( $originRecord['title'] )) {
echo '(Sorry, This Title Is Empty)';
}
?><br><span class="navigation_font" style="font-size:1.4em">This image can be found in </span><a href="gallery_detail.php?<?php echo $originRecord['num'] ?>"><span class="navigation_font" style="font-size:1.2em; text-decoration:underline"><?php echo $title ?></span></a>
Thanks again.
Best,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php