go to previous,next records.. | Newbie | | Join Date: Jan 2009
Posts: 16
| | |
in my project, a table called 'hardware' contains merely 5000 records..its a mysql database.. the user can add, modify or can view this hardware details one by one.. while clicking 'view' button, the user is redirected to 'hardware_view.php' page and can view the hardware..
my question is.. while the hardware_view.php is loaded, it has to retrieve the first record from the db and has to display the values.. this page contains previous, next buttons.. while clicking next button, it has to display the next record and for previous button also the same... how to do this?.(while the page is loading itself, the records has to be displayed.. and according the next,previous buttons it has to move to corresponding records...)
|  | Needs Regular Fix | | Join Date: Jul 2007 Location: India
Posts: 407
| | | re: go to previous,next records..
At the present when user clicks views button 'hardware_view.php' must have the current ID/particular ID regarding to the item from the Db to display its details .give the link to the previous button saying (current ID-1) and next button (current ID+1) .This must help u with ur query .
| | Newbie | | Join Date: Jan 2009
Posts: 16
| | | re: go to previous,next records..
i dont have any ID.. because, if i delete any record from table, for example say 500, tat ID wont exist in the table.... so in table records like, 1,2..,499 then 501...etc will be available.. so i cant just increment or decrement to view the record.. so i dint have tat field..
|  | Needs Regular Fix | | Join Date: Jul 2007 Location: India
Posts: 407
| | | re: go to previous,next records.. Quote:
Originally Posted by sweetline priya i dont have any ID.. because, if i delete any record from table, for example say 500, tat ID wont exist in the table.... so in table records like, 1,2..,499 then 501...etc will be available.. so i cant just increment or decrement to view the record.. so i dint have tat field.. oh ok!!! u can always say in your mysql query at the end that limit 3 order by ID i guess....so that u get 3 available ID's and update the previous and next button.Try this ..
| | Newbie | | Join Date: Jan 2009
Posts: 16
| | | re: go to previous,next records..
ok sure... thank u for ur immediate reply..
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,752
| | | re: go to previous,next records..
For this particular purpose, you would want to use the LIMIT clause, pradeepjain is right.
You would simply pass the position of the item you want displayed as a GET or SESSION value, and them fetch it by adding "LIMIT #position, 1" to your SQL query.
But you should always try to include an ID clause in your tables, as a "row counter" of sorts. A field which uniquely identifies each row, even if there are gaps in the count.
If may be (and probably will be) useful later on.
| | Newbie | | Join Date: Jan 2009
Posts: 16
| | | re: go to previous,next records..
i ve used 'mysql_data_seek' function to retrieve the record from table... this is too functioning properly..
i ve another problem.. ie, i am dynamically creating a table and textboxes to display the values of each record..
during the page_load, first record should be displayed.. for that i ve written a code that should be executed only when the page is loaded for the first time(not executed during the button click)... during the page_load, i am checking if($n==0), if this condition is satisfied, this code is executed.. after displaying the values during the first page_load, i make the value $n=1, thus, the code within the condition($n==0) will not be executed again...
and i ve seperately written code(ie dynamic creation of tables) for 'next', 'prev' buttons.. this code within each post value of these buttons are executed accordingly.. my doubt is whether this is a proper way, is this a good coding... can anybody suggest and guide me???
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 914
| | | re: go to previous,next records..
For your checking if code is executed you could look into the include_once() function ( http://au2.php.net/include_once ) or start storing $n as a session variable: $_SESSION['n'] so you can change it and it is available for that session.
For your next/prev buttons, you can either do something with the $_SESSION or use a $_GET variable. Usually for pagination you use GET variables.
Here is a good pagination link: http://www.tonymarston.net/php-mysql/pagination.html
I hope this helps.
| | Newbie | | Join Date: Jan 2009
Posts: 16
| | | re: go to previous,next records..
thank you for ur suggestions :)
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 914
| | | re: go to previous,next records..
No problem. Let us know how you go.
| | Newbie | | Join Date: Jan 2009
Posts: 16
| | | re: go to previous,next records..
@TheServant..
u suggested to prefer 'pagination'.. but i want one record in a page.. so should i limit the num_of_records per page as 1 ???
|  | Needs Regular Fix | | Join Date: Jul 2007 Location: India
Posts: 407
| | | re: go to previous,next records.. Quote:
Originally Posted by sweetline priya @TheServant..
u suggested to prefer 'pagination'.. but i want one record in a page.. so should i limit the num_of_records per page as 1 ??? hey ur concept is similar to pagination as the page in which ur in will be static giving link to next and behind page.
| | Newbie | | Join Date: Jan 2009
Posts: 16
| | | re: go to previous,next records..
ok.. i ll try with pagination
|  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 914
| | | re: go to previous,next records.. Quote:
Originally Posted by sweetline priya ok.. i ll try with pagination Give it a go, and post back with your code so that we can make sure it's the best way to do it. Also obviously let us know if you get stuck with the pagination code too ;)
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|