haug1 Posted September 17, 2015 Share Posted September 17, 2015 (edited) Hello, I just started coding and I'm having some troubles getting my code working. It's just supposed to let me search for a string written in a "database" .txt document. It's supposed to get the search word from the value in the <select> thingie. Most of the variables and stuff are written in Norwegian, I hope that's not too confusing! Edit: When I run it, everything shows up nicely, but it just won't find matches with the database. <!doctype html> <html lang="no"> <meta charset="UTF-8"> <head> <title>Vis klasseliste</title> </head> <a href="klassereg.php">Registrer klasse</a> <a href="visklasse.php">Vis alle klasser</a> <a href="studentreg.php">Registrer student</a> <a href="visstudent.php">Vis alle studenter</a> <a href="klasseliste.php">Vis klasseliste</a> <h1>Vis klasseliste</h1> <br> <form method="POST" action="" name="visklasse" id="visklasse"> Klassekode: <select name="klassekode" id="klassekode"> <option value="">Velg klasse..</option> <?php $fil=fopen("x/klasse.txt","r"); while ($linje = fgets ($fil)) { $del=explode("!", $linje); print("<option value='$del[0]'>$del[0]</option>"); } fclose ($fil); ?> </select> <input type="submit" name="submit" id="submit" value="Vis klasse"><br> </form> <?php @$klassekode=$_POST["klassekode"]; $filb=fopen("x/student.txt", "r"); $treff="0"; while ($linjeb = fgets($filb)) { $delb=explode("!", $linjeb); $brukernavn=trim($delb[0]); $fornavn=trim($delb[1]); $etternavn=trim($delb[2]); $klassekodeb=trim($delb[3]); if ($klassekode == $klassekodeb) { $treff++; print("$brukernavn $fornavn $etternavn<br>"); } } print("<br>Antall treff: $treff"); ?> </html> Edited September 17, 2015 by haug1 Quote Link to comment https://forums.phpfreaks.com/topic/298198-why-is-my-code-not-working/ Share on other sites More sharing options...
hansford Posted September 17, 2015 Share Posted September 17, 2015 what does klasse.txt look like ? Post that file. Quote Link to comment https://forums.phpfreaks.com/topic/298198-why-is-my-code-not-working/#findComment-1521050 Share on other sites More sharing options...
Barand Posted September 17, 2015 Share Posted September 17, 2015 ... and is the file being opened? Check the value of $fil after opening to make sure it isn't = false. Quote Link to comment https://forums.phpfreaks.com/topic/298198-why-is-my-code-not-working/#findComment-1521052 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.