I’m writing a news script, and I’m having trouble making a way for the files to be deleted.
How can I change this script so that the <input type="checkbox" /> has a value of the file in the same row?
<?php
// This function reads all available news
function getNewsList(){
$fileList = array();
// Open the actual directory
if ($handle = opendir("news")) {
// Read all file from the actual directory
while ($file = readdir($handle)) {
if (!is_dir($file)) {
$fileList[] = $file;
}
}
}
rsort($fileList);
return $fileList;
}
// new
$list = getNewsList();
print("<table>\n");
print("<tr><td> </td><td>Article Title:</td><td>Post Date:</td></tr>\n");
foreach ($list as $value) {
$newsData = file("news/".$value);
$newsTitle = $newsData[0];
$submitDate = $newsData[1];
unset ($newsData['0']);
unset ($newsData['1']);
$newsContent = "";
foreach ($newsData as $value) {
$newsContent .= $value;
}
print("<tr>");
print("<td><input type=\"checkbox\" name=\"check_files[]\" value=\"$file\" /></td>");
print("<td>$newsTitle</td>");
print("<td>");
print("$submitDate");
print("</td>");
print("</tr>\n");
}
print("</table>\n");
I have a similar script for managing files, and it’s something like $dirArray[$index] for the file name, but I can’t figure out how to adapt this script to work the same way, because I’m too new to PHP.
Thanks for the help!
EDIT: This is the line where the file name needs to be: (instead of $file, which doesn’t work for some reason):
print("<td><input type=\"checkbox\" name=\"check_files[]\" value=\"$file\" /></td>");
The
$filevariable isn’t defined anywhere, you could try this:replace
with