0

I have a standard multi dimensional array listed below that will house my customers faq articles. The articles will include a title, a description and various tags per article. I am trying to setup a form on my website where a visitor can search any content inside the array. If there is a match, the site will return the article that matched the tag that was searched for.

My questions.

  1. Is this the best way to offer an FAQ search or should I be looking at something doing something with javascript and json?
  2. How should I search through the array and identify the correct article that includes the tag the visitor searched for?

Here is my array.

function get_articles_all() {

$articles = array();

$articles[0] = array(

    "title" => "What port does the Cisco TSP use?",
    "body" => "TAPI talks to the TAPI Service Provider (TSP) and the TSP talks to the CTI manager through port 2748.",
    "tag" => array("port","cisco")

    );

$articles[1] = array(

    "title" => "Title of second article.",
    "body" => "Body of second article. Body of second article. Body of second article. Body of second article. Body of second article. Body of second article. Body of second article. Rocky.",
    "tag" => array("second","number2")

    );

$articles[2] = array(

    "title" => "Title of third article.",
    "body" => "Body of third article. Body of third article. Body of third article. Body of third article. Body of third article. Body of third article. Body of third article. Rocky.",
    "tag" => array("third","number3")

    );

return $articles;

}

Here is my search function.

function article_tag_search($t) {
  $tagResults = array();
  $all = get_articles_all();

  foreach ($all as $article => $key) {

        if (stripos($key,$t) !== false) {
            $tagResults[] = $article;
       }

  }
  return $tagResults;
}   

Here is what I am using to test the code.

 $test = article_tag_search("port");

 echo '<pre>';
 print_r($test);
 echo '</pre>';

It results in the following output.

 Array
 (
   [0] => 0
   [1] => 1
   [2] => 2
 )

Any assistance would be greatly appreciated.

Dustin

  • You have arrays inside your array, so you need an extra `for` loop. `$key` in your `for` loop is for example `$articles[0] = array( .... )`. – putvande Jun 26 '15 at 21:20
  • possible duplicate of [in\_array() and multidimensional array](http://stackoverflow.com/questions/4128323/in-array-and-multidimensional-array) – putvande Jun 26 '15 at 21:22

1 Answers1

0

Is your FAQ multi-page? (eg, every question has a (pretty long) answer on a different page) If so, it should be ok to use the solution you're currently using. Although, from a UX point of view, I would appreciate one page with all questions listed one beneath the other, and a search input for filtering the list (with Javascript).

If your FAQ contents aren't too much, you can load them with a single AJAX call, else you can 'cap' the requests and load them gradually (eg. Facebook, if I remember correctly, sets a max. of 25 posts to retrieve from a public page at once).

As for question 2, I think a better (more portable) solution would be:

function get_items_by($prop, $value) {
    global $articles;
    $result = array();
    for ($i = 0; $i < count($articles); $i++) {
        // below split in else if just for readability
        if (is_array($articles[$i][$prop]) && in_array($value, $articles[$i][$prop]))
            array_push($result, $i);
        else if (is_string($articles[$i][$prop]) && strpos($articles[$i][$prop], $value) > -1)
            array_push($result, $i);            
    }
    return $result;
}

The function above will return the indexes of any array items whose $prop is either (1) an array and has a value in it matching $value, or (2) a string in which the $value is found. Use like so:

get_items_by('tag', 'port'); // returns [0]
get_items_by('body', 'Body of'); // returns [1,2]

Of course be aware that the function needs some customization if, eg, a search for parts of a tag should return results as well.

Instead of returning the indexes you could just as well return the items (array_push($result, $articles[$i])), and update your list with AJAX when the user hits the search button/ enter. An example with jQuery:

(JS)

$('#search-button').on('click', function() {
   var searchFor = $('#search-field').val();
   $.ajax({
     url: "your/PHPFile.php",
     data: {search: searchFor}
   }).done(function(data) {
     var data = $.parseJSON(data);
     $.each(data, function(item) {
       // here you have access to each article's title, body, etc.. for output
     });
   });
});

(PHP handler file)

if (isset($_REQUEST) && isset($_REQUEST['search'])) {
  echo json_encode(get_items_by('tag', $_REQUEST['search']));
  die();
}
Tyblitz
  • 7,777
  • 3
  • 21
  • 45
  • Hi Tyblitz. Thanks for the response. The faq is single page only and the faq items are as long as the ones in my example. I think @putvande might be correct in that I need another foreach. I really am not sure if I should be doing this via javascript or php. I will attempt to use your fix and let you know. – darmstrong Jun 29 '15 at 11:49