Okay help with blog system.

Ask about a PHP problem here.
Post Reply
sturekdrf
Posts: 40
Joined: Fri Jun 15, 2012 8:25 pm

Okay help with blog system.

Post by sturekdrf »

So, I have figured out a large majority of this system kind of taking it step by step starting with the login and verification system. My biggest problem at the moment and I am sure it mixes in with css but don't quite have a good idea of how yet. Here is a small list of issues I am having problems comming up with ideas how to fix.

1. I want a blog to have a maximum of say 5 blog posts on the main page, than archive back.

2. I need to format them into boxes probably using css. Not quire sure how to interact with php and css on that level, but as a example. mmo-champion.com has news formated into a css box with a square title top. I probably would not want it to look like this but I need to figure out how to column the news so it looks nice.

Thats about really what I am trying to figure out atm.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Okay help with blog system.

Post by jacek »

sturekdrf wrote:1. I want a blog to have a maximum of say 5 blog posts on the main page, than archive back.

That's simple pagination, I think I did a video on it ;)

sturekdrf wrote:2. I need to format them into boxes probably using css. Not quire sure how to interact with php and css on that level, but as a example. mmo-champion.com has news formated into a css box with a square title top. I probably would not want it to look like this but I need to figure out how to column the news so it looks nice.

There us no interaction at all between php and css. You need to make sure you understand what php actually does or you will keep trying to do impossible things with it. PHP runs on the server, it produces text output (that is often HTML but it can be anything) that is interpreted by the browser. So to style each blog post you can put each post in a div

[syntax=php]<?php

foreach ($posts as $post){
?>
<div class="blog_post">
<h2><?php echo $post['title']; ?></h2>
<p>
<?php echo $post['body']; ?>
</p>
</div>
<?php
}

?>[/syntax]
Then you can think about styling it

[syntax=css]div.blog_post {
border: solid 1px #333;
margin: 6px 0px;
}

div.blog_post h2 {
margin: 0px;
padding: 0px;
font-size: 16px;
}

