SOLVED Extract data from an include and display partial data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
BBoyd
Forum Commoner
Posts: 31
Joined: Sat Jan 21, 2012 8:24 pm
Location: Charleston, SC

SOLVED Extract data from an include and display partial data

Post 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);
    }
Last edited by BBoyd on Tue Feb 28, 2012 5:03 am, edited 1 time in total.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: Extract data from an include and display partial data.

Post by litebearer »

Is there a reason the data is set up as it is rather than as a csv file?
User avatar
BBoyd
Forum Commoner
Posts: 31
Joined: Sat Jan 21, 2012 8:24 pm
Location: Charleston, SC

Re: Extract data from an include and display partial data.

Post 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.
User avatar
BBoyd
Forum Commoner
Posts: 31
Joined: Sat Jan 21, 2012 8:24 pm
Location: Charleston, SC

Re: Extract data from an include and display partial data.

Post 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.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: Extract data from an include and display partial data.

Post 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";
?>
User avatar
BBoyd
Forum Commoner
Posts: 31
Joined: Sat Jan 21, 2012 8:24 pm
Location: Charleston, SC

Re: Extract data from an include and display partial data.

Post 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.
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: Extract data from an include and display partial data.

Post 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
User avatar
BBoyd
Forum Commoner
Posts: 31
Joined: Sat Jan 21, 2012 8:24 pm
Location: Charleston, SC

Re: Extract data from an include and display partial data.

Post 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]);
        ?>
User avatar
BBoyd
Forum Commoner
Posts: 31
Joined: Sat Jan 21, 2012 8:24 pm
Location: Charleston, SC

Re: Extract data from an include and display partial data.

Post 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>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Extract data from an include and display partial data.

Post by Celauran »

Instead of

Code: Select all

foreach ($datainrow as $val)
you could use

Code: Select all

for ($i = 0; $i < count($datainrow) - 2; $i++)
User avatar
BBoyd
Forum Commoner
Posts: 31
Joined: Sat Jan 21, 2012 8:24 pm
Location: Charleston, SC

Re: Extract data from an include and display partial data.

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Extract data from an include and display partial data.

Post 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>
User avatar
BBoyd
Forum Commoner
Posts: 31
Joined: Sat Jan 21, 2012 8:24 pm
Location: Charleston, SC

Re: Extract data from an include and display partial data.

Post 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.
Post Reply