Hi everyone,
Im new to coding and php and need some assistance on how to read php information from a file and display it in an html table.
Basically the site my teacher has asked me to make is one where i can type in a url, link text into a text field, write that to file, then read it back and display them in a html table.(there is more functionality but it is not relevant to mention until I can read it back)
I have the information writing to file ok, its the reading back thats a problem. I am getting alot of Undefined Offset errors.
I'm guessing that im not echoing the php part in the table properly?
Any advice would be much appreaciated!
The code I have so far is:
<html>
<body>
<form action = "menu1.1.php" method = "post">
<b>Enter the URL:</b> http://www.<input name="url" type="text" /><br /><br />
<b>Enter the link text: </b><input name="linktxt" type="text" /><br />
<p><b>Please select a Category</b></p>
<select name="category">
<option>Funny</option>
<option>News</option>
<option>Charity</option>
<option>Food</option>
</select><br /><br /><br />
<input type="submit" name="submit" value="Add Favourite" />
</form><br />
<?php
if (isset($_POST['submit']))
{
//open the file for writing
$fh = fopen("myfile.txt","a+");
$url= $_POST['url'];
$linktxt= $_POST['linktxt'];
$category= $_POST['category'];
$now = time();
//assemble the data into a line variable
$data= $url . ":" . $linktxt . ":" . $category . ":" . $now . ":" . "\r\n";
// write the data to the file
fwrite($fh, $data);
//close the file
fclose($fh);
echo "data output to file complete...";
}
?>
<h1>List your favourites</h1>
<?php
$textfile = 'myfile.txt';
if (file_exists($textfile) && is_readable($textfile))
{
$data = file($textfile);
}
for($i = 0; $i < ($data); $i++)
{
//separate each element and store in temp array
$tmp = explode(":", $data[$i]);
//assign each element to array
$data = array('url' => $tmp[0], 'linktxt' => $tmp[1], 'category' => $tmp[2], 'now' => $tmp[3]);
}
foreach ($data as $key => $row)
{
$url[$key] = $row['url'];
$lintxt[$key] = $row['linktxt'];
$category[$key] = $row['category'];
$now[$key] = $row['now'];
}
echo "<html><table>
<tr><th>URL</th></tr>
<tr><td><?php $url ?></td></tr>
</table></html>"
?>