Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Location
    Portugal
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Question about the best way to make one previous records in a table

    Hi,
    I have one table with information and exists for example 3 pages.
    To make a Next Page, i catch the last record in the page 1 and put that information in a hidden field and submit a form to show page 2 begining with the previous catch record. If i want to show the page 3 i make the same process with page 1 to page 2.
    My problem is to make a previous page, because i don't have information stored in a hidden field.
    Don't forget that i not use a database and i need the information of all nexts pages before the selection of previous page to catch the last information.

    Thank all for the help,
    Pedro Américo.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why not have your data in an array that is linked to each the three pages, then use a startingIndex and totalRecords for each page
    Code:
    /***************************
    * code for external js file
    ***************************/
    var arrData = new Array('a','b','c','d','e','f','g','h'); // Or whatever
    nRecords = 5;
    
    /***************************
    * Code for pages
    ***************************/
    var sIndex; 
    
    // For page 1
    sIndex = 1; 
    
    // For page 2
    sIndex = 6; 
    nRecords = (nRecords > arrData.length) ? arrData.length : nRecords;
    
    // Code to display Data
    var i = sIndex-1;
    for (i; i<nRecords; i++) {
    document.write(arrData[i]+"<br>");
    }
    Or something like that...this can obviously become more sophisticated than what I have here. You could make sIndex a GET variable or 1 of several other possibilites...

  • #3
    New to the CF scene
    Join Date
    Aug 2002
    Location
    Portugal
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for this example, but my other question is, when i store the information key in array, i have a problem when i submit the page, because i lost all information of the array.

    Is possible not lost the information? or exists other way to not lost the information?
    The hidden field is possible to make a repository of the all information of array and load that information after submit the page??

    Thanks,
    Pedro Américo:

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Best I can figure is this. Make a js file named data.js, or whatever, and put in this
    Code:
    var arrData = new Array('a','b','c','d','e','f','g','h'); // Or whatever
    var nRecords = 3; // Number of records displayed per page
    var prevIndex = 1; // Previous index to jump to
    var nextIndex = nRecords+1; // Next index to Jump to
    
    function getRecords() {
    	// Get starting index from get variable
    	var start = 1;
    	if (top.location.search) {
    		var qString = top.location.search.substring(1);
    		var getPieces = qString.split(/\=/);
    		var getVars = new Array()
    		getVars[getPieces[0]] = getPieces[1];
    		start = parseInt(getVars['start']);
    		}
    	// Reset to 1 if start parameter is too big
    	if (start > arrData.length) start=1;
    	
    	// Set prev/next indexes
    	prevIndex = (start == 1) ? start : ((start-nRecords < 1) ? 1 : start-nRecords);
    	nextIndex = ((start+nRecords)-1 >= arrData.length) ? start : (start+nRecords);
    		
    	// Get data based in starting index
    	var rHTML = "";
    	var i = start-1;
    	lastRecord = (i+nRecords > arrData.length) ? arrData.length-i : nRecords;
    	lastRecord += i;
    	for (i; i<lastRecord; i++) {
    		rHTML += arrData[i]+"<br>";
    		}
    	return rHTML;/**/
    	}
    
    function getLinks() {
    	var rHTML = "";
    	rHTML += '<a href="temp2.htm?start='+prevIndex+'">Previous</a>';
    	rHTML += ' | ';
    	rHTML += '<a href="temp2.htm?start='+nextIndex+'">Next</a>';
    	return rHTML;
    	}
    The, make a page, display.htm, or something, and do this
    Code:
    <html>
    <head>
    <title>Test</title>
    
    <script src="data.js"></script>
    </head>
    
    <body>
    <script>
    document.write(getRecords());
    document.writeln('<br>');
    document.writeln(getLinks());
    </script>
    </body>
    
    </html>
    Now you have 1 page that dynmically displays your data, with 1 variable controlling the number of records per page. It can even be successfully manipulated by hand in the URL to start at a different index.

    Of course, you dont have to output the data exaclty like this, but I did it this way for a quickie demo. You could even add checks into the getLinks function so that the prev/next link isn't displayed on the first/last page. Stuff like that. There's lots that can be done in the optimization dept.

    If you have more get variables, they may get int he way of the top section of the code, but that is easily solved by a loop.

    Cheers
    Last edited by beetle; 08-21-2002 at 06:55 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •