Including upload info fields in multisearch results

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

By gkornbluth - July 4, 2020

Hi All,

Back in 2008 (ancient history...) Dave Edis announced that as of version 1.13 (more ancient history...) a multi search function had been added to CMSB and he offered some example implementation code (attached)

I’m looking at the possibility of utilizing that functionality on my personal family’s genealogy website. We’ve uploaded thousands of family photos into a multi-record editor, and have a great deal of identifying text (names, etc.) in the info fields for those uploads.

It would be great if the multi search results could include links to those images that match the search criteria.

Has any one explored the possibility of including image upload info fields into the multi search code?

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

multiSearch.php 4K

By daniel - July 7, 2020

Hi Jerry,

The multiple table search isn't exactly tailored to searching the uploads table, but it can still be used to do so - something like this:

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

It's not a perfect fit, but it should generally do the job. I doubt the uploads have a viewer page so viewerUrl isn't used, titleField and summaryField let you pick two fields to return (here I've chosen the file path for the image and primary thumbnail). If you want access to more fields, you could pick "num" as a return field and use mysql_get() to fetch the whole row. Also note that this doesn't have the ability to filter for a specific table/field the file was uploaded to, so if that's important, you may need to return the "tableName" and/or "fieldName" fields and use PHP to filter accordingly.

For a full list of the fields in the uploads table, you can check out "admin.php?menu=database&action=editTable&tableName=uploads" in your CMSB folder. 

Let me know if you have any other questions.

Thanks!

Daniel
Technical Lead
interactivetools.com

By gkornbluth - July 8, 2020

Thank you Daniel,

I'll try to work this through.

Sounds complicated.

I'm sure I'll have questions.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gkornbluth - July 8, 2020 - edited: July 8, 2020

Thank you Daniel,

Using your code I get what I think is a list of the names of the images containing the search criteria and the record number for the image

elsa-graduation-1956-340.jpg
thumb/elsa-graduation-1956-340.jpg
http://pollick.net/OK/test.php?815

What I'm really looking for is a viewer for that image, including the info field information, since all of the relevant information is in the annotations on that image.

Assuming that the # 815 is the upload record number, I tried to cobble together a test.php viewer for that record (attached) but have had no luck. (Invalid argument for foreach loop)

I guess I don’t really understand uploads, and I’ve tried to get my head around what you suggested, but I think I'll need more help then that to make this work.

If it’s not a quick change, what information would you need to give me an estimate on the cost to implement this?

Thanks

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

test.php 4K

By daniel - July 8, 2020

Hi Jerry,

Your test.php is most of the way there. Some quick background on upload fields: each file in an upload field is stored as a record in the "uploads" table. When you use getRecords() it detects upload fields and adds the details from the uploads table to the returned records. Here you're directly querying single records from the uploads table, so all the file info will be in $uploadsRecord and there's no need to loop. For example, instead of doing this:

<?php foreach ($uploadsRecord['filePath'] as $index => $upload): ?>
 
  <img src="<?php echo $upload['filePath'] ?>"/>

<?php endforeach; ?>

You should be able to just do this:

<img src="<?php echo $uploadsRecord['filePath'] ?>"/> 

Note: there are a few additional fields added during the getRecords() process that won't be available here (filename, extension, isImage, hasThumbnail). If you want to be able to see the full list of values available to you in the uploads record, you can use:

<?php
showme($uploadsRecord);
?>

Let me know if that clears anything up, or if you have any more questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

By gkornbluth - July 8, 2020 - edited: July 9, 2020

Wow Daniel,

PERFECT!

Thank you so much.

Jerry Kornbluth

For anyone curious, here's the final code for the MultiSearch page (called multisearch.php) including group labels for the various table groups and the Image Viewer page (called test.php) (no styling):

<?php
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/your_server_path/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

$searchOptions = array();
$searchOptions['keywords'] = @$FORM['q'];
$searchOptions['perPage'] = "100";
$searchOptions['debugSql'] = "0";

$searchTables = array();
$searchTables['family_stories'] = array(
'viewerUrl' => 'story_detail.php',
'titleField' => 'title',
'summaryField' => 'sub_title',
'searchFields' => array('title','sub_title','story'),

);

$searchTables['resources'] = array(
'viewerUrl' => 'resourcedetails.php',
'titleField' => 'title',
'summaryField' => 'category',
'searchFields' => array('title', 'category','description'),

);
$searchTables['faq'] = array(
'viewerUrl' => 'faqdetail.php',
'titleField' => 'question',
'summaryField' => 'category',
'searchFields' => array('category','question','answer'),

);

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

);

