View Full Version : I need to know how to do this.... MySQL experience required


iTux
01-28-2006, 09:59 PM
I am almost finished a blog system that I started a couple of days ago, it uses mysql and php, what I would like to do is sort them by month, like one page would be like for january and when I make a post in february it will make a new page for february and move januarys posts to a page like behind the page for february. Thanks for the help in advance, I hope I made this as clear as possible.

skinrock
01-29-2006, 03:50 AM
Well the dataset would be easy to generate. When you are grabbing the blogs, you would just add this to the mysql query:

"SELECT * FROM blogs ORDER BY dateField DESC"

Now it will order the records by the date of the blog, in descending order, or newest first. Hopefully your date field is something like a date, datetime or timestamp field.

Now, for a paging system, that will be a little tricky, but if you don't have anything super advanced, you might be able to do something like this. First, you need to decide on the number of blogs per page, you will probably want to store that in a variable. Next, you must draw the page links, and prepare your page to accept a page number in the URL. So you would do the following:

Run that mysql query and grab how many records there are. You need this to show how many pages. So let's say you get 133 records. The next step is to divide that by the number of blogs per page, let's say 10. You will want to use the ceil() function, just do:


$pages = ceil($records / $num_per_page);


You must use ceil, because in this case, the answer is 13.3, so you need 14 pages to show the last 3 items.

Then you'll want to draw the page links:


<?php
for ( $x = 1; $x <= $pages; $x++ ) {
?>
<a href="blogs.php?page=<?=$x?>"><?=$x?></a>
<?php
}
?>


You will also need the current page:


if ( isset($_GET['page']) {
$cur_page = $_GET['page'];
} else {
$cur_page = 1;
}


Lastly, you will need to run the mysql query to grab a range of records, you will grab the range based on the page and number of items:


$start = 0 + ( ($cur_page-1) * $num_per_page );
$query = "SELECT * FROM blogs ORDER BY dateField DESC LIMIT $start, $num_per_page";


Hopefully that works and gives you an idea of what you can do.

skinrock
01-29-2006, 04:07 AM
Weird, I can't edit my post. All I wanted to say was that I knew there was a tutorial on this in the Tutorial area, but I never got around to reading it. It's scary how similar mine is to douglas3131's. From what I can gather, the only real difference is how the page links look. If mine works, the page in the URL will be equal to the number of the page, so either 1, 2, 3, etc. If I understand douglas', it will show the offset, so something like 0, 10, 20, etc.

Should work either way. douglas' is a good tutorial if you ever need to look back at it for reference.

Douglas
01-29-2006, 03:46 PM
*Cough* I made a little mistake in the tutorial, I fixed it, you should read it though, makes a big difference for a little mistake ;) Anyway, I'm here to tell you you should do this:

<?php
for ( $x = 1; $x <= $pages; $x++ ) {
?>
<a href="blogs.php?page=<?php echo $x; ?>"><?php echo $x; ?></a>
<?php
}
?>

Instead of this:

<?php
for ( $x = 1; $x <= $pages; $x++ ) {
?>
<a href="blogs.php?page=<?=$x?>"><?=$x?></a>
<?php
}
?>

See where it prints out the $x? If he ever lets his script public, then people without support for that would have problems ;)

skinrock
01-29-2006, 05:42 PM
Which ini setting is that, the short_open_tag? I didn't realize that <?= ?> was controlled by any settings, obviously because I've never had an issue with it.

Douglas
01-29-2006, 11:01 PM
<?php
$num_per_page=15;

$pages = ceil($records / $num_per_page);

for ( $x = 1; $x <= $pages; $x++ ) {
?>
<a href="blogs.php?page=<?php echo $x; ?>"><?php echo $x; ?></a>
<?php
}

if ( isset($_GET['page']) {
$cur_page = $_GET['page'];
}
else {
$cur_page = 1;
}

$start = 0 + ( ($cur_page-1) * $num_per_page );
$query = "SELECT * FROM blogs ORDER BY dateField DESC LIMIT $start, $num_per_page";
?>


Change the $num_per_page variable to set how many perpage googleguy ;)

iTux
01-30-2006, 03:33 AM
Thanks everyone for the help!! I am now almost finished the system, Thanks again.

Douglas
01-30-2006, 05:39 PM
No problem googleguy, good luck with your system :D