Make a Blog System for specific user

Post here is you are having problems with any of the tutorials.
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Make a Blog System for specific user

Post by jaysus7 »

hey sorry i wrote a new topic on a the other one....so here it is....

i have followed the blog tutorial through and through and it works great!!! thank you!!! however i need it so that when i log in as jaysus lets say, i want it so that when i write a blog post, and preview it on my user page, that it only shows blog posts that jaysus has posted and not everybody on the site...????

where do i start with coding to make this happen....??????
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Make a Blog System for specific user

Post by jacek »

Add a column to the posts table for the users id. When they make a post store the ID in this column to get only there posts use a WHERE to limit the results to only the ones with their ID in the column.
Image
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Make a Blog System for specific user

Post by jaysus7 »

thanks jacek this makes sense to do. now i have tried various options that i could think would work that makes sense to me....
have a look and help guide me in the right direction please!! :D ;)

Blog List
<?php

include ("coreblog/initblog.php");
include ("core/init.inc.php");

?><?php

$posts = get_posts();

foreach($posts as $post){
		?>
    <h2><a href="blog_read.php?pid=<?php echo $post['id']; ?>"> <?php echo $post['title']; ?></a></h2>
    <h4>By <?php echo $post['user']; ?> on <?php echo $post['date']; ?></h4>
    <h4>(<?php echo $post['total_comment'];?>comments, last comment <?php echo $post['last_comment']; ?>)</h4>
    
    <hr />
    
    <p><?php $post['preview']; ?></p>
    
    <?php
}

?>
Blog_posts
<?php

include ("core/init.inc.php");
include ("coreblog/initblog.php");

if (isset($_POST['id'], $_POST['user'], $_POST['title'], $_POST['body'])){
	add_post($_POST['id'], $_POST['user'], $_POST['title'], $_POST['body']);
	header('Location: blog_list.php');
	die();	
}

?><form action="" method="post">
<p>
	<label for="user">Name</label>
    <input type="text" name="user" id="user" value="<?php echo $_SESSION['username']; ?>" />
</p>
<p>
	<label for="user">Title </label>
    <input type="text" name="title" id="title" />
</p>
<p>
  <textarea name="body" id="body" rows="20" cols="60"></textarea>
</p>
<p>
	<input type="submit" value="Add Research Post" />
</p>
</form>
posts.inc
<?php

//checks if the given post id is in the table
function valid_pid($pid){
	$pid = (int)$pid;
	
	$total = mysql_query("SELECT COUNT(`post_id`) FROM `posts` WHERE `post_id` = {$pid}");
	$total = mysql_result($total, 0);

	if ($total != 1){
		return false;
	}else{
		return true;
	}
}

//festches a summery of all the blog posts
function get_posts(){
	$sql = "SELECT
				`posts`.`post_id` AS `id`,
				`posts`.`post_title` AS `title`,
				LEFT(`posts`.`post_body`, 512) AS `preview`,
				`posts`.`post_user` AS `user`,
				DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i:%s') AS `date`,
				`comments`.`total_comments`,
				DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
				WHERE `user_id` = `id`,
			FROM `posts`
			LEFT JOIN (
				 SELECT
				  `post_id`,
					COUNT(`comment_id`) AS `total_comments`,
					MAX(`comment_date`) AS `last_comment`
				FROM `comments`
				GROUP BY `post_id`
			) AS `comments`
			ON `posts`.`post_id` = `comments`.`post_id`
			ORDER BY `posts`.`post_date` DESC";
	
	$posts = mysql_query($sql);
	
	$rows = array();
	while (($row = mysql_fetch_assoc($posts)) !== false){
		
		$rows[] = array(
			 'title'				=> $row['title'],
			 'preview'				=> $row['preview'],
			 'user'					=> $row['user'],
			 'date'					=> $row['date'],
			 'total_comments'		=> ($row['total_comments'] === null) ? 0 : $row['total_comments'],
			 'last_comment'			=> ($row['last_comment'] === null) ? 'never' : $row['last_comment']
		);
		
	}
	return $rows;
}

