Display results from different sections

14 posts by 2 authors in: Forums > CMS Builder
Last Post: October 27, 2010   (RSS)

Re: [degreesnorth] Display results from different sections

By Chris - October 6, 2010 - edited: October 6, 2010

Hi degreesnorth,

To find records, you're going to need a getRecords() call for each section you want to search. For example:

<?php
$keyword = @$_REQUEST['keyword'];
$like_keyword = "LIKE '%" . mysql_escape($keyword) . "%'";

list ($tours_summary_records) = getRecords(array(
'tableName' => 'tours_summary',
'where' => "title $like_keyword OR content $like_keyword", // the fields to search for the keyword
));

list ($country_records) = getRecords(array(
'tableName' => 'country',
'where' => "title $like_keyword OR content $like_keyword", // the fields to search for the keyword
));
?>


Then it's a question of how you want to present the information to the user. Many search engines do this in different ways, but a common approach is to show the first few results grouped together for each type of search. (To limit the number of results returned, add 'limit'=>5 to the above getRecords() calls.) For example:

<?php if (!empty($tours_summary_records)): ?>
<h2>Tour Results</h2>
<?php foreach ($tours_summary_records as $record): ?>
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a><br />
<?php endforeach ?>
<?php endif ?>

<?php if (!empty($country_records)): ?>
<h2>Country Results</h2>
<?php foreach ($country_records as $record): ?>
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a><br />
<?php endforeach ?>
<?php endif ?>


Finally, you'll probably want to include a link to view more search results for a specific section (if and only if there are more results available.) If you need help with this, please just ask. :)

Does this help get you started? Please let me know if you have any questions.
All the best,
Chris

Re: [chris] Display results from different sections

By degreesnorth - October 7, 2010

Hi Chris

This is probably a really stupid question, but where do I determine the search in the first place? For example, how (or where) do I specify that I want the Egypt results? Do I change the keyword to Egypt and repeat the same for every country (that is, change the keyword). If so, can this string of code all be placed on the same page (ie, if I have 15 countries, do I just repeat the replacement keyword for each country?)

Then, what's the likely URL that gets displayed so I can find it? For example, domainname.com.au/tours_summary.php?...what comes here so I can find the page?

Thanks

Re: [degreesnorth] Display results from different sections

By Chris - October 7, 2010

Hi degreesnorth,

I'm thinking now that I may have misunderstood what you're after. The example I posted above was for a search results page where a user has typed in a search (e.g. "egypt") and you want to display results from several sections which match what they've typed in. An example URL might be search.php?keyword=egypt . This would presumably be done with a <FORM>.

Can you provide some examples to show what it is that you want to accomplish?
All the best,
Chris

Re: [chris] Display results from different sections

By degreesnorth - October 20, 2010

Hi Chris

I somehow missed your reply. I have attached the example of the search box of what I am trying to achieve. There is no "keyword" search, rather, a list of drop downs which I'd prefer to be populated by the database (these are database fields on the tours, escorted tours, polar tours etc section editors), and when they do their selection of say, country, number of tour nights, etc, it gives the visitor a summary page of tours which match their search criteria. I hope that makes sense. Is that possible?

Thanks
Attachments:

search-box.jpg 27K

Re: [degreesnorth] Display results from different sections

By Chris - October 20, 2010

Hi degreesnorth,

Yes, that makes sense and should be possible. Doing this is fairly complicated and may be draining on server resources (since you're dealing with multiple fields and multiple tables). Here's an example of how this might be done:

<?php
$tableNames = array('tours', 'escorted_tours', 'polar_tours');
$fieldNames = array('countries', 'number_of_rooms');
$content = array();
foreach ($fieldNames as $fieldName) {
$content[$fieldName] = array();
}
foreach ($tableNames as $tableName) {
foreach ($fieldNames as $fieldName) {
$new_entries = array_pluck(mysql_query_fetch_all_array("SELECT $fieldName FROM `{$TABLE_PREFIX}$tableName` GROUP BY $fieldName"), 0);
$content[$fieldName] = array_merge($content[$fieldName], $new_entries);
}
}
foreach ($fieldNames as $fieldName) {
$content[$fieldName] = array_unique($content[$fieldName]);
sort($content[$fieldName]);
}
?>
<select name="country">
<?php foreach($content['countries'] as $value): ?>
<option><?php echo htmlspecialchars($value) ?></option>
<?php endforeach ?>
</select>

<select name="number_of_rooms">
<?php foreach($content['number_of_rooms'] as $value): ?>
<option><?php echo htmlspecialchars($value) ?></option>
<?php endforeach ?>
</select>


A potentially better solution would be to create separate Multi Record sections for each of your dropdowns and have your other sections use List Fields to select records in these new sections. It would then be very easy and fast to simply list each of these new sections into <SELECT />s.

I hope this helps. Please let me know if you have any questions.
All the best,
Chris

Re: [chris] Display results from different sections

By degreesnorth - October 20, 2010

Hi Chris

Could you clarify what you mean about multi records sections? Are you inferring that I should create a multi record for say, the country list? Sorry, just wanted to explore the "easiest" option that provides the quickest result for the visitor.

Thanks

Re: [degreesnorth] Display results from different sections

By Chris - October 20, 2010

Yes, exactly. You could create Multi Record sections to drive each of your List Fields.

Does that help?
All the best,
Chris

Re: [degreesnorth] Display results from different sections

By degreesnorth - October 20, 2010

Hi Chris
Ok, I get the section about the Multi records, which I've now set up and it works (whew!). So, that leaves me with creating the search box. I have the following multi records:
1) country_list, where the country name is called "title"
2) type_of_holiday, where the holiday type option is called "title"
3) number of days - and open text field, but I want to limit it to grouped results, ie, 1 to 5 days, 6 to 10 days, etc

I've read the various posts and your online documentation, but any help with starting the code off would be wonderful. IN PARTICULAR, I want the results to be displayed to this template page: http://acac6262.staging-zeus.netregistry.net/tours_summary.php but can't seem to find the documentation on what I'm supposed to do? Would really appreciate your guidance.

Many thanks in advance.

Re: [degreesnorth] Display results from different sections

By Chris - October 22, 2010

Hi degreesnorth,

You can generate these dropdowns using the CMS Builder getSelectOptions() function, and you can get a list of fields by using getRecords() and array_pluck(), like so:

<select name="country">
<?php
// load records
list($country_listRecords,) = getRecords(array(
'tableName' => 'country_list',
'allowSearch' => false,
));
$titles = array_pluck($country_listRecords, 'title');
echo getSelectOptions(@$_REQUEST['country'], $titles);
?>
</select>

<select name="type_of_holiday">
<?php
// load records
list($type_of_holidayRecords,) = getRecords(array(
'tableName' => 'type_of_holiday',
'allowSearch' => false,
));
$titles = array_pluck($type_of_holidayRecords, 'title');
echo getSelectOptions(@$_REQUEST['type_of_holiday'], $titles);
?>
</select>

<select name="number_of_days">
<?php
$values = array('1 to 5 days', '6 to 10 days', 'etc');
echo getSelectOptions(@$_REQUEST['number_of_days'], $values);
?>
</select>


I'd put your search box in a separate file. You can then include it on your tours_summary.php page and wherever else you might want it.

Does that help? Please let me know if you have any questions.
All the best,
Chris