div.blog_post p {
margin: 4px 0px 0px 0px;
padding: 0px;
}[/syntax]
would be an example of how to apply styling to each of the elements in the box (not a very pretty one, that's your job) :D
Image
sturekdrf
Posts: 40
Joined: Fri Jun 15, 2012 8:25 pm

Re: Okay help with blog system.

Post by sturekdrf »

Okay I can't for the life of me figure out a good solution to this problem I have having now. Currently I have scripted and have working the login system, the post system, I formatted the page so the news works. What I am having issues with is for my current webpage that I am working on I only need to show the latest news and I figured I would do what you suggested with pagination when I ran into a serious problem I was having problems figuring out how to go about it.

Currently I use this code and than string it together with the the other bit of a code like so.
[syntax=php]function get_total_posts() {
$result = mysql_query('SELECT COUNT(`post_id`) FROM `posts`');
return mysql_result($result, 0);
}[/syntax]

[syntax=php]<?php
$post = get_post(get_total_posts());
?>[/syntax]

the lower code is the one thats on the main webpage, and its how I figured out how to get the very last int and ensure that the last post made is always on the main page.

Here is where i run into my little problem. I can't for the life of me figure out how to use pagination and have it so I can go to previous news posts (if there are anyway) or go forward to newer ones. Because I can't figure out a way to update the posts as well.Since I doubt the get post string updates everytime you change. Basically I just want it so you can click back or forward and look to see the news that way anyway rest of the code for main page is here.

[syntax=php]<div id="content_middle">
<div id="post_title">
<?php
$post = get_post(get_total_posts());
?>

<p class="post_title_txt"> <?php echo $post['title'] ?> <?php echo $post['date'] ?></p>
</div>
<div id="post_body">
<p class="post_body_txt"> <?php echo $post['body'] ?> <br /> <?php echo get_total_posts(); ?></p>
</div>
<div id="page_select">
<p> </p>
</div>
</div>[/syntax]
sturekdrf
Posts: 40
Joined: Fri Jun 15, 2012 8:25 pm

Re: Okay help with blog system.

Post by sturekdrf »

http://monstersagainstcancer.com/indextest.php

this is kind of what I am using as a testing base. Everything in it is very sloopy its not done was just trying to get things to work.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Okay help with blog system.

Post by jacek »

[syntax=php]$post = get_post(get_total_posts());[/syntax]
This is not right, it will just return all of the posts :s

You need to calculate the start position in the table from the page, so if you have 30 rows and you have 5 items on each page. page 1 will show 0 - 5 and page 3 would show 10 - 15.

So I would modify your function to take two parameters, the page number and the number of items per page. once you have those you can work out the starting point by multiplying the page number - 1 by the number of items per page.

[syntax=php]$start = $page - 1 * $per_page;[/syntax]
Then you can use this in your query to only fetch the results for that page

[syntax=php]$sql = "... LIMIT {$start}, {$per_page}[/syntax]
The $page - 1 is because you want to start from the very beginning of the table for page 1, and the beginning of the table is row 0.
Image
sturekdrf
Posts: 40
Joined: Fri Jun 15, 2012 8:25 pm

Re: Okay help with blog system.

Post by sturekdrf »

Actually
[syntax=php]$post = get_post(get_total_posts());[/syntax]
does work, since it returns the total number of posts it would be used as the int for the first function causing it to always give me the very last post. At least every time I tested it to make thats what it did it worked.

The problem is that everytime you insert a new row its adding it to the end number, so doing it this way from what I understand you posting wouldnt work because it would make the page start at the begining of the row, when I need it to start from the latest (the highest number) and work backwords to the first row, or the oldest post.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Okay help with blog system.

Post by jacek »

You can do it the other way around with no problems though, you need the total number of posts to work out how many pages there are so do

[syntax=php]$sql = "... LIMIT {$total} - {$start}, {$per_page}[/syntax]
or something along those lines at least.
Image
sturekdrf
Posts: 40
Joined: Fri Jun 15, 2012 8:25 pm

Re: Okay help with blog system.

Post by sturekdrf »

Honestly could not fully figure out how to go from there but I found something that I got working, i just need to add the forward and backward part of it. But I will post the whole process that I got it to work with here, to see what you think.

[syntax=php]<?php
//Variable for amount of posts to show per page.
$per_page = 1;

//this calls the function to get the total number of posts.
$fullcount = get_total_posts();

//was part of the pagination but I don't use this currently.
//$pages_query = mysql_query("SELECT COUNT(`post_id`) FROM `posts`");
//$pages = ceil(mysql_result($pages_query, 0) / $per_page);

//checks to ensure page is there, uses a int for protection, if no value is there set default to newest post)
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : $fullcount;

//the variable I use below to get the current post. -1 because database starts at 0.
$start = ($page - 1) * $per_page;

?>[/syntax]

The part that makes it show up on the page. I have it echoing start for debugging purposes.

[syntax=php]
<?php
//using the above variable, should check the header for page number and adjust the post accordingly.
$post = get_post($start);
?>

<p class="post_title_txt"> <?php echo $post['title'] ?> <?php echo $post['date'] ?></p>
</div>
<div id="post_body">
<p class="post_body_txt"> <?php echo $post['body'] ?> <br /> <?php echo $start ?></p>
</div>
[/syntax]

This is basically from your tutorial, and I just went ahead and modified where I needed to fit the system I was working with.
[syntax=php]
<?php
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;

}

//Gets total amount of posts, and uses it to make sure the last post is always on index.html
function get_total_posts() {
$result = mysql_query('SELECT COUNT(`post_id`) FROM `posts`');
return mysql_result($result, 0);
}
?>
[/syntax]

Granted probably not the best way to handle this of yet, BUT at least I am getting the result i want so far.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Okay help with blog system.

Post by jacek »

That should work for a single post, but you want 5 don't you ?

The method you have there is pretty much it, to get it to display 5 you need to use a LIMIT in your query and add a few multiplications of 5 to get the bounds right.
Image
sturekdrf
Posts: 40
Joined: Fri Jun 15, 2012 8:25 pm

Re: Okay help with blog system.

Post by sturekdrf »

For my next site yes ill have to change it to be 5, for my current site because how its designed it just needs to be 1.

monstersagainstcancer.com/indextest.html is how its currently setup. Its not on my main page but its what I am using right now to get it setup right and adjust things.
sturekdrf
Posts: 40
Joined: Fri Jun 15, 2012 8:25 pm

Re: Okay help with blog system.

Post by sturekdrf »

Okay NVM I figured it out.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Okay help with blog system.

Post by jacek »

sturekdrf wrote:Okay NVM I figured it out.

Hooray :D
Image
Post Reply