我需要在我的项目上有一个页面轮询根目录,如果它看到任何新的php文件,它需要将文件名添加到数据库,以便我可以处理权限。那部分似乎有效。
但是,对于我的生活,我无法弄清楚如何告诉它查看根目录并让它删除数据库中不再存在的任何页面。一些问题是底部的很多代码都不是OOP,我将$ dbpages作为对象返回。 $ dbpages看起来像这样:
array (size=4)
0 =>
object(stdClass)[5]
public 'id' => string '56' (length=2)
public 'page' => string 'includeme.php' (length=13)
public 'private' => string '1' (length=1)
1 =>
object(stdClass)[8]
public 'id' => string '57' (length=2)
public 'page' => string 'index.php' (length=9)
public 'private' => string '0' (length=1)
虽然$ pages看起来像这样:
array (size=3)
'includeme2.php' => string 'includeme2.php' (length=14)
'includeme2_2.php' => string 'includeme2_2.php' (length=16)
'index.php' => string 'index.php' (length=9)
这是我到目前为止所拥有的......
$pages = getPageFiles(); //retreives php files in root
$dbpages = fetchAllPages(); //Retrieve list of pages in pages table of db
$count = 0;
$dbcount = count($dbpages); //Thought I could use this for something
$creations = array();
$deletions = array();
//Check if any pages exist which are not in DB
foreach ($pages as $page){
if(!isset($dbpages->page)){
$creations[] = $page;
}
}
// dump($creations);
//Enter new pages in DB if found
if (count($creations) > 0) {
createPages($creations) ;
}
//EVERYTHING is working above this point
if (count($dbpages) > 0){
//Check if DB contains pages that don't exist
foreach ($dbpages as $page){
if(!isset($page->page)){
$deletions[] = $page->id;
}
}
}
// //Delete pages from DB if not found
if (count($deletions) > 0) {
deletePages($deletions);
}
//Update DB pages
$dbpages = fetchAllPages();
作为参考,我的getPageFiles()函数看起来像这样..
//Retrieve a list of all .php files in root files folder
function getPageFiles() {
$directory = "../";
$pages = glob($directory . "*.php");
foreach ($pages as $page){
$fixed = str_replace('../','',$page);
$row[$fixed] = $fixed;
}
return $row;
}
我的fetchAllPages()看起来像这样: //获取所有页面上的信息
function fetchAllPages() {
$db = DB::getInstance();
$query = $db->query("SELECT id, page, private FROM pages");
$pages = $query->results();
return $pages;
}
if (isset($row)){
return ($row);
}
我对OOP PHP有些新意,所以任何帮助都会很棒。这是我赠送的开源项目的一部分。
答案 0 :(得分:1)
我看到了问题。其中一些实际上是:
$dbpages
数组中是否存在页面时,您没有正确迭代。您的初始foreach
中需要另一个foreach
才能遍历$dbpages
数组。break;
。我要做的是以下内容(阅读代码中的注释):
$pages = getPageFiles(); //retreives php files in root
$dbpages = fetchAllPages(); //Retrieve list of pages in pages table of db
$creations = array();
/*
* iterate through each page in directory, check each page to each page
* found in DB, if the page exists in the DB, remove that page object
* from the $dbpages array and set a bool flag to true. Once all pages in
* the DB are checked, check if the page was found, if not, add that to
* the $creations array
*/
foreach ($pages as $page) {
$page_exists = false;
foreach ($dbpages as $k => $dbpage) {
if ($dbpage->page === $page) {
unset($dbpages[$k]);
$page_exists = true;
break;
}
}
if (!$page_exists) {
$creations[] = $page;
}
}
/*
* Remaining DB pages (not found) are to be deleted.
* This function turns the remaining objects in the $dbpages
* array into the $deletions array using the 'id' key.
*/
$deletions = array_column(array_map(function ($o) {
return (array)$o;
}, $dbpages), 'id');
//Enter new pages in DB if found
if (count($creations) > 0) {
createPages($creations);
}
// //Delete pages from DB if not found
if (count($deletions) > 0) {
deletePages($deletions);
}
//Update DB pages
$dbpages = fetchAllPages();
?>
希望这有帮助。
答案 1 :(得分:0)
我不确定我是否理解你的问题。
你说"一切都在这一点上工作"。
在下面的代码中,您将遍历数组 $ dbpages 的所有条目。这些条目包含一个对象,其字符串属性为 id 和 page 。我没有看到你如何访问这些属性。
因此,如果你想检查,如果文件存在 page 属性,你可能意味着 file_exists()而不是 isset()。哪个会给出这个:
if (count($dbpages) > 0){
//Check if DB contains pages that don't exist
foreach ($dbpages as $page){
// if(!isset($page->page)){
if (!file_exists($page->page)){
$deletions[] = $page->id;
}
}
}