Search Display from DropDown

15 posts by 4 authors in: Forums > CMS Builder
Last Post: October 11, 2010   (RSS)

Re: [Liz1001] Search Display from DropDown

By Damon - December 3, 2009

Hi Liz,

I think this is a quick fix. [:)]

At the top of the code, change this:

'allowSearch' => '0',

to this:

'allowSearch' => '1',

Give that a try and let me know if that works and if you have any other questions.
Cheers,
Damon Edis - interactivetools.com

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

Re: [Damon] Search Display from DropDown

By Liz1001 - December 3, 2009

Hi Damon -

I changed the allowSearch to 1 in both places and it still displays all the listings.

I have a feeling that maybe I am not sending the data from the drop down selection to the form to tell it to only display the listings in the category picked.

Liz

Re: [Liz1001] Search Display from DropDown

By Damon - December 3, 2009

Hi Liz,

Typing this into the browser returns search results:
http://www.mormonbusiness.com/search_page.php?business_category=Orthodontia

You are using <select name="business_listing"> but need to use <select name="business_category">

Here is the updated code that should work:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

require_once "/home/content/m/o/r/mormonbusiness/html/cmsAdmin/lib/viewer_functions.php";

list($business_listingRecords, $business_listingMetaData) = getRecords(array(
'tableName' => 'business_listing',
'perPage' => '12',
));


?>

