doulu4976
doulu4976
2015-10-18 02:03
浏览 29

如何在数组中找到2个元素并以PHP形式返回数组?

I have a little project about databases and arrays where I have a text file database; call it 'database.txt'. I have a page that I wish to run a query ('username' and 'password') from 2 text boxes, and then the submit button is clicked. Code below:

<1--LOGIN PAGE--> <form action="process.php" method="post"> Username: <input type="text" name="customer"> Password: <input type="password" name="password"> <button type="submit">Login</button> </form>

From here, I want to be able to recall an array from the 'database.txt' file, where the server must search the file for an array where the username and password are in the same array (eg. IF PASSWORD and USERNAME are in the same ARRAY, RECALL all the elements in the array ONLY) and then the server recalls all the elements in that array. Something like a customer database pretty much. I want these elements to go into text boxes so that if I want to edit that record (the record being the array), I then type in the box where the particular element of the array is and hit a save button to save the record to the database.txt file

<1--PROCESS PAGE--> <p>Username: <?php echo ''.$username.''; ?></p><br /> <p>Other information: <? echo ''.$name.', '.$address.', '.$phone_number.'' //** this area for other elements in array **// ?></p>

The array format would be the following:

`array(username=>'',password=>'',name=>'',address=>'',phone_number=>'',`

enter code here...,...,...,) (the ... indicating other elements)

I have studied Software Development this year in High school so my knowledge is limited, I do however know the basic operations of HTML and PHP. Also a new user to Stackoverflow...

I'm trying to keep the project to just HTML and PHP, and no SQL please (if thats possible)? Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanboshe0001
    duanboshe0001 2015-10-18 07:08
    已采纳

    You can use a text file with comma delimited data to store this. Then you can take advantage of PHP's fgetcsv:

    <?php
    function findUser($filename, $username, $password) {
        $f = fopen($filename, "r");
        $result = false;
        while ($row = fgetcsv($f)) {
            if ($row[0] == $username && $row[1] == $password) {
                $result = $row;
                break;
            }
        }
        fclose($f);
        return $result;
    }
    

    Your text file would look like this inside:

    johndoe,swordfish,John Doe,123 Main St.,111-222-3333,...,...
    janedoe,secret,Jane Doe,123 Other St.,444-555-6666,...,...
    
    点赞 评论

相关推荐