0

我是 PHP 新手,所以如果我的问题不清楚,我很抱歉。

我的目标是从一个简单的 HTML 表单中获取信息,该表单要求用户填写三个文本框,选择他们是用户 1 还是用户 2,然后上传文件。然后,我必须将信息存储在文本文件中,并在浏览器中直接在 HTML 表单下方显示该信息。信息必须按在第一个文本框中输入的任何内容的第一个字母按字母顺序排列。每个条目必须在自己的行上。

例如:

假设第 1 个人进入:Sally Mae Johnson 用户 1 Flowers.jpg 第 2 个人稍后出现并输入:George Michael Johnson 用户 2 books.jpg

现在它按输入的顺序显示如下: Sally Mae Johnson User 1 Flowers.jpg George Michael Johnson User 2 books.jpg

我需要它按名字的第一个字母按字母顺序显示,如下所示: George Michael Johnson user 2 books.jpg Sally Mae Johnson User 1 Flowers.jpg

这是我的代码:

$fone = @$_POST["one"];
$ftwo = @$_POST["two"];
$fthree = @$_POST["three"];
$fselect = @$_POST["select"];

if ($_FILES)
{
    $name = $_FILES['upload']['name'];
    (move_uploaded_file($_FILES['upload']['tmp_name'], "uploads/$name"));
}


//write to the file

$values = "$fone\t";
$values .= "$ftwo\t";
$values .= "$fthree\t";
$values .= "$fselect\t"; 
$values .= "<img src='uploads/$name' /><br />";


//open and write to the file

$fp = @fopen("store.txt", "a") or die("Couldn't open the file!");
$numBytes = @fwrite($fp, $values) or die ("Couldn't write values to file!");


@fclose($fp);


?>
<form action="test_it.php" enctype="multipart/form-data" method="post">
Box 1: <input type="text" name="one" size="15" /> 
Box 2: <input type="text" name="two" size="15" />
Box 3: <input type="text" name="three" size="15" />
Select One: <select name="select"><option value="empty">Please Select</option><option value="user1">User 1</option>
<option value="user2">User 2</option>
<p>Upload a File:</p>
<p><input type="file" name="upload" />
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input type="submit" name="submit" value="submit" />
<input type="hidden" name="submitted" value="submitted" />
</p>
</form>
<?php

print "<P>Here are the users:<br />";
$file = "store.txt";

if (file_exists($file))
{
    $file1 = fopen("store.txt" , "r");
        while (!feof($file1))
        {
            $display = fgets($file1, filesize("store.txt"));
            echo $display . "
            ";
            }
            fclose($file1);
            }
            else
            {
                echo "<P>Error occured! Please try again!</p>";
            }

任何帮助是极大的赞赏!提前致谢。

4

2 回答 2

0
$fone = @$_POST["one"];
$ftwo = @$_POST["two"];
$fthree = @$_POST["three"];
$fselect = @$_POST["select"];

if ($_FILES)
{
    $name = $_FILES['upload']['name'];
    (move_uploaded_file($_FILES['upload']['tmp_name'], "uploads/$name"));
}


// sort the entries 
$entries = array($fone, $ftwo, $fthree, $fselect);
sort($entries);

$values = implode("\t", $entries)."\t";
$values .= "<img src='uploads/$name' /><br />";


//open and write to the file

$fp = @fopen("store.txt", "a") or die("Couldn't open the file!");
$numBytes = @fwrite($fp, $values) or die ("Couldn't write values to file!");


@fclose($fp);
于 2012-05-03T18:08:07.440 回答
0

尝试先创建一个数组。

$valuesArray = array();
$valuesArray[] = $fone;
    ... etc.

然后,

$valuesArray = sort($valuesArray);

最后,准备正文:

$values = $valuesArray[0] . "\t";
    ... etc.

并写入文件。

$numBytes = @fwrite($fp, $values) or die ("Couldn't write values to file!");
于 2012-05-03T17:37:04.360 回答