Results 1 to 5 of 5

Thread: PHP Class

  1. #1
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP Class

    Hey DD,
    I'm trying to create a forum page where i have a class in my PHP script so that any time i make something of that class and use Forum->Display(); it displays a table in which i would like to display the Forum title, number of topics, views, whatever, that's the idea. So my code retrieves the information from the database, and then in my class Forum function Display(), i want to be able to echo the variable containing the Forum title. But when inside the class function it won't let me echo that variable. Here's my code (the names of the functions and classes and whatnot is different):
    Code:
    <?php
    $con = mysql_connect("localhost");
    
    if (!$con) {
    	die('Could not connect: ' . mysql_error());
    }
    
    $db = mysql_select_db("forum_db", $con);
    
    if (!$db) {
    	$sql = "create database forum_db";
    	if (mysql_query($sql,$con)) {
    		$db = mysql_select_db("forum_db", $con);
    	}
    }
    
    $sql = "insert into Forums
    (ForumTitle, NumTopics)
    values
    ('General', 0)";
    
    $allow = mysql_query($sql,$con);
    
    if (!$allow) {
    	$sql = "CREATE TABLE Forums
    	(
    	ForumTitle varchar(25),
    	NumTopics int(5),
    	LastPerson varchar(25)
    	)";
    
    	$tab = mysql_query($sql,$con);
    	$sql = "insert into Forums
    	(ForumTitle, NumTopics)
    	values
    	('General', 0)";
    
    	$allow = mysql_query($sql,$con);
    }
    
    $result = mysql_query("SELECT * FROM Forums");
    
    $row = mysql_fetch_array($result);
    
    echo $row['ForumTitle'];
    echo "<br>";
    echo $row['NumTopics'];
    echo "<br>";
    
    class Tab {
      function buildTable() {
    	echo "<table class='TopicDisplay' cellspacing=0>";
    	echo "<tr>";
    	echo "<td class='Indicator'>";
    	echo "<center>Content</center>";
    	echo "</td>";
    	echo "<td class='Topic'>";
    	echo "<center>" . $row['ForumTitle'] . "</center>";
    	echo "</td>";
    	echo "</tr>";
    	echo "<tr>";
    	echo "<td class='Indicator'>";
    	echo "<center>Content</center>";
    	echo "</td>";
    	echo "<td class='Topic'>";
    	echo "<center>Content</center>";
    	echo "</td>";
    	echo "</tr>";
    	echo "<tr>";
    	echo "<td class='Indicator'>";
    	echo "<center>Content</center>";
    	echo "</td>";
    	echo "<td class='Topic'>";
    	echo "<center>Content</center>";
    	echo "</td></tr></table>";
      }
    }
    
    mysql_close();
    ?>
    It doesn't echo anything in that first table row where i put the echo row variable. Any suggestions?

    Thanks in advance.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    $row hasn't been defined within that scope. I believe you need to define it in the class, or pass it as an argument to the function.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oh, I thought it had to do something with defining the variable inside the class.

    Thanks.

    Also, as you may've noticed, my script above is loaded with if/else so that if a table is already there, it doesn't try to recreate it, or if a database can't be selected it creates a new one. Is there any other way to do that other than tons of if/else statements?

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes, you can simply:
    Code:
    	$sql = "CREATE TABLE IF NOT EXISTS Forums
    	(
    	ForumTitle varchar(25),
    	NumTopics int(5),
    	LastPerson varchar(25)
    	)";
    
    	$tab = mysql_query($sql,$con);
    	$sql = "insert into Forums
    	(ForumTitle, NumTopics)
    	values
    	('General', 0)";
    
    	$allow = mysql_query($sql,$con);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That's perfect.

    Thanks Twey.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •