0

I am returning book data (title, ISBN, author) information from a text file into an array. I am able to pull the data from the text file, read it it and display it as an array but how do I display this information into an HTML table? The HTML table needs to be created using PHP. Where I print the $book in the foreach, all the books are returned as an array but How do I output them in an HTML table created using solely php. I am NOT receiving errors, just need to know how to insert the return the data into an HTML table using PHP. ? Here is what I have so far

PHP

  <?php

$infoArray = array(); //New empty array   

$filename = 'books.txt';

$lines = count(file($filename));

$fp = fopen($filename, 'r');

for($ii = 1; $ii <= $lines; $ii++){

    $line = fgets($fp); 
    array_push($infoArray, $line);
}

fclose($fp);

sort($infoArray);

list($title, $ISBN, $Author) = explode('*', $line);

$cntr = 0;

foreach ($dataReturned as $line){
    print $line;
    print '<br>';
}

This is what I want to print out as but it is not working. I am unsure how and where to incorporate this code.

$htmltable = "<table border='1'>";
$htmltable .= "<tr>";
$htmltable .= "<th>Title</th>";
$htmltable .= "<th>ISBN</th>";
$htmltable .= "<th>Author</th>";
$htmltable .= "</tr>";
$htmltable .= "<tr $style>";

            $htmltable .= "<td>".$title."</td>";
            $htmltable .= "<td>".$ISBN."</td>";
            $htmltable .= "<td>".$Author."</td>";
        $htmltable .= "</tr>\n";

print $htmltable;

TEXT FILE INFO These are a few lines being pulled from the text file.

Healthy Living*1-4988-9986-x*Smith
How to Live Life*1-5698-9865-x*Romero
Better Speech*1-6996-9989-x*Grimes
4
  • Can you please share text file data and what is error? Commented Sep 17, 2018 at 13:01
  • I am NOTreceiving errors, just need to know how to insert the return the data into an HTML table using PHP. Any help is appreciated. Commented Sep 17, 2018 at 13:08
  • array_push($dataReturned, $line); Where is defined $dataReturned ??? and if you use array_push the first argument must be an array... i tested it and is giving 4 errors, how can you say there are no errors! May be this is not the entire code you post Commented Sep 17, 2018 at 13:15
  • Sigma, Just corrected the code I’m sorry it should have read as info array Commented Sep 17, 2018 at 13:17

5 Answers 5

2

This is one possible approach:

PHP:

<?php
#
$filename = 'file-books.txt';
$file     = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

# 
echo "<table border='1'>";
echo "<thead>";
echo "<tr>";
echo "<th>Title</th>";
echo "<th>ISBN</th>";
echo "<th>Author</th>";
echo "</tr>";
echo "</thead>";

# Extract the lines.
echo "<tbody>";
foreach ($file as $row) {
    $fields = explode('*', $row);
    echo "<tr>";
    echo "<td>".$fields[0]."</td>";
    echo "<td>".$fields[1]."</td>";
    echo "<td>".$fields[2]."</td>";
    echo "</tr>";
}

#
echo "</tbody>";
echo "</table>";
?>

Text file:

Healthy Living*1-4988-9986-x*Smith
How to Live Life*1-5698-9865-x*Romero
Better Speech*1-6996-9989-x*Grimes
Sign up to request clarification or add additional context in comments.

Comments

2

Just reading the file as a CSV (although with a * delimeter)...

$fp = fopen($filename, 'r');
$style = "";

$htmltable = "<table border='1'>";
$htmltable .= "<tr>";
$htmltable .= "<th>Title</th>";
$htmltable .= "<th>ISBN</th>";
$htmltable .= "<th>Author</th>";
$htmltable .= "</tr>";

while ( $row = fgetcsv($fp, null, "*")) {
    $htmltable .= "<tr $style>";
    $htmltable .= "<td>".$row[0]."</td>";
    $htmltable .= "<td>".$row[1]."</td>";
    $htmltable .= "<td>".$row[2]."</td>";

    $htmltable .= "</tr>\n";
}
$htmltable .= "</table>\n";
fclose($fp);

echo $htmltable;

Comments

1
$html_table = '';

$html_table .= '<table>';
$html_table .= '<thead>';
$html_table .= '<th>Title</th>';
$html_table .= '<th>ISBN</th>';
$html_table .= '<th>Author</th>';
$html_table .= '</thead>';
$html_table .= '<tbody>';

foreach ($dataReturned as $line){
    $html_table .= '<tr>';
    $html_table .= "<th>$line[0]</th>";
    $html_table .= '<th>$line[1]</th>';
    $html_table .= '<th>$line[2]</th>';
    $html_table .= '</tr>';
}

$html_table .= '</tbody>';
$html_table .= '</table>';

echo $html_table;

--- Sorry for creating this as new answer, cant edit my last comment with code formatting. ---

Comments

-1

Can you show us an example of what data the variable $line contains? Because if its an array, you need to do another foreach inside the "foreach ($dataReturned as $line)" loop to get the data inside the variable $line or if its an object, try to get the respective attribute.

After knowing what you have, try to create an empty variable which is gonna contain all your HTML and use your foreach loop to append the columns and rows.

2 Comments

$line comes back as a long paragraph of books and authors and when I use var_dump it shows as an array. Can you please provide an example of the empty variable that will contain on my HTML to use with a for loop? Visual representation would help 1 million thank you so much
$html_table = ''; $html_table .= '<table>'; $html_table .= '<thead>'; $html_table .= '<th>Title</th>'; $html_table .= '<th>ISBN</th>'; $html_table .= '<th>Author</th>'; $html_table .= '</thead>'; $html_table .= '<tbody>'; foreach ($dataReturned as $line){ $html_table .= '<tr>'; $html_table .= "<th>$line[0]</th>"; $html_table .= '<th>$line[1]</th>'; $html_table .= '<th>$line[2]</th>'; $html_table .= '</tr>'; } $html_table .= '</tbody>'; $html_table .= '</table>'; echo $html_table;
-1

Are you sure that you have data in the array ? I was checking your code and look good, however you can use "echo" for print data in the browser.

You can test your code in this web page http://phptester.net/-

i hope that it could help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.