Searching Multiple fields at once
I needed to be able to search 3 category fields (text fields) at once, all of which got their option values and option names from a master catalog of categories. There were 3 fields because any of three books could be in more than one category. They could be “Art” books that also fit the “animals” category. To complicate the issue, my client wanted all of the Genre Categories to show in the pull down for admins but only a small subset of those would be shown to the public (non-admins).
Step 1 was to set up a master “Genre” catalog multi record editor. This editor was called “Genre Categories” and it had only 2 fields. A text field called ‘Genre” and a checkbox called “Include In public search”. (If I wanted to show a value to the public, then that box got checked). You should create 4 or 5 records for testing and check some to be in the public list.
Step 2 was to set up the multi record main books editor, called “Books”. This editor had many fields, but the 4 that are needed for the multi field search are list fields called, “ Genre’, Genre 2, Genre 3 and “Public Genre”. In addition, I’d add a “title” field for testing.
All list fields get their list options from “MYSQL query (advanced)”
The query for “Genre”, “Genre 2", and “Genre 3" are:
SELECT num, genre_names FROM `<?php echo $TABLE_PREFIX ?>genre_categories` ORDER BY genre_names ASC
And the query for “Public Genre” is:
SELECT num, genre_names FROM `<?php echo $TABLE_PREFIX ?>genre_categories` WHERE include_in_public_search = 1 ORDER BY genre_names ASC
Step 3 was to populate the books editor with some records and assign different values to Genre 1, Genre 2 and Genre 3 for testing.
Now for the list page viewer.
At the top of your page you’ll need to load your viewer library and load records from the books editor. Make sure that your load records call has “allow search” set to true, like this
<?php header('Content-type: text/html; charset=utf-8'); ?> <?php /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */ // load viewer library $libraryPath = 'cmsAdmin/lib/viewer_functions.php'; $dirsToCheck = ['','../','../../','../../../','../../../../']; // add if needed: '/your/server/public_html/path/' foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }} if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
// load records from 'books' list($booksRecords, $booksMetaData) = getRecords(array( 'tableName' => 'books', 'loadUploads' => true, 'allowSearch' => true, ));
?>
In the body of your page insert your search forms:
<form method="post" action= "search.php"> <input type="hidden" name="save" value="1" /> <table align="left" width="90%" border="0" cellspacing="0" cellpadding="2"> <tr> <td colspan="2"> </td> </tr> <tr> <td align="right" class="text_font"><b>Book Search 1:</b></td> <td align="left" valign="bottom" colspan="2"><select name = "genre, genre_2, genre_3_match[]" width="300" class="text_font" style="width: 300px; max-height: 50px; " multiple> <?php foreach (getListOptions('books', 'genre') as $value => $label43): ?> <option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['genre, genre_2, genre_3_match[]']);?>> <?php echo $label43; ?></option> <?php endforeach ?> </select></td> </tr> <tr> <td align="right" class="text_font"><b>Book Search 2:</b></td> <td align="left" valign="bottom" colspan="2"><select name = "genre, genre_2, genre_3_match[]" width="300" class="text_font" style="width: 300px; max-height: 50px; " multiple> <?php foreach (getListOptions('books', 'public_genre') as $value => $label43): ?> <option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['genre, genre_2, genre_3_match[]']);?>> <?php echo $label43; ?></option> <?php endforeach ?> </select></td> </tr> <tr> <td align="right" class="text_font"><b> </b></td> <td align="left" valign="bottom" colspan="2"><input type="submit" value="Submit Search Filters" ></td> </tr> </table> </form> <table align="left" width="90%" border="0" cellspacing="0" cellpadding="2"> <tr> <!--<td> </td>--> <td align="center" colspan="2"><form method="post" action="" > <INPUT TYPE="submit" VALUE="Cancel Search Filters - Start Another Search"> </form></td> </tr> </table>
Below that you’ll show the results of your search starting with a telltale that shows the number of records returned that match your search criteria:
<!-- BEGIN RESULTS DISPLAY--> <table align="left" width="90%" border="0" cellspacing="0" cellpadding="2"> <tr> <td colspan="2"> <?php if(@$_REQUEST['save']):?> <?php $count = 0 ?> <?php foreach ($booksRecords as $record): ?> <?php $count++ ?> <?php endforeach?> There are <?php echo $count; ?> records that match your search criteria <?php endif ?></td> </tr>
Followed by:
<tr> <td span class="text_font" colspan="2"><hr align="left" style="height:2px; width=90%" /> <?php foreach ($booksRecords as $record): ?> <b>Title:</b> <?php echo htmlencode($record['title']) ?> <?php if($record['genre']):?> <b>Genre 1:</b> <?php echo $record['genre:label'] ?> <?php endif ?> <?php if($record['genre_2']):?> <b>Genre 2:</b> <?php echo $record['genre_2:label'] ?> <?php endif ?> <?php if($record['genre_3']):?> <b>Genre 3:</b> <?php echo $record['genre_3:label'] ?> <?php endif ?> <?php // Add any additional fields here to show them in the results ?> <hr align="left" style="height:2px;" width="90%"/> <?php endforeach ?>
And that’s it. After you’ve tested your code, you can add <?php $CMS_USER = getCurrentUserFromCMS(); ?> to see If the current user is an admin, and surround the “Book Search 1" code with <?php if (@$CURRENT_USER['isAdmin']): ?> and <?php endif ?>.
You can use that code to restrict the displaying of any fields you like to admins only, and style the page any way that works for you.
|