I see tutorials all over the web with pagination and MySQl but not much out there (that is updated using PHP 5 without global variables) on doing this using ODBC connection with MSSQL.
This is what I found online but I am confused.
[PHP]<?php
//connecting to the database using ODBC
$db=odbc_connec t($dsn,$user,$p ass)or die("Error: Can't Connect to Database");
// assigns a value how many rows we will get from each page
$limit = 15;
/* assign variable with sql query of counting how many rows are there in the database.
take note you should assign * with a table data name and table_name with table name*/
$query_count = "SELECT COUNT(*) as value FROM table_name";
//executes the sql statement to the database
$result_count = odbc_exec($db, $query_count);
// get the result count from the database
$totalrows = odbc_fetch_obje ct($result_coun t);
if(empty($page) ){ // Checks if the $page variable is empty (not set)
$page = 1; // If it is empty, we're on page 1
}
//this will be the lowest limit
$limitvalue = $page * $limit - ($limit);
$limitnew = $limitvalue + $limit;
// Ex: (2 * 25) - 25 = 25 <- data starts at 25
$query = "SELECT * FROM table_name where ID between $limitvalue and $limitnew ORDER BY ID ASC";[/PHP]
I think this is where I get stuck. is the ID suppose to be the "primary key"?
PHP Code:
[PHP]//executes the sql statement to the database
$result = odbc_exec($db, $query);
echo "<table><tr><td >Title1</td>
<td>Title2</td>";
// open loop to list down contents from the database
while (($row = odbc_fetch_obje ct($result))) {
echo "<tr><td>$r ow->content_title1 </td>
<td>$row->content_title2 </td>";
//close the loop
}
//now we close the table
echo "</table>";
/*check if we are at page 1. if true, PREV will not be a link. if false, $page will be a link minus 1 page. */
if($page != 1){
$pageprev = $page -1;
echo(" <b> <a href=" . $_SERVER['PHP_SELF'] ."?page=$pagepr ev>PREV</a></b> ");
}else{
echo(" PRE V ");
}
/* $numofpages will be the quotient of the total rows divided by the limit value
in this example lets assume that we have 47 rows in the database divided by 15 will result 3.13 */
$numofpages = $totalrows->value / $limit;
/* now let us generate those page numbers*/
for($i = 1; $i <= $numofpages; ++$i){
if($i == $page){
echo(" [$i] ");
}else{
echo(" <b> <a href=" . $_SERVER['PHP_SELF'] ."?page=$i>$ i</a></b> ");
}
}
/* check if we have a remainder and print it at the end page list */
if(($totalrows->increm % $limit) != 0){
if($i == $page){
echo(" [$i] ");
}else{
echo(" <b> <a href=" . $_SERVER['PHP_SELF'] ."?page=$i>$ i</a></b> ");
}
}
/* finally we check the "next" link if we have a page to move on to, if yes we put it as a link adding another page.
in this example assuming that we have $totalrows->value=47
this will check if (47 - (15 * 1) = 32) > zero, results to true will give a link.*/
if(($totalrows->value - ($limit * $page)) > 0){
$pagenext = $page +1;
echo(" <b> <a href=" . $_SERVER['PHP_SELF'] ."?page=$pagene xt>NEXT</a></b> ");
}else{
echo(" NEX T ");
}
// now let us free the resources
odbc_free_resul t($result);
?>[/PHP]
When this is executed I do see the paging links (but they don't work) and there is no data. How does this script look?
This is what I found online but I am confused.
[PHP]<?php
//connecting to the database using ODBC
$db=odbc_connec t($dsn,$user,$p ass)or die("Error: Can't Connect to Database");
// assigns a value how many rows we will get from each page
$limit = 15;
/* assign variable with sql query of counting how many rows are there in the database.
take note you should assign * with a table data name and table_name with table name*/
$query_count = "SELECT COUNT(*) as value FROM table_name";
//executes the sql statement to the database
$result_count = odbc_exec($db, $query_count);
// get the result count from the database
$totalrows = odbc_fetch_obje ct($result_coun t);
if(empty($page) ){ // Checks if the $page variable is empty (not set)
$page = 1; // If it is empty, we're on page 1
}
//this will be the lowest limit
$limitvalue = $page * $limit - ($limit);
$limitnew = $limitvalue + $limit;
// Ex: (2 * 25) - 25 = 25 <- data starts at 25
$query = "SELECT * FROM table_name where ID between $limitvalue and $limitnew ORDER BY ID ASC";[/PHP]
I think this is where I get stuck. is the ID suppose to be the "primary key"?
PHP Code:
[PHP]//executes the sql statement to the database
$result = odbc_exec($db, $query);
echo "<table><tr><td >Title1</td>
<td>Title2</td>";
// open loop to list down contents from the database
while (($row = odbc_fetch_obje ct($result))) {
echo "<tr><td>$r ow->content_title1 </td>
<td>$row->content_title2 </td>";
//close the loop
}
//now we close the table
echo "</table>";
/*check if we are at page 1. if true, PREV will not be a link. if false, $page will be a link minus 1 page. */
if($page != 1){
$pageprev = $page -1;
echo(" <b> <a href=" . $_SERVER['PHP_SELF'] ."?page=$pagepr ev>PREV</a></b> ");
}else{
echo(" PRE V ");
}
/* $numofpages will be the quotient of the total rows divided by the limit value
in this example lets assume that we have 47 rows in the database divided by 15 will result 3.13 */
$numofpages = $totalrows->value / $limit;
/* now let us generate those page numbers*/
for($i = 1; $i <= $numofpages; ++$i){
if($i == $page){
echo(" [$i] ");
}else{
echo(" <b> <a href=" . $_SERVER['PHP_SELF'] ."?page=$i>$ i</a></b> ");
}
}
/* check if we have a remainder and print it at the end page list */
if(($totalrows->increm % $limit) != 0){
if($i == $page){
echo(" [$i] ");
}else{
echo(" <b> <a href=" . $_SERVER['PHP_SELF'] ."?page=$i>$ i</a></b> ");
}
}
/* finally we check the "next" link if we have a page to move on to, if yes we put it as a link adding another page.
in this example assuming that we have $totalrows->value=47
this will check if (47 - (15 * 1) = 32) > zero, results to true will give a link.*/
if(($totalrows->value - ($limit * $page)) > 0){
$pagenext = $page +1;
echo(" <b> <a href=" . $_SERVER['PHP_SELF'] ."?page=$pagene xt>NEXT</a></b> ");
}else{
echo(" NEX T ");
}
// now let us free the resources
odbc_free_resul t($result);
?>[/PHP]
When this is executed I do see the paging links (but they don't work) and there is no data. How does this script look?
Comment