আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML, CSS PHP,MySQL,JavaScript, AJAX, JQUERY, Bootstrap and Live Project কোর্সের ৮৭ তম ব্যাচ এবং ২৬ ডিসেম্বর-২০১৭ তারিখ থেকে শুরু হচ্ছে চার মাস ব্যাপী Zend PHP-7 Certified PHP Engineering (Advance PHP) কোর্সের ৩৫ তম ব্যাচ। প্রত্যেকটি কোর্স এর ফী নির্ধারণ করা হয়েছে ৩০,০০০/= আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722 81 75 91

Create a Content Management System With PHP and MySQLI Part-2

Welcome back guys! In the first article of this series I show you that how can you make a simple (html & css) template useable for CMS. And make menu & categories dynamic.
Now I wil try to explain how you make dynamic content for menus & categories. So let’s start.

Download The Project

In our menu table we nake a column “content”. When we click on “About” it will display the related content from database into index page.
When we click on category like “Basic PHP” it will display all articles which are posted under this category.
Now we need to make a new table “articles”.
Here is our “articles” table structure

CREATE TABLE IF NOT EXISTS `articles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `cat_id` int(11) NOT NULL,
  `title` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `content` text COLLATE utf8_unicode_ci NOT NULL,
  `status` enum('Publish','Unpublish') COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=12 ;

Int cat_id column we add the category id like if the article is related to “Basic PHP” we enter cat_id = 1. Because in categories table “Basic PHP” id is 1.
Now we add some demo data:

INSERT INTO `articles` (`id`, `cat_id`, `title`, `content`, `status`) VALUES
(1, 1, 'PHP Basic 1', 'show you as your_page_name from your database. This part, called a hash, is included in the current URL without a page refresh, creating an entry in the browser’s history. By monitoring this hash with javascript, we can change the loaded page by AJAX and provide a seamless browsing experience.', 'Publish'),
(2, 1, 'PHP Basic 2', 'show you as your_page_name from your database. This part, called a hash, is included in the current URL without a page refresh, creating an entry in the browser’s history. By monitoring this hash with javascript, we can change the loaded page by AJAX and provide a seamless browsing experience.', 'Publish'),
(3, 2, 'PHP MYSQLI 1', 'show you as your_page_name from your database. This part, called a hash, is included in the current URL without a page refresh, creating an entry in the browser’s history. By monitoring this hash with javascript, we can change the loaded page by AJAX and provide a seamless browsing experience.', 'Publish'),
(4, 2, 'PHP MYSQLI 2', 'show you as your_page_name from your database. This part, called a hash, is included in the current URL without a page refresh, creating an entry in the browser’s history. By monitoring this hash with javascript, we can change the loaded page by AJAX and provide a seamless browsing experience.', 'Publish'),
(5, 5, 'CSS Basic 1', 'show you as your_page_name from your database. This part, called a hash, is included in the current URL without a page refresh, creating an entry in the browser’s history. By monitoring this hash with javascript, we can change the loaded page by AJAX and provide a seamless browsing experience.', 'Publish'),
(6, 5, 'CSS Basic 2', 'you as your_page_name from your database. This part, called a hash, is included in the current URL without a page refresh, creating an entry in the browser’s history. By monitoring this hash with javascript, we can change the loaded page by AJAX and provide a seamless browsing experience.', 'Publish'),
(7, 4, 'HTML Basic 1', 'show you as your_page_name from your database. This part, called a hash, is included in the current URL without a page refresh, creating an entry in the browser’s history. By monitoring this hash with javascript, we can change the loaded page by AJAX and provide a seamless browsing experience.', 'Publish'),
(8, 4, 'HTML Basic 2', 'show you as your_page_name from your database. This part, called a hash, is included in the current URL without a page refresh, creating an entry in the browser’s history. By monitoring this hash with javascript, we can change the loaded page by AJAX and provide a seamless browsing experience.', 'Publish'),
(9, 3, 'Javascript Basic 1', 'show you as your_page_name from your database. This part, called a hash, is included in the current URL without a page refresh, creating an entry in the browser’s history. By monitoring this hash with javascript, we can change the loaded page by AJAX and provide a seamless browsing experience.', 'Publish'),
(10, 3, 'Javascript Basic 2', 'show you as your_page_name from your database. This part, called a hash, is included in the current URL without a page refresh, creating an entry in the browser’s history. By monitoring this hash with javascript, we can change the loaded page by AJAX and provide a seamless browsing experience.', 'Publish'),
(11, 1, 'PHP Basic 3', 'kgyfkhvhjvkyhfiugfigf  iuiufui fi tf iut tiu itgfi t ug  o fi o iyof gfygefgeriyuf geiyuf eihui ioure uiore uire hiuerh ueirh reiuh erui theuit heurit huihtr ti etherut reu euthut reut reuith eruiht uireh tureiht uriet huireth iuerht uire tuire tuire htuire tuier ture tue tuier reh e ier  e e rehre eo', 'Publish');

Ok come back to menu. When we click on “About” or “Contact Us”, in index.php view the data from “content” column respectively.
To do this, at first we need to pass “id” of that menu using url. For this we add this code in

<ul>
<li><a href="" class="current">Home</a></li>
<?php
foreach($mycms->showMenu("menu") as $m)
{
extract($m);
?>
<li><a href="index.php?menu_id=<?php echo $id; ?>"><?php echo $name; ?></a></li>
<?php
}
?>
</ul>

1

In here inside the red box code we declare a variable menu_id & we assign id of that menu in that variable. When we click on “About” in url we see “index.php?menu_id=1”. That means it take the “id” of that menu.
Now our job is to display the content of that menu in content page. In content page we need to receive the “id” and we use a method to read the content.
Add this code in in config.php

	public function readContent($id,$table)
	{
		$query = mysqli_query($this->conn,"SELECT * FROM $table WHERE id=$id AND status='Publish'");
		return mysqli_fetch_assoc($query);
	}

2

And add this code into in content.php

public function readContent($id,$table)
{
$query = mysqli_query($this->conn,"SELECT * FROM $table WHERE id=$id AND status='Publish'");
return mysqli_fetch_assoc($query);
}

3

We use a method called readContent() which take 2 parameter “menu_id” & “table”.
In if(isset($_REQUEST[‘menu_id’])) that means if $_REQUEST which is a global variable is set by “menu_id”, means if we get “menu_id” from url then do the following instruction.
If it returns true we extract() the mentod result. This readContent() method return us a single record from the table. Because u filter the result by a unique id.
Using the extract() method we convert all index into a variable. Now we can call all column heading in our desire location.
Our output will be look like this.

4

5

So, we can display all our menu content in our index page.
Lets do something on Categories.
Add this code to in sidebar.php

<ul class="templatemo_list">
<?php
foreach($mycms->showMenu("categories") as $m)
{
extract($m);
?>
<li><a href="index.php?cat_id=<?php echo $id; ?>"><?php echo $name; ?></a></li>
<?php
}
?>
</ul>

As the same way we pass the id of a category through url in “cat_id”.
Add this code into content.php

<?php
if(isset($_REQUEST['cat_id']))
{
foreach($mycms->readAllArticles($_REQUEST['cat_id']) as $art)
{
extract($art);
?>
<h2><?php echo $title; ?></h2>
<p><?php

$arr = explode(" ",$content);
$slice = array_slice($arr, 0 , 50);
echo $con = implode(" ",$slice);
?>
&nbsp;
<a href="index.php?art_id=<?php echo $id; ?>">Read More..</a></p>
<?php
}
}
?>

Here we find something new. We use foreach loop here. Because under a categories there may multiple articles. When we click a category we need to retrieve all the articles under that category. And then we use extract(). After that we place our variable in our desire place. All articles retrieve from “articles” table.
Look in content we try something new.

$arr = explode(" ",$content);

We make an array using explode() function. Separate all word depending on space(“ ”). All the word in content now in an $arr array.

 $slice = array_slice($arr, 0 , 50);

Now we use array_slice to take few word from that array. We take index 0 to index 50.

 echo $con = implode(" ",$slice);

Now we add all the slice array using a space(“ ”) using implodeI() function and then print it.

We do that because we display few word from every article and use a “read more..” link to read full article.

6

See we use

 <a href="index.php?art_id=<?php echo $id; ?>">Read More..</a>

In here we also pass a id if that article. When we click on that article full article will display in content.php.
Just add this code in content.php

<?php
if(isset($_REQUEST['art_id']))
{
extract($mycms->readContent($_REQUEST['art_id'],"articles"));
?>
<h2><?php echo $title; ?></h2>
<p><?php echo $content; ?></p>
<?php
}
?>

7

Now we want to display all articles in our home page. When we load the blog all articles will be displayed one by one.
For that we create a method in our config.php file

public function readArticles()
{
$query = mysqli_query($this->conn,"SELECT * FROM articles WHERE status = 'publish'");
return mysqli_fetch_all($query,MYSQLI_ASSOC);
}

Now add this few code into our content.php file

else
{
foreach($mycms->readArticles() as $all)
{
extract($all);
?>
<h2><?php echo $title; ?></h2>
<p><?php
$arr = explode(" ",$content);
$slice = array_slice($arr, 0 , 50);
echo $con = implode(" ",$slice);
?></p>
<?php
}
?>
<hr />
<?php
}

Now refresh the index.php page you will see all the content in your home page.

8

I think something wrong in there. Am I right? May be you people don’t want to watch all the articles in home page. So we need to display limited articles in home page and make pagination.
Yea, now we are going to make pagination in our content page. Let’s start.

We make some change in our method readArticles()

public function readArticles($page)
{
$perpage = 5;
$calc = $perpage * $page;
$start = $calc - $perpage;
/* Look if $page = 1, then $start = 0, so this Query retrive article index 0 to 5 */
$query=mysqli_query($this->conn,"SELECT * FROM articles WHERE status='publish' Limit $start, $perpage");
return mysqli_fetch_all($query,MYSQLI_ASSOC);
}

And create a new method for Pagination forPagination($page)

public function forPagination($page)
{
/*Display Per Page 5 Articles */
$perpage = 5;

/*To Find out how may rows in articles table & Store the result in a virtual column name "Total"*/
$result = mysqli_query($this->conn,"select count(*) As Total from articles");
$rs = mysqli_fetch_assoc($result);
$total = $rs["Total"];

/*Ceil will take the Highest value. like fi the result is 4.1 ceil make it 5*/
$totalPages = ceil($total / $perpage);
echo "<ul class='pagination'>";
if($page <=1 )
{
/*If the page is less the 1 or 1 the Prev will not hold any link*/
echo "<li><span id='page_links' style='font-weight: bold;'>&laquo; Prev</span></li>";
}
else
{
/*otherwise Prev always hold the previous page link*/
$j = $page - 1;
echo "<li><span><a id='page_a_link' href='index.php?page=$j'> &laquo; Prev</a></span></li>";
}
for($i=1; $i <= $totalPages; $i++)
{
if($i<>$page)
{
/*If the page not 1 the print the page number & hold the link.*/
echo "<li><span><a id='page_a_link' href='index.php?page=$i'>$i</a></span></li>";
}
else
{
/*If the page is 1 the Display 1 & don't hold any link*/
echo "<li><span id='page_links' style='font-weight: bold;'>$i</span></li>";
}
}
if($page == $totalPages )
{
/*If the page is last page then "Next" don't hold any link*/
echo "<li><span id='page_links' style='font-weight: bold;'>Next &raquo;</span></li>";
}
else
{	/*If the page is not last then "Next" hold the link of the next page*/
$j = $page + 1;
echo "<li><span><a id='page_a_link' href='index.php?page=$j'>Next &raquo;</a></span></li>";
}
echo "</ul>";
}

Now add this code in the bottom of content.php page

else
{
foreach($team44->showAllArticles($page) as $articles)
{
extract($articles);
?>
<h2><?php echo $title; ?></h2>
<p><?php
$article=explode(" ",$content);
$slice_article=array_slice($article,0,50);
echo implode(" ",$slice_article);
?>
<a href="index.php?art_id=<?php echo $id;?>">Read More...</a></p>
<?php
}
?>
<table width="400" cellspacing="2" cellpadding="2" align="center">
<tbody>
<tr>
<td align="center">
<?php
$team44->showPagination($page);
?>
</td>
</tr>
</tbody>
</table>
<?php
}

Now Refresh the index.php page, it will display first 5 articles & the bottom display the pagination.

9

So finally our pagination complete & also end of Part-2. Hope this will helpful for you.

2 comments on “Create a Content Management System With PHP and MySQLI Part-2

Leave a Reply

Your email address will not be published. Required fields are marked *