Please help me loop images

I know that this is probably very dumb question, but I need to combine the following two codes in order to get my images to loop when “next” and “previous” buttons are pushed. I’ve tried several combinations and its just not working. I would really appreciate it if anyone could help me!!!

// code #1//

function nextImage()

{

if (p <= (6 - 1))

{

	

	p++;

	

	eval("button" + p).onPress();



}

}

//code #2//

function nextImage()

{

p++;



if (p > 6)



{



	p = 0;



	eval("button" + p).onPress();



}

}