Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Previous and Next pages.

Status
Not open for further replies.

tyktok

Programmer
Jun 25, 2002
3
GB
I have found this code on the web to help me add prev/next pages when searching a database. I have had a look through the code, and I can understand what it does and how it does it, but there is no section where it actually connects to the database. Please could you take a look and let me know, cheers.

<?php

// set this to the number of results you wish on each page
$limit=5;
$offset = 0;
// if no offset has been passed, offset should be 0
if (!$offset) {
$offset=0;
}

?>
<?php

function pagenav() {
global $limit,$offset,$numpage,$where;
if ($where) {
$safewhere=urlencode($where);
}
echo &quot;
<TABLE CELLPADDING=0 BORDER=0 CELLSPACING=5 WIDTH=100>
<TR>
<TD ALIGN=RIGHT>&quot;;

if ($offset>=$limit) {
$newoff=$offset-$limit;

echo &quot;<A HREF=\&quot;$PHP_SELF?offset=$newoff&where=$safewhere\&quot;>
<-- PREV</A>
</TD>&quot;;
} else {
echo &quot;<-- PREV&quot;;
}

echo &quot;<TD ALIGN=CENTER>   &quot;;

for ($i=1;$i<=$numpage;$i++) {
if ((($i-1)*$limit)==$offset) {
print &quot;$i &quot;;
} else {
$newoff=($i-1)*$limit;

echo &quot;<A HREF=\&quot;$PHP_SELF?offset=$newoff&where=$safewhere\&quot;>
$i</A> &quot;;
}
}
echo &quot;  </TD>
<TD ALIGN=LEFT>&quot;;
if ($offset!=$limit*($numpage-1)) {
$newoff=$offset+$limit;
echo &quot;<A HREF=\&quot;$PHP_SELF?offset=$newoff&where=$safewhere\&quot;>
NEXT--></A>
</TD>&quot;;
}else{
echo &quot;NEXT--></TD>&quot;;
}
echo &quot;</TR>
</TABLE>&quot;;

} // END FUNCTION

// set this to the number of results you wish on each page
$limit=5;

// if no offset has been passed, offset should be 0
if (!$offset) $offset=0;

//$query_where=&quot;where one='$data[0]' AND two='$data[1]'&quot;;

$result=mysql_query(&quot;select count(*) from rochdale where price >'0' and price <'200000'&quot;);

list($numrec)=mysql_fetch_row($result);

#calc num pages
$numpage=intval($numrec/$limit);

if ($numrec%$limit) {
$numpage++; // add one page if remainder
}

$result=mysql_query (&quot;select * from rochdale where price >'0' and price <'200000' limit $offset,$limit&quot;);

//<!-- HTML headers and other non-relevent stuff -->

if ($numpage>1) {
pagenav();
print &quot;<P>&quot;;
}
//<!-- result display loop -->
if ($numpage>1) {
pagenav();
print &quot;<P>&quot;;
}

?>
 
You need to open a connection to a database,
Below s an example of a connection to a MYSQL database, ( I take it you using MYsql fromt the mysql_query

$testconn= mysql_connect(&quot;localhost&quot;,&quot;user&quot;,&quot;pass&quot;);
mysql_select_db(&quot;database&quot;,$testconn)

It needs to be in place before you can access your database.

Helpful links on the subject..
mysql_connect() establishes a connection to a MySQL server.

mysql_select_db() sets the current active database on the server.
'Does my thumb look big in this?'
homebut.gif
elshtroll
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top