Page 1 of 1
SOLVED Extract data from an include and display partial data
Posted: Mon Feb 27, 2012 8:39 am
by BBoyd
I have a file name 'inventory.php' that is formatted like...
<?php
$data =
"name year serial charge daysout revenue price hours dep maint
name year serial charge daysout revenue price hours dep maint
name year serial charge daysout revenue price hours dep maint
";
?>
and I need to extract the data and put into an array using strtok like below. Where I'm confused is that only the first 8 fields are needed and skip the last 2 fields. I may be close as a for loop will do this task, but I'm not sure how to skip the 2 fields.
How can this be accomplished? Am I close?
Code: Select all
include 'inventory.php';
$equipment['name'] = rtrim(strtok($data, " "));
$equipment['year'] = rtrim(strtok(" "));
$equipment['serial'] = rtrim(strtok(" "));
$equipment['charge'] = rtrim(strtok(" "));
$equipment['daysout'] = rtrim(strtok(" "));
$equipment['revenue'] = rtrim(strtok(" "));
$equipment['price'] = rtrim(strtok(" "));
$equipment['hours'] = rtrim(strtok(" "));
$equipment['dep'] = rtrim(strtok(" ")); //SKIP
$equipment['maint'] = rtrim(strtok("/n")); //SKIP
printf ("The values should display as...\n");
printf ("| name | year | serial | charge | daysout | revenue | price | hours |\n");
foreach ($equipment as $key => $value)
{
if ($key == 'dep' || $index == 'maint') printf ("");
else
printf ("| %15s", $value);
}
Re: Extract data from an include and display partial data.
Posted: Mon Feb 27, 2012 9:17 am
by litebearer
Is there a reason the data is set up as it is rather than as a csv file?
Re: Extract data from an include and display partial data.
Posted: Mon Feb 27, 2012 9:27 am
by BBoyd
litebearer, I wish that I could answer that question, but it's just the way the assignment is written. As with many assignments in school, it might not be the most logical, but it is what we must work with.
Re: Extract data from an include and display partial data.
Posted: Mon Feb 27, 2012 9:31 am
by BBoyd
I can print to screen all the fields with...
Code: Select all
foreach ($equipment as $value)
{
printf ("| %s ", $value);
}
But I'm having problems skipping the last 2 fields.
Re: Extract data from an include and display partial data.
Posted: Mon Feb 27, 2012 9:37 am
by litebearer
Here is how I would do it using EXPLODE (I am sure you can adapt it to use strtok IF that is part of the assigment requirement)
Note: this presumes there is a newline after each grouping of data.
Code: Select all
<?php
include 'inventory.php';
$lines = explode("\n", $data);
$c_lines = count($lines);
?>
<table>
<tr><td>Name</td><td>Year</td><td>Serial</td><td>Charge</td><td>Days Out</td><td>Revenue</td><td>Price</td><td>Hours</td></tr>
<?PHP
for($i = 0; $i < $c_lines; $i++) {
$temp_array = explode(" ", $lines[$i]);
?>
<tr>
<td><?PHP echo $temp_array[0]</td>
<td><?PHP echo $temp_array[1]</td>
<td><?PHP echo $temp_array[2]</td>
<td><?PHP echo $temp_array[3]</td>
<td><?PHP echo $temp_array[4]</td>
<td><?PHP echo $temp_array[5]</td>
<td><?PHP echo $temp_array[6]</td>
<td><?PHP echo $temp_array[7]</td>
</tr>
<?PHP
}
echo "</table";
?>
Re: Extract data from an include and display partial data.
Posted: Mon Feb 27, 2012 9:56 am
by BBoyd
The code you provided a syntax error. I copied/pasted directly into the body of the HTML page and get the following error.
Parse error: syntax error, unexpected '/' on line 20
which is... <td><?PHP echo $temp_array[0]</td>
The requirements is to use a while loop, for loop and strtok for this assignment. It's time for me to leave for work, but I will be back this evening to work on this more.
Re: Extract data from an include and display partial data.
Posted: Mon Feb 27, 2012 12:11 pm
by temidayo
close the <?php tag for each row as in
Code: Select all
<td><?PHP echo $temp_array[0]?></td>
to eliminate the syntax error
Re: Extract data from an include and display partial data.
Posted: Mon Feb 27, 2012 3:55 pm
by BBoyd
temidayo wrote:close the <?php tag for each row as in
Code: Select all
<td><?PHP echo $temp_array[0]?></td>
to eliminate the syntax error
I had already tried this, but then get an 'Undefined offset' error. Looking at the for loop, the '?> is added at the end of each array or at least that is what it appears to do.
Code: Select all
for($i = 0; $i < $c_lines; $i++) {
$temp_array = explode(" ", $lines[$i]);
?>
Re: Extract data from an include and display partial data.
Posted: Mon Feb 27, 2012 4:23 pm
by BBoyd
Here is another option and the code works in the sense that it extracts the data from the file and fills the tables cells. However, I'm not sure how to skip the last 2 fields and have tried a lot of combinations of if statements, but nothing works so far.
Code: Select all
<html>
<head>
<title>Equipment Rental Report</title>
</head>
<body>
<table border="1" cellpadding="1" cellspacing="2" align="center" bgcolor="silver">
<tr>
<td colspan="10" align="center">Rental and Repairs</td>
</tr>
<tr>
<td width="100px" align="left">EQUIPMENT</td>
<td width="100px" align="center">YEAR</td>
<td width="100px" align="center">SERIAL#</td>
<td width="100px" align="center">RENTALCHG</td>
<td width="100px" align="center">DAYSOUT</td>
<td width="100px" align="center">REVENUE</td>
<td width="100px" align="center">ORIGPRICE</td>
<td width="100px" align="center">HOURS</td>
<td width="100px" align="center">extra</td>
<td width="100px" align="center">extra</td>
</tr>
<?php
include 'inventory.php';
Createtable($data);
function CreateTable($data)
{
$MyRows= explode("\n", $data);
$NumberRows=sizeof($MyRows);
foreach ($MyRows as $row)
{
$datainrow= explode(" ", $row);
echo "<tr>";
foreach ($datainrow as $val)
{
$printdata = is_numeric($val) ? number_format($val, 2) : $val;
echo "<td>$printdata</td>";
}
echo "</tr>";
}
echo "<br /> \n\n";
}
?>
</table>
</body>
</html>
Re: Extract data from an include and display partial data.
Posted: Mon Feb 27, 2012 4:27 pm
by Celauran
Instead of
you could use
Code: Select all
for ($i = 0; $i < count($datainrow) - 2; $i++)
Re: Extract data from an include and display partial data.
Posted: Mon Feb 27, 2012 4:58 pm
by BBoyd
I cannot even describe what the table looks like after making the change you suggested. Although I have a lot of assignments to complete this week, this one I might have to go back to the drawing board as I'm drifting further and further away from the requirements of the assignment.
Re: Extract data from an include and display partial data.
Posted: Mon Feb 27, 2012 5:41 pm
by Celauran
Code: Select all
<?php
include 'inventory.php';
$token = strtok($data, "\n");
while ($token !== FALSE)
{
$lines[] = rtrim($token);
$token = strtok("\n");
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Whatever</title>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>Year</th>
<th>Serial</th>
<th>Charge</th>
<th>Days Out</th>
<th>Revenue</th>
<th>Price</th>
<th>Hours</th>
</tr>
<?php
foreach ($lines as $line)
{
$row = explode(" ", $line);
echo "<tr>";
for ($i = 0; $i < count($row) - 2; $i++)
{
echo "<td>{$row[$i]}</td>";
}
echo "</tr>";
}
?>
</table>
</body>
</html>
Re: Extract data from an include and display partial data.
Posted: Tue Feb 28, 2012 5:01 am
by BBoyd
Thank you Celauran. Your code works great and very clean. I really do appreciate your help. Now it's time to do the other part of the assignment.