//fetches a single post from the table
function get_post($pid){
	$pid = (int)$pid;
	
	$sql = "SELECT
			`post_title` AS `title`,
			`post_body` AS `body`,
			`post_user` AS `user`,
			`post_date` AS `date`
		FROM `posts`
		WHERE `post_id` = {$pid}";
		
	$post = mysql_query($sql);echo mysql_error();
	$post = mysql_fetch_assoc($post);
	
	$post['comments'] = get_comments($pid);
	
	
	return $post;
}

//adds new blog entry
function add_post($uid, $name, $title, $body) {
	$id 	= mysql_real_escape_string(htmlentities($id));
	$name 	= mysql_real_escape_string(htmlentities($name));
	$title 	= mysql_real_escape_string(htmlentities($title));
	$body 	= mysql_real_escape_string(nl2br(htmlentities($body)));
	
	mysql_query("INSERT INTO `posts` (`user_id`, `post_user`, `post_title`, `post_body`, `post_date`) VALUES ('{$id}',{$name}', '{$title}', '{$body}', NOW())");

}

//searches posts
function search_posts($term){
	$keywords = preg_split('#\s+#', mysql_real_escape_string($term));
	
	$title_where = "`post_title` LIKE '%" . implode("%' OR `post_title` LIKE '%", $keywords) . "%'";
	$body_where	 = "`post_title` LIKE '%" . implode("%' OR `post_body` LIKE '%", $keywords) . "%'";
	
	$sql = "SELECT 
				`post_title` AS `title`,
				LEFT (`post_body`, 100) AS `body`
				FROM `posts`
				WHERE {$title_where}
				OR {$body_where}";
				
	$result = mysql_query($sql);
				
	$results = array();
				
	while (($row = mysql_fetch_assoc($result)) !== false){
		$results[] = $row;
		
	}
	return $results;
				
}

?>
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Make a Blog System for specific user

Post by jacek »

In the add_user() function you use the parameter $uid but then try to use the variable $id. Also you don't want to pass the user id via the form like that, people will just be able to change it and post as another user. You can just use the session variable directly in the function call.

Other than that it looks like you have more or less got the right idea, what is the problem you are having ? :?
Image
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Make a Blog System for specific user

Post by jaysus7 »

just guidance on making sure where i put the WHERE statements and such are correct.... also if i look at let say billys account i want to only view his blogs on his page??? will i be able to so??
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Make a Blog System for specific user

Post by jacek »

This is the SQL you are using to fetch all of the posts
                        SELECT
                                `posts`.`post_id` AS `id`,
                                `posts`.`post_title` AS `title`,
                                LEFT(`posts`.`post_body`, 512) AS `preview`,
                                `posts`.`post_user` AS `user`,
                                DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i:%s') AS `date`,
                                `comments`.`total_comments`,
                                DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
                                WHERE `user_id` = `id`,
                        FROM `posts`
                        LEFT JOIN (
                                 SELECT
                                  `post_id`,
                                        COUNT(`comment_id`) AS `total_comments`,
                                        MAX(`comment_date`) AS `last_comment`
                                FROM `comments`
                                GROUP BY `post_id`
                        ) AS `comments`
                        ON `posts`.`post_id` = `comments`.`post_id`
                        ORDER BY `posts`.`post_date` DESC
so you can add a WHERE to this to have it only fetch the posts with a specific user.
Image
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Make a Blog System for specific user

Post by jaysus7 »

<?php

SELECT
                                `posts`.`post_id` AS `id`,
                                `posts`.`post_title` AS `title`,
                                LEFT(`posts`.`post_body`, 512) AS `preview`,
                                `posts`.`post_user` AS `user`,
                                DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i:%s') AS `date`,
                                `comments`.`total_comments`,
                                DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
                               ****WHERE `user_id` = `title`,*****
                        FROM `posts`
                        LEFT JOIN (
                                 SELECT
                                  `post_id`,
                                        COUNT(`comment_id`) AS `total_comments`,
                                        MAX(`comment_date`) AS `last_comment`
                                FROM `comments`
                                GROUP BY `post_id`
                        ) AS `comments`
                        ON `posts`.`post_id` = `comments`.`post_id`
                        ORDER BY `posts`.`post_date` DESC ?>
