Forum Discussion

streamotor's avatar
streamotor
Visitor
8 years ago

Writing a separate search function

Since we can no longer use the roSearch, I am working on writing a search algorithm for my code based on the heroScreen example. I have the keyboard and can get what the user types in, but after that I could use some help on where to begin with the algorithm

5 Replies

  • destruk's avatar
    destruk
    Streaming Star
    If you are using a server, send the search term to a script on the server and have the server return matches.
    If you're doing this on the end user device, you would need to loop through the content list and run your matches on the keys for the content like title, description, actors, etc.  Any match found would be copied into a results string, or copied into a result content set for display.
  • "destruk" wrote:
    If you are using a server, send the search term to a script on the server and have the server return matches.
    If you're doing this on the end user device, you would need to loop through the content list and run your matches on the keys for the content like title, description, actors, etc.  Any match found would be copied into a results string, or copied into a result content set for display.

    Do you have any sample code as an example? I can picture it in my head but still am wondering where to get started.
  • I am also trying to be able to make this possible I would like to know of some example to be able to begin
  • destruk's avatar
    destruk
    Streaming Star
    A minimal server-side sample with a database -

    <?php
    include 'includevars.php';

    //this file is only utilized by Roku Interface on back end of server using a GET request.

    if ($_SERVER["REQUEST_METHOD"] == "GET")
    {
    $con=@new mysqli($sitename,$masteraccessname,$masteraccesspw,$databasename);
    if (mysqli_connect_errno())
    {
    exit("Unable to connect to database.");
    }

    // read the post from ROKU
    $value2 = urldecode(stripslashes($_GET["Term"])); //search term

    if($value2=="")
    {
    $_xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>".chr(13);
    $_xml.="<feed>".chr(13);
    $_xml.="<item>".chr(13);
    $_xml.="<thumbnail>".$rootdir."Empty.jpg</thumbnail>".chr(13);
    $_xml.="<title>Placeholder</title>".chr(13);
    $_xml.="<streamUrl>".$baseserverip."Empty.mp4</streamUrl>".chr(13);
    $_xml.="</item>".chr(13);
    $_xml.="</feed>".chr(13);
    echo $_xml;
    exit();
    }

    if (strlen($value2)>2)
    {
    $sql="SELECT * FROM ".$maindbname." WHERE title LIKE '%".$value2."%' OR description LIKE '%".$value2."%' ORDER BY title"; //Do search query
    }
    else
    {
    $sql="SELECT * FROM ".$maindbname." WHERE title LIKE '".$value2."%' ORDER BY title"; //Do search query based on first letters of filename
    }

    $res=mysqli_query($con, $sql)
    or die("Error: ".mysqli_error($con));
    if (mysqli_num_rows($res)==0) //no shows
    {
    $_xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>".chr(13);
    $_xml.="<feed>".chr(13);
    $_xml.="<item>".chr(13);
    $_xml.="<thumbnail>".$rootdir."Empty.jpg</thumbnail>".chr(13);
    $_xml.="<title>Placeholder</title>".chr(13);
    $_xml.="<streamUrl>".$baseserverip."Empty.mp4</streamUrl>".chr(13);
    $_xml.="</item>".chr(13);
    $_xml.="</feed>".chr(13);
    echo $_xml;
    }
    else
    {
    $mx=0;
    while ($info=mysqli_fetch_array($res,MYSQLI_NUM))
    {
    $title[$mx]=$info[0]; //title
    $thumbnail[$mx]=$info[1]; //thumbnail
    $streamurl[$mx]=$info[2]; //streamurl
    $mx++;
    }

    $_xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>".chr(13);
    $_xml.="<feed>".chr(13);
    For ($i=0;$i<$mx;$i++)
    {
    $_xml.="<item>".chr(13);
    $_xml.="<title>".$title[$i]."</title>".chr(13);
    $_xml.="<thumbnail>".$thumbnail[$i]."</thumbnail>".chr(13);
    $_xml.="<streamUrl>".$streamurl[$i]."</streamUrl>".chr(13);
    $_xml.="</item>".chr(13);
    }
    $_xml.="</feed>".chr(13);
    echo $_xml;
    }
    mysqli_close($con);
    }

    ?>

  • destruk's avatar
    destruk
    Streaming Star
    Another tip to keep in mind -
    If you have a whole lot of TV shows for content and they are named SxxExx (Season/Episode) then you don't want those to be spammed titles when a user searches for a title starting with "S", so you'll want to check if it's an S and eliminate those from the results, or check for S and require two consecutive letters to search for title matches.  Alternately, if your episodes are all numeric then you'll want to ignore title matches if the search term starts with a number.  I'm sure there are more efficient/better methods of searching but the code posted is what I use and it gets the job done for what I required.