Page 1 of 1

in_array problem

Posted: Sun Jan 29, 2012 6:19 pm
by EcazS
Alright, so while working with a "client" he wanted the option to create static pages instead of being limited by dynamic pages; so I thought I would be good to implement static pages within my CMS so I did, I just have one problem right now with my init.inc file.
$main		= new Main;
$static         = $main->IsStatic();
$static_name 	= end(explode('/', $_SERVER['SCRIPT_NAME']));
	
if(in_array($static_name, $static ) {
	header("Location: http://google.com");
}else {
	$parser = new Parser;
	$parser->ParseTemplate();
}
However this is not working. The static variable is an array filled with the names of the static pages (services.php, portfolio.php, clients.php)
This is how the static array looks,
Array
(
    [0] => Array
        (
            [page_name] => services.php
        )

    [1] => Array
        (
            [page_name] => portfolio.php
        )

    [2] => Array
        (
            [page_name] => clients.php
        )

)
Is it not working because it's a multidimensional array?

Re: in_array problem

Posted: Tue Jan 31, 2012 7:26 pm
by jacek
Yeah in_array effectively looks like this
function in_array($test, $array){
    foreach ($array as $value){
        if ($value == $test){
            return true;
        }
    }
    
    return false;
}
you the way you are doing it will probably not work.

Why does the array even have to be 2D though :s ? Surely no pages will have the same name ?

Re: in_array problem

Posted: Tue Jan 31, 2012 7:40 pm
by EcazS
I'm using the fetch_array function from the MySQL class,
public function fetch_array(){
	$results = array();
		
	while ($this->fetch($row)){
		$results[] = $row;
	}
		
	return $results;
}

Re: in_array problem

Posted: Fri Feb 03, 2012 12:55 am
by jacek
I would try fetch and create the array manually
while ($mysql->fetch($row)){
    $static_pages[] = $row['page_name'];
}
something along those lines :)