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
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();
?>

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.