Forums

Drop down filter using how to doc (dynamic filters 2)

jjunkie 26 Oct, 2010
Hi Bob 😢

Over the last few days i have been trying to implement a drop down filter, using your recommended solution as detailed in:
http://greyhead.net/how-to-docs/chronoconnectivity-dynamic-filters-2

I have amended the code as best i can to fit my simple form data, however i cannot get it to work as required, possibly due to the nature of your code - it lists all documents that exist in joomla (i only want to allow a drop down of 4 static values), and also as the query used in your example which 2 data fields.

Please please..please could you assist me with this. I have a simple form which uses a radio element for a user to choose an RSVP response amongst other data - guest name, comments etc.

All i want is for chronoconnectivity to display the data collected in the RSVP submission form, and a drop down filter to allow a user to filter the responses from the total option of 4 different responses presented in the radio element on the RSVP form.

Right now the code works for the search facility (i have set to allow search on guest names or number of guests), and the drop down is populated with the radio options, however it displays the data from all entires ever made - not just the 4 different types.

The filter is working in the sense that if you click a particular RSVP radio entry, and press filter, it highlights, within the drop down, all of the same RSVP entires, i.e if 4 records state 'I will be attending the ceremony only' it will highlight 4 entries in the drop down.

Maybe this example isnt appropriate for my needs.

I have also read: http://www.chronoengine.com/forums/index.php?option=com_chronoforums&cont=posts&f=12&t=19179 is this better for what i want? Please show me the exact code, or a working example, as i cannot get either example to work, and have been trying all solutions i have found, and have every how to doc you have made available.

Here is my code:

WHERE SQL:
<?php 
// create the empty array 
$where = array(); 

// check the text filter 
$filter_text = JRequest::getString('filter_text', '', 'post'); 
if ( $filter_text ) {
$where[] = "( `gname` LIKE '%$filter_text%' 
   OR `numguests` LIKE '%$filter_text%')";
} 

// check the response filter 
$filter_response =& JRequest::getVar('filter_response', array(), 'post', 'array'); 
if ( count($filter_response) ) {
  $filter = implode(', ', $filter_response);
  $where[] = " `radio0` IN ($filter) ";
} 
if ( count($where) ) {
  $where = implode(' AND ', $where); 
  echo " WHERE $where ";
} 
?>



HEADER

<?php 
$db =& JFactory::getDBO(); 
global $count; 
$count = $db->loadResult(); 
$style = ""; 
$style .= " 
table.cf_listing {
margin-bottom: 12px;
} 
"; 
if ( $style) {
   $doc =& JFactory::getDocument(); 
   $doc->addStyleDeclaration($style);
} 
$script = ""; 
$script .= " 
$('clear').addEvent('click', function() {
   $('filter_text').value = '';
   $('filter_catid').selectedIndex = -1; 
});
"; 
if ( $script ) {
   $doc =& JFactory::getDocument(); 
   $script = "window.addEvent('domready', function() { $script });";
   $doc->addScriptDeclaration($script);
} 
$filter_text = JRequest::getString('filter_text', '', 'post'); 
$query = "
   SELECT `radio0`
      FROM `#__chronoforms_RSVPv1` 
      ;
"; 
$db->setQuery($query); 
$data = $db->loadObjectList(); 
?> 
<select name='filter_response[]' id='filter_response' multiple='multiple' size='5' > <?php 
$filter_response =& JRequest::getVar('filter_response', array(), 'post', 'array'); foreach ( $data as $d ) {
   $selected = ''; 
   if ( in_array($d->radio0, $filter_response) ) {
     $selected = "selected='selected'"; 
   }
   echo "<option value='".$d->radio0."' $selected >".$d->radio0."</option>";
} 
?> 
</select> 
<br /> 
<input type='text' name='filter_text' id='filter_text' value='<?php echo $filter_text; ?>'
/> <input type='submit' name='filter' id='filter' value='Filter' /> <input
type='button' name='clear' id='clear' value='Clear' /> <table class='cf_listing'>

<?php 
if ( !$count ) {
echo "<div>Sorry, no results were found.</div>"; 
} else {
?> <thead>
<tr> 
   <th style='width:130px; margin-left:12px;'>Guest name</th>
   <th style='width:450px; margin-left:12px;'>Response</th>
   <th style='width:100px; margin-left:12px;'>Total guests</th>
   <th style='width:250px; margin-left:12px;'>Date received</th>
</tr> 
</thead>
<tfoot> 
<tr>
<td colspan='4' style='height:4px; background:silver;'></td> 
</tr>
</tfoot>
<?php
} 
?>
   <tbody>


BODY:

