2. Dynamic Category and Category Articles show

1.Create Database Table articles

article

2.Crud.php file

<?php

class Crud {

    public $conn;

    public function __construct() {
        $this->conn = mysqli_connect("localhost", "root", "", "fresh");
        if (mysqli_connect()) {
            //echo "Connect Successfully";
        } else {
            die ("connection error");
            
        }
    }
   
    /* This is for category show */
    public function categories($table){
        $query = mysqli_query($this->conn, " SELECT * FROM $table");
        if(mysqli_num_rows($query)>0){
            return mysqli_fetch_all($query, MYSQL_ASSOC);
        } else{
            return false;
        }
    }
    
    /* This is for Category Article show*/
    public function CatArticle($table,$id){
        $query = mysqli_query($this->conn, "SELECT * FROM $table WHERE cat_id=$id");
        if(mysqli_num_rows($query)>0){
            return mysqli_fetch_all($query,MYSQL_ASSOC);
        } else{
            return false;
        }
    }

}

$obj = new Crud();

3.sidebar.php

<div class="content1_box2 flt_lt">
    <h2>Latest work</h2>
    <div class="content1_box2_img"><a href="#"><img src="images/img.jpg" width="300" height="135" alt="img" /></a></div>
    <p>Pellentesque diam ligula, egestas eget rhoncus vel, hendrerit in sem. Viva mus laoreet erat sit amet ante ullamcorper vestibulum. <a href="#">Read More</a></p>
    <h2>Categories</h2>
    <ul>
        <?php
        $categories = $obj->categories("categories");
        foreach ($categories as $category) {
            extract($category);
            ?> <li><a href="index.php?cat_id=<?php echo $cat_id; ?>"><?php echo $cat_name; ?></a></li>
            <?php
        }
        ?>
    </ul>
    <div class="clear"></div>
</div>

4.content.php

<div class="content1">
    <div class="content1_box1 flt_lt">

        <?php
        /* This is for category articles */
        if (isset($_REQUEST['cat_id'])) {
            $articles = $obj->CatArticle("articles", $_REQUEST['cat_id']);
            //echo "<pre>";
            //print_r($articles);
            foreach ($articles as $article) {
                echo "<h2>" . $article["art_title"] . "</h2>";
                echo "<p>" . $article["art_content"] . "</p>";
            }
        }
        ?>

        <br/>
    </div>

Output like following screenshot
category

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s