Blog Tutorial + Pagination -some problems..

Ask about a PHP problem here.
Post Reply
Jaami
Posts: 38
Joined: Wed Nov 23, 2011 11:22 pm
Location: GER

Blog Tutorial + Pagination -some problems..

Post by Jaami »

hello again,
i need some help from you. i would probably solve it on my own..sometime. but, i am frustrated a little bit atm. and maybe you can save me some time and tears.. :roll: ;)

so i managed to combine these two tutorials, where both using the same db-table and they are even showing up on the same page/s. but i cant get them both interact between each other correctly.

user_list.php:

[syntax=php]<?php
include('core/init.inc.php');
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;

?>


<?php
$posts = get_posts();

foreach(fetch_titles($page, 5) as $post){

}
?>
<?php



foreach($posts as $post){
?>
<div id="titel">
<a class="titeltxt" href="blog_read.php?pid=<?php echo $post['id']; ?>"><?php echo $post['title']; ?></a>
</div>

<h6>By <?php echo $post['user']; ?> on <?php echo $post['date']; ?></h6>
<div id="news">
<br />
<!-- Video Player HERE -->
<?php

if(empty($post['video'])){

}
else{
if(isset($post['videoplayer'])){
$videoplayer = $post['videoplayer'];
if($videoplayer == '1'){
include('vid_player.php');
}
else{
include('vid_player2.php');
}
}
else{

}
}

?>
<!-- Video Player END -->

</div>
<p><h4><?php echo $post['preview']; ?></p>
<h5>(<a href="blog_read.php?pid=<?php echo $post['id']; ?>"><?php echo $post['total_comments']; ?> comments, last comment <?php echo $post['last_comment']; ?></a>)</h5>

<?php
}

?>




<?php

$total_pages = ceil(fetch_total_titles() / 5);

for ($i = 1; $i <= $total_pages; ++$i){
echo " <a href=\"?page={$i}\">{$i}</a> ";
}
?>


<br />


<!-- Prev/Next -->
<?php
if($page >= 2){
echo '<a href="?page=', ($page + -1), '">Previous </a>';
}
?>

<?php
if($page <= 1 AND $page != $total_pages){
echo '<a href="?page=', ($page + 1), '"> Next</a>';
}
?>
[/syntax]
the code is just pasted from "blog_list.php" -thats where my problem is..

init.inc.php:
[syntax=php]<?php

mysql_connect('127.0.0.1', 'example_user', 'test');
mysql_select_db('blog')
or die("Fehler im System");

include("pagination/inc/users.inc.php");
include('inc/posts.inc.php');
include('inc/comments.inc.php');


?>[/syntax]

posts.inc.php:
[syntax=php]<?php


//pagination
// fetches a list of posts from the database.
function fetch_titles($page, $per_page){
$start = (int)($page - 1) * $per_page;
$per_page = (int)$per_page;

$query = mysql_query("SELECT `post_title` FROM `posts` LIMIT {$start}, {$per_page}");

while (($row = mysql_fetch_assoc($query)) !== false){
$posts[] = $row['post_title'];
}

return $posts;
}

function fetch_total_titles(){
$result = mysql_query("SELECT COUNT(`post_id`) FROM `posts`");

return mysql_result($result, 0);
}





