Website Search - with images

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

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

Re: [ryanGT] Website Search - with images

By Jason - October 20, 2011

Hi,

First thing we'll need to do is remove the line:

showme($images);

I accidentally left this in from when I was testing the code. I've removed it from the previous posts.

Next, this code produces an array of image records in the variable $images, So the code <?php echo $images; ?> would most likely just print the word "Array" to the screen. You'll need to loop through this variable to output individual images.

For example:

<?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 foreach ($images as $image): ?>
<img src = "<?php echo $image['thumbUrlPath'];?>" width = "<?php echo $image['thumbWidth'];?>" height = <?php echo $image['thumbHeight'];?> alt = "<?php echo $image['info1'];?>" />
<?php endforeach ?>


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/