0

I'm totally new to this and am taking a class where the professor is absolutely not helpful. I'm trying to read data from a file into a table, but the data isn't showing up for some reason. I've tried it a few different ways, but I honestly am kind of lost. Any advice is much appreciated

Code is:

$filename = 'data/'.'booklist.txt';
$find_title = $_POST['title'];
$ascending = $_POST['ascending'];
$descending = $_POST['descending'];
list($total_rows, $theTable) = displayTable($filename);
if ($total_rows != 'No File')
{
print $theTable;   
}
else
{
    print "No file found";
}
function displayTable($filename)
{ 
    $myTable =  "\n<table border='1'>";
    $myTable .= "<tr>";
    $myTable .= "   <th>Title</th>";
    $myTable .= "   <th>Type</th>";
    $myTable .= "   <th>Publication Date</th>";
    $myTable .= "   <th>ISBN</th>";
    $myTable .= "</tr>\n\n";
    $line_ctr = 0;
    $fp = fopen($filename, 'r');   //opens the file for reading
    if ($fp)
    {
        while(true)
        {
            $line = fgets($fp);
            if (feof($fp))
            {
                break;
            }
            $line_ctr++;
            $line_ctr_remainder = $line_ctr % 2;
            if ($line_ctr_remainder == 0)
            {
                $style = "style='background-color: #FFFFCC;'";
            } else {
                $style = "style='background-color: white;'";
            }
            list($title, $type, $publicationdate, $isbn) = explode('*', $line);
            $myTable .= "<tr $style>";
            $mytable .= "<td>".$title."</td>";
            $mytable .= "<td>".$type."</td>";
            $mytable .= "<td>".$publicationdate."</td>";
            $mytable .= "<td>".$isbn."</td>";
            $mytable .= "</tr>\n";   
        }
        fclose($fp );
        $myTable .= "</table>";
        $rtn = array($line_ctr, $myTable);
    } else {
        $rtn = array("No File", "dummy");
    }
    return $rtn;
    print $mytable;   //This prints the table rows
}
?>
</div>
</body>
</html>

Thank you!

user3783243
  • 5,368
  • 5
  • 22
  • 41
  • Can you show us examples from the data file? – Jonnix Oct 22 '20 at 19:10
  • https://stackoverflow.com/a/29867331/8775531 – Colin Gell Oct 22 '20 at 19:14
  • You might consider using https://www.php.net/manual/en/function.fgetcsv.php and use `*` as the delimiter. You also don't need a function for this and I wouldn't use one unless you're using this multiple times? – user3783243 Oct 22 '20 at 19:25
  • I can create the table without using functions, but it's required for the assignment. We have to use functions, arrays, and stripos and make the data in the table searchable. I used * because the data in the file is set up like this: Sushi, Anyone?*cooking*1998-06-12*1-7777-7777-x* The Busy Executive's Database Guide*business*1998-06-12*1-1032-1032-x* – newtothis Oct 22 '20 at 19:33
  • You might want to check your variables: PHP variables are case-sensitive! $mytable is different from $myTable. I hope that helps! – uotonyh Oct 22 '20 at 19:51
  • @uotonyh thank you! that fixed it :) – newtothis Oct 22 '20 at 21:36

2 Answers2

0

Problem is with

print $theTable;

which value is "NULL", therefore it does not work.

PHP returns error for your function:

Undefined index: title in <b>[...][...]</b> on line <b>4</b><br />
<br />
<b>Notice</b>:  Undefined index: ascending in <b>[...][...]</b> on line <b>5</b><br />
<br />
<b>Notice</b>:  Undefined index: descending in <b>[...][...]</b> on line <b>6</b><br />

Also the execution of function stops at return, so:

   return $rtn;
    print $mytable;   //This prints the table rows

Print will never execute, since you exited function with return.

Fksjii
  • 62
  • 4
0

Promoting this from a comment, since it is reported to work...

PHP has case-sensitive variables, so $mytable is not $myTable.

I hope this works for your assignment, and watch for that on the test!

uotonyh
  • 786
  • 5
  • 7