Jump to content

How to check an array for a matching string and return true - Newbie help


spryce

Recommended Posts

I have a web server which contains a public site and a private site. I want to be able to test for public or private using the page title and return the appropriate header which contains a different banner, css etc.

 

Firstly - the syntax below seems to be incorrect because it doesnt work correctly.

More importantly how do I add all the values to an array and than iterate through it to check for a matching page title?

 

public function publicSite() {
	if ($this->getTitle()== 'Home' || 'About Us'||'Registration' || 'Sitemap' || 'Contact Us' || 'Useful Links' || 'Feedback') {
		return true;
	}
	return false;
}

Then later on I would have:

	if ($this->publicSite()) {
		require($ROOT.'interface/pages/publicheader.php'); 
	}
	else {
		require($ROOT.'interface/pages/privateheader.php'); 
	}

 

Thanks heaps!

try this

 

public function publicSite() {
	if ($this->getTitle()== 'Home' || $this->getTitle()== 'About Us'|| $this->getTitle()=='Registration' || $this->getTitle()== 'Sitemap' || $this->getTitle()=='Contact Us' ||$this->getTitle()== 'Useful Links' || $this->getTitle()=='Feedback') {
		return true;
	}
	return false;
}

kney's reply is syntactically correct, but a more appropriate way will be this:-

 

$titles = array('Home', 'About Us', 'Registration', 'Sitemap', 'Contact Us', 'Useful Links', 'Feedback');

if (in_array($this->getTitle(), $titles))
{
    $this->publicSite() = true;
}

else
{
    $this->publicSite() = false;
}

kney's reply is syntactically correct, but a more appropriate way will be this:-

 

$titles = array('Home', 'About Us', 'Registration', 'Sitemap', 'Contact Us', 'Useful Links', 'Feedback');

if (in_array($this->getTitle(), $titles))
{
    $this->publicSite() = true;
}

else
{
    $this->publicSite() = false;
}

 

Thanks :)..

I didn't know how to use arrays anymore, that's why the 'crappy' reply :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.