Page 1 of 1

Splitting data into pages

Posted: Wed Jun 20, 2007 4:49 pm
by derek barnstorm
Hi, I'm afraid that I'm a complete novice and need a little help.

I have been using the following script to grab blog entries from a database and display them on a page. But obviously the page gets a little too long in the end, so I wanted to modify it so that after it had displayed five pages it would stop and then have a link to the next five and so on. I have had a go at doing it, but have made a complete muck up of it.

I get the following error:

Parse error: syntax error, unexpected T_VARIABLE in /home/path/to/my/site/html/blogs.inc.php on line 54

Any help would be appreciated.

Here's the original script:

Code: Select all

<?php
 if($_SERVER[PHP_SELF]=="/include/profiles/blogs.inc.php")
 {
    header("Location: /index.php");
    exit;
}

$sql_f = "
SELECT *
FROM $tbl_friends WHERE friend1 = \"$auth[member_id]\" AND confirmed = \"yes\"";

$result_f = @mysql_query($sql_f,$connection) or die("Couldn't execute friends query.");
$FRnum=mysql_num_rows($result_f);
if($FRnum == "0") {
$prv = "AND public = \"yes\"";
} else { $prv = ""; }
if($auth[member_id] == "$profile_id") {
$prv = "";
}

?>
<table cellpadding="4" cellspacing="0" width="100%">
<?

$data_p = mysql_query("SELECT * FROM $tbl_blogs WHERE blog_owner = \"$profile_id\" $prv
ORDER BY blog_id DESC") or die(mysql_error());
$Bnum=mysql_num_rows($data_p);
if($Bnum =="0") {
echo "<tr><td clospan=\"2\"><p>No Blogs yet.</p><p>&nbsp;</p></td></tr>";
} else {

while ($row = mysql_fetch_array($data_p)) {



$blog_content = "$row[blog_content]";
$blog_title = "$row[blog_title]";
$text = "$blog_title";
$good_blog_title = stripslashes($text);

if($row[public] == "yes") {
$public = "public post";
} else {
$public = "private post";
}
$text = "$blog_content";
$message = stripslashes($text);



$text = ereg_replace("\r\n", "\n", $text);
$text = ereg_replace("\r", "\n", $text);
$text = ereg_replace("\n\n", '</p><p>', $text);
$text = ereg_replace("\n", '<br />', $text);
$message = ereg_replace("\r\n", "\n", $message);
$message = ereg_replace("\r", "\n", $message);
$message = ereg_replace("\n\n", '</p><p>', $message);
$message = ereg_replace("\n", '<br />', $message);
$good_text = ereg_replace("\r\n", "\n", $good_text);
$good_text = ereg_replace("\r", "\n", $good_text);
$good_text = ereg_replace("\n\n", '</p><p>', $good_text);
$good_text = ereg_replace("\n", '<br />', $good_text);



if($auth[login] == "admin") {$show_del = "<a href=\"admin_delblog.php?blog_id=$row[blog_id]&
blog_title=$row[blog_title]\"><img src=\"$imgdir/trash.gif\" align=\"absmiddle\" border=\"0\"
alt=\"Delete post\"></a> ";}

if($auth[member_id] == "$row[blog_owner]") {$show_del = "<a href=\"/blog_edit.php?blog_id=$row[blog_id]\">
<img src=\"/$imgdir/edit.gif\" align=\"absmiddle\" border=\"0\"
alt=\"Edit post\"></a> ";}

echo "<tr>
        <td class=\"blog_title\">
        <p>Posted by: <a href=\"$userurl/$row[url]\">$row[author]</a> $show_del</p>
        </td>
        <td class=\"blog_title\">
            <p>Date: $row[blog_date]</p>
        </td>
    </tr>
    <tr>
        <td colspan=\"2\" class=\"blog_title\">
            <p>Title: <b>$good_blog_title</b>&nbsp;&nbsp;<i>($public)</i></p>
        </td>
    </tr>
    <tr>
        <td colspan=\"2\" class=\"blog_entry\">
            <p>$message</p>
        </td>
    </tr><tr><td colspan=\"2\"><p><hr></p></td></tr>";
}
}
?>
<tr><td align="center" colspan="2"><p>Back to <a href="index.php">My Profile</a>.</p></td></tr>
</table>
And here's the one that I had a go at, and messed up (please don't laugh, I am a complete begginner):

Code: Select all

<?php
 if($_SERVER[PHP_SELF]=="/include/profiles/blogs.inc.php")
 {
    header("Location: /index.php");
    exit;
}

$sql_f = "
SELECT *
FROM $tbl_friends WHERE friend1 = \"$auth[member_id]\" AND confirmed = \"yes\"";

$result_f = @mysql_query($sql_f,$connection) or die("Couldn't execute friends query.");
$FRnum=mysql_num_rows($result_f);
if($FRnum == "0") {
$prv = "AND public = \"yes\"";
} else { $prv = ""; }
if($auth[member_id] == "$profile_id") {
$prv = "";
}

