Create pages in blog CMS using PHP and MYSQL | Part 10


create dynamic pages in-blog using php

Pages are also very important for blog CMS. Although the content has been given importance in the blog, the page also has great importance in any blog website. Here we are learning to create a complete blog CMS from the beginning "how to create a blog from scratch in PHP" tutorial. Pages are very important for SEO. Pages represent the blog website, what kind of content is written on this blog website. Pages help you even if you have to take approval for your blog website. In order to create a blog website in a professional manner, the page has to be created in the blog.

In the previous tutorials, we have created separate pages for tags and categories; similarly, we will use PHP and MYSQL to build pages. First, let's understand the database table. To create pages in a blog, a table is created in the MYSQL database. Let's have a table in which the page data will be inserted.
Table name - techno_pages

Create pages in blog using PHP and MYSQL database

All the fields related to the page are taken in the table above. Make page ID auto increment. All these fields will be used to create pages and display page data. Page fields are created for SEO purpose, such as - Page Keywords and Meta Description.

Let's learn to add a page to dynamic blog CMS.

admin/blog-pages.php


First of all, create the main page for blog pages. It will contain all the blog pages on the HTML table. Here we will create edit, delete and add a new page button.
We have learned this operation in the last few tutorials. In this tutorial, we will understand the main part.

 
$stmt = $db->query('SELECT pageId,pageTitle FROM techno_pages ORDER BY pageId DESC');

As you can see in the query above, we are using here by fetching the title and id of the page. It becomes very simple. After this, we display the blog pages in descending order using the while loop.
Complete PHP code for blog pages - “


admin/blog-pages.php

 

<?php
//include connection file 
require_once('../includes/config.php');

//check login or not 
if(!$user->is_logged_in()){ header('Location: login.php'); }


if(isset($_GET['delpost'])){ 

    $stmt = $db->prepare('DELETE FROM techno_pages WHERE pageId = :pageId') ;
    $stmt->execute(array(':pageId' => $_GET['delpost']));

    header('Location: blog-pages.php?action=deleted');
    exit;
} 


?>

<?php include("head.php");  ?>

  Admin Page 
  
  <?php include("header.php");  ?>

<?php //show message from add / edit page if(isset($_GET['action'])){ echo '

Post '.$_GET['action'].'.

'; } ?> <table> <tr> <th>Article Title</th> <th>Update</th> <th>Delete</th> </tr> <?php try { $stmt = $db->query('SELECT pageId,pageTitle,pageDescrip,pageContent,pageKeywords FROM techno_pages ORDER BY pageId DESC'); while($row = $stmt->fetch()){ echo '<tr>'; echo '<td>'.$row['pageTitle'].'</td>'; ?> <td> <button class="editbtn"> <a href="edit-blog-page.php?pageId=<?php echo $row['pageId'];?>">Edit</a> </button></td><td> <button class="delbtn"> <a href="javascript:delpost('<?php echo $row['pageId'];?>','<?php echo $row['pageTitle'];?>')">Delete</a></button> </td> <?php echo '</tr>'; } } catch(PDOException $e) { echo $e->getMessage(); } ?> </table>

<button class="editbtn"><a href='add-blog-page.php'>Add New Page</a></button>

<?php include("sidebar.php"); ?> <?php include("footer.php"); ?>

Now, create another PHP file to create pages in blog CMS.


admin/add-blog-page.php


Creating a page becomes very easy if you have understood all the previous blog tutorials well. Building a page like creating blog posts, as a blog post, the page also has to write the title of the page, page tags, page description, and page content. If you have understood the operation of adding blog posts, then it will also become easy for you. Let's understand the main part.

 $ pageSlug = slug ($ pageTitle);// insert into database$ stmt = $ db-> prepare ('INSERT INTO techno_pages (pageTitle, pageSlug, pageDescrip, pageContent, pageKeywords) VALUES (: pageTitle,: pageSlug,: pageDescrip,: pageContent,: pageKeywords)');

In the code lines given above, you can see that all the fields related to the page have been inserted in the MYSQL database by the query. The slug() function is used to convert page title to slug. The slug function was created in previous tutorial. To make SEO friendly URL for the page, we convert page tile to slug.
Complete PHP code for add blog page-“
admin/add-blog-page.php

 <?php require_once('../includes/config.php'); 

if(!$user->is_logged_in()){ header('Location: login.php'); }
?>