<!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; }
.instructions { border: 3px solid #000; background-color: #EEE; padding: 10px; text-align: left; margin: 25px}
</style>
</head>
<body>

<!--Creates categories into form from database-->
<?php
list($business_categoryRecords,) = getRecords(array(
'tableName' => 'business_listing',
'loadUploads' => '0',
'allowSearch' => '0',
));
?>


<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>" >
<select name="business_category">
<option value="">select</option>
<?php foreach($business_categoryRecords as $record): ?>
<option><?php echo htmlspecialchars($record['business_category']) ?></option>
<?php endforeach ?>
</select>
<br />
<br />
<input name="Submit" type="submit" value="Search for Business" />
</form>

<!-- Now Display the listings from the Category that was chosen-->
<h1>Businesses</h1>
<?php foreach ($business_listingRecords as $record): ?>
<?php echo $record['business_name'] ?><br/>

<?php endforeach; ?>

</body>
</html>


Let me know if this works for you.
Cheers,
Damon Edis - interactivetools.com

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

Re: [Damon] Search Display from DropDown

By Liz1001 - December 3, 2009

Perfect! I knew that it has to be something simple I could just not see the problem because I had been looking at it too long, how obvious! Thanks, I really appreciate the second set of eyes.

Re: [Liz1001] Search Display from DropDown

I've got a similar issue.
Sorry for the thread revival but it is relative.

I borrowed the array that pulls the select options but one thing I noticed is that it only pulls options that have entries in for that category. (we're using State) Which actually turns out to be pretty cool because if your state is not on there, then you can't pick it. So its really obvious.
So cool but.....

I have a few issues with this search thing:

One:
I want the list code to be empty until I conduct a search.
What happens now in this code below the search form code:

<?php foreach ($find_a_retailerRecords as $record): ?>
<p><?php echo $record['title'] ?><br />
<?php echo $record['address'] ?><br />
<?php echo $record['city_state_zip'] ?><br />
<?php echo $record['phone'] ?><br />
<a href="<?php echo $record['web_site'] ?>" target="_blank"><?php echo $record['web_site'] ?></a>


It shows all results on page load. I don't want any until a search is performed.
If I can't have it show none until a search is conducted then I guess I can redo the text on the page to make the 'search' more like a filter instead of a true search.

BTW - I want the search and results on the same page, which I am doing now.

two
Second problem is I want to have a 'clear search results' so the page is reset without a reload. It would be cool if it was more like a postback.

Any Thoughts guys!?

Re: [sublmnl] Search Display from DropDown

By Chris - September 10, 2010 - edited: September 10, 2010

Hi sublmnl,

One way to check if nothing has been searched for yet is to examine the size of the $_REQUEST array. If it's zero, then no GET or POST keys have been set and you can assume that no search has been done. This won't work for users clicking your "Search" button without filling out any fields, but it is very simple and just might work for you:

if (sizeof($_REQUEST) == 0) {
$find_a_retailerRecords = array();
}
else {
list($find_a_retailerRecords,) = getRecords(array(
'tableName' => 'find_a_retailer',
));
}


If you need a more robust solution which deals with blank forms being submitted, let me know.

Your second question would require some JavaScript. While we don't normally provide free JavaScript support, I think I can point you in the right direction: if you're using jQuery, add an ID attribute to your search results DIV, and use $('#searchResultsDivId').html('Ready to search!') to blank it. You can also use $('#myform')[0].reset() to reset your form.

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

Re: [chris] Search Display from DropDown

awesome thanks but now the search drop down select options won't populate.

This is what I put above the <head>


// load records
list($find_a_retailerRecords, $find_a_retailerMetaData) = getRecords(array(
'tableName' => 'find_a_retailer',
'allowSearch' => '1',
'loadUploads' => '0',
'orderBy' => 'city_state_zip',

));

list($find_a_retailerRecords,) = getRecords(array(
'tableName' => 'find_a_retailer',
'loadUploads' => '0',
'allowSearch' => '1',
));

if (sizeof($_REQUEST) == 0) {
$find_a_retailerRecords = array();
}
else {
list($find_a_retailerRecords,) = getRecords(array(
'tableName' => 'find_a_retailer',
));
}


and this is the <form>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>" >
<select name="state">
<option value="">Select A State</option>
<?php foreach($find_a_retailerRecords as $record): ?>
<option><?php echo htmlspecialchars($record['state']) ?></option>
<?php endforeach ?>
</select>


<input type="submit" name="submit" value="Search">
</form>


I could go back to having the options hard coded, but I kinda liked having it populate based on what Retailers were entered in the CMS.

And thanks for the JS idea. I know where to go from there.

Re: [sublmnl] Search Display from DropDown

By sublmnl - September 12, 2010 - edited: September 12, 2010

Well one more thing I just noticed.

this code:

<?php foreach($find_a_retailerRecords as $record): ?>
<option><?php echo htmlspecialchars($record['state']) ?></option>
<?php endforeach ?>


Doesn't group multiple listings in the same state.
Each location is getting broken out as a different selection in the list.
So for example you end up with a drop down that looks like the atachment.
Attachments:

picture-3.png 31K

Re: [sublmnl] Search Display from DropDown

By Chris - September 12, 2010 - edited: September 12, 2010

Hi sublmnl,

You're overwriting your $find_a_retailerRecords variable here. I think you want to use two variables:

// load records
list($all_find_a_retailerRecords, $all_find_a_retailerMetaData) = getRecords(array(
'tableName' => 'find_a_retailer',
'allowSearch' => '1',
'loadUploads' => '0',
'orderBy' => 'state',
));

if (sizeof($_REQUEST) == 0) {
$find_a_retailerRecords = array();
}
else {
list($find_a_retailerRecords,) = getRecords(array(
'tableName' => 'find_a_retailer',
));
}


Then, when you're going through your $all_find_a_retailerRecords array to list your states, you're going to want to skip any states that you've seen before. If you sorting them by state, as above, you can just skip any record that has the same state as the last record:

<?php $lastState = null; ?>
<?php foreach($all_find_a_retailerRecords as $record): ?>
<?php if ($lastState == $record['state']) { continue; } else { $lastState = $record['state']; } ?>
<option><?php echo htmlspecialchars($record['state']) ?></option>
<?php endforeach ?>


Please note that this technique of loading absolutely all of your records to generate your downdown options like this might get a little slow if you end up with lots of records.

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