Jump to content

paging my site problem


vin_akleh

Recommended Posts

<?php
$pg=$_GET["pg"]-1;
if ($pg<0)$pg=0;
$pg=$pg*15;
$maxurl=mysql_query("select * from mawdoatadabia order by id desc limit ".$pg.",15");
	echo '<span class="forms">';
	echo '<br><table class="sample" style="width:95%;"><tbody><tr><th style="width:70%;">العنوان</th><th style="width:20%;">التاريخ</th></tr>';
	while ($result=mysql_fetch_array($maxurl))
	{
		echo '<tr><td>';
		echo '<a href="'.'index.php?page=new_adabia&'."maktoba_id=".''.$result["id"].'" style="font-size:130%;color:blue">'.$result["mawdoatadabia_title"].'</a>';
		echo '</td><td>';
		echo date("d-m-Y",$result[date]);
		echo '</td></tr>';
	}//end while
		echo '</tbody></table>';
	$count=mysql_fetch_array(mysql_query("select count(*) from mawdoatadabia"));
		$max_page=ceil($count['0']/15);
		if($max_page<1){ 
		$max_page=1;}

		$page_counter='';
		for($i=1;$i<=$max_page;$i++)
		{
		if ($i==$pg){
		$page_counter.='<a href="'."index.php?page=mawdoatadabia".'&pg='.$i.'" style="padding:7px;background-color:#f1f1f1;"><b>'.$i.'</b></a>';
		}else{
		$page_counter.='<a href="'."index.php?page=mawdoatadabia".'&pg='.$i.'" style="padding:7px;">'.$i.'</a>';
		}
		}
		echo '<br><span class="pad">...'.$page_counter.'...</span><br>';
	echo'</span>
	<br>';
?>

 

why this part of my code doesn't ever work

if ($i==$pg){
		$page_counter.='<a href="'."index.php?page=mawdoatadabia".'&pg='.$i.'" style="padding:7px;background-color:#f1f1f1;"><b>'.$i.'</b></a>';

i want to mark the current page that is being viewed??!!

need help please

Link to comment
https://forums.phpfreaks.com/topic/205315-paging-my-site-problem/
Share on other sites

http://www.phpeasystep.com/phptu/29.html

 

the best pagnation tutorial i ever found, can be used with about 3 simple edits.  i use it on my site too

 

i tried this code and it worked fine but when i try to apply it on my site i am having some problems !!! :(

 

<?php
/*
	Place code to connect to your DB here.
*/
include('connection.php');	// include your code to connect to DB.
$tbl_name="mawdoatseyaseya";		//your table name
// How many adjacent pages should be shown on each side?
$adjacents = 3;

/* 
   First get total number of rows in data table. 
   If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

/* Setup vars for query. */
$targetpage = "index.php?page=mawdoatseyaseya&"; 	//your file name  (the name of this file)
$limit = 15; 								//how many items to show per page
$page_num = $_GET['page_num'];
if($page_num) 
	$start = ($page_num - 1) * $limit; 			//first item to display on this page
else
	$start = 0;								//if no page var is given, set start to 0

/* Get data. */
$sql = "SELECT * FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);

/* Setup page vars for display. */
if ($page_num == 0) $page_num = 1;					//if no page var is given, default to 1.
$prev = $page_num - 1;							//previous page is page - 1
$next = $page_num + 1;							//next page is page + 1
$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1;						//last page minus 1

/* 
	Now we apply our rules and draw the pagination object. 
	We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{	
	$pagination .= "<div class=\"pagination\">";
	//previous button
	if ($page_num > 1) 
		$pagination.= "<a href=\"$targetpage?page_num=$prev\">« السابق</a>";
	else
		$pagination.= "<span class=\"disabled\">«  السابق</span>";	

	//pages	
	if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
	{	
		for ($counter = 1; $counter <= $lastpage; $counter++)
		{
			if ($counter == $page_num)
				$pagination.= "<span class=\"current\">$counter</span>";
			else
				$pagination.= "<a href=\"$targetpage?page_num=$counter\">$counter</a>";					
		}
	}
	elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
	{
		//close to beginning; only hide later pages
		if($page_num < 1 + ($adjacents * 2))		
		{
			for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
			{
				if ($counter == $page_num)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"$targetpage?page_num=$counter\">$counter</a>";					
			}
			$pagination.= "...";
			$pagination.= "<a href=\"$targetpage?page_num=$lpm1\">$lpm1</a>";
			$pagination.= "<a href=\"$targetpage?page_num=$lastpage\">$lastpage</a>";		
		}
		//in middle; hide some front and some back
		elseif($lastpage - ($adjacents * 2) > $page_num && $page_num > ($adjacents * 2))
		{
			$pagination.= "<a href=\"$targetpage?page_num=1\">1</a>";
			$pagination.= "<a href=\"$targetpage?page_num=2\">2</a>";
			$pagination.= "...";
			for ($counter = $page_num - $adjacents; $counter <= $page_num + $adjacents; $counter++)
			{
				if ($counter == $page_num)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"$targetpage?page_num=$counter\">$counter</a>";					
			}
			$pagination.= "...";
			$pagination.= "<a href=\"$targetpage?page_num=$lpm1\">$lpm1</a>";
			$pagination.= "<a href=\"$targetpage?page_num=$lastpage\">$lastpage</a>";		
		}
		//close to end; only hide early pages
		else
		{
			$pagination.= "<a href=\"$targetpage?page_num=1\">1</a>";
			$pagination.= "<a href=\"$targetpage?page_num=2\">2</a>";
			$pagination.= "...";
			for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
			{
				if ($counter == $page_num)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"$targetpage?page_num=$counter\">$counter</a>";					
			}
		}
	}

	//next button
	if ($page_num < $counter - 1) 
		$pagination.= "<a href=\"$targetpage?page_num=$next\">التالي »</a>";
	else
		$pagination.= "<span class=\"disabled\">التالي »</span>";
	$pagination.= "</div>\n";		
}
?>

<?php
	echo '<br><table class="sample" style="width:95%;"><tbody><tr><th style="width:70%;">العنوان</th><th style="width:20%;">التاريخ</th></tr>';
	while($row = mysql_fetch_array($result))
	{

		echo '<tr><td>';
		echo '<a href="'.$row["mawdoatseyaseya"].'"style="font-size:130%;color:blue">'.$row["mawdoatseyaseya_title"].' &nbsp(<label style="font-size:10px;">'.(round(filesize($row["mawdoatseyaseya"])/(1024*1024),2)).'م.ب</label>)</a>';
		echo '</td><td>';
		echo date("d-m-Y",$row[date]);
		echo '</td></tr>';
	}
	echo '</tbody></table><br><span class="center2">';
	echo $pagination;
	echo '</span>';
?>

<?php=$pagination?>

 

only the first page shows up, when i try to go to the next page it changes the pag_num from 1 to 2 but the displayed data is the same from the pag_num=1

i think the problem is here

	$targetpage = "index.php?page=mawdoatseyaseya&"; 	//your file name  (the name of this file)

but i cant figure it out

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.