big problem,need help..[blog]

Post here is you are having problems with any of the tutorials.
Post Reply
gt3000
Posts: 11
Joined: Wed Feb 20, 2013 11:46 am

big problem,need help..[blog]

Post by gt3000 »

Hi to all of you!I am making a web site and also I have complited blog tutorial, so I have big problem now and I cant understand what to do,so I hope for your help or some advice,please :?

Here is a screenshot and I need to make separate blog for each car (Honda civic and Mitsu 3000GT).
And those are two albums,and of course users can add their car that way,but if they will have 2 cars,their blog will duplicate..
So when user or I am making a blog for HONDA for example,everything duplicates to Mitsu 3000GT.
How can I fix ?Please help me.
I think I need to make an ID for each blog_list...but I dont know how,any ideas what to do?
Image

blog_list.php
[syntax=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 htmlspecialchars($post['user']); ?> on <?php echo $post['date']; ?></h4>
<h4>(<?php echo $post['total_comments']; ?> comments, last comment posted: <?php echo $post['last_comment']; ?>)</h4>

<hr />

<p><?php echo parseCodes(nl2br(htmlspecialchars($post['preview']))); ?></p>
<?php
}
?>[/syntax]

blog_post.php

[syntax=php]<?php
ob_start();

include('core/init.inc.php');

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

?>

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php
if(isset($_POST['body'])) {

} else {
}
?>

<form action="" method="post">
<p>
<label for="user">Name</label>
<input type="text" name="user" id="user" />
</p>

<p>
<label for="title">Title</label>
<input type="text" name="title" id="title" />
</p>

<p>
<textarea name="body" rows="20" cols="60"></textarea>
</p>
<p>
<input type="submit" value="Add comment" />
</p>
</form>

</body>
</html>[/syntax]

blog_read.php
[syntax=php]<?php
ob_start();
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');
}
}
?>
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<title>SITENAME</title>
</head>
<body>
<div>
<?php
if(isset($_GET['pid']) === false || valid_pid($_GET['pid']) === false) {
echo 'Invalid post ID.';
} else {
$post = get_post($_GET['pid']);
?>
<h2><?php echo $post['title']; ?></h2>
<h4>By <?php echo $post['user']; ?> on <?php echo $post['date']; ?> (<?php echo count($post['comments']); ?> comments)</h4>
<hr>
<p><?php echo parseCodes(nl2br(htmlspecialchars($post['body']))); ?></p>
<hr>
<?php
foreach($post['comments'] as $comment) {
?>
<h4>By <?php echo htmlspecialchars($comment['user']); ?> on <?php echo $comment['date']; ?></h4>
<p><?php echo parseCodes(nl2br(htmlspecialchars($comment['body']))); ?></p>
<hr>
<?php
}
if(isset($_POST['body'])) {

} else {
?>
<form action="" method="POST">
<p>
<label for="user">Name</label>
<input type="text" name="user" id="user">
</p>
<p>
<textarea name="body">text here</textarea>
</p>
<p>
<input type="submit" value="Add Comment">
</p>
</form>
<?php
}
}
?>
</div>
</body>
</html>[/syntax]

post.inc.php

[syntax=php]<?php

// controleert of opgegeven id in de tabel staat.
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;
}
}

// Fetches summary of all blog post
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`
ORDER BY `posts`.`post_date` DESC
";



$posts = mysql_query($sql);

$rows=array();
while(($row= mysql_fetch_assoc($posts)) !== false){
$rows[]=array(
'id'=>$row['id'],
'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 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 a new blog entry
function add_post($name,$title, $body){
$name = mysql_real_escape_string(htmlspecialchars($name));
$title = mysql_real_escape_string(htmlspecialchars($title));
$body = mysql_real_escape_string(nl2br(htmlspecialchars($body)));

mysql_query("INSERT INTO `posts` (`post_user`, `post_title`, `post_body`, `post_date`) VALUES ('{$name}', '{$title}', '{$body}', NOW())");


}
?>[/syntax]
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: big problem,need help..[blog]

Post by jacek »

You will need a new table to hold the list of blogs, call it `blogs` for now then you can add a `blog_id` column to the posts table to link each post to a specific blog. The comments are link to the posts so that is not a problem since they will only show up on their posts.

You then need to add a URL parameter for the blog ID so that you can view each one.

It's a bit of a big change but start with sorting the database out and then try to tackle the code one part at a time,
Image
gt3000
Posts: 11
Joined: Wed Feb 20, 2013 11:46 am

Re: big problem,need help..[blog]

Post by gt3000 »

Hello,Jacek!
I knew that I need to make a new table called "blogs",so I made it,and as you sad I added a field "blog_id" in "posts" table.
Do I need to make a function something like
"function get_blogs"?Because I dont realy understand what I have to modify or which functions I should add.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: big problem,need help..[blog]

Post by Temor »

Well, this is where much of your personal preference comes into play. How do you want this to be sorted?
Do you want it to be sorted under the Users ID or as a separate blog entirely?

If the second is true, then you would just have to fetch the blog id's from the table and print them in a loop like you have in blog_read.php.

If the first is true, then there's a little bit more to be done.
You would have to either key the users id to the blogs id in your database or in some other way match them together.
gt3000
Posts: 11
Joined: Wed Feb 20, 2013 11:46 am

Re: big problem,need help..[blog]

Post by gt3000 »

My idea was,when user add his car to the website(it means car details ,description and photo of car) just like on my screenshot you can see,than this car belongs to user ID,and I already made it by tutorials.It's like personal room,and on the screenshot those 2 cars belong to 1 user,so the only thing I need to make a blog for each car.

So,when you add a car,you see a "Garage" link,(Garage - it is a blog where user can write about repairs and other stuff)and each "Garage" link must contain personal ID.When you hover for example "Garage" on Honda Civic car it must show for example like "view_blog.php?blog=1",and the second car will be "view_blog.php?blog=2" and it will increase with each next car.

Yeah,I think it's a little bit hard to make it,isnt it?seems like a lot of code I need to modify,thats why unfortunatly I have stoped my project because I dont know how to make it,I did not own skills like you
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: big problem,need help..[blog]

Post by Temor »

This shouldn't actually be as hard as you think it would be. You don't need "skills", you just need to think outside the box a bit.

Here's what I would do ( I will not provide actual working code so try to work it out by yourself ).

Here is what I suppose your Database looks like right now:

Users
- User ID
- User Name
- User Password
- User etc etc...

Blogs
- Blog ID
- Blog Name
- Blog etc etc..


What I would do firstly is create a new column in Blogs and call it User or Owner ID.
And then, whenever a user creates a new blog for his car, you take his user ID and insert it into the User or Owner ID column.

[syntax=php]$sql = "INSERT INTO `blogs` (`Blog Name`,`Blog Owner`) VALUES ('$blog_name','$user_id')";[/syntax]

This will make sure that you can always figure out who the owner is with relative ease.

Now, on the page where you want to display the blogs owned by a given user:
Lets say your url looks like this: Blog.com/?user=1

This means that they want to access the list of blogs owned by user with id 1.

The SQL query to fetch these rows should look something like this:
[syntax=php]$sql = "SELECT `Blog id`,`Blog Name` FROM `blogs` WHERE `Blog Owner` = $_GET['user']";[/syntax]


This would give you a list of all the blogs that user has created, i.e one entry for each car in his "garage".
You would then just print them like you currently are at blog_list.

This is actually really simple and would just require a few modifications to your already existing code ( I don't think you'd have to write more than maybe a few lines of new code )
Post Reply