<?php include("head.php");  ?>
 
  Add New Page - Techno Smarter Blog
    <script src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script>
    <script>
          tinymce.init({
       mode : "specific_textareas",
    editor_selector : "mceEditor",
              plugins: [
                  "advlist autolink lists link image charmap print preview anchor",
                  "searchreplace visualblocks code fullscreen",
                  "insertdatetime media table contextmenu paste"
              ],
              toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
          });
  </script>

  <?php include("header.php"); 

   ?>

Add New Article

<?php //if form has been submitted process it if(isset($_POST['submit'])){ //collect form data extract($_POST); //very basic validations if($pageTitle ==''){ $error[] = 'Please enter the Page title.'; } if($pageDescrip ==''){ $error[] = 'Please enter the Page description.'; } if($pageContent ==''){ $error[] = 'Please enter the content.'; } if($pageKeywords ==''){ $error[] = 'Please enter the Page Keywords.'; } if(!isset($error)){ try { $pageSlug = slug($pageTitle); //insert into database $stmt = $db->prepare('INSERT INTO techno_pages (pageTitle,pageSlug,pageDescrip,pageContent,pageKeywords) VALUES (:pageTitle, :pageSlug, :pageDescrip, :pageContent,:pageKeywords)') ; $stmt->execute(array( ':pageTitle' => $pageTitle, ':pageSlug' => $pageSlug, ':pageDescrip' => $pageDescrip, ':pageContent' => $pageContent, ':pageKeywords' => $pageKeywords )); //redirect to index page header('Location: blog-pages.php?action=added'); exit; }catch(PDOException $e) { echo $e->getMessage(); } } } //check for any errors if(isset($error)){ foreach($error as $error){ echo '

'.$error.'

'; } } ?> <form action='' method='post'>


<input type='text' name='pageTitle' style="width:100%;height:40px" value='<?php if(isset($error)){ echo $_POST['pageTitle'];}?>'>


<textarea name='pageDescrip' cols='120' rows='6'><?php if(isset($error)){ echo $_POST['pageDescrip'];}?></textarea>


<textarea name='pageContent' id='textarea1' class='mceEditor' cols='120' rows='20'><?php if(isset($error)){ echo $_POST['pageContent'];}?></textarea>


<input type='text' name='pageKeywords' value='<?php if(isset($error)){ echo $_POST['pageKeywords'];}?>' style="width:100%;height:40px">

<input type='submit' class="editbtn" name='submit' value='Submit'>

</form>
<?php include("sidebar.php"); ?> <?php include("footer.php"); ?>

Now, add the update operation in blog CMS.

admin/edit-blog-page.php

The page can be edited using the update operation.
We have also understood the update operation in the previous tutorials.
We use the MYSQL update query to update the MYSQL database fields.
Complete PHP code for Update operation-


admin/edit-blog-page.php


<?php require_once('../includes/config.php'); 
if(!$user->is_logged_in()){ header('Location: login.php'); }
?>
<?php include("head.php");  ?>
    Update Page - Techno Smarter Blog
    <script src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script>
  <script>
          tinymce.init({
            mode : "specific_textareas",
    editor_selector : "mceEditor",
              plugins: [
                  "advlist autolink lists link image charmap print preview anchor",
                  "searchreplace visualblocks code fullscreen",
                  "insertdatetime media table contextmenu paste"
              ],
              toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
          });
  </script>
    <?php include("header.php");  ?>

Edit Post

