0

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'];
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
EasyBB
  • 6,176
  • 9
  • 47
  • 77

1 Answers1

1

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.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • Ok I'll take a look into creating a dynamically generated JSON file for the pages the user sets. Also I'm looking into `cURL` – EasyBB Jul 21 '14 at 02:36
  • cURL wont be needed if both your files are on same server. Don't need to waste 1 HTTP request when this can easily be done using file system but if the target PHP code has to process then yes, `cURL` or `file_get_contents` are relevant. – Hanky Panky Jul 21 '14 at 02:37
  • Ok Hanky I'll try and give the JSON a whirl ok. Just need to work on deleting entire property when the file is deleted. – EasyBB Jul 21 '14 at 02:39
  • Hanky I tried creating a JSON file and decode it but I get returned NULL when trying to var_dump it – EasyBB Jul 21 '14 at 02:50
  • That new format you are using is nice. First `var_dump($jsona);` to see if you are getting the response fine in the first place, then you can decode it later. – Hanky Panky Jul 21 '14 at 02:51
  • I echoed `$jsona` and I get the contents just not when I try to decode it...must be something silly I am doing. – EasyBB Jul 21 '14 at 02:52
  • `var_dump($jsona)` === `string(103) "{ pages:{ "index.php":{ enabled:true, theme:"dark", identifier:"KMS" } } }"` – EasyBB Jul 21 '14 at 02:54
  • Ok that's because the JSON format you have is invalid. Probably you hand built that json? it would be better to use json_encode o an array so you don't have to worry about the format. You are missing quotation marks on the array keys. http://codepad.org/8pPh4UuJ – Hanky Panky Jul 21 '14 at 02:57
  • oh we need dbl quotes through the entire thing ok. Yeah I will be doing this with encode, but right now all testing is going to be written hardcoded until I get everything sorted. I'm learning everything here on my own and the help of SO. – EasyBB Jul 21 '14 at 03:00
  • How to remove the page property? I'm trying `unset($data[$page])` – EasyBB Jul 21 '14 at 03:53