if (!(isset($_GET['pagenum'])))
{
$pagenum = 1;
}


$data_p = mysql_query("SELECT * FROM $tbl_blogs WHERE blog_owner = \"$profile_id\" $prv
ORDER BY blog_id DESC") or die(mysql_error());
$Bnum=mysql_num_rows($data_p);
if($Bnum =="0") {
echo "<tr><td clospan=\"2\"><p>No Blogs yet.</p><p>&nbsp;</p></td></tr>";
} 

$page_rows = 5;

$last = ceil($rows/$page_rows);
if($last == "0") {$last = "1";}

if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

$max = 'ORDER BY blog_id DESC limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
?>
<table cellpadding="4" cellspacing="0" width="100%">
<?
$sql_f = "
SELECT *
FROM $tbl_friends WHERE friend1 = \"$auth[member_id]\" AND confirmed = \"yes\ "$max") or die(mysql_error());

while ($row = mysql_fetch_array($data_p)) {



$blog_content = "$row[blog_content]";
$blog_title = "$row[blog_title]";
$text = "$blog_title";
$good_blog_title = stripslashes($text);

if($row[public] == "yes") {
$public = "public post";
} else {
$public = "private post";
}
$text = "$blog_content";
$message = stripslashes($text);



$text = ereg_replace("\r\n", "\n", $text);
$text = ereg_replace("\r", "\n", $text);
$text = ereg_replace("\n\n", '</p><p>', $text);
$text = ereg_replace("\n", '<br />', $text);
$message = ereg_replace("\r\n", "\n", $message);
$message = ereg_replace("\r", "\n", $message);
$message = ereg_replace("\n\n", '</p><p>', $message);
$message = ereg_replace("\n", '<br />', $message);
$good_text = ereg_replace("\r\n", "\n", $good_text);
$good_text = ereg_replace("\r", "\n", $good_text);
$good_text = ereg_replace("\n\n", '</p><p>', $good_text);
$good_text = ereg_replace("\n", '<br />', $good_text);


if($auth[login] == "admin") {$show_del = "<a href=\"admin_delblog.php?blog_id=$row[blog_id]&
blog_title=$row[blog_title]\"><img src=\"$imgdir/trash.gif\" align=\"absmiddle\" border=\"0\"
alt=\"Delete post\"></a> ";}

if($auth[member_id] == "$row[blog_owner]") {$show_del = "<a href=\"/blog_edit.php?blog_id=$row[blog_id]\">
<img src=\"/$imgdir/edit.gif\" align=\"absmiddle\" border=\"0\"
alt=\"Edit post\"></a> ";}

echo "<tr>
        <td class=\"blog_title\">
        <p>Posted by: <a href=\"$userurl/$row[url]\">$row[author]</a> $show_del</p>
        </td>
        <td class=\"blog_title\">
            <p>Date: $row[blog_date]</p>
        </td>
    </tr>
    <tr>
        <td colspan=\"2\" class=\"blog_title\">
            <p>Title: <b>$good_blog_title</b>&nbsp;&nbsp;<i>($public)</i></p>
        </td>
    </tr>
    <tr>
        <td colspan=\"2\" class=\"blog_entry\">
            <p>$message</p>
        </td>
    </tr><tr><td colspan=\"2\"><p><hr></p></td></tr>";

}

echo "<p align=\"center\"> --Page $pagenum of $last-- </p>";
?>

<p align="center">
<?
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page
//so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <img src=\"$imgdir/icons/$site_ico_set/home.gif\" border=\"0\" alt=\"Home\"></a></a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <img src=\"$imgdir/icons/$site_ico_set/back.gif\" border=\"0\" alt=\"Back\"></a></a> ";
}

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'><img src=\"$imgdir/icons/$site_ico_set/next.gif\" border=\"0\" alt=\"Next\"></a></a> ";
echo " ";

}
?>
</p>
<tr><td align="center" colspan="2"><p>Back to <a href="index.php">My Profile</a>.</p></td></tr>
</table>
Thanks,

Derek

Posted: Wed Jun 20, 2007 4:52 pm
by Weirdan
Look at the highlighted code, you have a syntax error there.

Pagination

Posted: Wed Jun 20, 2007 5:39 pm
by chexed
Perhaps you could look into pagination

Also, perhaps you could do it with switches

if you want to automate it, perhaps you could use a word count and store every 200 or so words in an array 2] and use some slick foreach implementation (with of course an echo, print, or return.

Re: Splitting data into pages

Posted: Thu Jun 21, 2007 11:16 am
by ghadacr
Have a look at this website it might help in your development.......

http://www.plus2net.com/php_tutorial/php_paging.php

Posted: Thu Jun 21, 2007 4:56 pm
by derek barnstorm
Okay, thanks very much for your replies, I'll look into everything.

Cheers,

Derek