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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2006
    Location
    Cork Ireland
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Paging using java script and i really need help

    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


    }

  • #2
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A few things...
    Firstly... when posting in forums, try to remember to use [CODE] ... [/CODE] 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:
    Code:
    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:
    Code:
    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:
    Code:
    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!
    Last edited by Pyth007; 08-08-2006 at 08:52 PM.
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!

  • #3
    New to the CF scene
    Join Date
    Jul 2006
    Location
    Cork Ireland
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much my code is working perfect now. You really help me out!!
    Its a load off my mind to have that finished

    Margaret


  •  

    Posting Permissions

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