the part with all the stars isn't in the code but its to point out what i have aded.my theory behind it was that if i add user id to be title then it would fetch only those titles to that user
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Make a Blog System for specific user

Post by jacek »

Well you need the WHERE to be at the end, so maybe something like
                            SELECT
                                    `posts`.`post_id` AS `id`,
                                    `posts`.`post_title` AS `title`,
                                    LEFT(`posts`.`post_body`, 512) AS `preview`,
                                    `posts`.`post_user` AS `user`,
                                    DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i:%s') AS `date`,
                                    `comments`.`total_comments`,
                                    DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
                            FROM `posts`
                            LEFT JOIN (
                                     SELECT
                                      `post_id`,
                                            COUNT(`comment_id`) AS `total_comments`,
                                            MAX(`comment_date`) AS `last_comment`
                                    FROM `comments`
                                    GROUP BY `post_id`
                            ) AS `comments`
                            ON `posts`.`post_id` = `comments`.`post_id`
                            WHERE `posts`.`user_id` = 7
                            ORDER BY `posts`.`post_date` DESC
you'd probably want to replace 7 here with a variable containing the id of the current user.
Image
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Make a Blog System for specific user

Post by jaysus7 »

invalid posts show up on the blog read, i don't know if i did the fetch posts right, and the user id isn't uploading to the database table, i tried different ways but it always switched information corresponding to the table.

Posts.inc
<?php

//checks if the given post id is in the table
function valid_pid($pid){
        $pid = (int)$pid;
       
        $total = mysql_query("SELECT COUNT(`post_id`) FROM `posts` WHERE `post_id` = {$pid}");
        $total = mysql_result($total, 0);
 
        if ($total != 1){
                return false;
        }else{
                return true;
        }
}
 
//festches a summery of all the blog posts
function get_posts(){
        $sql = "SELECT
                                    `posts`.`post_id` AS `id`,
                                    `posts`.`post_title` AS `title`,
                                    LEFT(`posts`.`post_body`, 512) AS `preview`,
                                    `posts`.`post_user` AS `user`,
                                    DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i:%s') AS `date`,
                                    `comments`.`total_comments`,
                                    DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
                            FROM `posts`
                            LEFT JOIN (
                                     SELECT
                                      `post_id`,
                                            COUNT(`comment_id`) AS `total_comments`,
                                            MAX(`comment_date`) AS `last_comment`
                                    FROM `comments`
                                    GROUP BY `post_id`
                            ) AS `comments`
                            ON `posts`.`post_id` = `comments`.`post_id`
                             WHERE `posts`.`user_id` = {$uid}
                            ORDER BY `posts`.`post_date` DESC";
       
        $posts = mysql_query($sql);
			echo mysql_error();
       
        $rows = array();
        while (($row = mysql_fetch_assoc($posts)) !== false){
               
                $rows[] = array(
                         'title'                        => $row['title'],
                         'preview'                      => $row['preview'],
                         'user'                         => $row['user'],
                         'date'                         => $row['date'],
                         'total_comments'               => ($row['total_comments'] === null) ? 0 : $row['total_comments'],
                         'last_comment'                 => ($row['last_comment'] === null) ? 'never' : $row['last_comment']
                );
               
        }
        return $rows;
}
 
//fetches a single post from the table
function get_post($pid){
        $pid = (int)$pid;
       
        $sql = "SELECT
                        `post_title` AS `title`,
                        `post_body` AS `body`,
                        `post_user` AS `user`,
                        `post_date` AS `date`
                FROM `posts`
                WHERE `post_id` = {$pid}";
               
        $post = mysql_query($sql);echo mysql_error();
        $post = mysql_fetch_assoc($post);
       
        $post['comments'] = get_comments($pid);
       
        return $post;
}
 
