Jump to content

Recommended Posts

I keep running into a bunch of error when I  declare the page variable in my model.  What syntax do I need to use throughout the class?

 

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Header_Model extends CI_Model{

var $page = substr(end(explode(DIRECTORY_SEPARATOR, $_SERVER['PHP_SELF'])), 0, -4);

function get_page_name()
{
	$this->load->library('common');

	$page_names = $this->common->page_names();

	$title = (array_key_exists($page, $page_names) !== false) ? $page_names[$page]: '';

	if (array_key_exists($page, $page_names) !== false)
	{
		$title .= " | Jason Biondo";
	}

	return $title;
}

function get_js_page_file()
{
	if (file_exists("./assets/js/pages/${page}.js"))
	{
		$javascript = "<script type=\"text/javascript\" src=\"./js/pages/${page}.js\"></script>";
	}

	return $javascript;
}

}

Link to comment
https://forums.phpfreaks.com/topic/249983-declare-variables-in-php-class/
Share on other sites

Here is my full class.  Can anyone optimize this?  I'm still getting errors where I have my page variables and I  don't know why.

 

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Header_Model extends CI_Model{

var $page 			= '';
var $nav_names		= array();
var $banner_imgs 	        = array();

public function __construct(){
	$this -> page = substr(end(explode(DIRECTORY_SEPARATOR, $_SERVER['PHP_SELF'])), 0, -4);
	$this -> nav_names = array('Contact', 'About', 'Tools', 'Portfolio', 'Writing');
	$this -> banner_imgs = array('contact.jpg', 'about.jpg', 'tools.jpg', 'portfolio.jpg', 'articles.jpg');
}

function get_page_name()
{
	$this->load->library('common');

	$page_names = $this->common->page_names();

	$title = (array_key_exists($this->$page, $page_names) !== false) ? $page_names[$this->$page]: '';

	if (array_key_exists($this->$page, $page_names) !== false)
	{
		$title .= " | Jason";
	}

	return $title;
}

function get_gutter_value()
{
	$gutter_values 	= array('307', '230', '161', '84', '0');

	foreach($nav_names as $k => $name)
	{
		if($this->$page === strtolower($name))
		{
			$g_value = $gutter_values[$k];
		}
		else
		{
			$g_value = 0;
		}
	}

	return $g_value;
}

function get_js_page_file()
{
	$page = $this->$page;

	if (file_exists("./assets/js/pages/${page}.js"))
	{
		$javascript = "<script type=\"text/javascript\" src=\"./js/pages/${page}.js\"></script>";
	}

	return $javascript;
}
}

What are your errors?  Where, specifically (read: line numbers), are they appearing?  How are you trying to use your class?

 

Error is.. Fatal error: Cannot access empty property in /home/www-data/demo.jason.com/system/core/Model.php on line 50

 

I am trying to use my class to set up the header data I need.  Page title, navigation info, page name...

 

There is also an error on line 21

 

$title = (array_key_exists($this->$page, $page_names) !== false) ? $page_names[$this->$page]: '';

Also, in your get_js_page_file() method, try:

 

	function get_js_page_file()
{	
	if (file_exists("./assets/js/pages/{$this->page}.js"))
	{
		$javascript = "<script type=\"text/javascript\" src=\"./js/pages/{$this->page}.js\"></script>";
	}

	return $javascript;
}

 

Keep in mind, that if the file doesn't exist, your method will still attempt to return a then null $javascript variable since the return statement is outside of your conditional.

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.