in_array problem

Ask about a PHP problem here.
Post Reply
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

in_array problem

Post 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.

[syntax=php]
$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();
}[/syntax]
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,
[syntax=text]
Array
(
[0] => Array
(
[page_name] => services.php
)

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

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

)
[/syntax]
Is it not working because it's a multidimensional array?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: in_array problem

Post by jacek »

Yeah in_array effectively looks like this

[syntax=php]function in_array($test, $array){
foreach ($array as $value){
if ($value == $test){
return true;
}
}

return false;
}[/syntax]
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 ?
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: in_array problem

Post by EcazS »

I'm using the fetch_array function from the MySQL class,
[syntax=php]
public function fetch_array(){
$results = array();

while ($this->fetch($row)){
$results[] = $row;
}

return $results;
}
[/syntax]
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: in_array problem

Post by jacek »

I would try fetch and create the array manually

[syntax=php]while ($mysql->fetch($row)){
$static_pages[] = $row['page_name'];
}[/syntax]
something along those lines :)
Image
Post Reply