//adds new blog entry
function add_post($title, $body, $name){
        $title  = mysql_real_escape_string(htmlentities($title));
        $body   = mysql_real_escape_string(nl2br(htmlentities($body)));
        $name   = mysql_real_escape_string(htmlentities($name));
       
        mysql_query("INSERT INTO `posts` (`post_title`, `post_body`,  `post_user`, `post_date`) VALUES ('{$title}', '{$body}', '{$name}', NOW())");
 
			
}
 
//searches posts
function search_posts($term){
        $keywords = preg_split('#\s+#', mysql_real_escape_string($term));
       
        $title_where = "`post_title` LIKE '%" . implode("%' OR `post_title` LIKE '%", $keywords) . "%'";
        $body_where      = "`post_title` LIKE '%" . implode("%' OR `post_body` LIKE '%", $keywords) . "%'";
       
        $sql = "SELECT
                                `post_title` AS `title`,
                                LEFT (`post_body`, 100) AS `body`
                                FROM `posts`
                                WHERE {$title_where}
                                OR {$body_where}";
                               
        $result = mysql_query($sql);
                               
        $results = array();
                               
        while (($row = mysql_fetch_assoc($result)) !== false){
                $results[] = $row;
               
        }
        return $results;
                               
}
 
?>
Blog Lists
<?php

include ("coreblog/initblog.php");
include ("core/init.inc.php");

?>

<?php

$posts = get_posts();

foreach($posts as $post){
		?>
<h2><a href="blog_read.php?pid=<?php echo $post['id']; ?>"> <?php echo $post['title']; ?></a></h2>
    <h4>By <?php echo $post['user']; ?> on <?php echo $post['date']; ?></h4>
    <h4>(<?php echo $post['total_comment'];?>comments, last comment <?php echo $post['last_comment']; ?>)</h4>
    
    <hr />
    
    <p><?php $post['preview']; ?></p>
    
    <?php
}

?>

blog posts
<?php

include ("core/init.inc.php");
include ("coreblog/initblog.php");

if (isset($_POST['title'], $_POST['body'], $_POST['name'])){
	add_post($_POST['title'], $_POST['body'], $_POST['name']);
	header('Location: blog_list.php');
	die();	
}

?>
<p>
	<label for="name">Name</label>
    <input type="text" name="name" id="name" value="<?php echo $_SESSION['username']; ?>" />
</p>
<p>
	<label for="title">Title </label>
    <input type="text" name="title" id="title" />
</p>
<p>
  <textarea name="body" id="body" rows="20" cols="60"></textarea>
</p>
<p>
	<input type="submit" value="Add Research Post" />
</p>
</form>
blog read
<?php
include ("coreblog/initblog.php");
include ("core/init.inc.php");

if (isset($_GET['pid'], $_POST['user'], $_POST['body'])){
	if (add_comment($_GET['pid'], $_POST['user'], $_POST['body'])){
		header("Location: blog_read.php?pid={$_GET['pid']}");
	}else{
		header('Location: blog_list.php');
	}
	die();
}

?> 

  <?php

if (isset($_GET['pid']) === false || valid_pid($_GET['pid']) === false){
	echo 'Invalid post ID.';
}else{
	$post = get_post($_GET['pid'])

	?>
<h2><a href="blog_list.php?pid=<?php echo $post['id']; ?>"><?php echo $post['title']; ?></a></h2>
    <h3>By: <?php echo $post['user']; ?> on: <?php echo $post['date']; ?> (<?php echo count($post['comments']); ?> comments)</h3>
    
    <hr />
    
    <p><?php echo $post['body']; ?></p>
    
    <hr />
    <?php
	
	foreach ($post['comments'] as $comment){
		?>
    
	 <h3>By: <?php echo $comment['body']; ?> on <?php echo $comment['date']; ?></h3>
     <p><?php echo $comment['user']; ?></p>
     <hr />
     <?php
	}

	?>
<form action="" method="post">
<p>
	<label for="user">Name</label>
    <input type="text" name="user" id="user" value="<?php echo $_SESSION['username']; ?>" />
</p>
<p>
	<textarea name="body" rows="20" cols="60"></textarea>
</p>
<p>
	<input type="submit" value="Add Comment" />
</p>
</form>    
<?php

}

