0

I am pulling data lines from a .txt file and I am attempting to insert it into an HTML table. Right now I am stuck on outputting the array. I am able to turn the line I exploded into an array by re-exploding the line. I var_dumped the new line to ensure it is an array. I need to return a function that outputs the data into an HTML table. I do not know where to go from here. Any help is appreciated. This is as far as I got. For the sample there are about 10 books in the .txt file. Do I include HTML in the same function? or outside and how do I implement? Thank you.

PHP

function displayTable($filename)
{


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

        $line_ctr = 0;

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

ens the file for reading

    if ($fp)
    {
        while(true)
        {

            $line = fgets($fp);

            if (feof($fp))
            {
                break;


    }

    $line_ctr++;

    //Explode into string
list($title, $author, $isbn, $city) = explode('*', $line);
$new_line = explode('*', $line);

var_dump($new_line);
}

    fclose($fp ); //Close file
}

CURRENT OUTPUT SAMPLE

array(5) { [0]=> string(14) "Smart Living" [1]=> string(7) "health" [2]=> string(10) "2005-09-12" [3]=> string(13) "1-4145-5896-x" [4]=> string(2) " " } 

 array(5) { [0]=> string(14) "Make Money Sleeping" [1]=> string(7) "health" [2]=> string(10) "2009-01-08" [3]=> string(13) "1-4888-5896-x" [4]=> string(2) " " } 

2 Answers 2

0

I saw your commnet. You want to sort the table. I've rewritten this function, hopefully, to help you.

You can split it to 3 step: 1.read file, 2.sort data, 3.output HTML.

function displayTable($filename)
{
    // 1. read file to array
    $data = array(); // Save all data read from txt file.
    $fp   = fopen($filename, 'r'); //opens the file for reading
    if ($fp) {
        while (true) {
            $line = fgets($fp);
            if (feof($fp)) {
                break;
            }
            $new_line = explode('*', $line);
            $data[]   = $new_line; // save to data
        }
        fclose($fp); //Close file
    }

    // 2. sort data, example for sort by ISBN
    if ($data) {
        foreach ($data as $key => $value) {
            $ISBN[$key] = $value[2]; //sort by ISBN
        }
        array_multisort($ISBN, SORT_ASC, $data);
    }

    // 3. output to HTML
    $table = "\n<table border='1'>";
    $table .= "<tr>";
    $table .= "<th>Title</th>";
    $table .= "<th>Author</th>";
    $table .= "<th>ISBN</th>";
    $table .= "<th>City</th>";
    $table .= "</tr>\n\n";
    foreach ($data as $key => $value) {
        $table .= "<tr><td>" . join("</td><td>", $value) . "</td></tr>";
    }
    $table .= "</table>";
    return $table;
}
-1
    $new_line = explode('*', $line);
    $table .= "<tr><td>" . join("</td><td>", $new_line) . "</td></tr>";
}


return $table;
2
  • 2
    Welcome to Stack Overflow! Consider adding some explanatory text to say how this solves the problem, and what the root cause is. Please also take the tour and visit the help center.
    – CJ Dennis
    Sep 20, 2018 at 3:43
  • @Williams, thank you for the input, this code does output the table, the only thing is I need it to return with the ability to be sorted. Can you show me how I would sort this table? Thank you. Sep 20, 2018 at 12:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.