list($searchRows, $searchDetails) = searchMultipleTables($searchTables, $searchOptions);
?>
<!-- /STEP1: Load Record List -->
<?php echo "<?xml version='1.0'?>\n"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<style type="text/css">
body {
font-family: arial;
font-size: 12px;
}
</style>
</head>
<body>
<h1>Search Viewer</h1>
<form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type="text" name="q" value="<?php echo htmlspecialchars(@$FORM['q']); ?>" size="50">
<input type="submit" name="" value="Search">
<br/>
<br/>
<table border=0 cellpadding=2 cellspacing=0 width=100%>
<tr>
<td bgcolor="#EEEEEE"><font size=2 class="maintext">Search results for <b><?php echo htmlspecialchars(@$FORM['q']); ?></b></font> - <font size=2 class="maintext">This page shows results <b><?php echo htmlspecialchars(@$searchDetails['pageResultsStart']); ?></b> to <b><?php echo htmlspecialchars(@$searchDetails['pageResultsEnd']); ?></b> out of <b><?php echo htmlspecialchars(@$searchDetails['totalRecords']); ?></b> found.</font></td>
</tr>
</table>
<br/>
<!-- STEP3: Display Page Links (Paste anywhere below "Load Record List") -->
<?php if ($searchDetails['prevPage']): ?>
<a href="<?php echo $searchDetails['prevPageLink'] ?>">&lt;&lt; previous page</a>
<?php else: ?>
&lt;&lt; prev
<?php endif ?>
- page <?php echo $searchDetails['page'] ?> of <?php echo $searchDetails['totalPages'] ?> -
<?php if ($searchDetails['nextPage']): ?>
<a href="<?php echo $searchDetails['nextPageLink'] ?>">next page &gt;&gt;</a>
<?php else: ?>
next &gt;&gt;
<?php endif ?>
<!-- /STEP3: Display Page Links -->

<!-- show errors -->
<?php if ($searchDetails['invalidPageNum']): ?>
Results page '<?php echo $searchDetails['page']?>' not found, <a href="<?php echo $searchDetails['firstPageLink'] ?>">start over &gt;&gt;</a>.<br/>
<?php elseif ($searchOptions['keywords'] && $searchDetails['noRecordsFound']): ?>
No records matched search query!<br/>
<br/>
<?php elseif ($searchOptions['keywords'] == ""): ?>
Enter a keyword to search.<br/>
<br/>
<?php endif ?>

<!-- STEP2: Display Record List -->

<?php $previous_table = ''; ?>
<?php foreach ($searchRows as $record): ?>

<?php if ($previous_table != $record['tablename']): ?>
<?php if ($record['tablename'] == 'family_stories'): ?>
<h2 class="navigation_font" style="font-size:30px">STORIES</h2>
<?php elseif ($record['tablename'] == 'resources'): ?>
<h2 class="navigation_font" style="font-size:30px">RESOURCES</h2>
<?php elseif ($record['tablename'] == 'faq'): ?>
<h2 class="navigation_font" style="font-size:30px">FAQS</h2>
<?php elseif ($record['tablename'] == 'uploads'): ?>
<h2 class="navigation_font" style="font-size:30px">IMAGES</h2>
<?php endif; ?>
<?php $previous_table = $record['tablename']; ?>
<?php endif; ?>

<a href="<?php echo $record['_link'] ?>"><span class="navigation_font" style="font-size:16px; text-decoration:underline"><?php echo $record['_title'] ?></span></a><br/>
<?php if ($record['tablename'] != 'uploads'): ?>
<?php if ($record['_summary']): ?>
<span class="navigation_font" style="font-size:16px;"><?php echo $record['_summary'] ?></span><br/>
<?php else: ?>
<span class="navigation_font" style="font-size:16px;">No description is available for this page.</span><br/><?php endif ?><?php elseif ($record['tablename'] == 'uploads'): ?>&nbsp;
<?php endif ?>
<hr align="left" style="width:50%; color:#FFF"/ >
<?php endforeach; ?>


<!-- /STEP2: Display Record List -->

</form>
</body>
</html>

And for the Image Viewer called test.php (no styling)

<?php
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/your_server_path/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

list($uploadsRecords, $uploadsMetaData) = getRecords(array(
'tableName' => 'uploads',
'where' => whereRecordNumberInUrl(0),
'loadUploads' => true,
'allowSearch' => false,
'limit' => '1',
));
$uploadsRecord = @$uploadsRecords[0]; // get first record

?>

<a href='/multisearch.php'><span class=" navigation_font" style="font-size:24px;">&lt; &lt; BACK TO SEARCH PAGE</span></a><br /><br />

<div > <img src="http://your_site.com/cmsAdmin/uploads/<?php echo $uploadsRecord['filePath'] ?>"/> <br /> <br />
<?PHP $uploadsRecord['info1'] = preg_replace("/[\"]/", "''", $uploadsRecord['info1'] ); ?>
<?PHP $uploadsRecord['info2'] = preg_replace("/[\"]/", "''", $uploadsRecord['info2'] ); ?>
<?PHP $uploadsRecord['info3'] = preg_replace("/[\"]/", "''", $uploadsRecord['info3'] ); ?>
<?PHP $uploadsRecord['info4'] = preg_replace("/[\"]/", "''", $uploadsRecord['info4'] ); ?>
<?PHP $uploadsRecord['info5'] = preg_replace("/[\"]/", "''", $uploadsRecord['info5'] ); ?>
<div align='center' style='font-size:1.4em; vertical-align:top; text-align:left;' class='text_font' ><?php echo($uploadsRecord['info1']) ?> <?php echo($uploadsRecord['info2']) ?> <?php echo($uploadsRecord['info3']) ?> <?php echo($uploadsRecord['info4']) ?> ?php echo($uploadsRecord['info5']) ?></div>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gkornbluth - July 9, 2020 - edited: July 9, 2020

