jeudi 29 mars 2018

PHP search doesn't work?

I am trying to create a search algorithm for my website but it doesn't work. It should show the title of an entry when what is searched is inside the title it gets from json files in a folder on the site. My HTML body in search.php:

<body>
<p>Your search: <script type="text/plain" style="display: inline;"><?php
                $query = $_GET['q'];
                echo $query;
                ?></script>

</p>
<p>Results: <?php
                $files = array();
                foreach (glob("jsons/*.json") as $file) {
                  $files[] = $file;
                }

                //echo($files[0]);

                foreach ($files as $file) {
                    $content = file_get_contents($file);
                    $json = json_decode($content);
                    $title = $json->title;
                    //echo 'x'.$title;
                    if (strpos("x".$title,$query !== false)) {
                       echo $title;
                    }
                }
            ?></p>
</body>

The file structure:

root
  jsons
    test.json
  index.html
  search.php

The content of test.json:

{
    "title": "Test",
    "text": "I am a test",
    "attachment": "test.txt"
}

The URL: http://example.com/path/to/page/search.php?q=Test

What I get when accessing the page:

Your search: Test

Results:

What it should return:

Your search: Test

Results:
Test

Does anyone here know why it doesn't find it or does anyone know a easier way?




Aucun commentaire:

Enregistrer un commentaire