I have a form which uploads a text.txt file with action="profit-process.php"
In profit-process.php I convert the .txt file into an array:
<?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("
", $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>";
?>
What I would like to do is display the data in .txt (which goes into the array) with a table instead of print_r($lines).
So the goal formatting would be:
<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>
There will be multiple rows of text file therefore the number of rows in the table will reflect the text file data.
Any help much appreciated.
Cheers
Your final program should be:
Note: read extract() first.
<?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("
", $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>
Try with -
<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>
Try this
<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>