|
I've seen it done on PB, as well as bunch of other sites: Since I'm making a blog system, I'm wondering how to spawn pages in the following way: gunny.com/blog.php?post=23Any help on this would be great.
|
|
|
|
You mean like grabbing the blog entries that would correspond to that page, or just getting that value?
|
|
|
|
|
$max = 10; //maximum posts or whatever per page $page = ($_GET['post'])? $_GET['post']:1; //what page the user is on. $start = ($page*$max)-$max;
$db_username = 'username'; $db_password = 'password'; $db_ = 'database'; mysql_connect(localhost,$db_username,$db_password); mysql_select_db($db); $result = mysql_query("SELECT*FROM tableid"); while($row = mysql_fetch_assoc($result)){ if($row['id'] >= $start && $row['id'] <=($start+$max)){ //echo your stuff out } } $rows = mysql_num_rows($result); $pages = ceil($rows/$max)+1; echo 'Pages:'; for($p=1;$p<$pages;$p++){ echo '<a href="?post='.$p.'">'.$p.'</a>'; echo ($p!=($pahes-1))? ', ':'.'; }
Last Edit: Feb 26, 2006 15:26:27 GMT by crazynarutard
|
|
|
|
Can you take me through the whole thing from scratch? This is how I see it:
Have template for pages
Then I:
Put content in MySQL
Echo out onto the ?page pages
|
|
|
|
So this page you can have after the form (after submitting), so what we'll have to do is submit the data first, like you said. <?php $max = 10; //maximum posts or whatever per page $page = ($_GET['post'])? $_GET['post']:1; //what page the user is on. $start = ($page*$max)-$max; //Where the starting post is
$db_username = 'username'; $db_password = 'password'; $db_ = 'database'; mysql_connect(localhost,$db_username,$db_password); mysql_select_db($db); if( isset($_POST['submit']) ){ //check if you just submitted //insert the data into the table } /*So now, your new data plus the other stuff that have been submitted before, are in the table. We need to print that out now */
$result = mysql_query("SELECT*FROM tableid"); //select all data from that table while($row = mysql_fetch_assoc($result)){ if($row['id'] >= $start && $row['id'] <= ($start+$max)){ /* explanation what the if statement does: $max is for the maximum posts per page right? Well if you're on page 1 that means you have to see post 1-10, if you're on page 2 you have to see post 11-20. So the 1 and the 11 is the $start and the 10 and 20 is the ($max+$start). Just a little simple Math. So depending on the page you print different posts out*/ //echo your stuff out } } $rows = mysql_num_rows($result); //count all the rows in the table $pages = ceil($rows/$max)+1; /*total rows divided by max posts per page becomes the total pages. The plus one is just so there is no page zero (see the loop also)*/ echo 'Pages:'; for($p=1;$p<$pages;$p++){ echo '<a href="?post='.$p.'">'.$p.'</a>'; echo ($p!=($pages-1))? ', ':'.'; //if it is not end of loop, add a comma else add a dot. }
I added tons of comments in the code, it's basicly a small tutorial now. In your mysql table you have to have a "id" field with auto_increment and in the post form you need a submit button with the name "submit". =P
Last Edit: Feb 26, 2006 19:36:46 GMT by crazynarutard
|
|
|
|
for($p=1;$p<$pages;$p++){ echo '<a href="?post='.$p.'">'.$p.'</a>'; echo ($p!=($pahes-1))? ', ':'.'; //if it is not end of loop, add a comma else add a dot. } Type in there. Should be this. (You misspelled the final $pages as $pahes) for($p=1;$p<$pages;$p++){ echo '<a href="?post='.$p.'">'.$p.'</a>'; echo ($p!=($pages-1))? ', ':'.'; //if it is not end of loop, add a comma else add a dot. }
|
|
|
|
|
Hmm... I'll try it. Thanks for your help guys.
|
|
|