?>
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Make a Blog System for specific user

Post by jacek »

You used the $uid variable in the get_posts() function but that variable is not defined within the function so it wont have a value.

You should really set error_reporting to E_ALL which would highlight these errors.
Image
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Make a Blog System for specific user

Post by jaysus7 »

ok i got it to add the current logged in user into the table so that each post is different depending on who posted it, however i can't and don't understand how to get the user id post to show on their member page....
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Make a Blog System for specific user

Post by jacek »

jaysus7 wrote:i can't and don't understand how to get the user id post to show on their member page....
By adding the WHERE to the SELECT query :? Which part don't you understand exactly ?
Image
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Make a Blog System for specific user

Post by jaysus7 »

lol i can here the sarcasm in your wording there lol :lol: :lol: :lol: ummm i got to add the user id when submitting a post, however i don't know what to put after the WHERE `posts`.`user_id` = ??? to actually retrieve it I've tried "uid" "0" "user" "username" ???
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Make a Blog System for specific user

Post by jacek »

Oh right,
        $sql = "SELECT
                                   `posts`.`post_id` AS `id`,
                                   `posts`.`post_title` AS `title`,
                                   LEFT(`posts`.`post_body`, 512) AS `preview`,
                                   `posts`.`post_user` AS `user`,
                                   DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i:%s') AS `date`,
                                   `comments`.`total_comments`,
                                   DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
                           FROM `posts`
                           LEFT JOIN (
                                    SELECT
                                     `post_id`,
                                           COUNT(`comment_id`) AS `total_comments`,
                                           MAX(`comment_date`) AS `last_comment`
                                   FROM `comments`
                                   GROUP BY `post_id`
                           ) AS `comments`
                           ON `posts`.`post_id` = `comments`.`post_id`
                            WHERE `posts`.`user_id` = {$uid}
                           ORDER BY `posts`.`post_date` DESC";
In this SQL you need to replace $uid with something that actually contains the user id. So you could use the session if you stored the Id there ?
        $sql = "SELECT
                                   `posts`.`post_id` AS `id`,
                                   `posts`.`post_title` AS `title`,
                                   LEFT(`posts`.`post_body`, 512) AS `preview`,
                                   `posts`.`post_user` AS `user`,
                                   DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i:%s') AS `date`,
                                   `comments`.`total_comments`,
                                   DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
                           FROM `posts`
                           LEFT JOIN (
                                    SELECT
                                     `post_id`,
                                           COUNT(`comment_id`) AS `total_comments`,
                                           MAX(`comment_date`) AS `last_comment`
                                   FROM `comments`
                                   GROUP BY `post_id`
                           ) AS `comments`
                           ON `posts`.`post_id` = `comments`.`post_id`
                            WHERE `posts`.`user_id` = {$_SESSION['uid']}
                           ORDER BY `posts`.`post_date` DESC";
Or the username if you wan to do it by that
        $sql = "SELECT
                                   `posts`.`post_id` AS `id`,
                                   `posts`.`post_title` AS `title`,
                                   LEFT(`posts`.`post_body`, 512) AS `preview`,
                                   `posts`.`post_user` AS `user`,
                                   DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i:%s') AS `date`,
                                   `comments`.`total_comments`,
                                   DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
                           FROM `posts`
                           LEFT JOIN (
                                    SELECT
                                     `post_id`,
                                           COUNT(`comment_id`) AS `total_comments`,
                                           MAX(`comment_date`) AS `last_comment`
                                   FROM `comments`
                                   GROUP BY `post_id`
                           ) AS `comments`
                           ON `posts`.`post_id` = `comments`.`post_id`
                            WHERE `posts`.`user_name` = {$_SESSION['username']}
                           ORDER BY `posts`.`post_date` DESC";
It might be more useful to have the username passed in as an argument to the function too.

Make sense ?
Image
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Make a Blog System for specific user

Post by jaysus7 »

Excellent your brilliant mate!!!! worked like a charm....i used session kid before but the fetch current kid was working properly so when i did it i didn't work lol makes you god of php thanks a ton
Just a helpless cause!!!!
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Make a Blog System for specific user

