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 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,...,...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体