简体   繁体   English

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

[英]How can I find 2 elements in an array and return the array in a form in PHP?

I have a little project about databases and arrays where I have a text file database; 我有一个关于数据库和数组的小项目,我有一个文本文件数据库。 call it 'database.txt'. 称之为“ 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. 我有一个页面,希望从2个文本框中运行查询(“用户名”和“密码”),然后单击“提交”按钮。 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> <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. 从这里开始,我希望能够从“ database.txt”文件中调出一个数组,服务器必须在该文件中搜索用户名和密码位于同一数组中的数组(例如,如果PASSWORD和USERNAME位于相同的数组,仅调用数组中的所有元素),然后服务器调用该数组中的所有元素。 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 我希望这些元素进入文本框,以便如果我要编辑该记录(记录是数组),然后在该数组的特定元素所在的框中键入内容,然后单击保存按钮以将记录保存到database.txt文件

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

The array format would be the following: 数组格式如下:

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

enter code here...,...,...,) (the ... indicating other elements) enter code here...,...,...,) (...表示其他元素)

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. 我今年在高中学习了软件开发,所以我的知识有限,但是我知道HTML和PHP的基本操作。 Also a new user to Stackoverflow... 还是Stackoverflow的新用户...

I'm trying to keep the project to just HTML and PHP, and no SQL please (if thats possible)? 我正在尝试将项目保留为HTML和PHP,而不要使用SQL(如果可以的话)? Thanks 谢谢

You can use a text file with comma delimited data to store this. 您可以使用带有逗号分隔数据的文本文件来存储该文件。 Then you can take advantage of PHP's fgetcsv : 然后,您可以利用PHP的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,...,...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MongoDB:如何在多维数组中查找(并返回)元素? - MongoDB: How to find (and return) elements in a multidimensional array? 如何返回带编号的字符串数组的元素? - How can I return elements of an array in a numbered string? 如何为数组中的第一个元素返回属性值,如果没有元素则返回0? - How can I return a property value for the first element in an array or 0 if there are no elements? 如何在javascript中查找和排序数组中对象元素的索引 - How can I find and sort the indexes of the elements of an object in array in javascript 如何将PHP数组返回到调用它的javascript函数 - How can I return a PHP array to the javascript function that called it 如何从PHP数组返回JavaScript对象? - How can I return a JavaScript object from a PHP array? 如何使用ajax将数组从php返回到javascript - how can I return array from php to javascript using ajax 如何在数组 [i] = i 的数组 JavaScript 中查找元素? - How to find elements in array JavaScript where array[i] = i? 我尝试编写一个函数,该函数将返回等于数组中传递的数字的所有数组元素的总和。 我该怎么做? - I try to write a function that will return the sum of all array elements that are equal to the number passed in the array. How can I do it? 如何更改内部元素和数组 - How can I change elements inside and array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM