Search from multiple fields in form

6 posts by 2 authors in: Forums > CMS Builder
Last Post: December 2, 2010   (RSS)

Re: [4cdg] Search from multiple fields in form

By Jason - November 19, 2010

Hi Clay,

You can definitely do this. All you need to do is ensure that each of your form fields has the same name as the field in your table that you're searching against. So, for example, if you have a field in your section called "city" that you wanted to search against, your form field would look like this:

<input type="text" name="city" />

For your second issue, what you can do is use getRecords to select all the users from the accounts table and use this to populate a <select> box. The field in your table that you would want to search against is called createdByUserNum.

It would look something like this.

1) Get all the users from the database:
list($accountRecords,$accountMetaData)=getRecords(array(
'tableName' => 'accounts',
'allowSearch' => false,
));


Then use the result to populate your select box:

<select name="createdByUserNum">
<option value="">-Select-</option>
<?php foreach($accountRecords as $account): ?>
<option value="<?php echo $account['num'];?>"><?php echo $account['fullname'];?></option>
<?php endforeach ?>
</select>


This should get you started. Give this a try and if you're still having a problem, please attach both your search page and your search results page to this thread so I can see the code you're using.

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] Search from multiple fields in form

By 4cdg - November 30, 2010

Jason,

Search seems to be working great now. Just running into one issue. I need to search one more dropdown from the accounts field. it is called msba_region and it is a dropdown with 17 regions that each user chooses. i want to display all listings that are listed by someone in a certain region. How is that code different from the created by code?

here is my page. www.surplusprop.com/search.php

thanks

clay

Re: [4cdg] Search from multiple fields in form

By Jason - December 1, 2010

Hi Clay,

This one is a little more complicated since msba_region is associated with a user and not with a listing.


The first thing you'll need to do in on search.php. You'll need to create a list of all the possible regions from the accounts table like this:
*Note* put this below the query where we select all the user accounts
$regions = array();

foreach($accountRecords as $account){
if($account['msba_region']){
$regions[$account['msba_region']]=$account['msba_region:label'];
}
}


Next, you can output this into your drop down menu for selecting a region. Like this:
<select name="searchRegion">
<option value="">-SELECT-</option>
<?php foreach($regions as $value => $label):?>
<option value="<?php echo $value;?>"><?php echo $label;?></option>
<?php endforeach ?>
</select>


Since we can't get the search for this to happen automatically, we'll need to add some code to our search results page:

Put this BEFORE your getRecords statement on the search results page:

$andWhere = "1";
if(@$_REQUEST['searchRegion']){
$query = "SELECT * FROM `{$TABLE_PREFIX}accounts` WHERE msba_region='".mysql_escape($_REQUEST['searchRegion'])."'";
$users = mysql_query_fetch_all_assoc($query);
$userNums = join(",",array_pluck($users,'num'));
if($userNums){
$andWhere.=" AND createdByUserNum IN($userNums)";
}
}


This selects all the user numbers that have the selected region and ads them to a variable that we can put into our getRecords Query.

Finally, add this to the getRecords query on the search results page:

'where' => $andWhere,

This should take care of it. Give it a try and if you run into any issues, attach search.php and your search results page to this thread so I can take a look at your code.

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] Search from multiple fields in form

By 4cdg - December 2, 2010

jason,

everything is working great except for one thing. The msba region dropdown populates out of order. Kind of like the regions that have them most listings go to the top of the dropdown. I need them to show 1 - 17.

I have attached my search page
Attachments:

search_003.php 18K

Re: [4cdg] Search from multiple fields in form

By Jason - December 2, 2010

Hi,

A sort like this is getting a little more complicated than what we can accomplish in the forum. If you're interested in creating this, please send an email to consulting@interactivetools.com and we can go over some options with you.

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/