Jump to content

OOP - Having to redeclare a class mid script


liam1412

Recommended Posts

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.