...

View Full Version : PHP Pagination not showing CSS styling



bucket
03-20-2011, 04:04 PM
Hey guys, I have a PHP pagination script, but for some reason the while looping part that shows each row doesnt show the css styling I have.

Here is my code.




<?php


$tableName="news";
$targetpage = "archive.php";
$limit = 10;

$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];

$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}

// Get page data
$query1 = "SELECT * FROM $tableName LIMIT $start, $limit";
$result = mysql_query($query1);

// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;


$paginate = '';
if($lastpage > 1)
{




$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
}else{
$paginate.= ""; }



// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
}

// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}

$paginate.= "</div>";


}
//echo $total_pages.' Results';
// pagination
echo $paginate;
?>
<div class="newsBorder">
<table cellspacing="0" border="0" id="newsList" class="width100">
<tbody>
<tr class="row_a">
<td width="30%" class="1 first" id="titleCat">
<b>Category</b>
</td>
<td width="50%" class="item" id="titleTitle">
<b>News Item</b>
</td>
<td width="20%" align="right" class="date">
<b>Date</b>
</td>
</tr>
</tbody>

</table>




<?php


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


?>
<tr class="pad">

<td valign="top">
<a href="list.ws?cat=3&amp;page=1" class="catLink">
<img alt="" src="http://www.runescape.com/img/news/mini_customer_support.gif">
<span>Website</span>
</a>
</td>

<td valign='middle'><a href="news.php?newsid=<?php echo $row['newsid'] ?>" class="titleLink"><?php echo $row['title']; ?> </a></td>

<td valign="middle" align="right" class="date"><?php echo $row['dtime']; ?></td></tr><br>
<?php
}
?>

</div>


Here is how it looks right now:
http://i.imgur.com/nuo0q.png

Here is how I want it to look like.
http://i.imgur.com/adLhv.png

Its basically everything inside the while is not formatting.

I have all the CSS I need so that isnt the problem.

Thanks for the help ahead of time.

djm0219
03-20-2011, 04:30 PM
Do you have a link to the page available? Have you looked at the source for the page? Have you run the page through the validator to see if there are any errors that might be contributing to the problem?

At a glance you are closing your table before you start the while which is likely resulting in the complete lack of formatting.

bucket
03-20-2011, 04:41 PM
Thanks mate, I moved my </table> and </tbody> and it worked.

I am getting one last error:


Notice: Undefined index: page in C:\Program Files\EasyPHP5.2.10\www\yanille\archive.php on line 137

By any chance can you help me out?

Its reffering to this page.


$page = mysql_escape_string($_GET['page']);

The thing is, when I first load the page, I load it without the ?page=1

So I want it to automalicaly know its page 1 without the ?page=1

djm0219
03-20-2011, 04:48 PM
Any time you are going to use a form variable, either POST or GET, check to see if it is set before blindly retrieving or using it.



$page = '';
if (isset($_GET['page'])) {
$page = mysql_escape_string($_GET['page']);
}

What you default $page to, blank or something else, depends on whether or not the rest of your code depends on it and how it will behave if it is an empty string as I showed above.

bucket
03-20-2011, 04:59 PM
Works great. Thanks.

bucket
03-20-2011, 05:01 PM
Also, is there another way to ouput the date in a different format, so it doesnt show the time is was submitted, just the date. like a d - y -m. What is the part that shows the time called?

djm0219
03-20-2011, 05:09 PM
The PHP date function (http://us2.php.net/manual/en/function.date.php) provides options to format dates and times in just about any fashion desired.

bucket
03-20-2011, 05:55 PM
Thing is, how would i make it echo from the database a certain way? How would I query that so it would reformat it.


EDIT --------------
would something like this work?


date('m-d-Y', $row['dtime']);

So instead of showing it like this:
2011-03-19 14:48:43

It would show as:
15-Mar-2011

djm0219
03-20-2011, 06:04 PM
What type of column is dtime and what does it contain? The date function expects a Unix epoch time.

bucket
03-20-2011, 06:05 PM
dtime is a regular column, but when I inserted the date into it I used the script NOW()

And it formatted it like: 2011-03-19 14:48:43

djm0219
03-20-2011, 06:23 PM
Then you'll need to use the strtotime function to convert it before you use date to get it in the format desired.


$date_to_print = date('m-d-Y', strtotime($row['dtime']));

bucket
03-20-2011, 09:21 PM
Okay thanks, also is there any way I can abreviate the month word to like 3 letters?
Like MARCH would be MAR or june would be JUN.

tangoforce
03-20-2011, 09:31 PM
Yes use M instead.

Click here for more options. (http://uk.php.net/manual/en/function.date.php)

djm0219
03-20-2011, 09:32 PM
Upper case M gets you the 3 character month abbreviation. From the manual for the date function:


M A short textual representation of a month, three letters Jan through Dec



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum