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: [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/