我有一个脚本,应该从存储在服务器上的文本文件中输出某些参数。这是我的源代码(抱歉格式化):
<?php
$traw = file_get_contents("path/to/transactions.file");
$lns = explode("\n",$traw);
$mode['t'] = false;
$mode['c'] = false;
$mode['f'] = false;
# $t = array();
# $f = array();
$fs=0;
$ts=0;
for($i=0;$i<count($lns);$i++){
$ln = $lns[0];
if($ln=="==START TRANSACTION DATA=="){$mode['t']=true;}
elseif($ln=="=START CLIENT INFO="){$mode['c']=true;}
elseif($ln=="=END CLIENT INFO="){$mode['c']=false;}
elseif($ln=="==END TRANSACTION DATA=="){$mode['t']=false;$ts++;}
elseif($ln=="==START FAILED TRANSACTION=="){$mode['f']=true;}
elseif($ln=="==END FAILED TRANSACTION=="){$mode['f']=false;$fs++;}
else{
if($mode['t'] && $mode['c']){
$kv = explode(": ",$ln);
$t[$ts]['client'][strtolower($kv[0])] = $kv[1]; // set key to value in client data
}
elseif($mode['t'] && !$mode['c']){
$kv = explode(": ",$ln);
$t[$ts]['p'][strtolower($kv[0])] = $kv[1]; // set key to value in purchase data
}
elseif($mode['f']){
$kv = explode(": ",$ln);
$f[$fs][strtolower($kv[0])] = $kv[1]; // set key to value in failed data
}
else{
continue; // This line means nothing, and is most likely blank
}
}
}
for($i=0;$i<count($t);$i++){
$d = $t[$i];
echo '<tr>';
echo '<td>'.$d['photo id'].'</td>';
echo '<td>'.$d['print'].'</td>';
echo '<td>'.$d['quantity'].'</td>';
echo '<td>'.$d['price'].'</td>';
echo '<td>'.$d['date'].'</td>';
echo '<td>'.$d['additional info'].'</td>';
echo '<td><button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="top" title="'.$d['client']['address'].'">'.$d['client']['name'].'</button><a href="mailto:'.$d['client']['email'].'"><'.$d['client']['email'].'></a></td>';
echo '</tr>';
}
for($i=0;$i<count($f);$i++){
$d = $f[$i];
echo '<tr>';
echo '<td>-</td>';
echo '<td>-</td>';
echo '<td>-</td>';
echo '<td>-</td>';
echo '<td>'.$d['date'].'</td>';
echo '<td>-</td>';
echo '<td>-</td>';
echo '</tr>';
}
print_r($t);
print_r($f);
?>
这是在标准HTML <tbody>
元素中输出的。我知道事务文件已正确加载,因为在测试脚本时正确输出文件。我也知道脚本会迭代正确的次数,因为如果我使用echo $i
,它会返回预期的结果。但是,当我运行脚本时,表中没有任何输出。
这是交易文件的内容:
==START FAILED TRANSACTION==
Date: 05-2-2017 (Sunday, February 5th, 2017) UTC
==END FAILED TRANSACTION==
==START FAILED TRANSACTION==
Date: 05-2-2017 (Sunday, February 5th, 2017) UTC
==END FAILED TRANSACTION==
==START FAILED TRANSACTION==
Date: 05-2-2017 (Sunday, February 5th, 2017) UTC
==END FAILED TRANSACTION==
==START FAILED TRANSACTION==
Date: 05-2-2017 (Sunday, February 5th, 2017) UTC
==END FAILED TRANSACTION==
==START FAILED TRANSACTION==
Date: 05-2-2017 (Sunday, February 5th, 2017) UTC
==END FAILED TRANSACTION==
==START TRANSACTION DATA==
=START CLIENT INFO=
Address: {ADDR}
Name: {NAME}
Email: {EMAIL}
=END CLIENT INFO=
Inventory ID: {INV}
Photo ID: {ID}
Quantity: {QTY}
Print: {PRNT}
Price: {PRC}
Date of transaction: {DATE}
Raw Braintree transaction data: {TRNS}
Additional info: {ADTL}
==END TRANSACTION DATA==
这可能是我犯的一个非常明显的错误!
P.S。该页面使用Bootstrap和jQuery,如果这会影响任何内容。
P.P.S。边问题:由于某种原因,在表外输出任何echo
语句。并不是特别重要,只要表格行和单元格在里面。
答案 0 :(得分:1)
快速浏览一下,第一个循环似乎没有被循环变量索引,只需将元素置于零位置:
$ln = $lns[0]
应该是:
$ln = $lns[$i]
另外当你说“我也知道脚本迭代正确的次数”时,你检查过所有循环并尝试打印预期的值吗?