Echoing search criteria won't work when searching miltiple fields

8 posts by 2 authors in: Forums > CMS Builder
Last Post: September 13, 2022   (RSS)

By gkornbluth - September 8, 2022 - edited: September 8, 2022

Hi All,

I’m trying to extend the concept of echoing the search criteria that are entered at the top of the search results, which works for fields that get their option values from a pull down list and are searching a single field, to fields that get their option values from a pull down list and are searching across 3 fields (in the same editor), but I can’t seem to get it to work.

You can see the concept in operation at https://popupdude.com/search17.php

A "Genre 1 Book Type" search" (searching only 1 field) echos the search criteria, but an "Admin Book Type" search and a "Public Book Type" search (each searching 3 fields) does not. The search results are correct in all cases.

Here is what I think is the relevant code: (The entire page code is attached to this post)

<select name = "genre, genre_2, genre_3_match[]" multiple>
<?php foreach (getListOptions('books', 'genre') as $value => $label13): ?>
<option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['genre, genre_2, genre_3_match[]']);?>> <?php echo $label13; ?></option>
<?php endforeach ?>
</select>

// Start Book Type Search
@$booksearchsearchCriteria = ""; // set default value
if ((@$_REQUEST['save'] == 1) && @$_REQUEST['genre, genre_2, genre_3_match[]']) {
@$booksearchCriteria = "<b>Admin Book Search 1: </b>"; // set default value
// cycle through each genre selected
foreach ($_REQUEST['genre, genre_2, genre_3_match[]'] as $genre_3) {

// lookup genre number in database
$genreRecord = mysql_get("genre_categories", $genre_3);

if ($genreRecord) {
// add "genre" value to output
@$booksearchCriteria .= $genreRecord['genre_names'] .", ";
}
}
}
// End Book Type Search


and in the telltale:

<?php if (@$booksearchCriteria):?><?php echo @$booksearchCriteria ?><?php endif ?>

<?php if (@$_REQUEST['genre, genre_2, genre_3_match[]']) { $searchCriteria30 .= "and with a <b>Book Search</b> including: {$_REQUEST['genre, genre_2, genre_3_match[]']}"; } ?>
<?php if ( @!$searchCriteria30 ==''):?>
<?php echo @$searchCriteria30 ?>
<?php endif?>

For comparison, the working code for a Genre 1 search follows:

<select name = "genre_match[]" multiple>
<?php foreach (getListOptions('books', 'genre') as $value => $label7): ?>
<option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['genre']);?>> <?php echo $label7; ?></option>
<?php endforeach ?>
</select>


// Start Genre 1
@$genresearchCriteria = ""; // set default value
if ((@$_REQUEST['save'] == 1) && @$_REQUEST['genre_match']) {
@$genresearchCriteria = "<b>Genre 1: </b>"; // set default value
// cycle through each genre selected
foreach ($_REQUEST['genre_match'] as $genre) {

// lookup genre number in database
$genreRecord = mysql_get("genre_categories", $genre);

if ($genreRecord) {
// add "genre" value to output
@$genresearchCriteria .= $genreRecord['genre_names'] .", ";
}
}
}
// End Genre 1

@$genresearchCriteria = chop(@$genresearchCriteria, ', '); // remove trailing , or spaces
@$genresearchCriteria = chop(@$genresearchCriteria, '- '); // remove trailing - or spaces

And in the telltale:

<?php if (@$genresearchCriteria):?><?php echo @$genresearchCriteria ?> <br /><?php endif ?>

Any thoughts appreciated...

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

search17.php 56K

By daniel - September 8, 2022

Hey Jerry,

In the $_REQUEST variables, try replacing "genre, genre_2, genre_3_match[]" with "genre, genre_2, genre_3_match". In a select/input name, the [] indicates that the field will be returned as an array, but it's not part of the name used by $_REQUEST. (You can similarly see how the Genre 1 select field has name="genre_match[]", but uses $_REQUEST['genre_match'])

For that reason, though, this line will not work, even if you remove the []:

<?php if (@$_REQUEST['genre, genre_2, genre_3_match[]']) { $searchCriteria30 .= "and with a <b>Book Search</b> including: {$_REQUEST['genre, genre_2, genre_3_match[]']}"; } ?>

$_REQUEST['genre, genre_2, genre_3_match'] will contain an array of record numbers, not the actual genre names. I think you'll want to use $booksearchCriteria instead since there's a loop earlier that populates this with the genre names.

Let me know if that helps, or if you have any other questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

By daniel - September 9, 2022

Hey Jerry,

Sorry if I was unclear, in addition to using $booksearchCriteria, you also need to change this:

$_REQUEST['genre, genre_2, genre_3_match[]']

To this:

$_REQUEST['genre, genre_2, genre_3_match']

anywhere that it occurs. You'll notice that none of your working examples include the "[]" at the end of the request key.

Also, just for robustness's sake, I'd recommend using this:

<?php if (@$booksearchCriteria) { $searchCriteria30 .= "and with a <b>Book Search</b> including: {$booksearchCriteria}"; } ?>

This way it will work even if you change how $booksearchCriteria is populated.

Let me know if that works!

Thanks,

Daniel
Technical Lead
interactivetools.com

By gkornbluth - September 9, 2022 - edited: September 9, 2022

Hi Daniel,

I tried your suggestion, but when I remove the [] (as in http://popupdude.com/search19.php) the search results do not return the records for a multi value search, so, for example, when I search for both animals and Advertising Popups, the results returned are only for animals.  This multi value search works in http://popupdude.com/search18.php with the [] left in the code.

http://popupdude.com/search.php is the live search page with the complete code

I wonder if you could take a deeper look at this issue for me.

Thanks,

Jerry

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

search19.php 56K

search18.php 56K

search.php 55K

By daniel - September 9, 2022

Hey Jerry,

In search19.php you also removed the [] from the "name" property of the <select> elements - it should remain here, as it is in the working examples. It only needs to be removed from the $_REQUEST keys.

Here are some resources to better understand the purpose/usage of the [] in form input names, multiple-selects in particular:

If this still doesn't work, go ahead and send a support request (https://www.interactivetools.com/support/request/) and I can have a look into it.

Thanks,

Daniel
Technical Lead
interactivetools.com

Just to follow up in case anyone comes across a similar issue - the remaining issue was that there were spaces in the input name "genre, genre_2, genre_3_match" which get converted to underscores after submit: "genre,_genre_2,_genre_3_match". Removing the spaces seems to have solved the issue.

Daniel
Technical Lead
interactivetools.com

By gkornbluth - September 13, 2022 - edited: September 14, 2022

Thank you Daniel,

I really appreciate that you are there for us and that you're so good at this.

The working page is attached.

Hope it helps someone.

Best,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

search.php 53K