<tr><td>{gname}</td><td>{radio0}</td><td>{numguests}</td><td>{recordtime}</td></tr>



FOOTER:

 </tbody> 
</table>
<?php 
// get the row count and show the pagination if needed 
global $count;
if ( $count ) {
?> 
{pagination} 
<?php
} 
?>     




Using the example in post: http://www.chronoengine.com/forums.html?cont=posts&f=12&t=19179

My code -

WHERE SQL:
<?php
$pyear = JRequest::getVar('pyear', '', 'post');
if ( $pyear > 0 ) {
    echo " WHERE `pyear` = '$radio0' ";
}
?>


HEADER:
    {new_record}<br />
    <table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="0">
      <tbody>

<input type='submit' name='filter' id='filter' value='Filter' />

<tr>
<select name="pyear" id="pyear">
  <option value="0" default>Select</option>
  <option value="I/WE WILL be attending the Ceremony ONLY">I/WE WILL be attending the Ceremony ONLY</option>
  <option value="I/WE WILL be attending the Reception ONLY">I/WE WILL be attending the Reception ONLY</option>
  <option value="I/WE WILL be attending BOTH the ceremony and Reception">I/WE WILL be attending BOTH the ceremony and Reception</option>
  <option value="I/WE WILL NOT be attending the Ceremony or Reception">I/WE WILL NOT be attending the Ceremony or Reception</option>
</select>
</tr>
        <tr>
          <td style="font-weight: bold; width: 20%;">Guest Name</td>
          <td style="font-weight: bold; width: 48%;">Response</td>
          <td style="font-weight: bold; width: 20%;">Total guests</td>
          <td style="font-weight: bold; width: 15%;">Date received</td>
        </tr>
      </tbody>
    </table>
    <div class="clr" style="border-bottom: 3px solid #222; padding: 3px; margin-bottom: 10px;"></div>



BODY:
    <table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="0">
      <tbody>
        <tr<?php if ($i % 2) echo ' style="background-color: #EBEBEB;"';?>>
          <td style="width: 20%;">{gname}</td>
          <td style="width: 50%;">{radio0}</td>
          <td style="width: 10%;">{numguests}</td>
          <td style="width: 10%;">{recordtime}</td>
          <td style="width: 5%;">{edit_record}</td>
        </tr>
      </tbody>
    </table>
    <?php $i++ ?>


FOOTER:

N/A


Many thanks Bob🙂
GreyHead 27 Oct, 2010
Hi jjunkie,

The problem is that your radio buttons are returning string values and I was using integers. The results need to have quotes added. Here's an amended chunk of the WHERE SQL code:
// check the response filter
$filter_response =& JRequest::getVar('filter_response', array(), 'post', 'array');
if ( count($filter_response) ) {
  // !! add the following three lines !!
  foreach ( $filter_response as $k => $v ) {
    $filter_response[$k] = "'$v'";
  }
  $filter = implode(', ', $filter_response);
  $where[] = " `radio0` IN ($filter) ";
}


Bob
jjunkie 27 Oct, 2010
Hi Bob,

Thank you very much for your reply, i have substituted the code chunk you supplied in the WHERE SQL section for my Chronoconnection. Im sorry if i misunderstood your post, having made the changes, the filter still displays duplicate radio entries (every form input for radio0 is displayed in the drop down), and the results still do not display below the filter box as they should, the filter just highlights all of the entries in the actual drop down itself.

In addition the search no longer works. Would i be able to PM you the login details so you could kindly have a look?

Many thanks

jjunkie
GreyHead 27 Oct, 2010
Hi jjunkie,

Sure send them over. I built a test version here using your code and that was working OK but maybe I overlooked something.

Bob
jjunkie 27 Oct, 2010
Hi Bob,

Much appreciated, will PM you now.

Regards

Jjunkie

🤔 ( PM Sent )
GreyHead 27 Oct, 2010
Hi Jjunkie,

I'm pretty certain that the problem is with the values in your options.
<option value="I/WE WILL be attending BOTH the ceremony and Reception">I/WE WILL be attending BOTH the ceremony and Reception</option>
The '/' in there is almost certainly breaking things.

If you are going to use options for any processing then keep the value part short and sweet and use PHP to look up the long versions when you need them.
$attend_array = array(
  'both' => 'I/WE WILL be attending BOTH the ceremony and Reception',
  'recep' => 'I/WE WILL be attending the Reception ONLY'
);


Bob
jjunkie 27 Oct, 2010
Hi Bob,

Please check the site if possible, i have implemented the changes to both possible code solutions, unfortunately still not working 😶

Regards

