Website Search - with images

4 posts by 2 authors in: Forums > CMS Builder
Last Post: October 20, 2011   (RSS)

By ryanGT - October 19, 2011

Hi all,

I have been searching the forums, and cant find a solution for the problem that i am having.

I have created a search page for the website that will return results from three tables. but i was wondering how i go about getting the images from the uploads table to the relative article.

i have created a working page search, but just need to tie in the uploads table. i have the following as a starting block:
$searchOptions = array();
$searchOptions['keywords'] = @$_REQUEST['s'];
$searchOptions['perPage'] = "14";
$searchOptions['debugSql'] = "1";

$searchTables = array();
$searchTables['features'] = array(
'viewerUrl' => '/features/article.php',
'titleField' => 'title',
'summaryField' => 'content',
'searchFields' => array('title','content'),
'field1' => 'updatedDate'
);
$searchTables['reviews'] = array(
'viewerUrl' => '/reviews/article.php',
'titleField' => 'title',
'summaryField' => 'content',
'searchFields' => array('title','content'),
'field1' => 'updatedDate'
);
$searchTables['news'] = array(
'viewerUrl' => '/news/event.php',
'titleField' => 'title',
'summaryField' => 'content',
'searchFields' => array('title','content'),
'field1' => 'updatedDate'
);
list($searchRows, $searchDetails) = searchMultipleTables($searchTables, $searchOptions);
?>


Any help would be greatly appreciated.

Many thanks

Ryan

Re: [ryanGT] Website Search - with images

By Jason - October 19, 2011 - edited: October 20, 2011

Hi Ryan,

What you will need to do is query the uploads table for each record as you are looping through to output them.

First, we need to create an array where we store the name of the upload field for each section we're returning.

For example:

<?php
$searchOptions = array();
$searchOptions['keywords'] = @$_REQUEST['s'];
$searchOptions['perPage'] = "14";
$searchOptions['debugSql'] = "1";

$searchTables = array();
$searchTables['features'] = array(
'viewerUrl' => '/features/article.php',
'titleField' => 'title',
'summaryField' => 'content',
'searchFields' => array('title','content'),
'field1' => 'updatedDate'
);
$searchTables['reviews'] = array(
'viewerUrl' => '/reviews/article.php',
'titleField' => 'title',
'summaryField' => 'content',
'searchFields' => array('title','content'),
'field1' => 'updatedDate'
);
$searchTables['news'] = array(
'viewerUrl' => '/news/event.php',
'titleField' => 'title',
'summaryField' => 'content',
'searchFields' => array('title','content'),
'field1' => 'updatedDate'
);
list($searchRows, $searchDetails) = searchMultipleTables($searchTables, $searchOptions);

$tableNameToUploadField = array('features' => 'images', 'reviews' => 'image', 'news' => 'photos');

?>


So here, we're saying that the features table has a field called images, reviews has a field called image and news has a field called photos.

Next, when we are outputting our records, we can query the database to get all the image records associated with that field in that record:

<!-- STEP2: Display Record List -->
<?php foreach ($searchRows as $record): ?>

<?php
$images = array();

if (array_key_exists($record['tablename'], $tableNameToUploadField)) {
$query = "SELECT * FROM `{$TABLE_PREFIX}uploads` WHERE tableName = '".mysql_escape($record['tablename'])."'
AND fieldName = '".mysql_escape($tableNameToUploadField[$record['tablename']])."'
AND recordNum = '".intval($record['num'])."'";

$images = mysql_query_fetch_all_assoc($query);

}

?>

<a href="<?php echo $record['_link'] ?>"><?php echo $record['_title'] ?></a><br/>
<?php if ($record['_summary']): ?>
<?php echo $record['_summary'] ?><br/>
<?php else: ?>
No description available for page.<br/>
<?php endif ?>
<a href="<?php echo $record['_link'] ?>" style="color: #008000"><?php echo $record['_link'] ?></a><br/><br/>
<?php endforeach ?>
<!-- /STEP2: Display Record List -->


Now inside that loop you have access to an $images variable that you can output where ever you like.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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

Re: [Jason] Website Search - with images

By ryanGT - October 20, 2011 - edited: October 20, 2011

Hi Jason,

excellent, thats certainly a step in the right direction.

However i am getting some very strange results.

The image that is being returned is the same image for all the records, and the image that is being displayed isn't in the searchable tables.

not too sure whats happening to be honest.

I have attached my viewing code, to see if it is something in there.

<?php $counter=0; ?>
<?php foreach ($searchRows as $record): ?>

<?php $counter++; ?> <?php
$images = array();

if (array_key_exists($record['tablename'], $tableNameToUploadField)) {
$query = "SELECT * FROM `{$TABLE_PREFIX}uploads` WHERE tableName = '".mysql_escape($record['tablename'])."'
AND fieldName = '".mysql_escape($tableNameToUploadField[$record['tablename']])."'
AND recordNum = '".intval($record['num'])."'";

$images = mysql_query_fetch_all_assoc($query);

}
?>
<?php echo $images?>
<?php if ($counter % 2):?>
<div class="halfbox" style="margin-right:20px; background: center no-repeat url(<?php echo $upload['urlPath'] ?>);">
<a href="<?php echo $record['_link'] ?>"><img src="/images/blank.gif" width="280" height="250" border="0" /></a>
<div class="info">
<h3 class="headline"><a href="<?php echo $record['_link'] ?>"><?php echo $record['_title'] ?></a></h3>
</div>
</div>
<?php else:?>
<div class="halfbox" style="background: center no-repeat url(<?php echo $upload['thumbUrlPath'] ?>);">
<a href="<?php echo $record['_link'] ?>"><img src="/images/blank.gif" width="280" height="250" border="0" /></a>
<div class="info">
<h3 class="headline"><a href="<?php echo $record['_link'] ?>"><?php echo $record['_title'] ?></a></h3>
</div>
</div>
<?php endif;?>
<?php endforeach ?>


Again your help is greatly appreciated.

Many thanks