Search Question

13 posts by 2 authors in: Forums > CMS Builder
Last Post: August 25, 2011   (RSS)

Hi All,

I’ve got 2 hopefully simple questions about a search that I'm setting up on a list viewer.

1) How would I make the following searches cumulative? (If a main category is selected the sub categories are chosen only from the selected main category)

2) How can I automatically clear the previous search query on page refresh so that all records are again shown?

Thanks,

Jerry Kornbluth

<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">

<select name = "event_keyword" >
<option value="">Please Choose a Category</option>
<?php foreach (getListOptions('master_subscription_pages', 'event') as $value => $label): ?>
<option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['event']);?>>
<?php echo $label; ?></option>
<?php endforeach ?>
</select>

<input type="submit" name="submit" value="Search" >
</form>
<br />
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">

<select name = "event_sub_category_keyword" >
<option value="">Please Choose a Sub Category</option>
<?php foreach (getListOptions('master_subscription_pages', 'event_sub_category') as $value => $label): ?>
<option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['eventsub_category']);?>>
<?php echo $label; ?></option>
<?php endforeach ?>
</select>

<input type="submit" name="submit" value="Search" >
</form>
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Search Question

By Jason - August 18, 2011

Hi Jerry,

So for your first issue, you want to make it so that when a selection is made from "event_keyword", "event_sub_category_keyword" is populated only with the subcategories of the selected "event_keyword". Is that right?

If so, there are a couple of options. First, you can use jquery to update "event_sub_category_keyword", being triggered on the onchange event of "event_category". This is probably the most typical technique.

The other option would be to hide the second select box and have the onchange event of the first select box submit the form to itself and get all the sub category records. This solution is a little more awkward.

Either way, you won't be able to use the getListOptions() function to get your sub categories. Assuming that the section being used for "event" and "event_sub_category" in your master_subscription_pages section is a category menu, you can use the 'rootCategoryNum' option of getCategories to return only the sub categories of a given category number. For example:



list($subCategories, ) = getCategories(array(
'tableName' => 'category',
'rootCategoryNum' => "'".intval(@$_REQUEST['event_keyword'])."'",
));


You would then use $subCategories to populated your second select box.

For the second issue, probably the best option would be to just have a "Reset" button that just links back to the current page. You don't want to do an actual page refresh, because this will simply re-submit your form and execute your query again.

Hope this helps get you started.
---------------------------------------------------
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 Question

Hi Jason,

Thanks for looking at this.

The reset button idea worked.

For the other, jquery seems logical but I must admit I don't even know where to start.

Could you (or someone who has done this) offer an example of the code required to apply the concept to my situation?

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Search Question

By Jason - August 19, 2011

Hi Jerry,

Here is a really simple example from a project I recently did that may help you get started. I had 2 select boxes: 1 for make, and 1 for model. So the model list was dependent on the make list.

Here was the HTML:

<td width="123">
<select name="make" id="make" class="effect" onchange="updateModel();">
<option value = "">Select Make</option>
<option value = "">All Makes</option>
<?php foreach (mysql_select('makes') as $make): ?>
<option value = "<?php echo $make['num'];?>" ><?php echo $make['make'];?></option>
<?php endforeach ?>
</select>
</td>

<td width="96">
<select name="model" class="effect" id="model">
<option value = "" selected="selected">All Models</option>
</select>
</td>


So when the page first loads, the model list is empty. The onchange event of the make list calls a jquery function called "updateModel()". This is stored in the attached .js file. This function makes a jquery call to an updateModel.php page (attached) that loads the models associated with the selected make.

Hope this helps get you started
---------------------------------------------------
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 Question

Thank you Jason.

Looks like this weekend's project.

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [Jason] Search Question

By gkornbluth - August 19, 2011 - edited: August 19, 2011

Hi Jason,

I just couldn't wait for the weekend.

Here’s what I came up with that seems to (almost) work, using 2 multi-record sections with 2 list fields and showing only those records that match the “Make” and “Model” criteria selected. (It still lists an option for each record created)

The result is at http://96.0.19.10/2-level-search2.php

I know I’m probably missing something simple, but I can’t seem to modify the code to do a “Make” and “Model” search from a single multi-record section with 2 list fields. (and show only one set of options in each search field)

Thanks,

Jerry Kornbluth

______________________

Here’s what I’ve done so far:

Created 2 multi-record sections, one called “Models” and one called “Makes”

Each section has 2 list fields, one called “Make” and one called “Model”

Each section also has a text field (called “text”) that spells out which make and model was selected for that particular record, IE: “Make 1 and Model 1 from Models” or Make1 Model 2 from Makes”.

At the top of the list viewer I used:
<?php

require_once("cmsAdmin/lib/viewer_functions.php");


list($modelsRecords, $modelsMetaData) = getRecords(array(
'tableName' => 'models',
));


list($makesRecords, $makesMetaData) = getRecords(array(
'tableName' => 'makes',
));

$where = "TRUE";
if (@$_REQUEST['make']) {
$where = "make = '".intval(@$_REQUEST['make'])."'";
}

$models = mysql_select('models', $where);
?>


In the head:
<script src="../Scripts/updateModel.js"></script>

