0

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>"
	?>
2
Contributors
1
Reply
4
Views
6 Years
Discussion Span
Last Post by sudeepjd
0

There is one major reason why your code is not working as it should. While pulling the $data into the for loop, you need to give a sizeof to set an upper bound for the loop.

As a web designer, I would like to give you a few tips.

  1. Always make sure you have a doctype when you write HTML. This will make sure that your code is standards compliant and hence cross browser compatible.
  2. Your documentation is great, with comments and everything.
  3. Put the processing php code at the beginning of the document. This will ensure that anything you pull after the process would reflect the latest values.
  4. Use the arrays wisely. I see you have overwritten you $data array later in the code, which is causing a problem.

I have cleaned up your code a bit. This will work.

<?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
		if (fwrite($fh, $data)){
			$output = "Data output to file complete...";
		}

		//close the file
		fclose($fh);
	
     } 
	 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>My Favorite List</title>
	<style type="text/css">
		body{
			font: 12px Verdana, Arial, Helvetica, sans-serif;
		}
	</style>
</head>
<body>
<?php if($output!=""){ echo $output; } ?>
<form action = "<? echo $_SERVER['PHP_SELF']?>" method = "post">
	<div style="border:1px solid #cccccc;margin:10px;padding:10px;width:600px;">
	<table width="80%">
	<tr><td><b>Enter the URL:</b></td><td>http://www.<input name="url" type="text" style="width:125px;" /></td></tr>
	<tr><td><b>Enter the link text: </b></td><td><input name="linktxt" type="text" style="width:200px;" /></td></tr>
	<tr><td><b>Please select a Category:</b></td><td>
	
	<select name="category" style="width:200px;"> 
	<option>Funny</option>
	<option>News</option>
	<option>Charity</option>
	<option>Food</option>
	</select></td></tr>
	</table>
	<input type="submit" name="submit" value="Add Favourite" />
	</div>
</form>


	
	<h1>List your favourites</h1>
	<?php
		$textfile = 'myfile.txt';
		
		if (file_exists($textfile) && is_readable($textfile))
		{
			$data = file($textfile);
		}
	
		for($i = 0; $i < sizeof($data); $i++)
		{
			//separate each element and store in temp array
			$tmp = explode(":", $data[$i]);
			
			//assign each element to array
			$data1[] = array('url' => $tmp[0], 'linktxt' => $tmp[1], 'category' => $tmp[2], 'now' => $tmp[3]);
		}
		echo "<table cellspacing='0' cellpadding='5'>";
		if ($data1!=""){
			echo "<tr><th style='background:#eeeeee'>Link</th><th style='background:#eeeeee'>Category</th><th style='background:#eeeeee'>Time</th></tr>";
			foreach ($data1 as $key => $row)
			{
				$url[$key] = $row['url'];
				$lintxt[$key] = $row['linktxt'];
				$category[$key] = $row['category'];
				$now1[$key] = $row['now'];
				echo "<tr><td><a href='$url[$key]'>{$lintxt[$key]}</a></td><td>{$category[$key]}</td><td>".date('d-M-y h:i:sa',$now1[$key])."</td></tr>";
			}
		}
		echo "</table>";
		
	?>
</body>
</html>

All the best!

Edited by sudeepjd: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.