Hi Daniel,

Thanks for your help with getting the image search to work.

Since the search results are now returning lists of records from multiple tables, I’ve been trying to add headings to the groups of search results from each table.

No matter what I’ve done to modify the heading code, only the first table’s heading appears at the top of the entire search results list.

You can see where I'm stuck at http://www.pollick.net/OK/searchA.php?q=elle

Any ideas where I’m going wrong?

Thanks,

Jerry Kornbluth

Here’s what I’ve got so far.

In the get records calls:

<?php
$searchOptions = array();
$searchOptions['keywords'] = @$FORM['q'];
$searchOptions['perPage'] = "100";
$searchOptions['debugSql'] = "0";

$searchTables = array();
$searchTables['family_stories'] = array(
'viewerUrl' => 'story_detail.php',
'titleField' => 'title',
'summaryField' => 'sub_title',
'searchFields' => array('title','sub_title','story'),
$group1 = 'STORIES',
);

$searchTables['faq'] = array(
'viewerUrl' => 'faqdetail.php',
'titleField' => 'question',
'summaryField' => 'category',
'searchFields' => array('category','question','answer'),
$group2 = 'FAQS',
);

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

list($searchRows, $searchDetails) = searchMultipleTables($searchTables, $searchOptions);
?>

And then in the code that displays the search results:

<!-- STEP2: Display Record List -->
<hr align="left" style="width:50%; color:#FFF"/ >
<?php $old_group1 = ''; // init blank var ?>
<?php $old_group2 = ''; // init blank var ?>
<?php $old_group3 = ''; // init blank var ?>
<?php foreach ($searchRows as $record): ?>


<?php if ($group1 != $old_group1):?>
<?php echo "<h2>$group1</h2>" ?>

<?php elseif($group2 != $old_group2):?>
<?php echo "<h2>$group2</h2>" ?>

<?php elseif($group3 != $old_group3):?>
<?php echo "<h2>$group3</h2>" ?>

<?php endif?>

<a href="<?php echo $record['_link'] ?>"><span class="navigation_font" style="font-size:16px; text-decoration:underline"><?php echo $record['_title'] ?></span></a><br/>
<?php if ($record['_summary']): ?>
<span class="navigation_font" style="font-size:16px;"><?php echo $record['_summary'] ?></span><br/>
<?php else: ?>
<span class="navigation_font" style="font-size:16px;">No description is available for this page.</span><br/>
<?php endif ?>


<?PHP $old_group1 = $group1; // retain sub-group name before moving to new record. ?>
<?PHP $old_group2 = $group2; // retain sub-group name before moving to new record. ?>
<?PHP $old_group3 = $group3; // retain sub-group name before moving to new record. ?>

<hr align="left" style="width:50%; color:#FFF"/ >
<?php endforeach ?>
<!-- /STEP2: Display Record List -->

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By daniel - July 9, 2020 - edited: July 9, 2020

Hi Jerry,

First I'll just point out that, in this case, defining a variable inside an array is not likely doing what you are expecting. As an example:

// this
$arr = array(
  $foo = 'bar',
);

// is identical to
$foo = 'bar';
$arr = array(
  'bar',
);

PHP will perform the variable assignment first ($foo = 'bar') and then places the variable's value into the array, but the variable then exists independent of the array. This means that in your code $group1, $group2, and $group3 are always populated regardless of what's happening in the loop.

In situations like this, my first place to look is for values within the loop that can be used to reliably reference the necessary info. (an easy way to do this is with "showme($searchRows);") Here we have the 'tablename' key that shows the table that the result came from, meaning you can do something like this:

<?php foreach ($searchRows as $record): ?>

<?php if ($record['tablename'] == 'family_stories'): ?>
  <h2>STORIES</h2>
<?php elseif ($record['tablename'] == 'faq'): ?>
  <h2>FAQS</h2>
<?php endif; ?>

<?php endforeach; ?>

And if you want to only show the header if it changes, you can throw this into an if block that checks if tablename has changed, something like this:

<?php $previous_table = ''; ?>
<?php foreach ($searchRows as $record): ?>

<?php if ($previous_table != $record['tablename']): ?>
  <?php if ($record['tablename'] == 'family_stories'): ?>
    <h2>STORIES</h2>
  <?php elseif ($record['tablename'] == 'faq'): ?>
    <h2>FAQS</h2>
  <?php endif; ?>
  
  <?php $previous_table = $record['tablename']; ?>
<?php endif; ?>

<?php endforeach; ?>

The above code is untested - you may need to modify to get it to fit your code. Let me know if you have any other questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

By gkornbluth - July 9, 2020 - edited: July 9, 2020

Hey Daniel,

Thanks for the education. 

This worked perfectly and I've added the code to my post above.

Best,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php