belbin09 Posted November 9, 2017 Share Posted November 9, 2017 I have a form that asks for the users name and student number. Then in a separate script I going to test it against a txt file that looks like this: Jim Smith, 400424565 Sarah Hillier, 534712479 Jonathan Quinlan, 764134296 Keith Roberts, 123456789 Sarah Hillier, 200343656 Chloe Butler, 678123987 I am new to php so I am not sure where I went wrong. I keep getting warnings: Notice: Undefined index: snumber in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 9Warning: in_array() expects parameter 2 to be array, string given in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 21 enrolled.php <?php $students = "student.txt"; // text file for students and student number //converting a string into a variable $name = $_POST["name"]; $number = $_POST["snumber"]; //open student file and explode into an array $sfile = fopen($students, 'r+') or die ("Student file does not exist"); while ($sline = fgets ($sfile)) { $list = explode(",", $sline); //test array against text input if (in_array($name, $list[0])) { if (in_array($number, $list[1])) { echo $name; echo $number; } // end of inner if } //end of outer if } // end of while fclose($sfile) ?> Quote Link to comment https://forums.phpfreaks.com/topic/305606-text-input-to-variable-then-variable-to-array/ Share on other sites More sharing options...
Barand Posted November 9, 2017 Share Posted November 9, 2017 Are you sure your input form has an input with name "snumber"? The second message is self-explanatory - $list[0] and $list[1] are not arrays. However, $list is an array. Quote Link to comment https://forums.phpfreaks.com/topic/305606-text-input-to-variable-then-variable-to-array/#findComment-1553606 Share on other sites More sharing options...
Barand Posted November 9, 2017 Share Posted November 9, 2017 P.S. You should also note that $sline will contain the trailing linefeed character. You need to trim() the line before exploding otherwise $line[1] will also have the linefeed and will not, therefore, match the number. Quote Link to comment https://forums.phpfreaks.com/topic/305606-text-input-to-variable-then-variable-to-array/#findComment-1553608 Share on other sites More sharing options...
phpmillion Posted November 9, 2017 Share Posted November 9, 2017 Didn't you post this on other forums yesterday? I believe I answered your question. Quote Link to comment https://forums.phpfreaks.com/topic/305606-text-input-to-variable-then-variable-to-array/#findComment-1553610 Share on other sites More sharing options...
belbin09 Posted November 9, 2017 Author Share Posted November 9, 2017 I figured out what was wrong with the text name snumber. I forgot the equal sign on the html side of the form. Made the correction from list[1] to list. No errors. However without list[1] meaning the name how will it be compared to the txt file for name and number? Quote Link to comment https://forums.phpfreaks.com/topic/305606-text-input-to-variable-then-variable-to-array/#findComment-1553611 Share on other sites More sharing options...
Barand Posted November 9, 2017 Share Posted November 9, 2017 You can use if ($name == $list[0]or if (in_array($name, $list)) Similar for the number using $list[1]. Don't forget the trim(); Quote Link to comment https://forums.phpfreaks.com/topic/305606-text-input-to-variable-then-variable-to-array/#findComment-1553612 Share on other sites More sharing options...
belbin09 Posted November 9, 2017 Author Share Posted November 9, 2017 Didn't you post this on other forums yesterday? I believe I answered your question. You did. Where I rewrote my code I wasn't sure if the same answer applied. Quote Link to comment https://forums.phpfreaks.com/topic/305606-text-input-to-variable-then-variable-to-array/#findComment-1553613 Share on other sites More sharing options...
belbin09 Posted November 9, 2017 Author Share Posted November 9, 2017 You can use if ($name == $list[0]or if (in_array($name, $list))Similar for the number using $list[1]. Don't forget the trim(); Thank you. My code is now running Quote Link to comment https://forums.phpfreaks.com/topic/305606-text-input-to-variable-then-variable-to-array/#findComment-1553614 Share on other sites More sharing options...
belbin09 Posted November 9, 2017 Author Share Posted November 9, 2017 I was having someone help me but they are confusing me more than anything. So I do apologize for the questions. I have the code reading the text file perfectly. However as soon as I include the else statement it outputs it 6 times which I know is because its within the while statement. However when I leave it out of the while statement and create a new if statement it will always show the form if ($name != $list[0] && $number != $list[1]) { include 'index.php'; } while ($sline = fgets ($sfile)) { $list = explode(",",trim ($sline)); //test array against text input if ($name == $list[0] && $number == $list[1]) { echo "Welcome $name $number"; } else { include 'index.php'; } } // end of while fclose($sfile) ?> Quote Link to comment https://forums.phpfreaks.com/topic/305606-text-input-to-variable-then-variable-to-array/#findComment-1553615 Share on other sites More sharing options...
Barand Posted November 9, 2017 Share Posted November 9, 2017 Try something like this found = 0 while (get next line) { if number and name match { found = 1 echo welcome message break; } } if not found { include form } Quote Link to comment https://forums.phpfreaks.com/topic/305606-text-input-to-variable-then-variable-to-array/#findComment-1553616 Share on other sites More sharing options...
belbin09 Posted November 9, 2017 Author Share Posted November 9, 2017 Try something like this found = 0 while (get next line) { if number and name match { found = 1 echo welcome message break; } } if not found { include form } That worked brilliantly! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/305606-text-input-to-variable-then-variable-to-array/#findComment-1553618 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.