Pulling MySQL Data, printing and counting in PHP
Posted: Mon Feb 14, 2005 9:20 pm
I am attempting to write my own blog software, and I am at a component which is a little confusing to me, so I am wondering if maybe someone here can assist me in finishing up this little side project.
Here is my content page, which has built in pagnation. I know some of it is busted, just trying to see what direction I need to go in, or what I need to do to make this thing work.
Here is my content page, which has built in pagnation. I know some of it is busted, just trying to see what direction I need to go in, or what I need to do to make this thing work.
Code: Select all
<?PHP
include ("db.inc.php");
$limit = 20;
$query_count = "SELECT * FROM blog, blogcomments ORDER BY id DESC";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);
if(empty($page))
{
$page = 1;
};
$limitvalue = (($page * $limit) - $limit);
// $query = "SELECT blog.id, blog.title, blog.cat, blog.date, blogcomments.name, blogcomments.fid COUNT(blogcomments.fid) as commentstotal FROM blogcomments, blog GROUP BY blogcomments.name ORDER BY blog.id DESC LIMIT $limit, $limit";
$query = "SELECT COUNT(fid) AS commenttotal FROM blogcomments WHERE blogid=58";
$query2 = "SELECT * FROM blog ORDER BY id DESC LIMIT $limitvalue, $limit";
$result = mysql_query($query);
$result2 = mysql_query($query2);
print "$query<br />";
print $commenttotal;
print $query2;
if(mysql_num_rows($result) == 0 || mysql_num_rows($result2) == 0)
{
echo "Nothing to Display!";
exit();
};
while ($r = mysql_fetch_array($result,$result2))
{
extract($r);
print $r;
if ($cat == "private")
{
print "<p><a href="blogprivate.php?id=$id">$title</a><br />";
if ($leadin == "")
print "";
else
print "$leadin ї<a href="blogprivate.php?id=$id">More</a>]<br />";
print "<strong>Date:</strong> $date | <strong>View:</strong> Private | Comments ($commenttotal)</p>";
}
else if ($cat == "public")
{
print "<p><a href="blogpublic.php?id=$id">$title</a><br />";
if ($leadin == "")
print "";
else
print "$leadin ї<a href="blogpublic.php?id=$id">More</a>]<br />";
print "<strong>Date:</strong> $date | <strong>View:</strong> Public | Comments (0)</p>";
}
echo "<hr>";
if ($totalrows > 1) {echo "";};
};
if($page != 1)
{
$pageprev = $page - 1;
echo "<strong><a href='$PHP_SELF?page=$pageprev'><< PREV</a></strong> ";
}
else
{
echo "<strong>PREV</strong> ";
};
$numofpages = $totalrows / $limit;
for($i = 1; $i <= $numofpages; $i++)
{
if($i == $page)
{
echo "<strong>$i </strong>";
}
else
{
echo "<strong><a href='$PHP_SELF?page=$i'>$i</a></strong> ";
}
}
if(($totalrows % $limit) != 0)
{
if($i == $page)
{
echo "<strong>$i</strong> ";
}
else
{
echo "<strong><a href='$PHP_SELF?page=$i'>$i</a></strong> ";
}
}
if(($totalrows - ($limit * $page)) > 0)
{
$pagenext = $page + 1;
echo "<strong><a href='$PHP_SELF?page=$pagenext'>NEXT >></a></strong>";
}
else
{
echo " <strong>NEXT</strong>";
};
$page = $_GETї"page"];
mysql_free_result($result);
?>