For example, I've a single table (playlist) that contains a number of text fields, one of which is the "Artist". I want to be able to see all the Artist names that are in the table and pick one from a drop-down and then filter the data based on the selected value.
See attachment for screenshot showing what I mean. This is such a common filter that I would have thought that it would be a tickable option when generating the code.... maybe in the next version?
However it can also be achieved fairly easily with the hook system.
In this case the hook we're interested in is the _header / tableview hook.
Normally this is:
Code: Select all
case 'tableview':
break;
Code: Select all
case 'tableview':
$header='<%%HEADER%%>';
// Add the text that says "Select"
$header .= "<p>Select</p>";
// Drop down to filter by artist
// If we've requested an artist by the drop-down then we'll get the artist name back and store it so we
// can show it in the list as already selected
$showingartist=$_REQUEST['FilterValue'][1];
// If we've cleared filters then we'll make the selected name blank
If (isset($_REQUEST[NoFilter_x]) && $_REQUEST[NoFilter_x] = 1 ) $showingartist="";
// The code to create a form (I'm using the full domain/path to the file)
$header .= '<div class="btn btn-default"><form method="post" action="http://www.forgotten80sclub.com/playlist/playlist_view.php">
<input name="FilterAnd[1]" value="" type="hidden">
<input name="FilterField[1]" value="3" type="hidden">
<input name="FilterOperator[1]" value="<=>" type="hidden">
<i class="glyphicon glyphicon-filter"></i>Artist:
<select name="FilterValue[1]">
<option value="">All</option>';
// Now we get the "distinct" different values for the artist field from the table.
$res = sql( "SELECT DISTINCT(artist) FROM playlist ORDER BY artist", $eo);
// Now if we've any artists we need to add them to the dropdown as an <option>"
// Note that if the artist is the same as the one that's currently selected we mark it as such.
while($row = db_fetch_assoc($res)){
$myartist=$row['artist'];
$header .= "<option ".($myartist == $showingartist ? " selected " : "" )." value=\"".$myartist."\">".$myartist."</option>\n";
}
// Close the dropdown off.
$header .= "</select>\n";
// Make a submit button for the form
$header .= '<button type="submit" class="btn btn-default">Select</button></form>';
$header .= "</div>";
break;