Jump to content

can i put my code into a single global in oop ???


ricky spires

Recommended Posts

hello.

 

I have some oop code that pulls information about a page out of a mysql db but i dont want to put the code on every page. Instead in want to put it in the a function and pass it back to each page.

 

do i put it in a function in my functions.php file or can i put it into my includes/pages.php includes file ??

 

the code looks for the page name on the page and then pulls that pages info from the db.

this code works on the page - home.php

 

<?PHP
require_once("../includes/initialize.php");

$currentPages = Pages::find_all();

$pName = "adminHome";

$page_id = "";
$visible = "";
$pageZones = "";
$pageCRUD = "";
$pageTypes = "";
$module = "";
$title = "";
$sub_title = "";
$description = "";
$image = "";
$about = "";


foreach($currentPages as $currentPage){

$page_id = $currentPage->id;
$pageName = $currentPage->pageName;
$visible = $currentPage->visible;
$pageZone = $currentPage->pageZones_id;
$pageCRUD = $currentPage->pageCRUD_id;
$pageType = $currentPage->pageTypes_id;
$module = $currentPage->module_id;
$title = $currentPage->title;
$sub_title = $currentPage->sub_title;
$description = $currentPage->description;
$image = $currentPage->image;
$about = $currentPage->about;



if($pageName == $pName){

echo $pageName.'<br/>';
echo $page_id.'<br/>';
echo $visible.'<br/>';
echo $pageZone.'<br/>';
echo $pageCRUD.'<br/>';
echo $pageType.'<br/>';
echo $module.'<br/>';
echo $title.'<br/>';
echo $sub_title.'<br/>';
echo $description.'<br/>';
echo $image.'<br/>';
echo $about.'<br/>';
	}
}
?>

 

i would like to be able to just put global $page or something like that to get all the db info for each page.

 

 

i tried this in my  includes/pages.php includes file but it cant get the page name.

 

public static function find_by_pageName(){
	global $database;
	global $pName;

	$sql = "SELECT * FROM ".self::$table_name." WHERE pageName=".$pName."";
	$result_array = self::find_by_sql($sql);
	return !empty($result_array) ? array_shift($result_array) : false;
}

 

 

 

any suggestions ???? whats the best way to do this ?

 

thanks

ricky

ok, so im new to this oop stuff. how would i make it oop then ?

 

OOP is a topic far too broad to adequately address in a forum post.  Suffice it to say, OOP isn't about using objects as mere function collections.

 

If you really want to learn OOP in PHP, get the book PHP 5 Objects, Patterns, and Practice by Matt Zandstra.  It's the best introduction to OOP available.

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.