PHP - Outputting Blog Comments For Each Blog Post
hey, i need some help if possible.
I basicly have 2 tables, one has the blog posts and one has the blog comments. I'm trying to display the latest 5 blog posts (which works) but underneath each of the blog posts i need to display about 3 of the latest comments for that post but it isn't showing any. any ideas? here's my code: Code: [Select] <? session_start(); $myusername = $_SESSION['myusername']; $db_host = 'localhost'; $db_user = 'HIDDEN'; $db_pwd = 'HIDDEN'; $database = 'tjwebsol_dev'; $table = 'posts'; $table2 = 'blog_comments'; $tab = '&#38;#160;&#38;#160;&#38;#160;&#38;#160;&#38;#160;&#38;#160;&#38;#160;&#38;#160;&#38;#160;&#38;#160;&#38;#160;&#38;#160;'; if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); // sending query $result = mysql_query("SELECT * FROM {$table} ORDER BY blog_id DESC LIMIT 5"); if (!$result) { die("Query to show fields from table failed"); } $sql = mysql_query("SELECT * FROM {$table2} ORDER BY comment_id ASC LIMIT 5 "); $fields_num = mysql_num_fields($result); $fields_num2 = mysql_num_fields($sql); while($row2 = mysql_fetch_assoc($sql)) { $blog_comments = $row2['comment_content']; $date_commented = $row2['date_commented']; $time_commented = $row2['time_commented']; $blog_username2 = $row2['username']; $blogid = $row2['blogid']; $comment_id = $row2['comment_id']; } while($row = mysql_fetch_assoc($result)) { $blog_title = $row['title']; $blog_content = $row['content']; $blog_username = $row['username']; $blog_date = $row['date_posted']; $blog_time = $row['time_posted']; $blog_id = $row['blog_id']; echo "<hr>"; echo "<div class='post'>"; echo "<h2 class='title'><a href='#'>$blog_title</h2></a>"; echo "<p class='byline'>posted by <b>$blog_username</b> on <b>$blog_date</b> ($blog_time)</p>"; echo "<br>"; echo "<div class='entry'>$blog_content</div><br>"; if ($blogid == $blog_id) { echo "<b>User Posted Comments:</b><br><br>"; echo "$tab<b>$comment_id</b>. $blog_comments"; echo "<br><br>"; echo "Commented posted by <b>$blog_username2</b> on <b>$date_commented ($time_commented)</b>"; echo "<br><br><br>"; } if (!isset($_SESSION['logged']) || $_SESSION['logged'] !== true) { // not logged in, move to login page echo "<b><a href='login.php'>Login</a></b> to post your comments!"; } echo "<br><br><br>"; include('blognewcomment.php'); echo ""; echo "</div>"; } mysql_free_result($result); ?> Similar TutorialsThis topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=311235.0 This topic has been moved to Other Libraries and Frameworks. http://www.phpfreaks.com/forums/index.php?topic=347050.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=343263.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=323879.0 Hello, I made myself a simple blog system, but in my case "simple" becomes difficult when it comes to writing an article. Have you ever seen that movie about how facebook was created (I think it's called "Social network" or something like that)? As a newbie web developer I noticed how "Mark" was writting his blog post. I think it was an textarea, and when he was writing article, he wrote it with html tags, like <p>. So, I have now the same problem: when I'm writing article, I have to write it with all div's and p's and a's and img's and so on. I want to avoid that. Few ideas did pop to my head: use preg_replace for some elements, and set up some javascript WYSIWYG editor (but i really don't want it, and can't explain why, I just don't like it). I have a couple of css classes for images, both of them resizes images, one resizes it a lot, other not so much, both make them smaller. One class is for image to be on the left, so the text would wap that image from the right. After paragraph I must clear floats with css class. Other image class just takes full width available and if image is bigger that that available width - it resizes image. So no need to clear floats. How can I make my life easier when I'm writing article to my blog? Hello everyone. I'm new here and to php mvc. Been trying to hone my skills with mvc, mysql, classes and functions. But I am stuck. Can display all blog posts from DB but not a single post. The Readmore link only leads to post 1 (or the next published post from the db). Please I need your help. My code is below. Thanx in advance. BlogsModel.php (PDO db): public static function getBlog($blogsID) { try { $db = static::getDB(); $stmt = $db->query('SELECT * FROM blogs WHERE blogsStatus=1 AND blogsID=blogsID '); $result = $stmt->fetch(PDO::FETCH_ASSOC); return $result; } catch (PDOException $e) { echo $e->getMessage(); } } Blogs.php (Controller): public function blogAction() { $blog = BlogsModel::getBlog($blogsID = 'blogsID'); $user = BlogsModel::getBlog($blogsUserID = 'blogsUserID'); $data = [ 'blog' => $blog, 'user' => $user ]; View::renderTemplate("Services/Blogs/blog.php", $data); } index.php (front controller): $router->add('blogs/{id:[\w-]+}', ['controller' => 'Services\Blogs', 'action' => 'show']); ReadMore link: <a id="readMore" href="/blogs/{{ blog.blogsAlias }}" >ReadMore</a>
Hello! I am a 17 year old beginner Php student and I am currently creating a page for which I would like to create a blog management admin interface. I went very well, I can already add the categories to the database, through the admin interface or I can list them on the website. However, there would be a problem with creating a Blog post. Because I use the same create () function as for adding a category, but unfortunately the Blog Post does not add to the database. I don't get any php error code just when I try to list the post id after the create function. Instead of a value of 1, I get a 0. I enclose the code of the blog post and its control, as well as the data of my database!
Blog Posting form admin html and php code (ujblogposzt.php) ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<?php require_once('templates/head.php') ?> <!-- includes database and user controll -->
<?php include('app/controllers/posztok.php');?> <!-- Add new blog post controll file -->
<!-- Main content -->
<div class="card-footer">
</div>
<!-- bs-custom-file-input --> <?php require_once('templates/footer.php') ?>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Blogpost controll code (posztok.php)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<?php
if (isset($_POST['add-post'])) {
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- I see everything okay in these codes, because I add the category to the database in the same way when adding a category my function called dd is at the end of the line of code, it's just for checking, it shows results. my create() function seen like this:
function create($table, $data)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- The strange thing is that if I add 1-2 items to the database statically without php code, it is detected and displayed in the blog editing menu, but I can't add a post to the database via the admin interface itself, that's why I wrote here ! :) From testing, I also print the end result of the create function at the end of the control code, which should return an id, but instead of id 1 I get a value of 0 and the post doesn't added to the database after pressing the post button['add-post'] and unfortunately I can't figure out why it is ?! I also attach the pictures of the problem!
The pictures of my problem can be viewed he https://drive.google.com/drive/folders/1WTVNv4LlbJiosWvBZoIRzjiUDkvhMgYk?usp=sharing
Thanks to anyone in advance !!!
Hi, I am trying to get a facebook like button displayed under each of my blog post, but so far I am just getting three like buttons displayed above the blog post instead of below each one. Below is the function code to display my blog post and also where I cam currently placing the FB like button code. <?php include 'blogpost.php'; $connection = mysql_connect('localhost', 'username', 'pw') or die ("<p class='error'>Sorry, we were unable to connect to the database server.</p>"); $database = "DBName"; mysql_select_db($database, $connection) or die ("<p class='error'>Sorry, we were unable to connect to the database.</p>"); function GetBlogPosts($inId=null, $inTagId =null) { if (!empty($inId)) { $query = mysql_query("SELECT * FROM blog_posts WHERE id = " . $inId . " ORDER BY id DESC LIMIT 2"); } else if (!empty($inTagId)) { $query = mysql_query("SELECT blog_posts.* FROM blog_post_tags LEFT JOIN (blog_posts) ON (blog_post_tags.postID = blog_posts.id) WHERE blog_post_tags.tagID =" . $tagID . " ORDER BY blog_posts.id DESC"); } else { $query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC"); } $postArray = array(); while ($row = mysql_fetch_assoc($query)) { $myPost = new BlogPost($row["id"], $row['title'], $row['post'], $row['postfull'], $row["author_id"], $row['date_posted']); array_push($postArray, $myPost); echo '<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like show_faces="true" width="450"></fb:like>';//this is where I am currently implementing the FB Like button } return $postArray; } ?> I have a problem about Blog Post Categorization Script. Can anyone review it ? Instead of showing how much posts that category have it just repeats. Code: [Select] <? $sqlCategories = "SELECT * FROM blog_entry, categories where blog_entry.CategoryID=categories.CategoryID and blog_entry.UserID=".$_REQUEST['UserID']." order by Category asc"; $resultCategories = mysql_query($sqlCategories, $conn); if (@mysql_num_rows($resultCategories)!=0){ $strcategory=""; while($row_categories = @mysql_fetch_array($resultCategories)) { $categoryduplicate=0; $arrcat = explode(",",$strcategory); for($i=0;$i<strlen($arrcat);$i++) { if($arrcat[$i]==$row_categories['CategoryID']) { $categoryduplicate=1; } } if($categoryduplicate==0) { if($strcategory=="") { $strcategory=$row_categories['CategoryID']; } else { $strcategory=$strcategory.",".$row_categories['CategoryID']; } $sqlCategories2 = "SELECT * FROM blog_entry where CategoryID=".$row_categories['CategoryID']." and UserID=".$_REQUEST['UserID']; $resultCategories2 = mysql_query($sqlCategories2, $conn); if($row_categories['CategoryID'] == $_REQUEST['CategoryID']) { echo " <Tr> <td class='content'><a href='postss.php?CategoryID=".$row_categories['CategoryID']."&UserID=".$_REQUEST['UserID']."&Category=".$row_categories['Category']."' class='in_sel'>".$row_categories['Category']."</a> [".@mysql_num_rows($resultCategories2)."]</td> </Tr> "; } else { echo " <Tr> <td class='content'><a href='posts.php?CategoryID=".$row_categories['CategoryID']."&UserID=".$_REQUEST['UserID']."&Category=".$row_categories['Category']."' class='in_sel'>".$row_categories['Category']."</a> [".@mysql_num_rows($resultCategories2)."]</td> </Tr> "; } } } } ?> Hi, I have a blog page I am working on and I am trying to make some enhancements that I need some help with. In MySQL I have a table with id, title and post. In my page display I want to enable the user to click on the title and display a post on it's own so the URL would look like www.mysite.com/blog/?id=1 or something like that. Here is the PHP function to display the post from MySQL. all on one page. function GetBlogPosts($inId=null, $inTagId =null) { if (!empty($inId)) { $query = mysql_query("SELECT * FROM blog_posts WHERE id = " . $inId . " ORDER BY id DESC LIMIT 2"); } else if (!empty($inTagId)) { $query = mysql_query("SELECT blog_posts.* FROM blog_post_tags LEFT JOIN (blog_posts) ON (blog_post_tags.postID = blog_posts.id) WHERE blog_post_tags.tagID =" . $tagID . " ORDER BY blog_posts.id DESC"); } else { $query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC"); } $postArray = array(); while ($row = mysql_fetch_assoc($query)) { $myPost = new BlogPost($row["id"], $row['title'], $row['post'], $row['postfull'], $row["author_id"], $row['date_posted']); array_push($postArray, $myPost); } return $postArray; } Hi all - I'm setting up a custom PHP blog. It pulls the data from a MySQL database which includes HTML tags (<p><div><span> etc...). I would like to display only up to 50 words per post on the blog page, which users can then read and click a link to then see the entire post. I've developed some code which does this, however, it seems to be stripping my HTML tags... Very sad! Would be very very grateful if one (or more) of you kind lot would have a look at my code and let me know if there is a easier (I'm all for easy) and proper way of implementing this so that it works without stripping my HTML tags. Cheers!!! Code: [Select] // Counts number of blog words in the content $blog_content_words = str_word_count($blog["content"], 1); // Sets the blog_words variable to 0 $blog_words = 0; // Prints title on page as a permalink to a post echo '<h1><a href="blog.php?id='.$blog["id"].'">'.$blog["title"].'</a></h1><p>'; // Loops while blog_word is under 50 while($blog_words < 50) { // Prints a word from blog_content_words array of blog post and adds a space afterwards echo $blog_content_words["$blog_words"]." "; // Adds 1 to the blog_words counter ++$blog_words; } // Adds a read more link to the post which links to full blog post echo '... <a href="blog.php?id='.$blog["id"].'">[read more]</a></p>'; I had a wordpress site setup for my maintenance company. I am not trying to setup a blog on the site. I have the blogroll showing up on this page: http://handymore.com/blog/ but when you click the individual articles the single post page shows a Error 404 message. Could anyone point me in the right direction as to how to fix this so that the individual post pages are found? Any guidance would be appreciated. Hello. I'm new to pHp and I would like to know how to get my $date_posted to read as March 12, 2012, instead of 2012-12-03. Here is the code: Code: [Select] <?php $sql = " SELECT id, title, date_posted, summary FROM blog_posts ORDER BY date_posted ASC LIMIT 10 "; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) { $id = $row['id']; $title = $row['title']; $date_posted = $row['date_posted']; $summary = $row['summary']; echo "<h3>$title</h3>\n"; echo "<p>$date_posted</p>\n"; echo "<p>$summary</p>\n"; echo "<p><a href=\"post.php?id=$id\" title=\"Read More\">Read More...</a></p>\n"; } ?> I have tried the date() function but it always updates with the current time & date so I'm a little confused on how I get this to work. So I started a blog project just to help me out with learning php. This is my post form <form action="insert.php" method="post"> Title: <input type="text" name="title"> <br> Post: <input type="text" name="post"> <br> Author: <input type="text" name="author"> <br> <input type="submit"> </form> Insert.php <?php $con = mysqli_connect("localhost","test","","test"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // escape variables for security $title = mysqli_real_escape_string($con, $_POST['title']); $content = mysqli_real_escape_string($con, $_POST['post']); $author = mysqli_real_escape_string($con, $_POST['author']); $sql="INSERT INTO article (title, content, author) VALUES ('$title', '$content', '$author')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } echo "1 record added"; mysqli_close($con); ?> But when I try to display that information he <h1>Title: </h1> <?php echo $title; ?> <h2>Content: </h1> <?php echo $content; ?> <h3>Posted by: </h1> <?php echo $author; ?>It doesn't work and I get this: ( ! ) Notice: Undefined variable: title in C:\wamp\www\test\index.php on line 33 Call Stack # Time Memory Function Location 1 0.0000 239144 {main}( ) ..\index.php:0 Content: ( ! ) Notice: Undefined variable: content in C:\wamp\www\test\index.php on line 34 Call Stack # Time Memory Function Location 1 0.0000 239144 {main}( ) ..\index.php:0 Posted by: ( ! ) Notice: Undefined variable: author in C:\wamp\www\test\index.php on line 35 Call Stack # Time Memory Function Location 1 0.0000 239144 {main}( ) ..\index.php:0The form works because when I looked at the database, the information was there. The problem is getting that information and displaying it in the right place, how can i fix that? Just in case, this is my index: <?php include ('connect.php'); include ('header.php'); ?> <div id="container"> <div id="rightcol"> <form action="insert.php" method="post"> Title: <input type="text" name="title"> <br> Post: <input type="text" name="post"> <br> Author: <input type="text" name="author"> <br> <input type="submit"> </form> </div> <div id="content"> <h1>Title: </h1> <?php echo $title; ?> <h2>Content: </h1> <?php echo $content; ?> <h3>Posted by: </h1> <?php echo $author; ?> </div> </div> <?php include "footer.php"; ?> </div> I have looked around. And what I came across was not necessarily what I was looking for.
I was wondering if somebody can recommend a good tutorial for building a blog in PHP OOP?
Also, I was wondering if somebody perhaps knows a good blog app written in PHP OOP, where I could be learning from the script files?
Hey I have this blog http://www.nikita-andrews.com/ingrid/?page_id=34 ... Im trying to remove the prettyphoto enlargement on posts to this page ... and not sure how to go about it. The code for thumbnail.php is Code: [Select] <?php if (has_post_thumbnail()) { $data = get_post_meta( $post->ID, 'key', true ); if ($data[ 'project_link' ]) { ?> <a class="externalLink tooltip" title="View Live Site" target="_blank" href="<?php echo $data[ 'project_link' ]; ?>">∞</a> <?php } ?> <?php $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large'); if ($data[ 'video_link' ]) { ?> <a class="thumbLink video" rel="prettyPhoto" href="<?php echo $data[ 'video_link' ]; ?>"> <?php } else { echo '<a class="thumbLink" href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >'; } the_post_thumbnail(); echo '</a>'; } ?> I want the thumbnails to still show but just not enlarge. Infact itd be even better if they just linked directly to the post itself. Any help would be much appreciated! I am building a blog server with dreamweaver and phpMyAdmin so I have an wpindex.php page showing the topics and a topic page, wptopic.php showing only that topic. I need to create a link from the wpindex.php to wptopic.php. The link is: Code: [Select] wptopic.php?ID=<?php echo $row_Recordset1['ID']; ?> *Recordset1 is a query of the blog post table* However, when I tested it, all post's link are 'wptopic.php?ID=1'. How can I fix it? Also, in the wptopic.php page, there is a place to show comments. However, when there is no comment, there is still a 'says' word in the page. how can I make the 'says:' disappear when there is no comments? The Code: Code: [Select] <p>Comment:</p> <p align="right"><?php echo $row_Recordset2['comment_author']; ?>says:</p> <p align="right"><?php echo $row_Recordset2['comment_content']; ?>*Recordset2 is a query of the blog reply table* Thks! hello, i have been trying to find scripts or write a custom php script so i can have a feedback/comment type blog on my website. im trying to go for the same type of design layout and function as the blog on this site: http://www.jessicaswift.com/treasuring/ I have found a few scripts but they are on sites that want me to pay for the scripts and coding, but im not sure if its legit or even what im looking for cause they dont have full demos of them. if anyone could help me out by pointing me in the right direction with a website or tutorial for what im trying to do. Im not sure if javascript would be better for what im trying to do. cause i dont want to have a full out blog like from wordpress or blogger.com my site im building for my sister is www.jessicadurrant.com if you go to the blog page you will see its a wordpress blog, which i dont want to use. i would like to set up something like the blog that is on the first website link i posted about. Again any help would be greatly appreciated. Hello Im trying to figure out how to use flat textfiles to my blogproject but cant get the grip on it. It has to be filebased. I can't find any descent tutorial on the web either and it starts really getting on my nerves... 1. How do i save new blogposts in a textfile so they can be easily read? Should i use only one file with append and write all blogposts to it one after another? or save a new textfile for each new blogpost/entry? If so, how do i name them so they don't write over each other accidentally. I need to save (date+writer+title+blogpost). 2. How do i read the files so i can sort them by date/time , latest entry up/oldest entry down? they should appear on my blog startside in the middle one after another. Should i read each blogpost into an array so i can sort them or what? If so how do i do that? 3. I need a list of links connected to each blogpost so when you click link the correct blogpost is shown in the middle of the page same side as the links. the links should be sorted so all posts from oktober is shown under "oktober" and so on... I have ripped half my hair off by now so any general advice how i should do would be appreciated... Hi all, I am trying to implement a search of a blog, using Object Oriented PHP, a concept which seems a little alien to me. I have had an attempt at it but it doesn't seem to work, but am not really sure why. I think that i have told the search on post to refer to the command BlogSearch and then within that command look for the titles of the blogs that are the same as the search and display them. Any help greatly appreciated. <?php /*views/index.php*/ require_once("ViewHelper.php"); ViewHelper::DisplayHeader("Blog index");?> <h1>Blog index</h1> <p>You can <a href="index.php?cmd=BlogAddForm">add messages</a> !</p> <form action="index.php?cmd=BlogSearch" method="post"> Search: <input name="search"> <input type="submit" value="Search!"> </form> <? foreach($data as $message) echo("<div class=\"message\"> <h2>{$message['title']}</h2> <p>{$message['message']}</p> <a href=\"index.php?cmd=BlogDelete&id={$message['id']}\">Delete this message</a> </div>"); ?> <?ViewHelper::DisplayFooter();?> <?php require_once("ViewHelper.php"); ViewHelper::DisplayHeader("Blog index");?> <? foreach($title as $search) echo("<div class=\"message\"> <h2>{$message['title']}</h2> <p>{$message['message']}</p> </div>"); ?> <?ViewHelper::DisplayFooter();?> ?> |