如何循环一个文件来构建一个PHP表
我需要循环浏览一个文件并生成一个包含信息(姓名、电话号码、电子邮件)的表,但我似乎可以得到它。这是到目前为止我的PHP:如何循环一个文件来构建一个PHP表,php,file,loops,Php,File,Loops,我需要循环浏览一个文件并生成一个包含信息(姓名、电话号码、电子邮件)的表,但我似乎可以得到它。这是到目前为止我的PHP: <?php $sortedArr = array(); $file = file("files/info.txt"); foreach($file as $v){ $tempArr = explode(",",$v); $tempArr[1] = substr($tempArr[1],0,-1); $str = "$tempArr[1], $te
<?php
$sortedArr = array();
$file = file("files/info.txt");
foreach($file as $v){
$tempArr = explode(",",$v);
$tempArr[1] = substr($tempArr[1],0,-1);
$str = "$tempArr[1], $tempArr[0]";
array_push($sortedArr,$str);
}
$arrLen = count ($sortedArr);
$rowLen = count ($sortedArr[0]);
$tbl = "<table border= '1'>";
$tbl .= "<tr>";
for ($i=0;$i<$arrLen;$i++)
{
$tbl .= "</tr>";
for ($l=0;$l<$rowLen;$l++)
{
$tbl .= "<td>" . $arr[$i][$l] . "</td>";
}
}
$tbl .= "</tr>";
$tbl .= "</table>";
?>
好的,第一件事是第一件事。在PHP中,使用增量
for()
循环并跟踪$i
几乎从来都不是正确的做法。简单迭代最好使用foreach()
循环来完成。通过切换到foreach
,您将不再需要所有count()
记帐,$i,$l
//从头开始:
$sortedar=array();
$file=file(“files/info.txt”);
foreach($v文件){
$tempArr=爆炸(“,”,$v);
//获取第一个和第二个值
//不确定substr()的作用是什么
//因为它会删除姓氏的最后一个字母。。。
//让我们忽略这一点。
$str=$tempArr[0].'.$tempArr[1];
//看起来您想加入名称0,1
//剩下的就照原样用吧。。。
//将连接的字符串返回到第一个索引中
$tempArr[0]=$str;
//把第二个扔掉,因为它和第一个连在一起了
未设置($tempArr[1]);
//将阵列附加到大屏幕上:
阵列推送($sortedar,$tempArr);
}
//现在打开你的桌子,然后使用2个foreach
//循环以构建行和列,打开和关闭
//在外循环的每次迭代中关闭内部循环。
$tbl=“”;
//外循环为行
foreach($分拣机为$行){
//开始一行,稍后关闭它
$tbl.='';
//内部循环为列
foreach(行作为$col){
$tbl.=''.htmlspecialchars($col.'';
}
$tbl.='';
}
//关上你的桌子
$tbl.='';
您在哪里定义了$arr
?您有$sortedar
,但没有$arr
,它用于循环的内部增量。开发代码时,始终打开错误报告并在屏幕上显示错误。当PHP抱怨<代码>错误报告(E_全部);ini设置(“显示错误”,1)代码>@MichaelBerkowski是的,如果我把所有东西都标对了,那会有点帮助。真不敢相信我竟然没注意到。我把它换成了$sortedar,现在我只收到了第一个条目的第一个字母。你能不能也发一个小样本的info.txt
?查看substr()
调用的上下文会有帮助。@MichaelBerkowski我放了一个文件示例。谢谢你帮我做这件事。
tom,jones,5236895214,kjsdlfkjslfkj@ldjlf
jared,smith,2351547809,blahlbahlbah
john,doe,8745125489,dsjfksjfkjhsdkj
tom,atkins,5214523658,jhdfjashdfkjhsdkfj
//Starting from the beginning:
$sortedArr = array();
$file = file("files/info.txt");
foreach($file as $v){
$tempArr = explode(",",$v);
// Get the first and second values
// Not sure what the substr() was for
// since it would remove the last letter of the lastname...
// Let's omit that.
$str = $tempArr[0] . ' ' . $tempArr[1];
// Looks like you want to join the names 0,1
// and use the rest as they are...
// The joined string back into the first index
$tempArr[0] = $str;
// and get rid of the second since it's joined with the first
unset($tempArr[1]);
// Append the array onto your big:
array_push($sortedArr, $tempArr);
}
// Now open your table, then use 2 foreach
// loops to build the rows and columns, opening and
// closing the <tr> inside each iteration of the outer loop.
$tbl = "<table border='1'>";
// Outer loop is rows
foreach ($sortedArr as $row) {
// Start a row, close it later
$tbl .= '<tr>';
// Inner loop is columns
foreach ($row as $col) {
$tbl .= '<td>' . htmlspecialchars($col) . '</td>';
}
$tbl .= '</tr>';
}
// Close your table
$tbl .= '</table>';