DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   display fix set of data in table.... (http://www.daniweb.com/forums/thread103327.html)

manish_gajjar Jan 3rd, 2008 2:01 am
display fix set of data in table....
 
Hi guys...Good morning...I have following code.
<?php
$pid=$_GET['pid'];
$conn=mysql_connect('localhost','root','') or die("Cannot connect to server");
mysql_select_db('developer',$conn) or die("Cannot connect to database");
$sql="select cnode,item,pnode from listviews where cnode='$pid'";
$res=mysql_query($sql) or die("Cannot execute query");
while($ar=mysql_fetch_array($res))
{
$itm=$ar[1];
$pid=$ar[0];
}
$sql="select cnode,item,pnode from listviews where pnode='$pid'";
$res=mysql_query($sql) or die("Cannot execute query");
echo "<table border='1'>";
if($pid==0)
echo "<th colspan=2>Parent Items</th>";
else
echo "<th colspan=2>$itm</th>";
while($ar=mysql_fetch_array($res))
{
echo "<tr><td>$ar[0]</td><td>$ar[1]</td>";
}
echo "</table>";
?>

Now i want to display only five rows per page...and using next button i want to see next five records...How it is possible to display it using PHP...This page is called by ajax script...Please show me the way...Thank you...

ryan_vietnow Jan 3rd, 2008 2:36 am
Re: display fix set of data in table....
 
http://www.php-mysql-tutorial.com/php-mysql-paging.php

nav33n Jan 3rd, 2008 2:56 am
Re: display fix set of data in table....
 
Simplest 'next-forward' script, just to get the idea on how it works. You will find much more efficient code on the net.
<?php
$offset=isset($_REQUEST['offset'])?$_REQUEST['offset']:0;
$conn=mysql_connect("localhost","root");
mysql_select_db("test");
$total=mysql_num_rows(mysql_query("select * from test"));
$query="select * from test limit $offset,5";
$result=mysql_query($query);
while($row=mysql_fetch_array($result,MYSQL_ASSOC)){
        print_r($row);
}       
if($offset==0){
        $next_offset=$offset+1;
        $prev_offset=0;
} elseif($offset == $total) {
        $next_offset=$total;
        $prev_offset=$offset-1;
} else {
        $next_offset=$offset+1;
        if($next_offset==$total){
                $next_offset=$total;
        }
        $prev_offset=$offset-1;
}
echo "<a href=test.php?offset=$prev_offset>Prev</a>";
echo "<a href=test.php?offset=$next_offset>Next</a>";
?>

Cheers,
Nav


All times are GMT -4. The time now is 12:25 am.

Forum system based on vBulletin Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
©2003 - 2010 DaniWeb® LLC