So, I’m doing a program for homework and it’s failing utterly. From what you see, you’ll be able to tell I’m not a very good programmer. The objective of this assignment is to:
Use your script from assignment 3 (the one that reads your “links” file and outputs a page with all the links), and save it with a new name, such as “admin.php”.
Alter this version of the script so that the output page is a form, and add a checkbox in front of each link. Add a submit button at the bottom that says “Delete Checked Items”. (I used different file names because I have multiple versions)
Here is the page the user sees (it will have errors on it as they are what are supposed to be deleted)>
[php]
Link Page ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');
$file = file('../write/myfile.txt');
sort($file);
$main = array();
$n = 0;
foreach($file as $key =>$value)
{
$main[$key]=explode("|",$value);
}
//columns
$columns = array('Rating', 'URL', 'Description');
$counter=0;
$category='';
foreach($main as $key => $value){
if($counter==0)
{
echo "<form name=\"form\" method\"post\" action\"delete2.php\"> <table border=1 align=center><tr>";
foreach($columns as $col)
{
echo "<th> $col </th>";
}
echo "</tr>";
$counter++;
}
if($category !== $main[$key][0])
{
$category = $main[$key][0];
$newCat = true;
echo "<tr><td> <b>$category</b></td>";
}
else
{
$newCat = false;
}
for($i=1; $i< count($main[$key]); $i++)
{
$v = $main[$key][$i];
$z = $main[$i];
if($i==2)
{
$checkbox = array();
$n = 0;
echo "<td><input type=\"checkbox\" name='$key' /> <a href=$v>$v</> </td>";
$n++;
}
else
{
echo "<td> $v </td>";
}
}
echo "</tr>";
}
echo "<tr><td><input type=\"submit\" name=\"Submit\" value=\"Delete Checked Items\"/></td>";
echo "<td><input type=\"submit\" value=\"Cancel\" /></td>";
echo "</table>";
echo "</form>";
echo "<br />";
?>
From what I’m getting out of it, I don’t think it’s even sending to my other script. Also, it doesn’t even display right. I don’t know how to fix it so the table displays properly.
Here is the code for the script that is supposed to delete the rows that are checked.
[php]<?php
$lines = fopen("links2.php", "r");
$temp = fopen("../write/temp.txt", "a");
$content = file("../write/temp.txt");
foreach($_POST as $key => $val)
{
if(isset($_POST['$key']))
{
unset($content[$key]);
//$key - 1;
}
//echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
sort($content);
fwrite($lines, implode("\n", $content));
fclose($lines);
unlink("links2.php");
rename("../write/temp.txt", "links2.php");
header("Location:http://cis2.elgin.edu/mjanicki/links2.php");
?>[/php]
I think it’s filled with errors as neither page of code seems to work properly. Please, any help would be a hugeeeee lifesaver! Thank you so much.