Log in

View Full Version : PHP Class



InNeedofHelp
06-02-2006, 07:59 PM
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):

<?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.

Twey
06-02-2006, 08:56 PM
$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.

InNeedofHelp
06-03-2006, 12:58 AM
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?

Twey
06-03-2006, 11:32 AM
Yes, you can simply:
$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);

InNeedofHelp
06-03-2006, 02:53 PM
That's perfect.

Thanks Twey. :D