//checks if the given post id is in the table.
function valid_pid($pid){
$pid = (int)$pid;[/syntax]
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog Tutorial + Pagination -some problems..

Post by jacek »

If you take out all of the html bits you have this

[syntax=php]$posts = get_posts();

foreach (fetch_titles($page, 5) as $post){
// nothing
}

foreach ($posts as $post){
// some HTML stuff
}[/syntax]

So it looks like you are getting all of the posts, and one page of posts. Then completely ignoring the paged result.

It should be just the first bit but not an empty look, it should have the HTML stuff

[syntax=php]foreach (fetch_titles($page, 5) as $post){
// some HTML stuff
}[/syntax]
Image
Jaami
Posts: 38
Joined: Wed Nov 23, 2011 11:22 pm
Location: GER

Re: Blog Tutorial + Pagination -some problems..

Post by Jaami »

yeah, i tried that before already, but i get this:
edit: picture deleted by me...


[syntax=php]<?php
include('core/init.inc.php');
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;

?>


<?php
$post = get_posts();


foreach(fetch_titles($page, 5) as $post){
?>
<div id="titel">
<a class="titeltxt" href="blog_read.php?pid=<?php echo $post['id']; ?>"><?php echo $post['title']; ?></a>
</div>

<h6>By <?php echo $post['user']; ?> on <?php echo $post['date']; ?></h6>
<div id="news">
<br />
<!-- Video Player HERE -->
<?php

if(empty($post['video'])){

}
else{
if(isset($post['videoplayer'])){
$videoplayer = $post['videoplayer'];
if($videoplayer == '1'){
include('vid_player.php');
}
else{
include('vid_player2.php');
}
}
else{

}
}

?>
<!-- Video Player END -->

</div>
<p><h4><?php echo $post['preview']; ?></p>
<h5>(<a href="blog_read.php?pid=<?php echo $post['id']; ?>"><?php echo $post['total_comments']; ?> comments, last comment <?php echo $post['last_comment']; ?></a>)</h5>
<?php
}
?>









<?php

$total_pages = ceil(fetch_total_titles() / 5);

for ($i = 1; $i <= $total_pages; ++$i){
echo " <a href=\"?page={$i}\">{$i}</a> ";
}
?>


<br />


<!-- Prev/Next -->
<?php
if($page >= 2){
echo '<a href="?page=', ($page + -1), '">Previous </a>';
}
?>

<?php
if($page <= 1 AND $page != $total_pages){
echo '<a href="?page=', ($page + 1), '"> Next</a>';
}
?>

[/syntax]

EDIT: i have an idea, maybe its showing me only one char because of wrong db settings, i check that...
Last edited by Jaami on Tue Dec 06, 2011 11:16 pm, edited 2 times in total.
User avatar
louiegiezer
Posts: 57
Joined: Fri Oct 21, 2011 11:31 am
Contact:

Re: Blog Tutorial + Pagination -some problems..

Post by louiegiezer »

you don't need to put this on your code "$post = get_posts();" kill this part :lol:

Try to do this...
[syntax=php]
<?php
foreach(fetch_titles($page, 5) as $post){
?>
<div id="titel">
<a class="titeltxt" href="blog_read.php?pid=<?php echo $post['id']; ?>"><?php echo $post['title']; ?></a>
</div>

<h6>By <?php echo $post['user']; ?> on <?php echo $post['date']; ?></h6>
<div id="news">

<!-- Video Player HERE -->
<?php

if(empty($post['video'])){

}
else{
if(isset($post['videoplayer'])){
$videoplayer = $post['videoplayer'];
if($videoplayer == '1'){
include('vid_player.php');
}
else{
include('vid_player2.php');
}
}
else{

}
}

?>
<!-- Video Player END -->

</div>
<p><h4><?php echo $post['preview']; ?></p>
<h5>(<a href="blog_read.php?pid=<?php echo $post['id']; ?>"><?php echo $post['total_comments']; ?> comments, last comment <?php echo $post['last_comment']; ?></a>)</h5>

<?php
}

?>




<?php

$total_pages = ceil(fetch_total_titles() / 5);

for ($i = 1; $i <= $total_pages; ++$i){
echo " <a href=\"?page={$i}\">{$i}</a> ";
}
?>


<br />


<!-- Prev/Next -->
<?php
if($page >= 2){
echo '<a href="?page=', ($page + -1), '">Previous </a>';
}
?>

<?php
if($page <= 1 AND $page != $total_pages){
echo '<a href="?page=', ($page + 1), '"> Next</a>';
}
?>

[/syntax]
Jaami
Posts: 38
Joined: Wed Nov 23, 2011 11:22 pm
Location: GER

Re: Blog Tutorial + Pagination -some problems..

Post by Jaami »

mmh, i forgot to delete that line. and you didnt change much besides that one line?! so i still have the problem as shown in screenshot..
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog Tutorial + Pagination -some problems..

Post by jacek »

The array that your fetch_titles function creates is not the same form as the one you are trying to use.

All you have is an array of the post titles on that page, you need all of the information. You should be doing somethign similar to the cod ein the get_posts() function.
Image
Jaami
Posts: 38
Joined: Wed Nov 23, 2011 11:22 pm
Location: GER

Re: Blog Tutorial + Pagination -some problems..

Post by Jaami »

oh noes..i guess i have to go thru this brain damaging procedure..
Jaami
Posts: 38
Joined: Wed Nov 23, 2011 11:22 pm
Location: GER

Re: Blog Tutorial + Pagination -some problems..

Post by Jaami »

hah, i did it!! *feels_good_man.jpg* 8-)

it wasn`t actually that bad. just merging the two functions to one..

THANK YOU AGAIN, for your little directive in the right "line"!!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog Tutorial + Pagination -some problems..

Post by jacek »

Jaami wrote:hah, i did it!! *feels_good_man.jpg* 8-)

it wasn`t actually that bad. just merging the two functions to one..

THANK YOU AGAIN, for your little directive in the right "line"!!

Good job :D
Image
Post Reply