Click to See Complete Forum and Search --> : Dividing an array


circlemaker
08-23-2004, 11:33 AM
Hello,

I'm new to javasccript and can't work out how to do the following. I need to ensure that a number of images are displayed per line on a page (4). However, I will not know how many images will be returned from a db.

I am using XSL to get the images and then adding them to a javascript array. This works fine i.e I can return and add 6 images to my array. However, with this scenario I would need to display 4 images on one 1 and then 2 on the next. What I can't work out is how to loop through the array dividing it up so that it displays the correct amount.

I would be grateful to anyone who could offer any advice/links to a script etc that handles this sort of thing. I have looked on a number of sites but don't really know what sort of category this would be under.

Thanks in advance

circlemaker.

gil davis
08-23-2004, 12:40 PM
Use the modulus operator (%) when you step through the array. When you test the array index mod 4 = 0, then you are at a page boundary.

for (var i=0; i<ary.length; i++) {
if ((i % 4 == 0) && !(i == 0))
{// throw a page break, except first time}
}

HTH

steelersfan88
08-23-2004, 02:30 PM
Originally posted by gil davis
if ((i % 4 == 0) && !(i == 0)) Are you against the inequality operator of comparison? hehe:if ((i % 4 == 0) && (i != 0)) The problem is, it seems the request is for four images on line 1, and then 2 on line 2. This is confusing, and therefore gil's code might not be what you need.

Please try to explain the way the images are to be displayed more clearly if gil's script doesn't work as you intended.

gil davis
08-23-2004, 03:20 PM
Originally posted by steelersfan88
Are you against the inequality operator of comparison?
No, it is all good. In this imperfect world, some things are more equal than others. ;-)