[SOLVED] OOP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rs131
Forum Newbie
Posts: 5
Joined: Sat Jan 22, 2005 12:52 pm
Contact:

[SOLVED] OOP

Post by rs131 »

can somebody help me out with this code, when I open the file, it shows a blank white page (no links) I asked a guy that was really good with php and he had no clue what was wrong with it either...

Code: Select all

<?php

//Start class Template
class template {
	
	//Define class variables
	var $tblinks,
	$linkname,
	$linkurl,
	$linkid,
	$sqluser,
	$sqlpass,
	$sqldb,
	$sqlhost;
	
	//Start Links Function
	function Links(){
		
	$this->sqluser = 'root';
	$this->sqlpass = '';
	$this->sqldb = 'portal';
	$this->sqlhost = 'localhost';
        $this->tblinks = 'links';
	
	$dbcnx = @mysql_connect($this->sqlhost,$this->sqluser,$this->sqlpass);
        if(!$dbcnx) {
        echo( 'Could not connect to the MySQL server' );
        }
        mysql_select_db($this->sqldb);
	
	$get_links = mysql_query("SELECT ID, Name, URL FROM $this->tblinks");

	// Get Links From Database
	while ($link_data = mysql_fetch_array($get_links)) {
	$this->linkid = $link_data['ID'];
	$this->linkname = $link_data['Name'];	
	$this->linkurl = $link_data['URL'];
	
	//Display Links
	echo("<br><a href=$url>$name</a>");
	}
	
	//Set new variable
	$SiteLinks = new template;
	$Links = $SiteLinks->Links();
	}
}
//End Class Template
	
	
?> 

<?=$Links;?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. the class is never instantiated
  2. $Links inside Links() is a function scope level variable and will not be exported to the global namespace.
  3. If you called Links(), you'd get a infinite circular creation loop as

    Code: Select all

    $SiteLinks = new template;
        $Links = $SiteLinks-&gt;Links();
    is inside Links();
rs131
Forum Newbie
Posts: 5
Joined: Sat Jan 22, 2005 12:52 pm
Contact:

Post by rs131 »

awesome man, thanks a lot! now the major problem with my template system has been solved.
Post Reply