Hi tamarteleco,
This is quite a long post - apologies. It's taken from the same ChronoConnection as the sort part, so I've left those lines of code in as well.
The finshed version looks like this.
[attachment=0]03-06-2010 15-53-33.png[/attachment]
You can see that there are three multi-select boxes and a text filter box. You may not need all of these but it's helpful to see how they work together.
The full header box code is here. (Note that at the end there is a function that I wrote to create the select boxes, you could hand code these instead - or copy the code over from a ChronoForm.
<?php
/* ensure that this file is called by another file */
defined('_JEXEC') or die('Restricted access');
include(JPATH_SITE.DS.'components'.DS.'com_chronocontact'.DS.'includes'.DS.'job_list'.DS.'arrays.php');
$filter =& JRequest::getString('filter', '', 'post');
$filter_order =& JRequest::getString('filter_order', '', 'post');
$filter_order_Dir =& JRequest::getString('filter_order_Dir', '', 'post');
$location =& JRequest::getVar('location', array(), 'post');
$salary =& JRequest::getVar('salary', array(), 'post');
$job_type =& JRequest::getVar('job_type', array(), 'post');
$count_rows = 0;
global $count_rows;
//echo '<div>$lists: '.print_r($lists, true).'</div>';
//jimport('joomla.filesystem.file');
JHTML::_('behavior.mootools');
$scripts = $styles = array();
$scripts[] = JURI::base().'components/com_chronoconnectivity/assets/smoothbox.js';
$styles[] = JURI::base().'components/com_chronoconnectivity/assets/smoothbox.css';
$script = "
function clearFilter() {
$('filter').value = '';
$('salary').selectedIndex = -1;
$('location').selectedIndex = -1;
$('job_type').selectedIndex = -1;
$('adminForm').submit();
}
window.addEvent('domready', function() {
$('filter_reset').addEvent('click', clearFilter );
});
";
?>
<form name="adminForm" id="adminForm" method="post" action="index.php?option=com_chronoconnectivity&connectionname=job_listing">
<div style='padding:6px 0;'>
<table style='width:100%;'>
<tr>
<td><?=createSelect('Location', 'location', $location_array, $location )?></td>
<td> </td>
<td><?=createSelect('Salary (max)', 'salary', $salary_array, $salary )?></td>
<td> </td>
<td><?=createSelect('Job type', 'job_type', $type_array, $job_type )?></td>
</tr>
</table>
<input type='text' name='filter' id='filter' value='<?=$filter?>' />
<input type='submit' name='filter_submit' id='filter_submit' value='Filter' />
<input type='button' name='filter_reset' id='filter_reset' value='Clear all' />
<input type='hidden' name='filter_order' id='filter_order' value='<?=$filter_order?>' />
<input type='hidden' name='filter_order_Dir' id'filter_order_Dir' value='<?=$filter_order_Dir?>' />
</div>
<table style='width:100%; border-collapse:collapse;'>
<thead>
<tr>
<th><?=JHTML::_('grid.sort', 'Job title ', 'job_title', @$filter_order_Dir, @$filter_order )?></th>
<th style='max-width:25%;'><?=JHTML::_('grid.sort', 'Location ', 'location', @$filter_order_Dir, @$filter_order )?></th>
<th><?=JHTML::_('grid.sort', 'Salary range ', 'salary_to', @$filter_order_Dir, @$filter_order )?></th>
<th><?=JHTML::_('grid.sort', 'Closing date ', 'closing_date', @$filter_order_Dir, @$filter_order )?></th>
</tr>
</thead>
<tbody>
<?php
$doc =& JFactory::getDocument();
if ( $script ) {
$doc->addScriptDeclaration($script);
}
if ( count($scripts) ) {
foreach ($scripts as $script ) {
$doc->addScript($script);
}
}
if ( count($styles) ) {
foreach ($styles as $style ) {
$doc->addStylesheet($style);
}
}
function createSelect($title='', $name='', $option_array=array(), $values=array() )
{
if ( !is_array($option_array) ) {
return;
}
?>
<div class="form_item">
<div class="form_element cf_slect">
<label class="cf_label" style="width: 150px; font-weight:bold;"><?=$title?></label>
<div class="float_left">
<select name='<?=$name?>[]' id='<?=$name?>' multiple='multiple' size='9' style="width: 150px;">
<?php
$i = 0;
$select_array = array();
foreach ( $option_array as $k => $v ) {
$selected = '';
if ( in_array($k, $values ) ) {
$selected = "selected='selected'";
}
$select_array[] = "<option value='$k' $selected >$v</option>";
}
echo implode('<br />', $select_array);
?>
</select>
</div>
</div>
<div class="cfclear"> </div>
</div>
<?php
}
?>
The code starts by using JRequest to get the variables from the last submission so that the last search values are re-displayed.
Then some scripts are loaded, the smoothbox scripts are used to show the details of a single record. The script snippet is linked to the 'Clear filter' button.
At the start of the form is a table containing the select inputs followed by inputs for the text filter and the Search & Clear buttons.
There is some PHP to load the css & scripts and finally the createSelect() function.
The remaining code is in the WHERE box
<?php
/* ensure that this file is called by another file */
defined('_JEXEC') or die('Restricted access');
$path = JPATH_SITE.DS.'components'.DS.'com_chronocontact'.DS.'includes'.DS.'job_list'.DS;
include($path.'arrays.php');
global $mainframe, $task;
$limit = $mainframe->getUserStateFromRequest("$option.limit", 'limit', 14, 'int');
$limitstart = JRequest::getInt('limitstart');
$filter =& JRequest::getString('filter', '', 'post');
$location =& JRequest::getVar('location', array(), 'post');
$salary =& JRequest::getVar('salary', array(), 'post');
$job_type =& JRequest::getVar('job_type', array(), 'post');
$task =& JRequest::getString('task', '', 'get');
//$mainframe->enqueuemessage('$task: '.print_r($task, true));
$where = $where_filter = $where_salary = array();
if ( $task == 'show_deleted' ) {
$where[] = " ( `published` = 0 OR CAST(`closing_date` AS DATE) <= CURDATE() ) ";
} else {
$where[] = " ( `published` = 1 AND CAST(`closing_date` AS DATE) > CURDATE() ) ";
}
if ( $filter ) {
$where_filter[] = "`job_title` LIKE '%$filter%' ";
$where_filter[] = "`company` LIKE '%$filter%' ";
$where_filter[] = "`job_description` LIKE '%$filter%' ";
$where_filter[] = "`job_ref` LIKE '%$filter%' ";
if ( !empty($where_filter) ) {
$where[] = "( ".implode(' OR ', $where_filter)." ) ";
}
//echo " LIMIT $limitstart, $limit ";
}
if ( count($location) ) {
foreach ( $location as $k => $v ) {
$location[$k] = "'$v'";
}
$where[] = " `location` IN ( ".implode(', ', $location)." ) ";
}
if ( count($salary) ) {
$min = 0;
$where_salary[] = " ( `salary_to` + `salary_from` = 0 ) ";
foreach ( $salary_array as $k => $v ) {
if ( in_array($k, $salary) ) {
$where_salary[] = " ( `salary_to` >= $min AND `salary_to` < $k ) ";
$where_salary[] = " ( `day_rate_to` >= $min/200 AND `day_rate_to` < $k/200 ) ";
}
$min = $k;
}
if ( !empty($where_salary) ) {
$where[] = "( ".implode(' OR ', $where_salary)." ) ";
}
reset($salary_array);
}
if ( count($job_type) ) {
foreach ( $job_type as $k => $v ) {
$job_type[$k] = "'$v'";
}
$where[] = " `job_type` IN ( ".implode(', ', $job_type)." ) ";
}
if ( !empty($where) ) {
//$mainframe->enqueuemessage(" WHERE ".implode(' AND ', $where));
echo " WHERE ".implode(' AND ', $where);
}
?>
This loads in a set of arrays used for the select box values then uses JRequest to get the data from the form.
Most of the rest of the code is a series of tests to check if there is a value set in the search boxes and then to build a corresponding code snippet to go into the WHERE part of the MySQL query.
The one for the text filter is
if ( $filter ) {
$where_filter[] = "`job_title` LIKE '%$filter%' ";
$where_filter[] = "`company` LIKE '%$filter%' ";
$where_filter[] = "`job_description` LIKE '%$filter%' ";
$where_filter[] = "`job_ref` LIKE '%$filter%' ";
if ( !empty($where_filter) ) {
$where[] = "( ".implode(' OR ', $where_filter)." ) ";
}
}
This checks if there is a filter and if there is it build ups a mySQL query in 4 columns from the database table. The final result might look like
( `job_title` LIKE '%xxx%' OR `company` LIKE '%xxx%' OR `job_description` LIKE '%xxx%' OR `job_ref` LIKE '%xxx%' )
Then right at the end the final WHERE statement is built with this snippet:
if ( !empty($where) ) {
//$mainframe->enqueuemessage(" WHERE ".implode(' AND ', $where));
echo " WHERE ".implode(' AND ', $where);
}
Apologies for the long post, didn't have time for the shorter one.
Bob