这里是我创建另一个文件的php代码:
$title = $_POST['title'];
$location = $_POST['location'];
$category = $_POST['category'];
$b = './'.$title.".html";
$a = fopen("$b", "w");
fwrite($a, $title);
fwrite($a, $location);
fwrite($a, $category);
fclose($a);
这里只有几行变量,我还有更多的变量需要编写。我的问题是如何有效地使用while函数,这样我就不需要重复fwrite那么多次,或者请教我一个更好的方法。。?
Thx提前
您必须读入所有行并创建一个数组。然后,您可以简单地将这些行再次写入另一个文件。
$file = fopen('file.txt', 'w');
foreach ($yourLines as $lines)
{
fwrite($file, $lines->getAttribute('id') . "'n");
}
将所有变量发布在一个数组中,如下所示:
$vararr[] = $_POST['title'];
$vararr[] = $_POST['location'];
foreach($vararr as $data){
fwrite($a, $data);
}
只是一个建议,你可以将你的行添加到一个数组中,并以这种方式添加,例如:
$file = fopen('file.txt', 'w');
foreach ($yourValues as $lines)
{
fwrite($file, $lines->getAttribute('id') . "'n");
}
您可以在数组中一次性获取所有表单值,然后循环遍历。
$data = array();
foreach($POST as $key => $value){
$data[$key] = $value;
}
foreach($data as $key => $line){
$string = "$key = $line";
fwrite($a, $string);
}