Including upload info fields in multisearch results

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

By gkornbluth - July 4

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

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
PHP Programmer
interactivetools.com

By gkornbluth - July 8

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 daniel - July 8

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
PHP Programmer
interactivetools.com

By gkornbluth - July 8 - edited: July 9

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 - edited: July 9

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 - edited: July 9

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
PHP Programmer
interactivetools.com

By gkornbluth - July 9 - edited: July 9

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

By gkornbluth - July 11 - edited: July 12

Hi Daniel,

I feel like I’m beginning to taking advantage or your expertise, but I’ve come up with 2 hopefully easy questions regarding the showing of images that I hope you're willing to help with.

I’ve set up an image viewer (RoyalSlider) to show a slide show of all the search result images on the search pages and that works pretty well.

However, I’m having trouble:

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

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.

Hope this is a quick fix for you.

Thanks,

Jerry Kornbluth

Here’s my latest effort that still throws unknown index errors. Same errors without the _info(n) underscores:

At the top of the page:

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

$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['help_pages'] = array(
'viewerUrl' => 'help.php',
'titleField' => 'title',
'summaryField' => 'title',
'searchFields' => array('title', 'content'),

);

if($CURRENT_USER['isAdmin'])
{ $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',
'info1Field' => 'info1',
'info2Field' => 'info2',
'info3Field' => 'info3',
'info4Field' => 'info4',
'info5Field' => 'info5',
'searchFields' => array('filePath', 'info1','info2', 'info3', 'info4', 'info5'),

);

list($searchRows, $searchDetails) = searchMultipleTables($searchTables, $searchOptions);
?>
And in the Display Record List code:

<!-- STEP2: Display Record List -->
<hr align="left" style="width:50%; color:#FFF">
<?php $previous_table = ''; ?>
<?php $count = '' ?>
<?php foreach ($searchRows as $record): ?>

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

<?php if ($record['tablename'] == 'uploads'): ?>
<?php $count++ ?>
<?php if ($count ==1 ):?><div class="royalSlider rsDefault"><?php endif ?>

<div > <a class="rsImg " href="http://pollick.net/cmsAdmin/uploads/<?php echo $record['_title'] ?>">
<?PHP $record['_info1'] = preg_replace("/[\"]/", "''", $record['_info1'] ); ?>
<?PHP $record['_info2'] = preg_replace("/[\"]/", "''", $record['_info2'] ); ?>
<?PHP $record['_info3'] = preg_replace("/[\"]/", "''", $record['_info3'] ); ?>
<?PHP $record['_info4'] = preg_replace("/[\"]/", "''", $record['_info4'] ); ?>
<?PHP $record['_info5'] = preg_replace("/[\"]/", "''", $record['_info5'] ); ?>
<br><br>
<div align='center' style='font-size:1.4em; vertical-align:top; text-align:left;' class='text_font' ><?php echo($record['_info1']) ?> <?php echo($record['_info2']) ?> <?php echo($record['_info3']) ?> <?php echo($record['_info4']) ?> <?php echo($record['_info5']) ?></div>
</a>
<div class="rsTmb" ><img class="rsTmb" src="http://pollick.net/cmsAdmin/uploads/<?php echo $record['_summary'] ?>"/></div>
</div>
<?php else:?> <a href="<?php if ($record['tablename'] == 'help_pages'): ?>help.php?#<?php echo $record['num']?><?php else : ?><?php echo $record['_link'] ?><?php endif ?>"><span class="navigation_font" style="font-size:1.25em; 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:1.2em;"><?php echo $record['_summary'] ?></span><br/>
<?php else: ?>
<span class="navigation_font" style="font-size:1.2em;">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 endif ?>
<?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
Attachments:

searchH.php 13K