Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 1st, 2004, 02:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo
Default advanced search

hey guys

i have a two questions, but before that i'll describe my problem... i'm doing an advanced
search and i'm trying to store in an array the ID of a record that contains the match for the search and
the number of words or phrases which were found in a description field in the table. example:


id description
___________________________

1 some text here
2 some text here ...


now i want to build an array which will go thru all the records, find which id has the matches in it
and store the number of the matches found in a description field. let's say that this is the result array:
($matches[] = "number_of_hits:ID" - this is the notation for below)


$matches[0] = "2:12";
$matches[1] = "2:11";
$matches[2] = "1:19";
$matches[3] = "0:5";
$matches[4] = "3:9";


so my first question is: is there a better way to store the number of hits and ID of an match in maybe two dimensional
array, and second question is: I want to delete from array elements that contain 0 (zero) at the beginning (zero matches
found in a record) so that code above array becomes:


$matches[0] = "3:9";
$matches[1] = "2:12";
$matches[2] = "2:11";
$matches[3] = "1:19";


notice there's no "0:5" in that array.

thx :D

the genuine genius
__________________
www.campusgrind.com - college portal
 
Old February 2nd, 2004, 04:43 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There's two ways to do this. The first is to continue using your "hits:id" string in a single-dimensional array. Why, oh why, would you want to do this, though? It forces you to perform regular expression type matches on EVERY STRING in the array to extract the "hits" part.

Granted, since you're string is of the form <integer>:<integer>, you can use PHP's built-in string-to-integer conversion for the task. This conversion will work properly because a colon is NOT a valid character in a number. (Good thing you didn't choose to use a decimal point.)

The next thing you need to do is figure out how to remove an element from an array. That's easy -- unset().

But what you're really talking about doing is removing some undetermined SET of elements from an array using a fixed criteria. You can write a function to calculate this criteria, and pass the name of this function to one of the several array walking functions that filters an array based on the boolean result of a callback function.

Okay, that was more than likely over most people's heads, so lemme explain a few things:

A "callback" function is a function that you write that you don't explicitly call yourself -- rather, you tell some other function or object that you want them to call that function when it's appropriate for them.

"Walking" an array means to iterate over each element in an array and (typically) apply some processing on each element.

When you walk an array and just call a function on each element (this function is a callback function), this is called "mapping" a function onto an array.


Using that knowledge, we can see that we simply need to write a function that determines whether an element should be kept in the array or not.

function has_hits($str)
{
   return ((int)$str) > 0);
}

This converts $str to an integer, and returns TRUE if that integer is greater than zero, false otherwise.


There are several functions available in PHP that perform walking, mapping, filtering, etc. Here's a list of them:
  http://www.php.net/array_map
  http://www.php.net/array_walk
  http://www.php.net/array_reduce
  http://www.php.net/array_filter


Judging by the names, it seems obvious that "array_filter" is probably the most applicable to our problem.

$hits_only = array_filter($matches, 'has_hits');

array_filter will run has_hits() on every element in the $matches array, and if has_hits() returns true, that element will be added to the result array.

A PHP implementation of array_filter() would be:

function array_filter(&$array, $func)
{
   $ret = array();

   foreach($array as $key => $val)
   {
       if ($func($val))
       {
           $ret[$key] = $val;
       }
   }

   return $ret;
}



Okay, so now we're still left with the (imho) sloppy implementation of encoding both hits and id's in the same string. Why not use a nested array, with both 'hits' and 'id' indexes?

$matches SHOULD look like this:
(
   [0] => Array
   (
      [hits] => 2
      [id] => 12
   )
   [1] => Array
   (
      [hits] => 2
      [id] => 11
   )
   [2] => Array
   (
      [hits] => 1
      [id] => 19
   )
   [4] => Array
   (
      [hits] => 0
      [id] => 5
   )
   [5] => Array
   (
      [hits] => 3
      [id] => 9
   )
)


has_hits() would then be implemented as:

function has_hits($match)
{
    return $match['hits'] > 0;
}



Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this forum too advanced for me? Randy Visual Basic 2008 Essentials 9 June 11th, 2008 08:49 PM
Where is Advanced Search ? MikeW2 BOOK: Beginning SharePoint 2007: Building Team Solutions with MOSS 2007 ISBN: 978-0-470-12449-9 2 August 13th, 2007 03:36 AM
ASP Advanced Search cancer10 Classic ASP Databases 11 October 17th, 2006 08:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.