0

I have an array that is linked to a text file where I need the information of a students name and student number that has been entered by user input.

if (isset ($_POST['stnum']) && isset ($_POST['stname'])) 
    {
        $studentNum = htmlentities ($_POST['stnum']);
        $studentName = htmlentities ($_POST['stname']);
        $DB = fopen ($students, 'r') or die ("$students cannot be opened for reading.");

        while ($record = fgets ($DB) and ! $foundNum and ! $foundName) 
        {
            $studentField = explode ("$$", htmlentities (trim ($record)));

            $foundNum = $studentNum === $studentField[0];
            $foundName = $studentName === $studentField[1];  
        }

        fclose ($DB);

        if ($foundNum && $foundName) 
        {
            echo $studentField[0], $studentField[1];
        }
    }

I cannot figure out how to search for the students that have the same name but different student number. And the file is written like this

DA-708-3304$$Elizabeth Organ
GB-217-1214$$John Alexander
SE-412-2175$$Odell Thomas
SH-433-3012$$John Saunders
HU-737-1176$$Frederica Elias
DU-941-4244$$Nancy Sauceda
CC-671-5984$$Margaret Coppa
DA-220-7070$$Walter Snyder
HU-658-4475$$Elizabeth Organ
DU-255-9787$$John Saunders
CC-777-8752$$Hubert Green

For example If I try searching for John Saunders with a student number DU-255-9787 it doesn't work but John Saunders SH-433-3012 does because it appears first in the file.

4
  • Not an answer but you have a duplicate line before the while block: "$studentField = explode ("$$", htmlentities (trim ($record)));" Commented Mar 13, 2016 at 16:56
  • Yes I did notice that just a couple of minutes after posting, thank you for that though Commented Mar 13, 2016 at 17:00
  • Google for "return all array entries similar" ... stackoverflow.com/questions/19966490/… Commented Mar 13, 2016 at 17:04
  • I am sure you don't need htmlentities() here. Commented Mar 13, 2016 at 17:04

1 Answer 1

2

Why not use break within the if condition? When the condition satisfies?

 while ($record = fgets ($DB) and ! $foundNum and ! $foundName) {
     $studentField = explode ("$$", htmlentities (trim ($record)));

     if (($studentName === $studentField[1]) &&  empty($studentNum))) {

          /* When only name is entered by user. So, the user whose number appears first in the list should be printed. */ 

         echo $studentField[1].", ".$studentField[0];
         break;

     } else if (($studentNum === $studentField[0]) &&  ($studentName === $studentField[1])) { 

        /* When both name and number is entered by user */

         echo $studentField[1].", ".$studentField[0];
         break;
      }


  }

Hope this helps.

Peace! xD

Sign up to request clarification or add additional context in comments.

5 Comments

Using break statement within a loop provides a lot of flexibility in your code since you want to get only the first search result.
This doesn't answer his question... Neither it solves his issue.
Yes, it does. You may wanna take a closer look before you jump into conclusions, mate. :)
Yeah, after you've edited a big chunk of code? Also what's about putting variables within single quotes? '$studentField[0], $studentField[1]' That's not going to output any data. Anyway your answer got accepted, good luck with your code.
Good point about the single quotes, bud. Thanks! I've edited it now. It was a syntax error, though. It's the logic and optimization that matters a lot more. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.