Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在数据库中保留php文件的更新列表_Php_Oop - Fatal编程技术网

在数据库中保留php文件的更新列表

在数据库中保留php文件的更新列表,php,oop,Php,Oop,我需要在我的项目上有一个页面轮询根目录,如果它看到任何新的php文件,它需要将文件名添加到数据库中,以便我可以处理权限。那部分似乎起作用了 然而,就我的一生而言,我不知道如何告诉它查看根目录并让它删除数据库中不再存在的任何页面。一些问题是底部的许多代码不是OOP,我将$dbpages作为对象返回。$dbpages如下所示: array (size=4) 0 => object(stdClass)[5] public 'id' => string '56' (

我需要在我的项目上有一个页面轮询根目录,如果它看到任何新的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)
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看起来像这样:

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)
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有点陌生,所以任何帮助都会很好。这是我正在赠送的一个开源项目的一部分。

我不确定是否理解您的问题

你说“一切都在这一点上运行”

在下面的代码中,您将遍历数组$dbpages的所有条目。这些条目包含一个具有字符串属性id和页面的对象。我看不出您访问这些属性的方式有任何错误

因此,如果您希望检查,如果属性页中的文件存在,您可能是指文件\u 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;
        }
    }
}

我明白问题所在。实际上,其中有几个:

  • 第一个问题是,在检查当前
    $dbpages
    数组中是否存在页面时,没有正确迭代。在初始的
    foreach
    中,需要另一个
    foreach
    来迭代
    $dbpages
    数组
  • 另一个问题(必须更多地考虑效率)是,一旦找到页面,您就不会离开迭代,这会使您的算法性能变慢。您需要使用
    break
  • 每次迭代都应该给出您想要的结果。如果您再次迭代以获得“删除”,那么您所做的工作将是原来的两倍。使用一次迭代来获得您所需要的一切
我要做的是(阅读代码中的注释):


希望这有帮助。

getPageFiles()
中,为什么要将页面名称设置为数组键?老实说,我不太确定,但这允许我将实际的文件名弹出到数据库中,它起了作用,所以我使用了它。我必须编辑一下删除函数,但这正是我需要的。非常感谢。