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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Next / Prev

Status
Not open for further replies.

paulsim

Programmer
Dec 17, 2001
3
NZ
I have a table of people with about 30 columns 1700 rows and a primary key. The names are not in order alphabetically. I search for one person by name. I then want to have a Next and a Prev button to click on to get the next or previous person alphabetically by name.
I am using PHP and MySQL.
Any suggestions please?

 
I always use this "little" piece of PHP code:


<?PHP
$PAGE = (isset($page)) ? $page : 1;
//We hope that page, not PAGE is in the querystring,
//if not we set it here

$dc = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname,$dc);

$QUERY = &quot;SELECT * FROM table&quot;;
// You MUST change this :)!
$QRETURN= @mysql_query($QUERY);
$QNUM = @mysql_num_rows($QRETURN);

$DISPPAGE = 15;
// DISPPAGE is the number of items displayed per page.

$NUMPAGES = ceil($QNUM / $DISPPAGE);
//NUMPAGES to show how many pages you WILL get.

if($QRETURN):
//Sanity check on query, only runs if valid query.

if($QNUM > 0): //We actually got some rows.

echo &quot;<div align=\&quot;right\&quot;>\n<font face=\&quot;Tahoma, Geneva, sans-serif\&quot; size=\&quot;1\&quot;>\n&quot;;

for($i = 1; $i <= $NUMPAGES; $i++):
//loop to print << 1 2 3... $NUMPAGES >>

if($i == 1 && $PAGE > 1) //Prints the << first to goto the previous page (not on page 1)

echo &quot;<a href=\&quot;$PHP_SELF?page=&quot;.($PAGE - 1).&quot;\&quot; onMouseOver=\&quot;status='Go to the Previous Page';return true;\&quot; onMouseOut=\&quot;status=' ';return true;\&quot;>&#0171;&nbsp;</a>&quot;;

if($i == $PAGE) //#Doesn't print a link itself, just prints page number
echo &quot;<font color=\&quot;#ff3333\&quot;>&nbsp;$i&nbsp;</font>&quot;;

if($i != $PAGE) //Other links that aren't this page go here
echo &quot;<a href=\&quot;$PHP_SELF?page=$i\&quot; onMouseOver=\&quot;status='Go to Page $i';return true;\&quot; onMouseOut=\&quot;status=' ';return true;\&quot;>&nbsp;$i&nbsp;</a>&quot;;

if($i == $NUMPAGES && $PAGE != $NUMPAGES)
//Link for next page >> (not on last page)
echo &quot;<a href=\&quot;$PHP_SELF?page=&quot;.($PAGE + 1).&quot;\&quot; onMouseOver=\&quot;status='Go to the Next Page';return true;\&quot; onMouseOut=\&quot;status=' ';return true;\&quot;>&nbsp;&#0187</a>&quot;;

endfor;

echo &quot;</font>\n</div>\n&quot;;
echo &quot;<font face=\&quot;Verdana, Arial, Helvetica, sans-serif\&quot; size=\&quot;2\&quot;>\n&quot;;

$START = ($PAGE - 1) * $DISPPAGE;

mysql_data_seek($QRETURN,$START);
//Moves the pointer to right row

//This loop will go until you a) reach $DISPPAGE or b) there aren't anymore entries

for($i = 1; $i <= $DISPPAGE && $ARET = @mysql_fetch_array($QRETURN); $i++):

$VAR = $ARET[&quot;col&quot;];

//Here's the bit you should update!

echo &quot;$VAR\n<br><br>\n\n&quot;; //echoes the field...

endfor;

echo &quot;</font>\n&quot;;
endif;

if($QNUM == 0) //if we get no rows
echo &quot;<div align=\&quot;center\&quot;>The database is empty</div>\n&quot;;
endif;

if(!$QRETURN): //Bogus Query, or mysqld isn't running...
echo &quot;<div align=\&quot;center\&quot;>\n&quot;;
echo &quot;<font face=\&quot;Arial, Verdana, Helvetica, sans-serif\&quot; color=\&quot;#ff3333\&quot; size=\&quot;3\&quot;>\n&quot;;
echo &quot;Either the database is down, or the query was invalid\n&quot;;
echo &quot;</font>\n</div>\n&quot;;
endif;

?>

Just do what is said in the comment lines, and you'll be fine.

Steijssen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top