jjunkie
jjunkie 08 Nov, 2010
Many thanks Bob !

It all works great now 😀 😀 😀 !

A refreshing beer is on its way to you as we speak, cheers!

jjunkie
iwdaddy 22 May, 2011
Great thread! I followed everything step-by-step and the filter looks great on my site. Unfortunately, pressing the filter button doesn't work as was also noted by jjunkie. So close...

I'm guessing the answer must lie somewhere between the 2 previous posts.
GreyHead 23 May, 2011
Hi iwdaddy,

There's not enough info here to give you any answer.

Please post a link to the listing so we can take a quick look.

Bob
iwdaddy 24 May, 2011
My apologies. In jjunkie's first post, there are two sets of code for creating a dropdown filter. I used both of them on my own site as a model and was able to get the second set to work. I would like to use the first set and create a dropdown filter as well as an optional text filter.

After modifying the code to work with my own data and including the correction stated in your (1st) reply, everything looks perfect. The only problem is the data won't filter when I press the filter button. Neither by selecting a dropdown item or by typing in a text filter. Apparently jjunkie ran into the same problem but I don't see how it was resolved on this thread.

Here's my code:

WHERE
<?php 
// create the empty array 
$where = array(); 

// check the text filter 
$filter_text = JRequest::getString('filter_text', '', 'post'); 
if ( $filter_text ) {
$where[] = "( `radio0` LIKE '%$filter_text%')";
} 

// check the response filter
$filter_response =& JRequest::getVar('filter_response', array(), 'post', 'array');
if ( count($filter_response) ) {
  // !! add the following three lines !!
  foreach ( $filter_response as $k => $v ) {
    $filter_response[$k] = "'$v'";
  }
  $filter = implode(', ', $filter_response);
  $where[] = " `radio0` IN ($filter) ";
}
?>


HEADER
<?php 
$db =& JFactory::getDBO(); 
global $count; 
$count = $db->loadResult(); 
$style = ""; 
$style .= " 
table.cf_listing {
margin-bottom: 12px;
} 
"; 
if ( $style) {
   $doc =& JFactory::getDocument(); 
   $doc->addStyleDeclaration($style);
} 
$script = ""; 
$script .= " 
$('clear').addEvent('click', function() {
   $('filter_text').value = '';
   $('filter_catid').selectedIndex = -1; 
});
"; 
if ( $script ) {
   $doc =& JFactory::getDocument(); 
   $script = "window.addEvent('domready', function() { $script });";
   $doc->addScriptDeclaration($script);
} 
$filter_text = JRequest::getString('filter_text', '', 'post'); 
$query = "
   SELECT DISTINCT `radio0`
      FROM `#__chronoforms_b2bform`
      ;
"; 
$db->setQuery($query); 
$data = $db->loadObjectList(); 
?> 
<select name='filter_response[]' id='filter_response' multiple='multiple' size='5' > <?php 
$filter_response =& JRequest::getVar('filter_response', array(), 'post', 'array'); foreach ( $data as $d ) {
   $selected = ''; 
   if ( in_array($d->radio0, $filter_response) ) {
     $selected = "selected='selected'"; 
   }
   echo "<option value='".$d->radio0."' $selected >".$d->radio0."</option>";
} 
?> 
</select> 
<br /> 
<input type='text' name='filter_text' id='filter_text' value='<?php echo $filter_text; ?>'
/> <input type='submit' name='filter' id='filter' value='Filter' /> <input
type='button' name='clear' id='clear' value='Clear' /> <table class='cf_listing'>

<?php 
if ( !$count ) {
echo "<div>Sorry, no results were found.</div>"; 
} else {
?> <thead>
<tr> 
   <th style='width:130px; margin-left:12px;'>Company</th>
   <th style='width:450px; margin-left:12px;'>Type</th>
   <th style='width:100px; margin-left:12px;'>Date</th>
   <th style='width:250px; margin-left:12px;'>Notes</th>
</tr> 
</thead>
<tfoot> 
<tr>
<td colspan='4' style='height:4px; background:silver;'></td> 
</tr>
</tfoot>
<?php
} 
?>
   <tbody>


BODY
<tr><td>{biz}</td><td>{radio0}</td><td>{recordtime}</td><td>{notes}</td></tr>


FOOTER
</tbody> 
</table>
<?php 
// get the row count and show the pagination if needed 
global $count;
if ( $count ) {
?> 
{pagination} 
<?php
} 
?> 



I'd love to know how to only show the data for the logged-in user as well. I've found posts that solve this but I can't figure out the correct way to work it into the above WHERE code. Thanks for the help Greyhead. You da man!
GreyHead 26 May, 2011
Hi iwdaddy,

