Jump to content

[SOLVED] array_key_exists() problem?


kratsg

Recommended Posts

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!

Link to comment
Share on other sites

See the Output Posted Above:

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]

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.