And in the body:
<table>
<tr>
<td >
<form> <select name="make" class="effect" id="make" onChange="updateModel();">
<option value = "">Select Make</option>
<option value = "">All Makes</option>
<?php foreach (mysql_select('makes') as $make): ?>
<option value = "<?php echo $make['make'];?>" ><?php echo $make['make'];?></option>
<?php endforeach ?>
</select>
<br /><br />
<select name="model" class="effect" id="model">
<option value = "">Select Model</option>
<option value = "">All Models</option>
<?php foreach ($models as $model): ?>
<option value = "<?php echo $model['model'];?>"><?php echo $model['model'];?></option>
<?php endforeach ?>

</select>
<br /><br />
<input type="submit" name="submit" value="Search" >
</form>
<FORM ACTION="">
<br /><br />
<INPUT TYPE="submit" VALUE="Cancel Filters">
</FORM>
</td>
</tr><tr><td><br />
<?php foreach ($modelsRecords as $record): ?>
<?php echo $record['text'] ?><br />
<?php endforeach ?>
<hr />
<?php foreach ($makesRecords as $record): ?>
<?php echo $record['text'] ?><br />
<?php endforeach ?>
</td></tr>
</table>
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Search Question

By Jason - August 22, 2011

Hi Jerry,

The first thing I noticed on your page is that there is a jquery error when you select a make. You need to include a version of jquery in the head section before the updateModel.js file. Something like this:


<script type = "text/javascript" src = "cmsAdmin/3rdParty/jquery/jquery1.4.1.js"></script>

<script src="../Scripts/updateModel.js"></script>


Also all of the code that populates the models select box should be contained in the .php file (updateModel.php in this case) that is called by the jquery function.

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 Question

Hi Jason,

Well, adding the jquery call helped, but I’m still lost, more confused then ever, and after I get this to work I still have no idea how to make this search work for a single multi-record table.

When I remove the:
$where = "TRUE";
if (@$_REQUEST['make']) {
$where = "make = '".intval(@$_REQUEST['make'])."'";
}

$models = mysql_select('models', $where);

from my viewer page, which is what I think you meant, I get an:

Undefined variable: models in /hsphere/local/home/gkornblu/qartguide.com/2-level-search5.php on line 44

Warning: Invalid argument supplied for foreach() in /hsphere/local/home/gkornblu/qartguide.com/2-level-search5.php on line 44

and no values in the “models” search box, except for the options “select model” and “all models”.

I’ve moved the .js to the same directory as the other files, tried adding load records calls to updateModel.php, and tried everything else I can think of, but I haven’t been able to figure out what should be a simple issue.

Any suggestions, code corrections?

Thanks,

Jerry Kornbluth

Here’s the present viewer code...

Top:
<?php

require_once("cmsAdmin/lib/viewer_functions.php");

// load records
list($modelsRecords, $modelsMetaData) = getRecords(array(
'tableName' => 'models',
));

// load records
list($makesRecords, $makesMetaData) = getRecords(array(
'tableName' => 'makes',
));

?>

Head:
<script type = "text/javascript" src = "cmsAdmin/3rdParty/jquery/jquery1.4.1.js"></script>
<script src="updateModel.js"></script>


Body:
<table>
<tr>
<td >
<form> <select name="make" class="effect" id="make" onChange="updateModel();">
<option value = "">Select Make</option>
<option value = "">All Makes</option>
<?php foreach (mysql_select('makes') as $make): ?>
<option value = "<?php echo $make['make'];?>" ><?php echo $make['make'];?></option>
<?php endforeach ?>
</select>

<br /><br />

<select name="model" class="effect" id="model">
<option value = "">Select Model</option>
<option value = "">All Models</option>
<?php foreach ($models as $model): ?>
<option value = "<?php echo $model['model'];?>"><?php echo $model['model'];?></option>
<?php endforeach ?>
</select>
<br /><br />
<input type="submit" name="submit" value="Search" >
</form>

<FORM ACTION="">
<br /><br />
<INPUT TYPE="submit" VALUE="Cancel Filters">
</FORM>
</td>
</tr>
<tr>
<td>
<br />
<?php foreach ($modelsRecords as $record): ?>
<?php echo $record['text'] ?><br />
<?php endforeach ?>
<hr />
<?php foreach ($makesRecords as $record): ?>
<?php echo $record['text'] ?><br />
<?php endforeach ?>
</td>
</tr>
</table>
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Search Question

By Jason - August 25, 2011

Hi Jerry,

Your actually getting close.

The errors are coming from your foreach loop that is "populating" your model select field. Since you took out the query, there is no $models variable to use anymore.

The most common approach is to not populate your select field in the viewer page at all. This seems counter-intuitive, but the reason is when your viewer page loads, there no make has been selected. And if no make has been selected, there is no list of related models to put into the box. Therefore we leave it empty.

The page updateModel.php, which is called by the jquery function when the make select field is changed, replaces all the HTML that is currently on the page with a duplicate that has actually been populated. That's why there is another select field being output in updateModel.php.

Hope this starts to make sense. Once you wrap your mind around it, it does start to get easier
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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