It looks as though you are missing these lines from the end of the WHERE box (see jjunkie's example)
if ( count($where) ) {
  $where = implode(' AND ', $where);
  echo " WHERE $where ";
} 

Bob
FloB 10 Sep, 2013
Hello!
I know this topic is a little bit old, but I couldn't find something else.
I've tried to set up a filter with a dynamic drop down in the header of the custom listing.
I'm using CC v4 and I have read that the "ChronoConnectivity dynamic filters 2 tutorial" by Grey Head was for the CC v2.
I don't know how I need to adapted this code for yhe CC v4.

Here is my code :

Where SQL
<?php
//récupération identité user
$user =& JFactory::getUser();
$iduser=$user->id;
echo '`article`.`created_by`='.$iduser;

// create the empty array
$where = array();
// check the catid filter
$filter_catid =& JRequest::getVar('filter_catid', array(), 'post', 'array');
if ( count($filter_catid) ) {
$filter = implode(', ', $filter_catid);
$where[] = " `photo`.`catid` IN ($filter) ";
}
if ( count($where) ) {
$where = implode(' AND ', $where);
echo  ' AND '.$where ;
}
?>


Header
<?php
$user =& JFactory::getUser();
$iduser=$user->id;
$db =& JFactory::getDBO();
global $count;
$count = $db->loadResult();
$style = "";
$style .= "
table.cf_listing {
margin-bottom: 12px;
}
";
if ( $style) {
$doc =& JFactory::getDocument();
$doc->addStyleDeclaration($style);
}
$script = "";
$script .= "
$('clear').addEvent('click', function() {
$('filter_catid').selectedIndex = -1;
});
";
if ( $script ) {
$doc =& JFactory::getDocument();
$script = "window.addEvent('domready', function() { $script });";
$doc->addScriptDeclaration($script);
}
$query = "
SELECT photocat.`id`, photocat.`title`
FROM `#__phocagallery_categories` AS `photocat`
JOIN `#__content` AS `art`
ON photocat.`alias` = art.`alias`
WHERE art.`created_by`=".$iduser."
ORDER BY photocat.`id`;
";
$db->setQuery($query);
$data = $db->loadObjectList();
?>
<select name='filter_catid[]' id='filter_catid' multiple='multiple' size='5' >
<?php
$filter_catid =& JRequest::getVar('filter_catid', array(), 'post', 'array');
foreach ( $data as $d ) {
$selected = '';
if ( in_array($d->id, $filter_catid) ) {
$selected = "selected='selected'";
}
echo "<option value='".$d->id."' $selected >".$d->title."</option>";
}
?>
</select>
<br />
<input type='submit' name='filter' id='filter' value='Afficher uniquement la ou les sortie(s) sélectionnée(s)' />
<br />
<input type='button' name='clear' id='clear' value='Effacer la sélection' />
<table class='cf_listing'>
<?php
if ( !$count ) {
echo "<div>Désolé, aucun résultat pour cette recherche.</div>";
} else {
?>

<h1>Liste des photos</h1>
<hr style='margin:6px;' />
<thead>
    <tr>
      <th style='width:50px; text-align:center;' >ID</th>
      <th style='width:50px; text-align:center;' >Sélection photo</th>
      <th style='width:200px; text-align:center;' >Photo</th>
      <th style='width:240px;' >Légende</th>
      <th style='width:240px;' >Catégorie</th>
     </tr>
  </thead>
<tfoot>

</tfoot>
<?php
}
?>
<tbody>


Body
<tr>
  <td style='text-align:center;'>{photo.id}</td>
  <td style='text-align:center;'>{checkbox}</td>
  <td style='text-align:center;'>
<?php
$ex_thumb=explode("/",$row['photo']['filename']);
$thumb=$ex_thumb[0].DS.$ex_thumb[1].DS.$ex_thumb[2].DS."thumbs".DS."phoca_thumb_m_".$ex_thumb[3];
echo "<img src=".chr(042)."images/phocagallery/".$thumb.chr(042).">";
?>
</td>
  <td>{photo.description}</td>
  <td>{phoca.title}</td>
</tr>


Foot
</tbody>
</table>
<hr />
<?php
// get the row count and show the pagination if needed
global $count;
if ( $count ) {
?>
{paginator}
<?php
}
?>


You can find the Frontend View at this url :http://club-alpin-francais-vienne.fr/component/chronoconnectivity/essai_phocagallery_search?limitstart=0
I just replace in the code the var $iduser by my id so that you can see something.

My problem is that when I click on the submit button, it seems I can't have a correct url so there is an error.

What's wrong please.

Flo
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger