...

View Full Version : Question about the best way to make one previous records in a table



Americo
08-21-2002, 03:09 PM
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.

beetle
08-21-2002, 03:59 PM
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 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... :D

Americo
08-21-2002, 04:56 PM
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::confused:

beetle
08-21-2002, 06:37 PM
Best I can figure is this. Make a js file named data.js, or whatever, and put in this
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
<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 :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum