kratsg Posted September 14, 2009 Share Posted September 14, 2009 Manual: http://us2.php.net/manual/en/function.array-key-exists.php PHP Code function get_files($dir='[RELATIVE DIRECTORY HERE]/pages',$level=0,$files=array()){ $tab = null; for($i=0;$i<$level;$i++) $tab .= "\t"; $directory_listings = array_merge(glob($dir."*.php"),glob($dir."*",GLOB_ONLYDIR)); if(!count($directory_listings)){return false;} foreach($directory_listings as $file){ $disp_file = str_replace('[RELATIVE DIRECTORY HERE]/','',$file); $files[$disp_file] = (is_dir($file))?get_files($file.'/',$level+1):true; } return $files; } function get_page($page){ $valid_files = get_files();//see above function PHPFreaks if(!isset($page) || empty($page)){$page = 'pages/index';}//if empty, set $page = 'index' //Everything right here was echoing out on the page for you guys, ouput pasted below echo "<pre>"; var_dump($valid_files); echo "</pre>"; var_dump($page.'.php'); //Everything above here was echoing out on the page for you guys, output pasted below if(array_key_exists($page.'.php',$valid_files)){return 'pages/'.$page.'.php';} else {return 'pages/404.html';} } Output [pre] ["pages"]=> array(8) { ["pages/end.php"]=> bool(true) ["pages/index.php"]=> bool(true) ["pages/intro.php"]=> bool(true) ["pages/step1.php"]=> bool(true) ["pages/step2.php"]=> bool(true) ["pages/step3.php"]=> bool(true) ["pages/step4.php"]=> bool(true) ["pages/step5.php"]=> bool(true) } } string(15) "pages/index.php" [/pre] Problem There is no error at all (error_reporting/ini_set) added at the top of the page. It tells me that the page could not be found (IE: it included 404.html). if(array_key_exists($page.'.php',$valid_files)){return 'pages/'.$page.'.php';} else {return 'pages/404.html';} Some common questions answered: "Why are you setting the relative urls to the files as the keys?" - Well, if it is a file, it will contain the value true (meaning linkable). - If it is a directory, it can either contain the value false (meaning empty directory) or an array of what's inside the directory and so on. As you can imagine, this self-created function produces an output that is relatively easy to loop through and handle. "What is $level used for? You just include it in the function get_files() but it's not used anywhere!" - Right now, I don't have a huge directory of files working with. I may decide to have it stop nesting at a certain depth level and thus check the value of $level before opening the directory contents. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/174142-solved-array_key_exists-problem/ Share on other sites More sharing options...
AviNahum Posted September 14, 2009 Share Posted September 14, 2009 array( "file1.php" => "value", "file2.php" => "value", "file3.php" => "value", "file4.php" => "value", "file5.php" => "value", ); is this the way your array built? Quote Link to comment https://forums.phpfreaks.com/topic/174142-solved-array_key_exists-problem/#findComment-917967 Share on other sites More sharing options...
kratsg Posted September 14, 2009 Author Share Posted September 14, 2009 See the Output Posted Above: Output [pre] ["pages"]=> array( { ["pages/end.php"]=> bool(true) ["pages/index.php"]=> bool(true) ["pages/intro.php"]=> bool(true) ["pages/step1.php"]=> bool(true) ["pages/step2.php"]=> bool(true) ["pages/step3.php"]=> bool(true) ["pages/step4.php"]=> bool(true) ["pages/step5.php"]=> bool(true) } } string(15) "pages/index.php" [/pre] Quote Link to comment https://forums.phpfreaks.com/topic/174142-solved-array_key_exists-problem/#findComment-917968 Share on other sites More sharing options...
mikesta707 Posted September 14, 2009 Share Posted September 14, 2009 well one thing I saw right off the bat was return 'pages/'.$page.'.php' that will return "pages/pages/index.php" because $page already contains "pages/index.php"; I'm not sure if this is intentional or not, but this may be causing the 404 page to pop up Quote Link to comment https://forums.phpfreaks.com/topic/174142-solved-array_key_exists-problem/#findComment-917969 Share on other sites More sharing options...
kratsg Posted September 14, 2009 Author Share Posted September 14, 2009 well one thing I saw right off the bat was return 'pages/'.$page.'.php' that will return "pages/pages/index.php" because $page already contains "pages/index.php"; I'm not sure if this is intentional or not, but this may be causing the 404 page to pop up Right, well, I just fixed that. (that would've been a second bug had the first been fixed xD). As you can see in the code: var_dump($page.'.php'); //Everything above here was echoing out on the page for you guys, output pasted below if(array_key_exists($page.'.php',$valid_files)){return $page.'.php';} else {return 'pages/404.html';} } With the output being: [pre]string(15) "pages/index.php"[/pre] Quote Link to comment https://forums.phpfreaks.com/topic/174142-solved-array_key_exists-problem/#findComment-917973 Share on other sites More sharing options...
kratsg Posted September 14, 2009 Author Share Posted September 14, 2009 Just figured it out >.< So, it's actually that the function was fine, but I'll have to recursively loop through the array for obtaining all the array keys. It only checked the "pages" key. So I'll have to do a if(is_arr($val)){//loop} , so it's fine :-D Function: array_keys() returned only "pages" which helped in debugging. Quote Link to comment https://forums.phpfreaks.com/topic/174142-solved-array_key_exists-problem/#findComment-917977 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.