PHP Ignoring all but last array element -
i'm playing around basic php site own knowledge , have text file filled "passwords". i've opened text file array page, when search specific element, password shows valid last option, , seems ignore every other element in array.
so if password list is
passwords
12345
test
qwerty
when search first 3 elements, says not exist in array. however, if searched last value, 'qwerty' here, match.
<?php $file = explode("\n", file_get_contents("passwords.txt")); //call function , pass in list if (isset($_post['submit_login'])) { //set 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 $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 found in index #" .$key. "<br />"; } else { echo "you searched for: " . $search_query . "</br>"; echo "your search did not match of items. <br />"; echo "<a href='searchform.html'>try again?</a>"; } } ?>
however, if searched '12345' example, index 1 in array, output
you searched for: 12345
your search did not match of items.
but, searching 'qwerty' last element yields desired response.
don't make complicated, use this:
(here first read file file()
. use array_search()
see if value in array , key)
<?php $lines = file("passwords.txt", file_ignore_new_lines) //call function , pass in list if (isset($_post['submit_login'])) { //set 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 found in index #" .$key. "<br />"; } else { echo "you searched for: " . $search_query . "</br>"; echo "your search did not match of items. <br />"; echo "<a href='searchform.html'>try again?</a>"; } } ?>
Comments
Post a Comment