Search Form - Grab Options from Database

10 posts by 2 authors in: Forums > CMS Builder
Last Post: April 17, 2012   (RSS)

By theclicklab - April 15, 2012

Hi there,

How do we grab options from a database for generating a search form?

E.g.

Field Type (List) - checkboxes (multi value):

A|Option A
B|Optin B
C|Option C

Search form:

<select name="option_match">
<option value="A">Option A</option>
<option value="B">Option B</option>
<option value="C">Option C</option>
</select>

Thanks

Re: [theclicklab] Search Form - Grab Options from Database

By (Deleted User) - April 16, 2012

Hi Jan,

Here's an option for getting a preset selection list and outputting the html for the form:
$schemaFields = getSchemaFields($tablename);
foreach ($schemaFields as $fieldname => $fieldSchema) {
if ( $fieldName == $desiredFieldName ) {
$selectBoxHtml = getSelectOptionsFromSchema($selectedValue,$fieldSchema,$showEmptyOptionFirst);
}
}
echo $selectBoxHtml;


First we get the fieldSchema for the desired field from the table, pass that into the getSelectOptionsFromSchema function which builds builds the selectbox and returns the html, then we output the html.

The arguments for getSelectOptionsFromSchema are the selectedValue (whatever the currently selected value is), $fieldSchema (passed from getSchemaFields) and $showEmptyOptionFirst (either true or false).

Let me know if this helps,

Tom

Re: [Tom P] Search Form - Grab Options from Database

By theclicklab - April 16, 2012

Hi Tom,

Have replaced the tablename and field in the code but I'm getting the following error:

Undefined variable: product_database in ... on line 63 loadSchema: no tableName specified!

<?php
list($product_databaseRecords, $product_databaseMetaData) = getRecords(array(
'tableName' => 'product_database',
));

$schemaFields = getSchemaFields($product_database);
foreach ($schemaFields as $fieldname => $fieldSchema) {
if ( $fieldName == $infusion ) {
$selectBoxHtml = getSelectOptionsFromSchema($selectedValue,$fieldSchema,$showEmptyOptionFirst);
}
}
echo $selectBoxHtml;
?>


thanks

Re: [theclicklab] Search Form - Grab Options from Database

By (Deleted User) - April 16, 2012

Hi Jan,

In the getSchemaFields function you've got "$product_database" which refers to a variable named $product_database where you want to use 'product_database' (quotes, no dollar sign) to refer to a string 'product_database' (the name of the table).

So it looks like this:
Code

<?php
list($product_databaseRecords, $product_databaseMetaData) = getRecords(array(
'tableName' => 'product_database',
));

$schemaFields = getSchemaFields('product_database');
foreach ($schemaFields as $fieldname => $fieldSchema) {
if ( $fieldName == $infusion ) {
$selectBoxHtml = getSelectOptionsFromSchema($selectedValue,$fieldSchema,$showEmptyOptionFirst);
}
}
echo $selectBoxHtml;
?>


Let me know if this helps,

Tom

Re: [Tom P] Search Form - Grab Options from Database

By theclicklab - April 16, 2012

Hi Tom,

That doesn't work, I'm getting this:

Notice: Undefined variable: infusion in ... on line 63
Notice: Undefined variable: fieldName in ... on line 63
Notice: Undefined variable: selectedValue in ... on line 64
Notice: Undefined variable: showEmptyOptionFirst in ... on line 64 Unknown optionsType ''!

thanks for your help on this

Re: [theclicklab] Search Form - Grab Options from Database

By theclicklab - April 16, 2012

One last thing...

To make this perfect the only thing I need to do now is bring in the selected value from the url...

$selectedValue = @$_GET["fieldname"];

Not quite sure how to do that, any suggestions?

Many thanks

Re: [theclicklab] Search Form - Grab Options from Database

By (Deleted User) - April 17, 2012

Hi Jan,

How is the field name being passed into the URL? Normally, cmsb generated links will have the record number at the end so you can use getLastNumberInUrl() (if the record number is the last number in the url).

If you could provide an example url (and let me know hos it's generated) it'll help us get the right solution.

Thanks,

Tom

Re: [Tom P] Search Form - Grab Options from Database

By theclicklab - April 17, 2012

Hi Tom,

here you go: http://www.purefiji.dreamhosters.com/extranet/database/list.php?name_query=&range_match=P&category_match=1&infusion_match=C&submit=Search

So I am trying to get the search form in the left column to have the options pre-selected based on the values in the url above

Thanks again for all you help :)

Re: [theclicklab] Search Form - Grab Options from Database

By (Deleted User) - April 17, 2012

Hi Jan,

If you're checking after the search has been submitted, you can get the data from the url by checking the $_REQUEST array, for example to get the value for the category you would check $_REQUEST['category_match'].

Some of those keys won't have a value, so remember to allow for that.

Let me know if this helps,

Tom