mahalleday Posted October 17, 2010 Share Posted October 17, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/216100-php-class-help/ Share on other sites More sharing options...
ignace Posted October 18, 2010 Share Posted October 18, 2010 class CertifiedAds { function getAd($id = null) { return $id === null ? new AdList($this->db->fetchAll('SELECT * FROM ads')) : new AdList($this->db->fetchRow('SELECT * FROM ads WHERE id = ..')); } } Quote Link to comment https://forums.phpfreaks.com/topic/216100-php-class-help/#findComment-1123335 Share on other sites More sharing options...
mahalleday Posted October 20, 2010 Author Share Posted October 20, 2010 I am pretty new to object orientated php so I'm not too sure how the method works exactly. It looks like it is depending an other database class to retrieve to info from the database. Perhaps I am missing some thing here can you explain? Quote Link to comment https://forums.phpfreaks.com/topic/216100-php-class-help/#findComment-1124547 Share on other sites More sharing options...
AbraCadaver Posted October 20, 2010 Share Posted October 20, 2010 Main issue is this: if(isset($this-id)) { Should be this ( note the ->): if(isset($this->id)) { Quote Link to comment https://forums.phpfreaks.com/topic/216100-php-class-help/#findComment-1124552 Share on other sites More sharing options...
ignace Posted October 20, 2010 Share Posted October 20, 2010 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 .. } } Quote Link to comment https://forums.phpfreaks.com/topic/216100-php-class-help/#findComment-1124558 Share on other sites More sharing options...
mahalleday Posted October 25, 2010 Author Share Posted October 25, 2010 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] Quote Link to comment https://forums.phpfreaks.com/topic/216100-php-class-help/#findComment-1126019 Share on other sites More sharing options...
mahalleday Posted October 26, 2010 Author Share Posted October 26, 2010 Just bumping this back up. Hopefully someone can give me a hand here. Quote Link to comment https://forums.phpfreaks.com/topic/216100-php-class-help/#findComment-1126682 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.