php - Limiting Maximum Number of Search Results

I have the php file linked to xml file with a number of links. My aim is to limit the maximum number of search results to show up up-on typing in the search box. For example to have only 8 or 10 results. I don't know where to start. How can I go about this?.

php file

<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");

$x=$xmlDoc->getElementsByTagName('link');

//get the q parameter from URL
$q=$_GET["q"];

//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
  $hint="";
  for($i=0; $i<($x->length); $i++) {
    $y=$x->item($i)->getElementsByTagName('title');
    $z=$x->item($i)->getElementsByTagName('url');
    if ($y->item(0)->nodeType==1) {
      //find a link matching the search text
      if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
        if ($hint=="") {
          $hint="<a href='" .
          $z->item(0)->childNodes->item(0)->nodeValue .
          "' target='_blank'>" .
          $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        } else {
          $hint=$hint . "<br /><a href='" .
          $z->item(0)->childNodes->item(0)->nodeValue .
          "' target='_blank'>" .
          $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        }
      }
    }
  }
}

// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
  $response="no suggestion";
} else {
  $response=$hint;
}

//output the response
echo $response;
?>

Answer

Solution:

My understanding of the issue is that you want to limit the amount of hints returned from the search. You aren't attempting to stop the search but rather to just restrict the output?

The changes which I've made is to turn $hint into an array, then at the bottom of the code it will use array_slice to start at the first position and return $amountToShow. Adding the element to $hint is done with sprintf that's purely for readability sake.

<?php

$xmlDoc = new DOMDocument();
$xmlDoc->load("links.xml");

$x = $xmlDoc->getElementsByTagName('link');

//get the q parameter from URL
$q = $_GET["q"];
$hint = [];

//lookup all links from the xml file if length of q>0
if (strlen($q) > 0) {
    for ($i = 0; $i < ($x->length); $i++) {
        $y = $x->item($i)->getElementsByTagName('title');
        $z = $x->item($i)->getElementsByTagName('url');
        if ($y->item(0)->nodeType == 1) {
            //find a link matching the search text
            if (stristr($y->item(0)->childNodes->item(0)->nodeValue, $q)) {
                $hint[] = sprintf(
                    "<a href='%s' target='_blank'>%s</a>",
                    $z->item(0)->childNodes->item(0)->nodeValue,
                    $y->item(0)->childNodes->item(0)->nodeValue
                );
            }
        }
    }
}

// Set output to "no suggestion" if no hint was found
// or to the correct values
$amountToShow = 10;
if (count($hint) == 0) {
    echo "no suggestion";
} else {
    echo implode("", array_slice($hint, 0, $amountToShow));
}

Source