Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
OOP Recursive Function Problem
Old 05-26-2009, 09:23 PM OOP Recursive Function Problem
Junior Talker

Posts: 4
Trades: 0
Well, I think it's the recursive function. This is my first post so might as well make it a hard one. On my local server, I am getting the results I want. My webhost isn't the most agreeable however.

Anyway, I am stumped so thanks to anyone with any suggestions.

The Task:

I was trying to display an error message when a page couldn't be found in the database. Eg: index.php?page=home is valid, but index.php?hom is not. My local wamp server is displaying the "Page Not Found" page (stored in the database) but my webhost is displaying an Internal Server Error.

There are essentially 3 files I would like to focus on. I have trimmed the files down to relevant content. (??? implies missing content)

/index.php
/includes/header.php
/classes/page.php

index.php
PHP Code:
<?php include_once('includes/header.php'); ?>
    ???
    <?php $page->display(); ?>
    ???
header.php
PHP Code:
<?php
    
???
    
// filename() returns the name of the current script
    
if(filename() == 'index.php')
    {
        if(isset(
$_GET['page']) && !empty($_GET['page']))
        {
            
$ptitle $_GET['page'];
            
$page = new Page($ptitle);
        }
        else if(!isset(
$_GET['page']))
            
$page = new Page('Home');
        else
            
$page = new Page('Page Not Found');
    }
?>
    ???
    <li><a href="index.php?page=Home">Home</a></li>
    ???
page.php
PHP Code:
<?php
    
class Page
    
{
        ???
        const 
ERROR_PAGE 'Page Not Found';
        
        ???

        public 
$title;
        public 
$content;
        public 
$image;
        public 
$redirect;
        public 
$meta_description;
        public 
$meta_keywords;
        public 
$timestamp;
        
        ???
    
    
// Retrieve the page from the database
        
private function retrieve($title='')
        {
            if(empty(
$title))
                
$title $this->title;
            
            
// Sanitize required data
            
$title $this->database->sql_safe($title);
            
            
$query "Select * From $this->table Where $this->pkey = '$title'";
            
            if(
$this->database->query($query))
            {
                if(
$this->database->num_rows() > 0)
                {
                    
$data $this->database->get_result_array();
                    
                    
$this->title $data['title']; // Replaces the title with the one stored in the database
                    
$this->content $data['content'];
                    
$this->image $data['image'];
                    
$this->redirect $data['redirect'];
                    
$this->timestamp $data['timestamp'];
                    
                    
// If the meta tags have been set for the page, overwrite the default ones
                    
if($data['meta_description'] != null)
                        
$this->meta_description $data['meta_description'];
                    if(
$data['meta_keywords'] != null)
                        
$this->meta_keywords $data['meta_keywords'];
                        
                    return 
true;
                }
                else
                    
$this->retrieve(self::ERROR_PAGE);
            }
            else
                return 
false;
        }

        ???
    
    
// Redirect the page to the specified url if it has been set
        
private function redirect_page()
        {
            if(
$this->redirect != null)
            {
                
$this->outputbuffer->clean(); // Clear the output buffer
                
header('Location: ' $this->redirect);
                exit();
            }
            else
                return 
false;
        }
    
// Print / Redirect the page
        
public function display()
        {
            if(!
$this->redirect_page())
                
$this->print_page();
        }
?>
I think that's understandable enough without any further explanation. Also, if you have any recommendations on doing things differently, please say. :-)
Taur is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-27-2009, 02:01 AM Re: OOP Recursive Function Problem
markAtRitetek's Avatar
Experienced Talker

Posts: 32
Name: Mark Hudson
Location: Atlanta, GA
Trades: 0
Try changing
"Select * From $this->table Where $this->pkey = '$title'"
to
"Select * From {$this->table} Where {$this->pkey} = '{$title}'"
__________________
Mark Hudson
Ritetek Design

Please login or register to view this content. Registration is FREE
markAtRitetek is offline
Reply With Quote
View Public Profile Visit markAtRitetek's homepage!
 
Old 05-27-2009, 05:06 AM Re: OOP Recursive Function Problem
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
If it is the case that zero rows are returned for the title self::ERROR_PAGE, then you'll be in an infinite recusion, which would result in a stack overflow (which would be reported as an internal server error). Verify that there are more than zero rows returned for the title self::ERROR_PAGE.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 05-27-2009, 09:36 AM Re: OOP Recursive Function Problem
Junior Talker

Posts: 4
Trades: 0
Thank you both for the replies.

The query is fine as it seems to work perfectly.

The following url's work:

index.php?page=Home
index.php?page=Page+Not+Found (url's are decoded)

This causes an internal server error:

index.php?page=Hom

................

I feel like such a fool. I am reluctant to tell you what the problem was lol I was using the error page "Error" but then changed it to "Page Not Found" on my local MySQL and didn't update my webhost. :-( My bad! You were right NullPointer. Thank you both for the help. I never even thought of it. :S

Last edited by Taur; 05-27-2009 at 09:51 AM..
Taur is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to OOP Recursive Function Problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.36628 seconds with 12 queries