2. Dynamic Category and Category Articles show

1.Create Database Table articles


2.Crud.php file


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");
            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");
            return mysqli_fetch_all($query,MYSQL_ASSOC);
        } else{
            return false;


$obj = new Crud();


<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>
        $categories = $obj->categories("categories");
        foreach ($categories as $category) {
            ?> <li><a href="index.php?cat_id=<?php echo $cat_id; ?>"><?php echo $cat_name; ?></a></li>
    <div class="clear"></div>


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

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


Output like following screenshot


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