PDA

View Full Version : pagination صفحه بندی اطلاعات



tehro0n
سه شنبه 30 شهریور 1389, 03:27 صبح
من می خوام با توجه به اطلاعات db که دارم آن ها را در صفحه های 10 تایی مرتب کنم.
هر چی گشتم چیز درست و حسابی پیدا نکردم.
چیزی مثل این لینک می خوام.. البته این کد ها نیز هم اشتباه داشت و هم (بعدی) (قبلی) رو نداشت که برای یک چنین چیزی به غیر از sort باید شماره صفحه رو هم داشته باشیم که بتونیم شماره صفحه بندی رو درست بچینیم!

http://www.9lessons.info/2009/09/pagination-with-jquery-mysql-and-php.html

sama01
سه شنبه 30 شهریور 1389, 11:58 صبح
1.
کد پیچیده‌ای نیست. با کمی تلاش می‌تونید یک کد صفحه‌بندی بسازید. حتی به صورت کلاس که بتوانید در هر جایی که خواستید استفاده کنید.

2.
در اینترنت پر از نمونه‌های آماده است. حتما به سایت phpclasses‌ سر بزنید. آن قدر مطالب در این زمینه دارد که یک شاخه‌ی مستقل به آن اختصاص داده است.

Reza1607
سه شنبه 30 شهریور 1389, 12:17 عصر
این ضمیمه رو بگیر شاید بتونی ازش استفاده کنی.

http://www.barnamenevis.org/forum/showpost.php?p=1030596&postcount=6

tehro0n
سه شنبه 30 شهریور 1389, 16:38 عصر
اگه با جستجو چیز به درد بخوری پیدا کرده بودم که الان کمک نمی خواستم، در همین مدل کلاس ها که دادید انقدر پوشه های زیاد و به درد نخور هست که آخرش هم کار نمی کنه که آدم خسته میشه..

من یک چنین چیزی می خواستم، اما در اسکریپتی که سورسش رو دادم pagination.php لود نمیشه و به همین دلیل مشکل داریم. لطفا اگه می تونید این سورسی که سر هم کردم را تصحیح نمایید.

ما در اینجا 2 اسکریپت داریم که یکی برای نمایش شماره های صفحه و دیگری چیزی مثل آژاکس.

style.css

#loading
{
width: 100%;
position: absolute;
}
li
{
list-style: none;
float: right;
margin-right: 16px;
padding:5px;
border:solid 1px #dddddd;
color:#0063DC;
}
li:hover
{
color:#FF0084;
cursor: pointer;
}jquery_pagination.js