Post by jaysus7 »

I'm getting an invalid id now in blog read ...just checked and its good so do i need to add the add user to the get post function??

also is there a way to put pictures within your blog post??

commenting
<?php

//fetches all the comments for a given blog post
function get_comments($pid){
	$pid = (int)$pid;
	
	$sql = "SELECT
				`comment_body` AS `body`,
				`comment_user` AS `user`,
				DATE_FORMAT(`comment_date`, '%d/%m/%Y %H:%i:%s') AS `date`
			FROM `comments`
			WHERE `post_id` = {$pid}";
	
	$comments = mysql_query($sql);
	echo mysql_error();
	$return = array();
	while (($row = mysql_fetch_assoc($comments)) !== false){
		$return[] = $row;
	}
		return $return;
}

//add a comment
function add_comment($pid, $user, $body){
	if (valid_pid($pid) === false){
		return false;
	}
	
	$pid	= (int)$pid;
	$user	= mysql_real_escape_string(htmlentities($user));
	$body	= mysql_real_escape_string(nl2br(htmlentities($body)));
	
	mysql_query("INSERT INTO `comments` (`post_id`, `comment_body`, `comment_user`, `comment_date`) VALUES ({$pid}, '{$user}', '{$body}', NOW())");
echo mysql_error();
	return true;
	
}

?>
psts.inc
<?php
error_reporting(E_ALL);
//checks if the given post id is in the table
function valid_pid($pid){
        $pid = (int)$pid;
       
        $total = mysql_query("SELECT COUNT(`post_id`) FROM `posts` WHERE `post_id` = {$pid}");
		echo mysql_error();
        $total = mysql_result($total, 0);
 
        if ($total != 1){
            return false;
        }else{
            return true;
        }
}
 
//festches a summery of all the blog posts
function get_posts(){
        $sql = "SELECT
                                    `posts`.`post_id` AS `id`,
                                    `posts`.`post_title` AS `title`,
                                    LEFT(`posts`.`post_body`, 512) AS `preview`,
                                    `posts`.`post_user` AS `user`,
                                    DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i:%s') AS `date`,
                                    `comments`.`total_comments`,
                                    DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
                            FROM `posts`
                            LEFT JOIN (
                                     SELECT
                                      `post_id`,
                                            COUNT(`comment_id`) AS `total_comments`,
                                            MAX(`comment_date`) AS `last_comment`
                                    FROM `comments`
                                    GROUP BY `post_id`
                            ) AS `comments`
                            ON `posts`.`post_id` = `comments`.`post_id`
							WHERE `posts`.`user_id` = {$_SESSION['uid']}
						    ORDER BY `posts`.`post_date` DESC";
       
        $posts = mysql_query($sql);
			echo mysql_error();
       
        $rows = array();
        while (($row = mysql_fetch_assoc($posts)) !== false){
               
                $rows[] = array(
						 'title'                        => $row['title'],
                         'preview'                      => $row['preview'],
                         'user'                         => $row['user'],
                         'date'                         => $row['date'],
                         'total_comments'               => ($row['total_comments'] === null) ? 0 : $row['total_comments'],
                         'last_comment'                 => ($row['last_comment'] === null) ? 'never' : $row['last_comment']
                );
               
        }
        return $rows;
}
 
//fetches a single post from the table
function get_post($pid){
        $pid = (int)$pid;
       
        $sql = "SELECT
                        `post_title` AS `title`,
                        `post_body` AS `body`,
                        `post_user` AS `user`,
                        `post_date` AS `date`
                FROM `posts`
                WHERE `post_id` = {$pid}";
               
        $post = mysql_query($sql);
        $post = mysql_fetch_assoc($post);
       
        $post['comments'] = get_comments($pid);
       
        return $post;
}
 
