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