<?php if(isset($_POST['submit'])){ //collect form data extract($_POST); //very basic validation if($pageId ==''){ $error[] = 'Invalid ID .'; } if($pageTitle ==''){ $error[] = 'Please enter the Page title.'; } if($pageDescrip ==''){ $error[] = 'Please enter the Page description.'; } if($pageContent ==''){ $error[] = 'Please enter the content.'; } if($pageKeywords ==''){ $error[] = 'Please enter the Article Keywords.'; } if(!isset($error)){ try { $pageSlug = slug($pageTitle); //insert into database $stmt = $db->prepare('UPDATE techno_pages SET pageTitle = :pageTitle, pageSlug = :pageSlug, pageDescrip = :pageDescrip, pageContent = :pageContent, pageKeywords = :pageKeywords WHERE pageId = :pageId') ; $stmt->execute(array( ':pageTitle' => $pageTitle, ':pageSlug' => $pageSlug, ':pageDescrip' => $pageDescrip, ':pageContent' => $pageContent, ':pageId' => $pageId, ':pageKeywords' => $pageKeywords )); //redirect to index page header('Location: blog-pages.php?action=updated'); exit; } catch(PDOException $e) { echo $e->getMessage(); } } } ?> <?php //check for any errors if(isset($error)){ foreach($error as $error){ echo $error.'
'; } } try { $stmt = $db->prepare('SELECT pageId, pageSlug,pageTitle, pageDescrip, pageContent, pageKeywords FROM techno_pages WHERE pageId = :pageId') ; $stmt->execute(array(':pageId' => $_GET['pageId'])); $row = $stmt->fetch(); } catch(PDOException $e) { echo $e->getMessage(); } ?> <form action='' method='post'> <input type='hidden' name='pageId' value='<?php echo $row['pageId'];?>'>


<input type='text' name='pageTitle' style="width:100%;height:40px" value='<?php echo $row['pageTitle'];?>'>


<textarea name='pageDescrip' cols='120' rows='6'><?php echo $row['pageDescrip'];?></textarea>


<textarea name='pageContent' id='textarea1' class='mceEditor' cols='120' rows='20'><?php echo $row['pageContent'];?></textarea>


<input type='text' name='pageKeywords' style="width:100%;height:40px;"value='<?php echo $row['pageKeywords'];?>'

<input type='submit' class="editbtn" name='submit' value='Update'> </form>
<?php include("sidebar.php"); ?> <?php include("footer.php"); ?>

admin/header.php


Add another link in header. Edit header file and add the line after categories.

  • <a href='blog-pages.php'>Pages</a>
  • After creating pages , the pages have to display in the listwise. Let's display the page in the menu. All the pages that are made will be displayed in the menu.
    blog/header.php
    Add the code lines below at the line number 10 (Hint- Add after the home list inside the ul tag )

    blog/header.php

    <?php
    $baseUrl="http://localhost/blog/page/"; 
            try {
    
                $stmt = $db->query('SELECT pageTitle,pageSlug FROM techno_pages ORDER BY pageId ASC');
                while($rowlink = $stmt->fetch()){
                    
                    echo '
  • <a href="'.$baseUrl.''.$rowlink['pageSlug'].'">'.$rowlink['pageTitle'].'</a>
  • '; } } catch(PDOException $e) { echo $e->getMessage(); } ?>

    In the code above, we are using page title and page slug. Here, the base URL is also defined, which represents the path of the page.

    blog/page.php

    After creating the page, the page content has to be displayed on another page. Like in the previous tutorials, the show.php file was created to display the post content, similarly, we will create another PHP file to display the page content separately.

    Complete PHP code for page

    blog/page.php

    We discussed this code in the first tutorial. Follow the same process and understand the code below.

     
    <?php require('includes/config.php'); 
    
    $stmt = $db->prepare('SELECT pageId,pageTitle,pageSlug,pageContent,pageDescrip,pageKeywords FROM techno_pages WHERE pageSlug = :pageSlug');
    $stmt->execute(array(':pageSlug' => $_GET['pageId']));
    $row = $stmt->fetch();
    
    //if post does not exists redirect user.
    if($row['pageId'] == ''){
        header('Location: ./');
        exit;
    }
    
    
    ?>
    
    <?php include("head.php");  ?>
    
      <?php echo $row['pageTitle'];?>
          
    
    
      <?php include("header.php");  ?>
    
    <?php echo '

    '.$row['pageTitle'].'

    '; ?>
    <?php echo '

    '.$row['pageContent'].'

    '; ?>
    <?php include("sidebar.php"); ?> <?php include("footer.php"); ?>
    If you click and visit the page now, the page will not open because we have need to add one line .htaccess file.
    Add the line after the catlinks .

    blog/.htaccess

     RewriteRule ^page/(.*)$ page.php?pageId=$1 [L] 

    In the code line above, the page is another PHP file for all pages from MYSQL database
    The complete code of the .htaccess file will be like this.

    RewriteEngine On
    RewriteBase /blog/
    RewriteRule ^category/(.*)$ catlinks.php?id=$1 [L]
    RewriteRule ^page/(.*)$ page.php?pageId=$1 [L]
    RewriteRule ^tag/(.*)$ taglinks.php?id=$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-d [NC]
    RewriteCond %{REQUEST_FILENAME} !-f [NC]
    RewriteRule ^(.*)$ show.php?id=$1 [QSA,L]
     

    In this way, you can create pages in the blog CMS using PHP and MYSQL database.



    Support Us - Subscribe Now

    Recommended Posts:-

    Previous Posts:-

    Shop Items:-


    Dynamic Countdown timer using JS ,PHP and MYSQL | Animated timer

    $10



    Paypal Payment form in PHP website with MYSQL database | International Payments

    $14



    Integrate Payumoney payment gateway in PHP website | Source Scripts

    $14







    free instagram followers free instagram likes
    escort istanbul
    escort mersin
    deutsche escort