4images Modifications / Modifikationen > Tutorials

[TUT] Making a "boolean" (tinyint 1) field searchable

(1/3) > >>

WhiteRabbit:
NOTE: I was wanting to post this on the mods section, but I don't know why I don't have permission to post there. If some admin wants to move the post, I will be grateful

Hello people:

There is a problem if you want to add a boolean field and make it searchable using this guide:
http://www.4homepages.de/forum/index.php?topic=1313.0

That works very well with varchar fields. But for boolean fields, it's actually a waste of time and space, because to index it you have to set MIN_SEARCH_KEYWORD_LENGTH to 1, what causes to almost ALL words to be indexed. That can be a problem if you have various searchable fields, as I have. And in case that you want to find a positive value you have to enter a 1 in the search terms...

This mod allows you to make a boolean field searcheable WITHOUT indexing it. It works almost in the same way wich search_new_images works.
I will show the example with the field "image_nocturnal", but thats your choice.

First, add a "boolean" (tinyint 1) field to the images table, with this tutorial:
If you are goin to use an "internal" field (as this example, there is no need to show "Nocturnal: Yes" in the details page) you can avoid the steps involving details.html .

http://www.4homepages.de/forum/index.php?topic=747.msg3277#msg3277

In file search.php :
Look for:

--- Code: ---if (isset($HTTP_POST_VARS['search_new_images']) || isset($HTTP_GET_VARS['search_new_images'])) {
  $search_new_images = 1;
  $show_result = 1;
}
else {
  $search_new_images = 0;
}
--- End code ---

Insert below :

--- Code: ---// NOCTURNAS
if (isset($HTTP_POST_VARS['search_nocturnal_images']) || isset($HTTP_GET_VARS['search_nocturnal_images'])) {
  $search_nocturnal_images = 1;
  $show_result = 1;
}
else {
  $search_nocturnal_images = 0;
}
//FIN NOCTURNAS
--- End code ---

find:

--- Code: ---if ($search_new_images && $show_result == 1) {
  $search_id['search_new_images'] = 1;
}
--- End code ---

insert below:

--- Code: ---//NOCTURNAS
if ($search_nocturnal_images && $show_result == 1) {
  $search_id['search_nocturnal_images'] = 1;
}
//FIN NOCTURNAS
--- End code ---

find:

--- Code: ---  if (!empty($search_id['search_new_images']) && $search_id['search_new_images'] == 1) {
    $new_cutoff = time() - 60 * 60 * 24 * $config['new_cutoff'];
    $sql_where_query .= "AND i.image_date >= $new_cutoff ";
  }
--- End code ---

add below:

--- Code: ---// NOCTURNAS
  if (!empty($search_id['search_nocturnal_images']) && $search_id['search_nocturnal_images'] == 1) {
    $sql_where_query .= "AND i.image_nocturnal = 1 ";
  }
// FIN NOCTURNAS
--- End code ---

find:

--- Code: ---  $site_template->register_vars(array(
    "search_keywords" => htmlspecialchars(stripslashes($org_search_keywords)),
    "search_user" => htmlspecialchars(stripslashes($org_search_user)),
    "lang_search_by_keyword" => $lang['search_by_keyword'],
    "lang_search_by_username" => $lang['search_by_username'],
    "lang_new_images_only" => $lang['new_images_only'],
    "lang_search_terms" => $lang['search_terms'],
    "lang_or" => $lang['or'],
    "lang_and" => $lang['and'],
    "lang_category" => $lang['category'],
    "lang_search_fields" => $lang['search_fields'],
    "lang_all_fields" => $lang['all_fields'],
    "lang_name_only" => $lang['name_only'],
    "lang_description_only" => $lang['description_only'],
    "lang_keywords_only" => $lang['keywords_only'],
    "category_dropdown" => get_category_dropdown($cat_id),
--- End code ---

add below
(note the comma on the previous line)

--- Code: ---    "category_dropdown" => get_category_dropdown($cat_id),
// NOCTURNAS
    "lang_nocturnal_images_only" => $lang['nocturnal_images_only']
// FIN NOCTURNAS
--- End code ---

Now in page_header.php :

Find:

--- Code: ---  "url_new_images" => $site_sess->url(ROOT_PATH."search.php?search_new_images=1"),
--- End code ---

Add below :

--- Code: ---// NOCTURNAS
  "url_nocturnal_images" => $site_sess->url(ROOT_PATH."search.php?search_nocturnal_images=1"),
// FIN NOCTURNAS
--- End code ---

Now go to main.php of your language directory, and define (before ?> or in any place that you want):


--- Code: ---$lang['nocturnal_images_only'] = "Sólo imágenes nocturnas";
--- End code ---

("Sólo imágenes nocturnas" = "Only nocturnal images" in english).

Now you can add in your template pages the following code:

In search_form.html, the checkbox to limit the search to the images that have a positive value on the field "image_nocturnal":

--- Code: ---<input type="checkbox" name="search_nocturnal_images" value="1" /> {lang_nocturnal_images_only}
--- End code ---

And the link to show directly all the images with a positive value (on any page of your template):

--- Code: ---<a href="{url_nocturnal_images}">
--- End code ---

In case that someone ask:

If you want to look for non-positive (zero) values in the field too, you can put a second instance of the mod (just below the new code added), and change the variable, function and tag names, and replace:

--- Code: ---$sql_where_query .= "AND i.image_nocturnal = 1 ";
--- End code ---

with:

--- Code: --- $sql_where_query .= "AND i.image_nocturnal = 0 ";
--- End code ---

Well, that's it...
Best regards.
WR.

Kjeld:
I can't locate this code in search.php...  :(


--- Code: ---if (isset($HTTP_POST_VARS['search_new_images']) || isset($HTTP_GET_VARS['search_new_images'])) {
  $search_new_images = 1;
  $show_result = 1;
}
else {
  $search_new_images = 0;
}
--- End code ---

I am using version 1.7.2. Perhaps the code has been changed? What should I do now to make my boolean field searchable?

V@no:
search for this instead:
--- Code: ---include(ROOT_PATH."global.php");
--- End code ---

Kjeld:
Thanks V@no, for helping again.

In the tutorial above it says to find (code #1):


--- Code: ---if (isset($HTTP_POST_VARS['search_new_images']) || isset($HTTP_GET_VARS['search_new_images'])) {
  $search_new_images = 1;
  $show_result = 1;
}
else {
  $search_new_images = 0;
}
--- End code ---

and then add (code #2):


--- Code: ---// NOCTURNAS
if (isset($HTTP_POST_VARS['search_nocturnal_images']) || isset($HTTP_GET_VARS['search_nocturnal_images'])) {
  $search_nocturnal_images = 1;
  $show_result = 1;
}
else {
  $search_nocturnal_images = 0;
}
//FIN NOCTURNAS
--- End code ---

However code #1 does not exist in my search.php.

What code should I change, and how? Or should I ignore code #2 and just follow the rest of the tutorial?

V@no:

--- Quote from: Kjeld on April 24, 2006, 01:54:41 AM ---In the tutorial above it says to find (code #1):

....


However code #1 does not exist in my search.php.

What code should I change, and how? Or should I ignore code #2 and just follow the rest of the tutorial?

--- End quote ---
Search for the line I posted in my previous reply instead of "code #1"

Navigation

[0] Message Index

[#] Next page

Go to full version