这是php和mysql web开发书中一个例子,目的就是把文件保存的订单信息炸开,用表格显示。我运行时为什么只显示表头,没有表格数据呢。
orders.txt中就是订单信息,用Tab分隔的,已正确读取到$orders中。貌似是后边的for循环中有问题。
<?php
//Read the entire file
//Each order becomes an element in the array
$orders = file("./orders.txt");
//count the number of orders in the array
$number_of_orders = count($orders);
if($number_of_orders = 0) {
echo "<p><strong>No orders pending. Please try again later.</strong></p>";
}
echo "<table border=\"1\">\n";
echo "<tr><th bgcolor=\"#CCCCFF\">Order Date</th>
<th bgcolor=\"#CCCCFF\">Tires</th>
<th bgcolor=\"#CCCCFF\">Oil</th>
<th bgcolor=\"#CCCCFF\">Spark plugs</th>
<th bgcolor=\"#CCCCFF\">Total</th>
<th bgcolor=\"#CCCCFF\">Address</th>
</tr>";
for($i = 0; $i < $number_of_orders; $i++) {
//Split up each line
$line = explode("\t", $orders[$i]);
//keep only the number of items ordered
$line[1] = intval($line[1]);
$line[2] = intval($line[2]);
$line[3] = intval($line[3]);
//output each other
echo "<tr>
<td>".$line[0]."</td>
<td align=\"right\">".$line[1]."</td>
<td align=\"right\">".$line[2]."</td>
<td align=\"right\">".$line[3]."</td>
<td align=\"right\">".$line[4]."</td>
<td>".$line[5]."</td>
</tr>";
}
echo "</table>";
?>
看上去没问题 把那个txt文件贴下看看
把代码贴上来让大家给你分析分析
大哥你能给个复制的么 我想粘过去测测都不行 算了 手敲吧
txt是我手打的 可能格式有点问题 最起码数据出来了
<?php
$orders = file("./orders.txt");
$number_of_orders = count($orders);
// echo $number_of_orders;
// echo "<Br />";
if($number_of_orders == 0) {//我操你这里为什么写成=0了 我擦坑爹啊 你给赋值操作了 $number_of_orders=0了 下面的for循环不执行了
//所以就是什么都不出的原因了
echo "<p><strong>No orders pending. Please try again later.</strong></p>";
}
echo "<table border=\"1\">\n";
echo "<tr><th bgcolor=\"#CCCCFF\">Order Date</th>
<th bgcolor=\"#CCCCFF\">Tires</th>
<th bgcolor=\"#CCCCFF\">Oil</th>
<th bgcolor=\"#CCCCFF\">Spark plugs</th>
<th bgcolor=\"#CCCCFF\">Total</th>
<th bgcolor=\"#CCCCFF\">Address</th>
</tr>";
// echo $number_of_orders;
for($i = 0; $i < $number_of_orders; $i++) {
$line = explode("\t", $orders[$i]);
// echo "123<br />";
// var_dump($line);die;
//keep only the number of items ordered
$line[1] = intval($line[1]);
$line[2] = intval($line[2]);
$line[3] = intval($line[3]);
//output each other
echo "<tr>
<td>".$line[0]."</td>
<td align=\"right\">".$line[1]."</td>
<td align=\"right\">".$line[2]."</td>
<td align=\"right\">".$line[3]."</td>
<td align=\"right\">".$line[4]."</td>
<td>".$line[5]."</td>
</tr>";
}
echo "</table>";
?>
这是我的txt
20:30, 31st March 2008 4Tires 1Oil 6Spark $434.00 22 Short St, Smalltown
21:42, 31st March 2008 1Tires 0Oil 0Spark $100.00 33 Main Rd, Newtown