Leo Wilson Leo Wilson - 4 years ago 85
PHP Question

A PHP for loop isn't displaying content from a file

I have a script that is supposed to output certain parameters from a text file stored on the server. This is my source code (sorry about the formatting):



<?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'].'">&lt;'.$d['client']['email'].'&gt;</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);
?>


This is output within a standard HTML
<tbody>
element. I know that the transactions file is loaded properly, since in testing the script output the file correctly. I also know that the script iterates the correct number of times, since if I use
echo $i
, it returns the expected result. However, nothing outputs in the table when I run the script.

This is the content of the transactions file:

==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==


This is probably a very obvious mistake that I made!

P.S. The page uses Bootstrap and jQuery, if that affects anything.

P.P.S. Side problem: Any
echo
statements output outside of the table for some reason. Not especially important, as long as the table rows and cells are inside.

Answer Source

At a quick glance it seems the first loop doesnt index by your loop-variable, you just get element at position zero:

$ln = $lns[0]

should probably be:

$ln = $lns[$i]

Also when you say "I also know that the script iterates the correct number of times", have you checked all loops and tried printing the expected value?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download