Jump to content

PHP Class help


mahalleday

Recommended Posts

I am building a classified ads module for a CMS I use and want to use a class to 'build' each ad before it is displayed on the browser.  What I have is something like this.

<?php
class classifedAds {
var $id;
//set method
function setData($data) {
$this->id=$data;
}
//ad method
function getAd()
if(isset($this-id)) {
$sql = "SELECT * FROM ads WHERE ad_id =".$this->id;
}
else {
$sql = "SELECT * FROM ads";
}
$query = mysql_query($sql);
while($data = mysqk_fetch_array($query, MYSQL_ASSOC)) {
//get ad images
$images = self::getImages($data['ad_id']);
//get ad category name
$category = self::getCategory($data[cat_id]);
$ad = array(
'title'=> $data['title'],
'desc'=> $data['desc'],
'price'=> $data['price'],
'image'=> $images['image'],
'category'->$category['category_name']
);

return $ad;
}//end getAd
//get images method
function getImages($ad_id) {
$sql = "SELECT image FROM images WHERE ad_id=$ad_id";
$query = mysqL_query;
$images = mysql_fetch_arry($query, MYSQL_ASSOC);
return $images;
//get categories method
function getImages($ad_id) {
$sql = "SELECT category FROM categories WHERE cat_id=$cat_id";
$query = mysqL_query;
$category = mysql_fetch_arry($query, MYSQL_ASSOC);
return $category;
}
}//end class
?>

 

This works ok if i set an ad id first like this....

 

<?php
$class = new classifedAds;
$class -> setData($some_ad_id_number);
$ad = $class->getAd();
?>

 

But because I need to display a list of ads same on the main page I would like to just do this...

 

<?php
$class = new classifedAds;
$ad = $class->getAd();
?>

 

Which does not work at all.  I'll post a screen shot later when I get home and run it.  Hopefully someone here can point me in the right direction.  The other Issue I seem to have is being able to loop through ads images.  So assuming that the getAd() method works fine I try this to get the images to display.

 

<?php
foreach($ad['image'] as $img) {
echo '<img scr="/path_to_images/'.$img.'"/>';
}
?>

 

This outputs something like this...

 

<img scr="/path_ti_images/(some random letter)"/>

 

for each of the images in the $ad['image'] array.

 

Any help on either of these issues is greatly appreciated.

Link to comment
Share on other sites

class CertifiedAds {
    function getAd($id = null) {
        $result =  $id === null
            ? mysql_query('SELECT * FROM ads')
            : mysql_query('SELECT * FROM ads WHERE id = ..');
        if($result && mysql_num_rows($result)) {
            while($row = mysql_fetch_assoc($result)) {
                //..
            }
        }
        return ..
    }
}

Link to comment
Share on other sites

Ok finally back on my home machine, here is the actual class I have made and the code used to access the file and output the info from the database in the browser.

 

class_myclass.php

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

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

 

Ideally I would like to have just one method called in index.php (getPost()) and have the other methods used within the getPost()  method.  Sorry if there is any confusion with me using this in terms of a blog but I plan to port the code over for the classifies project the blog tuff is being used cause it is simpler then what I plan on doing with the classifieds.

 

[attachment deleted by admin]

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.