$(document).ready(function(){

//Display Loading Image
function Display_Load()
{
$("#loading").fadeIn(900,0);
$(&quot;#loading&quot;).html(&quot;<img src='loading.gif' />&quot;);
}
//Hide Loading Image
function Hide_Load()
{
$(&quot;#loading&quot;).fadeOut('slow');
};


//Default Starting Page Results

$(&quot;#pagination li:first&quot;).css({'color' : '#FF0084'}).css({'border' : 'none'});

Display_Load();

$(&quot;C#‎‎ontent&quot;).load(&quot;pagination_data.php?sort =1&quot;, Hide_Load());


//Pagination Click
$(&quot;#pagination li&quot;).click(function(){

Display_Load();

//CSS Styles
$(&quot;#pagination li&quot;)
.css({'border' : 'solid #dddddd 1px'})
.css({'color' : '#0063DC'});

$(this)
.css({'color' : '#FF0084'})
.css({'border' : 'none'});

//Loading Data
var pageNum = this.id;

$(&quot;C#‎‎ontent&quot;).load(&quot;pagination_data.php?sort =&quot; + pageNum, Hide_Load());
});


});pagination.php

<?php
include_once(&quot;_config.php&quot;);
// Connects to your Database
$con = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS);

if ($con) {
mysql_select_db(MYSQL_DB, $con);
mysql_query(&quot;SET NAMES 'utf8'&quot;, $con);
} else {
printf(&quot;Connect failed: %s\n&quot;, mysql_error());
exit();
}

//This checks to see if there is a page number. If not, it will set it to page 1
if (isset($_GET['pagenum']))
$pagenum = $_GET['pagenum'];
else
$pagenum = 1;

//Calculating no of pages
$data = mysql_query(&quot;SELECT COUNT(*) as `count` FROM `hits`&quot;, $con);
$rows = mysql_fetch_row($data);


//This is the number of results displayed per page
$page_rows = 4;

//This tells us the page number of our last page
$last = ceil($rows[0]/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

?>

<link href=&quot;style.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;>
<script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js&quot;></script>
<script type=&quot;text/javascript&quot; src=&quot;jquery_pagination.js&quot;></script>

<div id=&quot;loading&quot;></div>
<div id=&quot;content&quot;></div>

<ul id=&quot;pagination&quot;>
<?php

// 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 &quot; <li id='1'><a href='{$_SERVER['PHP_SELF']}?pagenum=1'>>></a></li> &quot;;
$previous = $pagenum-1;
echo &quot; <li id='{$previous}'><a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'>></a></li> &quot;;
}


for ($i=1; $i <= $last; $i++)
echo &quot; <li id='{$i}'><a href='{$_SERVER['PHP_SELF']}?pagenum=$i'>$i</a></li> &quot;;


//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 &quot; <li id='{$next}'><a href='{$_SERVER['PHP_SELF']}?pagenum=$next'><</a></li> &quot;;
echo &quot; <li id='{$last}'><a href='{$_SERVER['PHP_SELF']}?pagenum=$last'><<</a></li> &quot;;
}
?>
</ul>pagination_data.php

<?php

include_once(&quot;_config.php&quot;);

$page_rows = 4;

if($_GET)
{
$sort=$_GET['sort'];
}

$con = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS);

if ($con) {
mysql_select_db(MYSQL_DB, $con);
mysql_query(&quot;SET NAMES 'utf8'&quot;, $con);
} else {
printf(&quot;Connect failed: %s\n&quot;, mysql_error());
exit();
}

//get table contents
$start = ($sort-1)*$page_rows;
$res = mysql_query (&quot;SELECT `id`, `time` FROM `hits` ORDER BY `id` LIMIT $start,$page_rows&quot;, $con);
?>

<table width=&quot;800px&quot;>

<?php
//Print the contents

while($row = mysql_fetch_object($res))
{

$time = $row->time;
$id = $row->id;

?>
<tr><td style=&quot;color:#B2b2b2; padding-left:4px&quot; width=&quot;30px&quot;><?php echo $id; ?></td><td><?php echo $time; ?></td></tr>
<?php
} //while
?>
</table>

binyaft
سه شنبه 30 شهریور 1389, 17:20 عصر
http://www.9lessons.info/2009/09/pagination-with-jquery-mysql-and-php.html

tehro0n
سه شنبه 30 شهریور 1389, 21:23 عصر
http://www.9lessons.info/2009/09/pagination-with-jquery-mysql-and-php.html

:لبخند:
پست اول رو یک نگاه بنداز
:لبخند:

funpatogh
سه شنبه 30 شهریور 1389, 22:06 عصر
صفحه بندی برات میکنه + لینک صفحه بعد و صفحه قبل هم دارد فقط آژاکس نیست که خودت باید تغییر بدهی


<?php
//----------------------------------------------------
$link=mysql_connect('localhost:3306','root','')or die(mysql_error());
mysql_select_db('test');
$table='table';
//------------------------------------------------------
if(!$_GET['Page']) $_GET['Page'] =1;
$tedad=10;
$start=($_GET['Page']-1)*$tedad;
$q=mysql_query("select * from `$table` ");
$count=mysql_num_rows($q);
$pages=ceil($count/$tedad);
$sql=mysql_query("select `title` from `$table` limit $start,$tedad");
while($rows=mysql_fetch_assoc($sql)){
echo $row['id']." - ".$rows['title']."<br>";
}

if($_GET['Page']<>1)
echo "<a style='text-decoration:none;' href=mypage.php?Page=".($_GET['Page']-1)."><</a>";
for($i=1;$i<=$pages;$i++){
if($_GET['Page']==$i){
$s="<b>{";
$e="}</b>";
}else
$s=$e=null;
echo "<a style='text-decoration:none;' href=mypage.php?Page=$i>$s $i $e </a>";
}

if($_GET['Page']<>$pages)
echo "<a style='text-decoration:none;' href=mypage.php?Page=".($_GET['Page']+1).">></a>";
?>

shahriyar3
سه شنبه 30 شهریور 1389, 22:30 عصر
<?php
$items = array("red","yellow",
"pink","green",
"purple","blue",
"orange","opal",
"ruby","brown",
"bronze","copper",
"ginger","tope",
"orange","light blue",
"light green","dark yellow",
"grey","black",
"white","gold",
"silver");

$thispage = $PHP_SELF ;
$num = count($items); // number of items in list
$per_page = 2; // Number of items to show per page
$showeachside = 5 // Number of items to show either side of selected page

if(empty($start))$start=0; // Current start position

$max_pages = ceil($num / $per_page); // Number of pages
$cur = ceil($start / $per_page)+1; // Current page number
?>
<style type="text/css">
<!--
.pageselected {
color: #FF0000;
font-weight: bold;
}
-->
</style>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="0" class="PHPBODY">
<tr>
<td width="99" align="center" valign="middle" bgcolor="#EAEAEA">
<?php
if(($start-$per_page) >= 0)
{
$next = $start-$per_page;
?>
<a href="<?php print("$thispage".($next>0?("?start=").$next:""));?>"><<</a>
<?php
}
?>
</td>
<td width="201" align="center" valign="middle" class="selected">
Page <?php print($cur);?> of <?php print($max_pages);?><br>
( <?php print($num);?> records )
</td>
<td width="100" align="center" valign="middle" bgcolor="#EAEAEA">
<?php
if($start+$per_page<$num)
{
?>
<a href="<?php print("$thispage?start=".max(0,$start+$per_page));?>">&gt;&gt;</a>
<?php
}
?>
</td>
</tr>
<tr><td colspan="3" align="center" valign="middle">&nbsp;</td></tr>
<tr>
<td colspan="3" align="center" valign="middle" class="selected">
<?php
$eitherside = ($showeachside * $per_page);
if($start+1 > $eitherside)print (" .... ");
$pg=1;
for($y=0;$y<$num;$y+=$per_page)
{
$class=($y==$start)?"pageselected":"";
if(($y > ($start - $eitherside)) && ($y < ($start + $eitherside)))
{
?>
&nbsp;<a class="<?php print($class);?>" href="<?php print("$thispage".($y>0?("?start=").$y:""));?>"><?php print($pg);?></a>&nbsp;
<?php
}
$pg++;
}
if(($start+$eitherside)<$num)print (" .... ");
?>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<?php
for($x=$start;$x<min($num,($start+$per_page)+1);$x++)print($items[$x]."<br>");
?>
</td>
</tr>
</table>