5 Replies - 1205 Views - Last Post: 02 September 2011 - 12:12 AM Rate Topic: -----

#1 serunox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 01-September 11

Comparing element from text file or array with entered data

Posted 01 September 2011 - 04:09 AM

Hey all.Im having trouble with a part of an assignment.Not asking for someone to do it,just asking for help :bigsmile:
I need to make a website with php functions.A user needs to provide a user ID in as a number.I need to check the number in a text file with a bunch of other user numbers.I need to use the text file. Ive read the file into an array,and then I went through the array and compared each item with the entered number.But somehow it doesnt work properly.

Heres different ways I tried it so far.Theres a bunch of stuff I print for myself for now and the results in the if statements are just plain print outs for me to see if it works or not.

Hope the code is in good order
<?php 
        if (isset($_POST['submit'])) {
            $handle = @fopen("studentID.txt", "r"); //Open file
            $token = $_POST['userID']; //Put the user id in var token
            $intArrCount=0;  //Just a counter for the no of elements

            if ($handle) {
              $superbuffer = array();  
                while (!feof($handle)) {
                  $buffer = fgets($handle); //read file into string
                  $superbuffer[] = $buffer; //read string into array
                  $intArrCount++; 
                } 
              fclose($handle);
            }
              echo join('<br>',$superbuffer); //Displaying the array for my own use 
              echo "<br>Array Element Count= $intArrCount <br><br>"; //displaying the count for my own use               

            //This is a part i tried that didnt work I commeted this part out when testing the rest
            /*  for($intCount=0;$intCount<$intArrCount;$intCount++){ 

                    echo "The Array Counter=$intCount <br>"; //Information for me
                    echo "Token= $token<br>";                //Information for me
                    echo "If Statement Begin<br>";          //Information for me

                //here I compare the enterd ID with the text.
                    if($superbuffer[$intCount]==$token){  
                        echo "ID found br>";
                        break;
                    }else{
                        echo "Nothing found....<br><br>"; //in this part doesnt matter what id i enter,it keeps going to the else part.
                    }
               }*/

                 //another piece that didnt work using the for_each statement
                foreach ($superbuffer as $value){
                    if($value==$token){
                        echo "value= $value & token= $token<br>"; //information for me
                        echo "Correct IDbr>";

                        break;
                    }else { echo "value= $value & token= $token<br>"; //keeps going to else part
                            echo "Wrong ID<br>";
                    }
                }
?>



So the problem I get,according to my theory,the entered ID should be compared with each element,and if its equal to one of the elements it should display ID found or Correct ID.But somehow it skips that part and goes onto the else part in the if statement and displays wrong ID even if its the right ID.If I make it != instead of == in the if statements,I get into the first part of the if statement but it doesnt iterate past the first element,meaning I get a correct ID even if its the wrong one.

What am I doing wrong?

This post has been edited by serunox: 01 September 2011 - 04:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Comparing element from text file or array with entered data

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Comparing element from text file or array with entered data

Posted 01 September 2011 - 05:44 AM

Could you post the output from your IF statement on lines 37 - 44?

P.S. XML would be a perfect format for you to store your data in since databases may be out of the question for your assignment. It would also be easier to retrieve the data using Objects like DOM and SimpleXML instead of having to manually iterate the file.

Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Comparing element from text file or array with entered data

Posted 01 September 2011 - 05:51 AM

For starters, if your assignment is to use functions then you really ought to use them!
Was This Post Helpful? 1
  • +
  • -

#4 serunox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 01-September 11

Re: Comparing element from text file or array with entered data

Posted 01 September 2011 - 02:49 PM

Cant really use XML.I know XML but the guy didnt "teach" it to us in the class.We should only use what he taught us html,javascript,cgi and php.So I need to use the text file and compare the entered ID with that in the file.

CTphpnwb Ill check out functions,he didnt mention it to us but Ill check it out.


foreach ($superbuffer as $value){
                    if($value==$token){
                        echo "value= $value & token= $token<br>";
                        echo "<h1>Correct ID!</h1><br>";
                        echo "$value your file has been submitted.<br>";
                        break;
                    }else { echo "value= $value & token= $token<br>"; echo "<h1>Wrong ID</h1><br>";}
                }

This puts out the following,the ID's displayed are in the file.After that I display what is being looked for in the token,and the current element in the array.As you can see it picks it up right but doesnt compare the two.

09100444
09100443
09100442
09100441
Array Count= 4

value= 09100444 & token= 09100444

Wrong ID

value= 09100443 & token= 09100444

Wrong ID

value= 09100442 & token= 09100444

Wrong ID

value= 09100441 & token= 09100444

Wrong ID

Am I correct that break; ,in line 6 in this current post, in the if statement will exit the whole loop?
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,224
  • Joined: 23-August 08

Re: Comparing element from text file or array with entered data

Posted 01 September 2011 - 04:39 PM

fgets includes the newline from the data. Try stripping it off with trim() before adding it to your array.
Was This Post Helpful? 0
  • +
  • -

#6 serunox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 01-September 11

Re: Comparing element from text file or array with entered data

Posted 02 September 2011 - 12:12 AM

I got it!

Used trim() for the values and then strcmp() to compare the two elements inside the foreach statement.Compared the two,if the return value is 0 it displays correct ID,if not I get wrong ID which shows me it works. Now for my next challenge,checking passwords along side the ID's :D

Thanks for the help guys,might return again with more problems
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1