我有一个上传text.txt文件的表单,其中包含action =“profit-process.php”
在profit-process.php中,我将.txt文件转换为数组:
<?php
$file = "text.txt";// Your Temp Uploaded file
$handle = fopen($file, "r"); // Make all conditions to avoid errors
$read = file_get_contents($file); //read
$lines = explode("\n", $read);//get
foreach($lines as $key => $value){
$code[] = $value[0];
$name[] = $value[1];
$cost[] = $value[2];
$selling_price[] = $value[3];
}
echo "<pre>";
print_r($lines); //explore results
echo "</pre>";
?>
我想要做的是使用表而不是print_r($ lines)在.txt(进入数组)中显示数据。
所以目标格式是:
<table>
<tr><th>Code</th><th>Name</th><th>cost</th><th>Selling Price</th></tr>
<tr><td>1234</td><td>Nike Air</td><td>30.00</td><td>60.00</td></tr>
</table>
将有多行文本文件,因此表中的行数将反映文本文件数据。
任何帮助都非常感激。
干杯
答案 0 :(得分:0)
你的最终节目应该是:
注意:首先阅读extract()。
<?php
$file = "text.txt";// Your Temp Uploaded file
$handle = fopen($file, "r"); // Make all conditions to avoid errors
$read = file_get_contents($file); //read
$lines = explode("\n", $read);//get
$arr = array();
foreach($lines as $key => $value){
$temp = array();
$temp['code'] = $value[0];
$temp['name'] = $value[1];
$temp['cost'] = $value[2];
$temp['selling_price'] = $value[3];
$arr[] = $temp;
}
echo "<pre>";
print_r($lines); //explore results
echo "</pre>";
?>
<table border="1" align="center">
<tr>
<th>Code</th>
<th>Name</th>
<th>Cost</th>
<th>Selling Price</th>
</tr>
<?php
if (! empty($arr)) {
foreach ($arr as $elem) {
extract($elem); // EXTRACT VARIABLES SO THAT, $elem['cost'] becomes $cost, read PHP documentation on extract();
?>
<tr>
<td><?php echo $code;?></td>
<td><?php echo $name;?></td>
<td><?php echo $cost;?></td>
<td><?php echo $selling_price;?></td>
</tr>
<?php
}
}
?>
</table>
答案 1 :(得分:0)
尝试 -
<table>
<tr><th>Code</th><th>Name</th><th>cost</th><th>Selling Price</th></tr>
<?php
foreach($lines as $key => $value) {
?>
<tr><td><?php echo $value[0]?></td><td><?php echo $value[1]?></td><td><?php echo $value[2]?></td><td><?php echo $value[3]?></td></tr>
<?php
}
?>
</table>
答案 2 :(得分:0)
试试这个
<table>
<tr><th>Code</th><th>Name</th><th>cost</th><th>Selling Price</th></tr>
<?php for($i=0;$i<count($code);$i++):?>
<tr><td><?php echo $code[$i];?></td>
<td><?php echo $name[$i];?></td>
<td><?php echo $cost[$i];?></td>
<td><?php echo $selling_price[$i];?></td></tr>
<?php endforeach;?>
</table>