//adds new blog entry
function add_post($title, $body, $name){
        $title  = mysql_real_escape_string(htmlentities($title));
        $body   = mysql_real_escape_string(nl2br(htmlentities($body)));
        $name   = mysql_real_escape_string(htmlentities($name));
       
        mysql_query("INSERT INTO `posts` (`user_id`, `post_title`, `post_body`,  `post_user`, `post_date`) VALUES ('{$_SESSION['uid']}','{$title}', '{$body}', '{$name}', NOW())");
 
			
}
 
//searches posts
function search_posts($term){
        $keywords = preg_split('#\s+#', mysql_real_escape_string($term));
       
        $title_where = "`post_title` LIKE '%" . implode("%' OR `post_title` LIKE '%", $keywords) . "%'";
        $body_where      = "`post_title` LIKE '%" . implode("%' OR `post_body` LIKE '%", $keywords) . "%'";
       
        $sql = "SELECT
                                `post_title` AS `title`,
                                LEFT (`post_body`, 100) AS `body`
                                FROM `posts`
                                WHERE {$title_where}
                                OR {$body_where}";
                               
        $result = mysql_query($sql);
                               
        $results = array();
                               
        while (($row = mysql_fetch_assoc($result)) !== false){
                $results[] = $row;
               
        }
        return $results;
                               
}
 
?>
blog read

<?php
include ("coreblog/initblog.php");
include ("core/init.inc.php");

if (isset($_GET['pid'], $_POST['user'], $_POST['body'])){
	if (add_comment($_GET['pid'], $_POST['user'], $_POST['body'])){
		header("Location: blog_read.php?pid={$_GET['pid']}");
	}else{
		header('Location: blog_list.php');
	}
	die();
}

?>  
 <?php

if (isset($_GET['pid']) === false || valid_pid($_GET['pid']) === false){
	echo 'Invalid post ID.';
}else{
	$post = get_post($_GET['pid'])

	?>
<h2><a href="blog_list.php?pid=<?php echo $post['id']; ?>"><?php echo $post['title']; ?></a></h2>
    <h3>By: <?php echo $post['user']; ?> on: <?php echo $post['date']; ?> (<?php echo count($post['comments']); ?> comments)</h3>
    
    <hr />
    
    <p><?php echo $post['body']; ?></p>
    
    <hr />
    <?php
	
	foreach ($post['comments'] as $comment){
		?>
    
	 <h3>By: <?php echo $comment['body']; ?> on <?php echo $comment['date']; ?></h3>
     <p><?php echo $comment['user']; ?></p>
     <hr />
     <?php
	}

	?>
<form action="" method="post">
<p>
	<label for="user">Name</label>
    <input type="text" name="user" id="user" value="<?php echo $_SESSION['username']; ?>" />
</p>
<p>
	<textarea name="body" rows="20" cols="60"></textarea>
</p>
<p>
	<input type="submit" value="Add Comment" />
</p>
</form>    
<?php

}

?>
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Make a Blog System for specific user

Post by jacek »

Do you have a pid in the url ? your valid_pid() function looks okay so the only thing I can think of is a typo of a variable name somewhere.
Image
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Make a Blog System for specific user

Post by jaysus7 »

it was good till i added the user id to the table the pid does show up if i manually put it in and then it works fine it just won't grab them????
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Make a Blog System for specific user

Post by jacek »

jaysus7 wrote:it was good till i added the user id to the table the pid does show up if i manually put it in and then it works fine it just won't grab them????
Do you mean that the problem is that the links from the main page don't have the post id in them ? If so make sure you have E_NOTICE level errors show as you probably typoed a variable somewhere.
Image
User avatar
jaysus7
Posts: 95
Joined: Wed Feb 22, 2012 9:06 pm
Location: Canada

Re: Make a Blog System for specific user

Post by jaysus7 »

It says E_NOTICE in the url so how do i set it what is the code is it

error_reporting(E_NOTICE);
Just a helpless cause!!!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Make a Blog System for specific user

Post by jacek »

jaysus7 wrote:It says E_NOTICE in the url so how do i set it what is the code is it
What do you mean in the URL ? :?

The notice error should tell you the name of a variable that has tried to be used when it has not been defined. So you need to work our why, or post the error and I will try to work out why for you :)
Image
Post Reply