在 php/Joomla 中创建复选框过滤器
Create checkbox filter in php/Joomla
首先,我必须告诉你我对这方面的经验很少。我遇到的问题是我正在尝试将 Joomla 生成的网站重新编码为将复选框显示为过滤器。现在我正在使用一个有效的多选列表,但我需要有复选框(这样用户在选择多个值时不需要按住 Shift 或 Ctrl)。有两个过滤器(一个用于州,另一个用于城市),城市的过滤器选项是根据州过滤器中的选择生成的。我无法弄清楚如何将所有内容都放入复选框中,从而根据 "State" 区域中的复选框继续生成 "city checkboxes"...
public static function makeStateList($req_country_id,$req_state_id,$name,$onChange,$firstOption,$style,$class="input-medium"){
global $configClass,$languages;
$db = JFactory::getDbo();
$stateArr = array();
$show_available_states_cities = $configClass['show_available_states_cities'];
$lgs = OSPHelper::getLanguages();
$translatable = JLanguageMultilang::isEnabled() && count($lgs);
$suffix = "";
if($translatable){
$suffix = OSPHelper::getFieldSuffix();
}
if((!HelperOspropertyCommon::checkCountry()) or ($req_country_id > 0)){
$query = "Select id as value,state_name".$suffix." as text from #__osrs_states where published = 1 ";
if($req_country_id > 0){
$query .= " and country_id = '$req_country_id'";
}else{
$query .= " and country_id = '".$configClass['show_country_id']."'";
}
if($show_available_states_cities == 1){
$query .= " and id in (Select state from #__osrs_properties where approved = '1' and published = '1')";
}
$query .= " order by state_name";
$db->setQuery($query);
$states = $db->loadObjectList();
if($firstOption != ""){
$stateArr[] = JHTML::_('select.option','',$firstOption);
$stateArr = array_merge($stateArr,$states);
}else{
$stateArr = $states;
}
return JHTML::_('select.genericlist',$stateArr,$name,'class="'.$class.'" multiple="multiple" '.$onChange.' '.$style,'value','text',$req_state_id);
}else{
$stateArr[] = JHTML::_('select.option','',$firstOption);
return JHTML::_('select.genericlist',$stateArr,$name,'class="'.$class.'" disabled','value','text');
}
}
/**
* Load City
*
* @param unknown_type $option
* @param unknown_type $state_id
* @param unknown_type $city_id
* @return unknown
*/
public static function loadCity($option,$state_id,$city_id,$class="input-medium"){
global $mainframe,$configClass,$languages;
$db = JFactory::getDBO();
$lgs = OSPHelper::getLanguages();
$translatable = JLanguageMultilang::isEnabled() && count($lgs);
$suffix = "";
if($translatable){
$suffix = OSPHelper::getFieldSuffix();
}
$availSql = "";
$show_available_states_cities = $configClass['show_available_states_cities'];
$cityArr = array();
$cityArr[]= JHTML::_('select.option','',JText::_('OS_ALL_CITIES'));
if($state_id > 0){
if($show_available_states_cities == 1){
$availSql = " and id in (Select city from #__osrs_properties where approved = '1' and published = '1')";
}
$db->setQuery("Select id as value, city".$suffix." as text from #__osrs_cities where published = '1' $availSql and state_id = '$state_id' order by city");
$cities = $db->loadObjectList();
$cityArr = array_merge($cityArr,$cities);
$disabled = "";
}else{
$disabled = "disabled";
}
return JHTML::_('select.genericlist',$cityArr,'city','class="'.$class.'" '.$disabled,'value','text',$city_id);
}
抱歉这么久了 post。我希望这是有道理的...谢谢你的帮助!
您基本上必须遍历数组($stateArr 和 $cityArr)并输出复选框,而不是使用内置的 html 助手 JHTML::_('select.genericlist',$ cityArr ... 在这两个函数中的每一个中,只要 JHTML 用于生成 select 列表,就将其替换为类似的内容:
foreach($stateArr as $key=>$value){
echo "<div><input type='checkbox' name='$name'
value='{$key}' />{$value}</div>";
}
您可能必须处理某些情况,例如是否已选中某些框等。
首先,我必须告诉你我对这方面的经验很少。我遇到的问题是我正在尝试将 Joomla 生成的网站重新编码为将复选框显示为过滤器。现在我正在使用一个有效的多选列表,但我需要有复选框(这样用户在选择多个值时不需要按住 Shift 或 Ctrl)。有两个过滤器(一个用于州,另一个用于城市),城市的过滤器选项是根据州过滤器中的选择生成的。我无法弄清楚如何将所有内容都放入复选框中,从而根据 "State" 区域中的复选框继续生成 "city checkboxes"...
public static function makeStateList($req_country_id,$req_state_id,$name,$onChange,$firstOption,$style,$class="input-medium"){
global $configClass,$languages;
$db = JFactory::getDbo();
$stateArr = array();
$show_available_states_cities = $configClass['show_available_states_cities'];
$lgs = OSPHelper::getLanguages();
$translatable = JLanguageMultilang::isEnabled() && count($lgs);
$suffix = "";
if($translatable){
$suffix = OSPHelper::getFieldSuffix();
}
if((!HelperOspropertyCommon::checkCountry()) or ($req_country_id > 0)){
$query = "Select id as value,state_name".$suffix." as text from #__osrs_states where published = 1 ";
if($req_country_id > 0){
$query .= " and country_id = '$req_country_id'";
}else{
$query .= " and country_id = '".$configClass['show_country_id']."'";
}
if($show_available_states_cities == 1){
$query .= " and id in (Select state from #__osrs_properties where approved = '1' and published = '1')";
}
$query .= " order by state_name";
$db->setQuery($query);
$states = $db->loadObjectList();
if($firstOption != ""){
$stateArr[] = JHTML::_('select.option','',$firstOption);
$stateArr = array_merge($stateArr,$states);
}else{
$stateArr = $states;
}
return JHTML::_('select.genericlist',$stateArr,$name,'class="'.$class.'" multiple="multiple" '.$onChange.' '.$style,'value','text',$req_state_id);
}else{
$stateArr[] = JHTML::_('select.option','',$firstOption);
return JHTML::_('select.genericlist',$stateArr,$name,'class="'.$class.'" disabled','value','text');
}
}
/**
* Load City
*
* @param unknown_type $option
* @param unknown_type $state_id
* @param unknown_type $city_id
* @return unknown
*/
public static function loadCity($option,$state_id,$city_id,$class="input-medium"){
global $mainframe,$configClass,$languages;
$db = JFactory::getDBO();
$lgs = OSPHelper::getLanguages();
$translatable = JLanguageMultilang::isEnabled() && count($lgs);
$suffix = "";
if($translatable){
$suffix = OSPHelper::getFieldSuffix();
}
$availSql = "";
$show_available_states_cities = $configClass['show_available_states_cities'];
$cityArr = array();
$cityArr[]= JHTML::_('select.option','',JText::_('OS_ALL_CITIES'));
if($state_id > 0){
if($show_available_states_cities == 1){
$availSql = " and id in (Select city from #__osrs_properties where approved = '1' and published = '1')";
}
$db->setQuery("Select id as value, city".$suffix." as text from #__osrs_cities where published = '1' $availSql and state_id = '$state_id' order by city");
$cities = $db->loadObjectList();
$cityArr = array_merge($cityArr,$cities);
$disabled = "";
}else{
$disabled = "disabled";
}
return JHTML::_('select.genericlist',$cityArr,'city','class="'.$class.'" '.$disabled,'value','text',$city_id);
}
抱歉这么久了 post。我希望这是有道理的...谢谢你的帮助!
您基本上必须遍历数组($stateArr 和 $cityArr)并输出复选框,而不是使用内置的 html 助手 JHTML::_('select.genericlist',$ cityArr ... 在这两个函数中的每一个中,只要 JHTML 用于生成 select 列表,就将其替换为类似的内容:
foreach($stateArr as $key=>$value){
echo "<div><input type='checkbox' name='$name'
value='{$key}' />{$value}</div>";
}
您可能必须处理某些情况,例如是否已选中某些框等。