...

View Full Version : Paging using java script and i really need help



Margaret101
08-08-2006, 05:56 PM
I am writing a simple paging script which is really getting on my nerves at this stage because all the major work is done and I now have a minor problem

The way i am doing this paging is quering the database once and then showing and hiding the results (I know not always the most efficient way of doing things)

My next button works fine it displays the results in groups of three's but the previous didplays the present results and all other previous results..

Can anyone see what i am doing wrong i just want the previous three results to appear??


<SCRIPT>
var intPosition=0;
function ShowFirstThreeItems()
{
var i;
i=1;
for (i=1; i<=3; i++)
{
eval('ItemRow'+i+'.style.display="block"');
intPosition=i;

}
}
function showNext()
{

var i;

for (i=1; i<=intPosition; i++)
{
eval('ItemRow'+i+'.style.display="none"');
}
for (i=intPosition+1; i<=intPosition+3; i++)
{
eval('ItemRow'+i+'.style.display="block"');
}
intPosition=i-1


}


function showPrev()
{

var i;


for (i=intPosition; i>0; i--)
{
eval('ItemRow'+i+'.style.display="block"');
}
for (i=intPosition+1; i<=intPosition-1; i--)
{
eval('ItemRow'+i+'.style.display="none"');
}
intPosition=i-3


}

Pyth007
08-08-2006, 08:44 PM
A few things...
Firstly... when posting in forums, try to remember to use
... tags to make it easier to read.

Secondly... eval() is a big processor hog. It's much better to use bracket notation whenever possible. Try:

document['ItemRow'+i].style.display = ...

Thirdly... the problem is in the first loop for showPrev(). You have it start at the current position and go down to 0 displaying each row in turn. It should be:

function showPrev()
{
var i;
var firstEnd=intPosition-3; // Saves on calculating thru each iteration.
for (i=intPosition; i>firstEnd; i--)
{
if (i==0) break; // Make sure loop doesn't underrun item list
document['ItemRow'+i].style.display="none"; // "Close" from current position downwards
}
intPosition=firstEnd;
var secondEnd=(firstEnd<3)?0:firstEnd-3; // Set end for second loop
for (i=firstEnd; i>secondEnd; i--)
{
document['ItemRow'+i].style.display="block";
}
}

Fourthly... Although showNext() should work fine, note that you are starting at ItemRow1 and going up to the current position to turn the display to "none". While early in the list this should be fine, on the hundreth page you'll be going thru 100+ items -- most of which are already "hidden". It might be better to start that first loop at i=intPosition-2. Or better yet to make sure loop doesn't underrun the list:

for(i=(intPosition<=3)?1:intPosition-2;i<=intPosition+3;i++)

Finally... A little tip in trying to debug this type of code. Grab a sheet of paper and keep track of the variable contents as you go thru the code. This way you can see what is happening during each iteration, and you can keep track of how the variabels change as you go thru different sequence of function calls. I even had to do this when posting this answer!

Margaret101
08-09-2006, 10:33 AM
Thank you so much my code is working perfect now. You really help me out!!
:thumbsup: Its a load off my mind to have that finished:o

Margaret



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum