phpvariablesob-start

Get page information and not the entire page


In index.php though the name can be anything, I have an array at the very top for now, just for testing purposes.

<?php
  $settings = array(
   enabled => true,
   id => "KMS",
   theme=>"kms_standard",
  );
?>

I started this to use as information to use in my CMS table. Basically I am scanning the main directory for pages and other directories. Then I echo a li out to the "table" showing information though I need more information than just the date it was created/edited.

EX:

Page Name       |       Identifier        |        Status       |      Theme     |     Created/Edited
 index.php             CMS                        Enabled              Dark          12/12/12 12:00      

To get the created/edited date I use date('l jS \of F Y h:i:s A',filemtime('../'.$page))

Is there a way to set data like this for a document and then get it, if not how would i use the array in a for each loop without including the entire file. I believe if I do include($file) it will parse all the HTML and PHP, but I only need the $settings variable from that page. I hope I am making myself clear enough. If not example below

function is_dir_empty($dir) {
  if (!is_readable($dir)) return NULL; 
      $handle = opendir($dir);
      while (false !== ($entry = readdir($handle))) {
         if ($entry != "." && $entry != "..") {
         return FALSE;
        }
      }
     return TRUE;
 }
$pages = scandir('../',0);
foreach($pages as $page){
    if(is_dir('../'.$page) && !is_dir_empty('../'.$page)){
       //iterate deeper to get these files as well
    }elseif(!is_dir('../'.$page)){
     $file = get_file('../'.$page);//basically get the array of information some how
     echo '<div class="row" id="'.removeExtensions($page).'"><ul>'.
          '<li class="fifths"><div class="checkbox_container">'.
          '<input type="checkbox" id="'.$kpage.'_check" value="'.$page.'" class="checkbox" name="delete_page[]" />'.
          '<label for="'.$kpage.'_check"><span></span></label></div>'.$kpage.'</li>'.
          '<li class="fifths">'.$file['id'].'</li>'.
          '<li class="fifths">'.$file['enabled'].'</li>'.
          '<li class="fifths">'.$file['theme'].'</li>'.
          '<li class="fifths" title="'.date('l jS \of F Y h:i:s A',filemtime('../'.$page)).'">'.date('l jS \of F Y h:i:s A',filemtime('../'.$page)).'</li></ul></div>';
    }
}

testing table for pages

UPDATE FOR FUTURE REFERENCES This works perfectly

JSON

{
    "index.php":{
       "enabled":"true",
       "theme":"dark",
       "identifier":"KMS"
    }
}

dashboard

$jsona = file_get_contents("../pages.json");
$jsonb = json_decode($jsona,true);      
$data = $jsonb['pages'];

Solution

  • Is there a way to set data like this for a document and then get it, if not how would i use the array in a for each loop without including the entire file. I believe if I do include($file) it will parse all the HTML and PHP, but I only need the $settings variable from that page.

    Code should normally be included using the include you don't wish to use. Since your $settings variable is PHP code, i'd suggest to use that method still.

    Any other way of including the file still involves reading the file and then parsing the required content from it, this in turn might be more resource intensive than using an include. When you include a file you get all that PHP code available to the file which includes it.

    However for this situation it would be better to store that data in either a database or at least a text format file and then parse that file for your purpose. There is no need to process a whole file if you just have to get some static data from it. You can store the data in XML or JSON for easy manipulation and not have to work with plain strings.

    As for your code in the Edit.

    if(isset($_GET['data'])){
     $settings = array(
       enabled => true,
       id => "KMS",
       layout=>"kms_standard",
      );
      return $settings;
      exit();
    }
    

    return in PHP has no meaning for an HTTP request. This will not output anything and your cURL wont get any response. Your $settings array has to be printed on the page. Now comes a question of how to print an array and have the output in a useable format. That's where those common formats like JSON or XML become even more important. You don't want to use string manipulation to read the array when you can simply use some native methods.