CREATING A BLOG (UPDATED FOR MYSQLI) - Jul 22nd, 2019
|
I recently put together a blog on one of my client’s sites and I thought I’d put the recipe up just in case someone could benefit from it.
BACKGROUND: The blog editor (called ‘blog’) has a title, date, contents and an image upload field. Image thumbnail2 is set for a max width of 200 px and a max height of 300 px
The client didn’t want to allow comments from others so that’s not included in the code. If they had, I would have used either the Website Comments plugin or a 3rd party solution like http://www.gentlesource.com/comment-script/ , http://www.phpjabbers.com/post-comment/ , http://www.adriantnt.com/products/comments_script/ , or http://js-kit.com/comments/
The images are padded and wrapped in the contents by the following css:
.pad { border-right: 20px solid transparent; border-bottom: 10px solid transparent; border-top: 5px solid transparent;} img { border:hidden;} img.floatLeft {float: left; margin: 10px;} img.floatRight {float: right; margin: 4px; }
I’ve allowed the visitor to decide if they want to view the earliest or most current posts first and retaining their choice for subsequent searches.
The use of the letters a and b in the form for added security are explained in the post called: ALLOWING VISITORS TO SET VIEWER ORDERBY OPTIONS
The number of records shown on the main viewer page is set in a single record editor called ‘common_information’.
A major key to creating the blog is the code that produces a listing of unique months and years for existing articles to act as an archive of blog posts.
Another was being able to display the archive month and year once at the top of the page, next to the word Archives
Here’s the code for the main blog page (blog1.php):
<span class="heading_font">Have It Your Way</span> <form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<select name="order"> <option value="b">earliest date at the top</option> <option value="a">most recent date at the top</option> </select> <input type="submit" name="submit" value="Click/Tap To Choose Display Order"> </form> <?php $orderBy = "date"; if (@$FORM['order'] == 'b') { $orderBy = "date"; } if (@$FORM['order'] == 'a') { $orderBy = "date DESC"; }
?> <?php $record_limit = $common_informationRecord['record_limit'] ?> <?php if (@!$record_limit ) { $record_limit1 = ""; } ?> <?php if (@$record_limit >=1 ) { $record_limit1 = $common_informationRecord['record_limit']; } ?>
<?php // load records from 'blog' list($blogRecords, $blogMetaData) = getRecords(array( 'tableName' => 'blog', 'loadUploads' => true, 'allowSearch' => false, 'orderBy' => $orderBy, 'limit' => $record_limit1, )); ?> <table width="50%" border="0" align="center" cellpadding="10"> <tr> <td width="75%" align="left" valign="top"><table width="100%" border="0" align="left" cellpadding="2"> <?php foreach ($blogRecords as $record): ?> <tr> <td align="left" ><hr /> <span class="text_font_bold"><?php echo date("D, M jS, Y ", strtotime($record['date'])) ?> <?php echo strtoupper($record['title']) ?></span> <div class=" pad" style="float:left"> <div style="float:none"> <?php foreach ($record['image'] as $upload): ?> <a target="_blank" href="<?php echo $upload['urlPath'] ?>"> <img src="<?php echo $upload['thumbUrlPath2'] ?>" width="<?php echo $upload['thumbWidth2'] ?>" height="<?php echo $upload['thumbHeight2'] ?>" alt="" /></a> <?php endforeach ?> </div> </div> <span class="text_font"><?php echo $record['content'] ?></span></td> </tr> <?php endforeach ?> </table></td> <td width="25%" align="left" valign="top"><p><span class="page_title_font"> Or Select An Archive</span> <?php // get list of unique months and years with articles $query = "SELECT DATE_FORMAT(date, '%M %Y') as dateAndYear, YEAR(date) as year, MONTH(date) as month FROM cms_blog GROUP BY dateAndYear ORDER BY date"; $result = mysqli()->query($query) or die("MySQL Error: ". htmlspecialchars(mysqli()->error) . "\n"); while ($record = $result->fetch_assoc()): ?> <a href="blog2.php?date_year=<?php echo $record['year'] ?>&date_month=<?php echo $record['month'] ?>&orderBy=<?php echo $orderBy ?>"><?php echo $record['dateAndYear']; ?> </a> <?php endwhile ?> </td> </tr> </table>
And here’s the code for the archive page (blog2.php)
<span class="heading_font">Have It Your Way</span> <form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>"> <input type="hidden" name="date_year" value="<?php echo @$_REQUEST['date_year']; ?>"> <input type="hidden" name="date_month" value="<?php echo @$_REQUEST['date_month']; ?>"> <input type="hidden" name="orderBy" value="<?php echo @$_REQUEST['orderBy']; ?>"> <select name="order"> <option value="b">earliest date at the top</option> <option value="a">most recent date at the top</option> </select> <input type="submit" name="submit" value="Click/Tap To Choose Display Order"> </form> <?php $orderBy =$_REQUEST['orderBy']; ?> <?php if (@$FORM['order'] == 'b') { $orderBy = "date"; } if (@$FORM['order'] == 'a') { $orderBy = "date DESC"; }
?>
<?php // load records from 'blog' list($blogRecords, $blogMetaData) = getRecords(array( 'tableName' => 'blog', 'loadUploads' => true, 'allowSearch' => false, 'orderBy' => $orderBy, )); ?> <table width="50%" border="0" align="center" cellpadding="10"> <tr> <td width="75%" align="left" valign="top"><table width="100%" border="0" align="left" cellpadding="2"> <tr> <td align="center"><?php $date_year = $_REQUEST['date_year']; ?> <?php $date_month = $_REQUEST['date_month']; ?> <?php $date = @$_REQUEST['date_year']."-".@$_REQUEST['date_month']."-28"; $formattedDate = date("F Y", strtotime($date)); ?> <span class="page_title_font"><?php echo $formattedDate ?> Blog Posts</span> </td> </tr> <?php foreach ($blogRecords as $record): ?> <?php $record_year = date("Y", strtotime($record['date'])) ?> <?php $record_month = date("n", strtotime($record['date'])) ?> <?php if (($date_year == $record_year) && ($date_month == $record_month) ):?> <tr> <td align="left" ><hr /> <span class="text_font_bold"><?php echo date("D, M jS, Y ", strtotime($record['date'])) ?> <?php echo strtoupper($record['title']) ?></span> <?php if($record['image'] ):?> <div class=" pad" style="float:left"> <div style="float:none"> <?php foreach ($record['image'] as $upload): ?> <a target="_blank" href="<?php echo $upload['urlPath'] ?>"> <img src="<?php echo $upload['thumbUrlPath2'] ?>" width="<?php echo $upload['thumbWidth2'] ?>" height="<?php echo $upload['thumbHeight2'] ?>" alt="" /></a> <?php endforeach ?> </div> </div> <?php endif ?> <span class="text_font"><?php echo $record['content'] ?></span></td> </tr> <?php endif ?> <?php endforeach ?> </table></td> <td width="25%" align="left" valign="top"><span class="page_title_font"> Or Select An Archive</span> <?php // get list of unique months and years with articles $query = "SELECT DATE_FORMAT(date, '%M %Y') as dateAndYear, YEAR(date) as year, MONTH(date) as month FROM cms_blog GROUP BY dateAndYear ORDER BY date"; $result = mysqli()->query($query) or die("MySQL Error: ". htmlspecialchars(mysqli()->error) . "\n"); while ($record = $result->fetch_assoc()): ?> <a href="blog3.php?date_year=<?php echo $record['year'] ?>&date_month=<?php echo $record['month'] ?>&orderBy=<?php echo $orderBy ?>"><?php echo $record['dateAndYear']; ?></a> <?php endwhile ?></td> </tr> </table>
|
The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.
Terms of Service
|