PDA

View Full Version : simple PHP coding needed!


bluemicrobyte
02-06-2006, 10:55 PM
So I also need a simple script that will contain the following 2 parts:


a simple html form with two fields - one for a url and one for a line of text. And a submit button. When submitted, it will add the URL and line of text to the top of a .txt file (or MySQL database if needed, but preferably .txt).

The second part is a php script that reads the text file with the data and outputs html code in the following format (or similar):



<a href="NEWEST URL" class="weblinks"> NEWEST LINE OF TEXT </a>

<br>

<a href="SECOND URL" class="weblinks"> SECOND LINE OF TEXT </a>

<br>

etc until X items have been listed - probably around 5

I'd also like another php file which lists every item in the log file for archival purposes.

The two parts are separate - the form is for me (and only me, don't worry about security issues) to add links to my webwatch box (http://www.microbyteproductions.com/home.php) on the home page. The second part is to display the latest links in that webwatch box on the home page.

Help with this is greatly appreciated!

TimP
02-07-2006, 10:39 PM
Make sure the file exists before you run either script, the code doesn't check for it. Just create an empty file called links.txt. If you want to use a different file name change the $file variable in both scripts. Make sure to change the form action on write.php if you rename it.

read.php

<?
$file = "links.txt";

$fh = fopen($file, 'r');

// read first record
$line = fgets($fh);

while(!feof($fh))
{

// get rid of Windows line breaks
$line = str_replace("\r\n", "", $line);

// get rid of Unix line breaks
$line = str_replace("\r", "", $line);

$split = strpos($line, " ");
$link = substr($line, 0, $split);
$title = substr($line, $split + 1);

echo "<a href=\"$link\">$title</a>\n";
echo "<br />";
// read next record
$line = fgets($fh);
}
fclose($fh);
?>

write.php

<?
$file = "links.txt";

// read and store the old links file
if(strlen($_POST['title']) > 0 && strlen($_POST['link']) > 0)
{
$lines = array();
$i = 0;

$fh = fopen($file, "r");
$line = fgets($fh);
while(!feof($fh))
{
$lines[$i] = $line;
$line = fgets($fh);
$i++;
}
fclose($fh);

$fh = fopen($file, "w");
$safelink = str_replace(" ", "%20", $_POST['link']);
fwrite($fh, $safelink . " " . $_POST['title'] . "\r\n");
for($j = 0; $j < $i; $j++)
fwrite($fh, $lines[$j]);
fclose($fh);
}
?>
<form action="write.php" method="post">
Title:
<input type="text" name="title" />
<br />
URL:
<input type="text" name="link" />
<br />
<input type="submit" />
</form>


And here's the logger.

logger.php
<?
$file = "links.txt";
$fh = fopen($file, "r");
$line = fgets($fh);
while(!feof($fh))
{
echo $line;
$line = fgets($fh);
}
fclose($fh);
?>

bluemicrobyte
02-07-2006, 11:14 PM
Huzzah! Hail TimP!

Would it be possible to change read.php so that it only displays the most recent x items? I'd prefer that the value be changable but if it isn't then 5 is a good number.

TimP
02-08-2006, 12:43 AM
<?
$file = "links.txt";
$display = 5;
$fh = fopen($file, 'r');

// read first record
$line = fgets($fh);
$i = 0;
while(!feof($fh) && $i < $display)
{

// get rid of Windows line breaks
$line = str_replace("\r\n", "", $line);

// get rid of Unix line breaks
$line = str_replace("\r", "", $line);

$split = strpos($line, " ");
$link = substr($line, 0, $split);
$title = substr($line, $split + 1);

echo "<a href=\"$link\">$title</a>\n";
echo "<br />";
// read next record
$line = fgets($fh);
$i++;
}
fclose($fh);
?>

bluemicrobyte
02-08-2006, 12:56 AM
Yay! Thanks TimP, 10 cookies for you :cookie: