liam1412 Posted February 15, 2010 Share Posted February 15, 2010 Hey there I am fairly new to OOP and am a little confused as to some behavior with a script I am writing. Here is the class forum.class.php class forum { function __construct() { require_once('config.php'); $this->db = new mysql(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); } function get_categories() { $this->db->connect(); $sql = "SELECT * FROM bb_categories ORDER BY category_disp_order"; $rows = $this->db->fetch_all_array($sql); return $rows; $this->db->close(); } function get_forums($id) { $this->db->connect(); $sql = "SELECT * FROM bb_forums WHERE forum_belongs_to='".$this->db->escape($id)."' ORDER BY forum_disp_order"; $rows = $this->db->fetch_all_array($sql); return $rows; $this->db->close(); } } and here is the page running it <?php session_start(); include 'functions.php'; $user = new user; $forum = new forum; $categories = $forum->get_categories(); include 'header.php'; ?> <div class="centrecol"> <div class="paddingleft"> <div class="contentbox"> <div class="contenthead"> <h3>Webmaster Forums</h3> </div> <div class="contentbody"> <div id="forumtop"> Forum Home </div> </div> </div> <?php foreach($categories as $category_details) { if($category_details['category_is_private'] == 0) { ?> <div class="contentbox"> <div class="contenthead"> <h3><?php echo $category_details['category_name']; ?></h3> </div> <div class="contentbody"> <table class="forum" cellspacing="0" cellpadding="0" border="0"> <?php $forums = $forum->get_forums($category_details['category_id']); foreach($forums as $forum_details) { ?> <tr> <td class="forumdets"> <div class="cellpadding"> <a href="viewforum.php?forumid=<?php echo $forum_details['forum_id']; ?>"><?php echo $forum_details['forum_name']; ?></a> </div> </td> <td class="forumlastpost"> <div class="cellpadding"> <br /> </div> </td> <td class="forumcounter1" align="center"> <br /> </td> <td class="forumcounter2" align="center"> <br /> </td> </tr> <?php } ?> </table> </div> </div> <?php } } ?> </div> </div> <?php include 'footer.php'; ?> When I try to run the script it calls the categories fine, but when trying to get the forums within each category it is giving failed to connect to DB error. This is resolved by including this line again, just before the 2nd query. $forum = new forum; Why is that even though the class has been declared once I have to declare it again before using it in another query. I know I could just use joins to get all this in 1 query but I really want to keep it as simple as possible? I was of the opinion to class was available till the script stopped running. Quote Link to comment Share on other sites More sharing options...
liam1412 Posted February 15, 2010 Author Share Posted February 15, 2010 Turns out its the DB class I am using. Doesn't like the $this->db->connect(); to be called twice. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.