Jump to content

paging database and fetch


simzam

Recommended Posts

Guys i badly need need to add paging  in this .

20 records per page how to accomplish ?

 

<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);

$http = 'myurl.com/other.php?like=' ;
$conn=odbc_connect('apple','','');
if (!$conn)
  {exit("Connection Failed: " . $conn);}
$sql="SELECT * FROM mytable ";
$rs=odbc_exec($conn,$sql);
if (!$rs)
  {exit("Error in SQL");}
echo "<table border= 1><tr>";
echo "<th>ID</th>";
echo "<th>Like</th>";
echo "<th>title</th></tr>";
while (odbc_fetch_row($rs))
  {
  $ids=odbc_result($rs,"ID");
// $links=odbc_result($rs,"links");
  $title=odbc_result($rs,"title");
  echo "<tr><td>$ids</td>";
  echo "<td><fb:like href=\"{$http}{$ids}\" layout=\"button_count\"  show_faces=\"false\" width=\"100\" font=\"tahoma\" colorscheme=\"dark\"></fb:like> </td>";
  echo "<td>$title</td></tr>";
  }
echo "</table>";
odbc_close($conn);
?>

Link to comment
https://forums.phpfreaks.com/topic/222009-paging-database-and-fetch/
Share on other sites

I tried but fails !

 

<?php
//Include the PS_Pagination class
include('ps_pagination.php');
//Connect to mysql db
$conn = odbc_connect('apple', '', '');

$sql = 'select title from mytable';
//Create a PS_Pagination object
$pager = new PS_Pagination($conn, $sql, 8, 3, 'param1=valu1&param2=value2');
//The paginate() function returns a mysql
//result set for the current page
$rs = $pager->paginate();
//Loop through the result set
while($row = odbc_fetch_row($rs)) {
	echo $row['title'];
}
//Display the navigation
echo $pager->renderFullNav();
?>

I'm close to finishing

 

The problem I am having is when the user clicks on one of the navigation links (i.e. First, Next, Prev, Last), the records do not display -- it remains on the first 10 records.  However, the page number URL variable do change appropriately.

 

 

<?php
// how many rows to show per page
$introwsPerPage = 10;

// by default show first page
$intpageNum = 1;

// if $_GET['intpage'] defined, use it as page number
if(isset($_GET['intpage']))
{
    $intpageNum = $_GET['intpage'];
}

// counting the offset
$offset = ($intpageNum - 1) * $introwsPerPage;

    //connect to a DSN ('Name of DSN', 'UserName', 'Password')
    //odbc_connect: Connect to a datasource
    $conn = odbc_connect('apple','','')
        or die('Unable to connect to database');

        //the SQL statement that will query the database
           $qryCandidate =
             "SELECT TOP 10 id, title FROM mytable";


        //perform the query
        //odbc_exec: Prepare and execute a SQL statement
          $qryresult=odbc_exec($conn, $qryCandidate);

        //retrieve the data from the database
        //odbc_fetch_row: Fetch a row
          while(@odbc_fetch_row($qryresult))
          {

            //odbc_result: Get result data
          $txtLastName = odbc_result($qryresult, "id");
          $txtFirstName = odbc_result($qryresult, "title");

            //Display Results
            echo ("<tr bgcolor=\"$color\">");
            echo ("<td height=\"10\">$txtLastName, $txtFirstName</td>");
            echo ("<td><a href=\"admin.php?Candidate=$Candidate \"><div align=\"center\">[Edit]</div></a></td>");

            echo ("</tr>");
            }

// how many rows we have in database
$querycount   = "SELECT COUNT(id) AS intnumrows FROM ";
$result  = odbc_exec($conn, $querycount);
$row = odbc_fetch_array($result);
$intnumrows = $row['intnumrows'];

// how many pages we have when using paging?
$intmaxPage = ceil($intnumrows/$introwsPerPage);

// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($intpage = 1; $intpage <= $intmaxPage; $intpage++)
{
    if ($page == $intpageNum)
    {
        $nav .= " $intpage ";   // no need to create a link to current page
    }
    else
    {
        $nav .= " <a href=\"$self?intpage=$intpage\">$page</a> ";
    }
}

// creating previous and next link
// plus the link to go straight to
// the first and last page

if ($intpageNum > 1)
{
    $intpage = $intpageNum - 1;
    $prev = " <a href=\"$self?intpage=$intpage\">[Prev]</a>  ";

    $first = " <a href=\"$self?intpage=1\">[First Page]</a>  ";
}
else
{
    $prev  = ' '; // we're on page one, don't print previous link
    $first = ' '; // nor the first page link
}

if ($intpageNum < $intmaxPage)
{
    $intpage = $intpageNum + 1;
    $next = " <a href=\"$self?intpage=$intpage\">[Next]</a>  ";

    $last = " <a href=\"$self?intpage=$intmaxPage\">[Last Page]</a>  ";
}
else
{
    $next = ' '; // we're on the last page, don't print next link
    $last = ' '; // nor the last page link
}

            //Display Navigation
            echo ("<tr>");
            echo ("<td colspan=\"5\" align=\"center\">");
            echo ("<br/><br/>$first $prev $next $last");
            echo ("<br/><br/>");
            echo ("<center>");
            echo ("<A HREF=\"page1.php\">Return to HomePage</a>");
            echo ("</center>");
            echo ("</td>");
            echo ("</tr>");
            echo ("</table>");

            //disconnect the db
            odbc_close($conn);
?>

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.