Jessie Chaisson
Jessie Chaisson

Reputation: 43

PHP Ignoring all but last array element

I'm playing around with a basic PHP site just for my own knowledge and I basically have a text file filled with "passwords". I've opened this text file as an array into my page, but when I search for a specific element, the only password that shows as valid is the last option, and it seems to ignore every other element in the array.

So if my password list is

passwords

12345

test

qwerty

When I search for the first 3 elements, it says that it does not exist in the array. However, if I searched for the last value, which is 'qwerty' here, it will match.

<?php
$file = explode("\n", file_get_contents("passwords.txt"));


//Call the function and pass in the list
if (isset($_POST['submit_login'])) {

    //Set the search variable
    $search = $_POST['password'];

    search_array($file, $search);
} else {
    //echo "You did not search! <br />";
    //echo "<a href='searchForm.html'>Try again?</a>";
}

function search_array($array_value, $search_query)
{
    echo "<span><h4>The Array List: </h4><span/>";
    foreach ($array_value as $key => $value) {
        echo $key." ";
        echo $value."<br />";


     }
    if ($value == $search_query) {
        echo "<h5>Search Stuff</h5>";
        echo "You searched for: " . $search_query . "</br>";
        echo "Your search was found in the index #" .$key. "<br />";


    } else {
        echo "You searched for: " . $search_query . "</br>";
        echo "Your search did not match any of the items. <br />";
        echo "<a href='searchForm.html'>Try again?</a>";
    }


}


?>

However, if I searched for '12345' for example, which is index 1 in the array, I will get the output

You searched for: 12345

Your search did not match any of the items.

But, searching for 'qwerty' which is the last element yields the desired response.

Upvotes: 0

Views: 71

Answers (3)

Rizier123
Rizier123

Reputation: 59701

Don't make everything so complicated, just use this:

(Here I first read your file with file(). Then just simply use array_search() to see if the value is in the array and also get the key)

<?php

    $lines = file("passwords.txt", FILE_IGNORE_NEW_LINES)

    //Call the function and pass in the list
    if (isset($_POST['submit_login'])) {

        //Set the search variable
        $search = $_POST['password'];
        search_array($lines, $search);

    } else {
        //echo "You did not search! <br />";
        //echo "<a href='searchForm.html'>Try again?</a>";
    }

    function search_array($array_value, $search_query) {
        echo "<span><h4>The Array List: </h4><span/>";

        if(($key = array_search($search_query, $array_value)) !== FALSE) {
            echo "<h5>Search Stuff</h5>";
            echo "You searched for: " . $search_query . "</br>";
            echo "Your search was found in the index #" .$key. "<br />";
        } else {
            echo "You searched for: " . $search_query . "</br>";
            echo "Your search did not match any of the items. <br />";
            echo "<a href='searchForm.html'>Try again?</a>";
        }

    }

?>

Upvotes: 0

user3415653
user3415653

Reputation: 335

Try to change your "search_array" method to this.

function search_array($array_value, $search_query)
{
    echo "<span><h4>The Array List: </h4><span/>";
    $match = false;
    foreach ($array_value as $key => $value) {
        echo $key." ";
        echo $value."<br />";

        if ($value == $search_query) {
           $match = true;
           break;
        }
     }
    if ($match) {
        echo "<h5>Search Stuff</h5>";
        echo "You searched for: " . $search_query . "</br>";
        echo "Your search was found in the index #" .$key. "<br />";
    } else {
        echo "You searched for: " . $search_query . "</br>";
        echo "Your search did not match any of the items. <br />";
        echo "<a href='searchForm.html'>Try again?</a>";
    }
}

The password check should be inside the foreach loop and after the check you can print some stuff.

Upvotes: 0

jfhs
jfhs

Reputation: 445

You have your if ($value == $search_query) not inside foreach loop. Move closing bracket for foreach below if-else blocks and it should work.

Upvotes: 2

Related Questions