Page 1 of 1

PHP Class Help

Posted: Mon Oct 25, 2010 6:45 pm
by halledega
I am building a module for a CMS I use and want to use a class to 'build' each post before it is displayed on the browser. What I have is something like this.

my_class.php

Code: Select all

<?php

class myClass{

var $id;

//set data method
    function setData($data) {$this->id=$data;}

//function to get number os total posts
    function postCount() {
        $sql = "SELECT post_id FROM blog_posts";
        $query = mysql_query($sql);
        $no_posts = mysql_num_rows($query);

        return $no_posts;
    }

//function to retreve dat from databse table
    function getPost() {

        if(isset($this->id)) {
            $sql = "SELECT * FROM blog_posts WHERE post_id=".$this->id;
        }
        else {
            $sql = "SELECT * FROM blog_posts";
        }

        $query = mysql_query($sql) or die($data = "<p>SQL: $csql</p><p>".mysql_error()."</p>");
        while($posts = mysql_fetch_array($query,MYSQL_ASSOC)) {

            $cats = self::getCategory($posts['category_id']);
            $sub_cats = self::getSubCategory($posts['subcategory_id']);
            $images = self::getImages($posts['post_id']);

            $data = array(
                          'post_id'=>$posts['post_id'],
                          'post_title'=>$posts['post_title'],
                          'post_short'=>$posts['post_short'],
                          'post_long'=>$posts['post_long'],
                          'category_name'=>$posts['category_id'],
                          'subcategory_name'=>$posts['subcategory_id'],
                          'image'=>$images['image_name'],
                          'post_start'=>$posts['post_start'],
                          'post_end'=>$posts['post_end']
                          );
        }
        return $data;
    }
//method to get category for post
    function getCategory($id = NULL,$list = 0) {
        $data = array();
        if($list == 0 AND $id = NULL) {
            $sql = "SELECT * FROM blog_categories WHERE category_id = $id";
        }
        else {
            $sql = "SELECT * FROM blog_categories";
        }
        $query = mysql_query($sql) OR die($data = "<p>SQL: $sql</p><p>".mysql_error()."</p>");
        while($ret_val = mysql_fetch_array($query,MYSQL_ASSOC)){
            $category[] = $ret_val;
        }
        return $category;
    }
//method to get subcategory for post
    function getSubCategory($id = NULL,$list = 0) {
        $data = array();
        if($list == 0 AND $id = NULL) {
            $sql = "SELECT * FROM blog_subcategories WHERE category_id = $id";
        }
        else {
            $sql = "SELECT * FROM blog_subcategories";
        }
        $query = mysql_query($sql) OR die($data = "<p>SQL: $sql</p><p>".mysql_error()."</p>");
        while($ret_val = mysql_fetch_array($query,MYSQL_ASSOC)){
            $subcategory[] = $ret_val;
        }

        return $subcategory;
    }
//method to get images from database
    function getImages($id) {
        $sql = "SELECT * FROM blog_images WHERE post_id  = $id";
        $query = mysql_query($sql) OR die($data = "<p>SQL: $sql</p><p>".mysql_error()."</p>");
        while($ret_val = mysql_fetch_array($query,MYSQL_ASSOC)) {
            $images[] = $ret_val;
        }


        return $images;
    }
}//end myClass
index.php

Code: Select all

<?php

include('class_myClass.php');

$myClass1 = new myClass();

$no_posts = $myClass1->postCount();

for($i=0;$i<=$no_posts-1;$i++) {
    $myClass1->setData($i+1);
    $posts[$i]=$myClass1->getPost();
}


$path = "images/";

foreach($posts as $post) {
    $images=$myClass1->getImages($post['post_id']);
    $cats=$myClass1->getCategory($post['category_name']);
    $subs=$myClass1->getSubCategory($post['subcategory_name']);
?>
<table>
    <tr>
        <td>
            <p>
                <?php echo $post['post_title'];?>  Posted on:  <?php echo $post['post_start'];?>  Category: <?php echo $cats['category_name'];?>
                <span style="float: right">
                    <?php foreach($images as $img) {echo '<img src="'.$path.$img['image_name'].'" alt="alt text" width="50" height="50"/>';}?>
                </span>
            </p>
            <p><?php echo $post['post_short'];?></p>
            <a href="#">Click to read more...</a>
        </td>
    </tr>
</table>

<?php $i++;}//end while ?>
I have attached a screen shot of what happens in the browser. No formatting or CSS implemented yet obviously the final product will look a lot better.

Image

Ideally I would like to have just one method called in index.php (getPost()) and have the other methods used within the getPost() method. Any help is much appreciated. I'm not that new to php but very new to oo php.

Mike

Re: PHP Class Help

Posted: Mon Oct 25, 2010 11:22 pm
by requinix
halledega wrote:I have attached a screen shot of what happens in the browser.
Oh? Because I could swear otherwise.

No really, I can swear "otherwise". Watch:
tasairis wrote:****
See? Even got wordfiltered.

Re: PHP Class Help

Posted: Tue Oct 26, 2010 2:31 pm
by halledega
How exactly was that helpful. I posted a legitimate question here, thanks for nothing.

Re: PHP Class Help

Posted: Tue Oct 26, 2010 2:58 pm
by Zyxist
You are not new to PHP, so you should have already come across such warnings and notices, because they have nothing to do with objects, but with your messy coding style. Before using any variable or array field, initialize it, and do not refer to unexisting ones! Do not put any data to foreach that are neither arrays nor objects. That's it - the messages perfectly show you where the problems are located.