Hai..

I need to display one image per day from an array depending upon the day in jsp/servlet.

Intially i am having 5 images in an array.

Then i want to display

sunday --- 1st image
monday--- 2nd image
tuesday -- 3rd image
wednesday--4th image
thursday -- 5th image
friday -- 1st image
Saturday-- 2nd image

number of images is dynamically changing.

if the array has one image.. daily that one image should display.
if the array has two image, then 1st day--- 1st image. 2nd day--- 2nd image, 3rd day--- again 1st image.

Can any one please tell me the logic to do this? waiting for your reply.

Thank you...

look at the documentation of Calendar.

Hai,

I didnt find out the logic for this.
Kindly tell me some suggestions.

look again... You'll notice you can get the day as a number.
An array is indexed on a number.

Put 2 and 2 together and you'll have insight.

Here is the code. array length is number of records available in the database. that is retrived in a query.

Calendar cal = Calendar.getInstance();
int dayofweek = cal.get(cal.DAY_OF_WEEK);

if(arraylength==2)
{
if(dayofweek==1)
imagenumber=1;
else if(dayofweek==2)
imagenumber=2;
else if(dayofweek==3)
imagenumber=1;
else if(dayofweek==4)
imagenumber=2;
else if(dayofweek==5)
imagenumber=1;
else if(dayofweek==6)
imagenumber=2;
else if(dayofweek==7)
imagenumber=1;
}
else if(arraylength==3)
{
if(dayofweek==1)
imagenumber=1;
else if(dayofweek==2)
imagenumber=2;
else if(dayofweek==3)
imagenumber=3;
else if(dayofweek==4)
imagenumber=1;
else if(dayofweek==5)
imagenumber=2;
else if(dayofweek==6)
imagenumber=3;
else if(dayofweek==7)
imagenumber=1;
}
else if(arraylength==4)
{
if(dayofweek==1)
imagenumber=1;
else if(dayofweek==2)
imagenumber=2;
else if(dayofweek==3)
imagenumber=3;
else if(dayofweek==4)
imagenumber=4;
else if(dayofweek==5)
imagenumber=1;
else if(dayofweek==6)
imagenumber=2;
else if(dayofweek==7)
imagenumber=3;
}
else if(arraylength==5)
{
if(dayofweek==1)
imagenumber=1;
else if(dayofweek==2)
imagenumber=2;
else if(dayofweek==3)
imagenumber=3;
else if(dayofweek==4)
imagenumber=4;
else if(dayofweek==5)
imagenumber=5;
else if(dayofweek==6)
imagenumber=1;
else if(dayofweek==7)
imagenumber=2;
}
else if(arraylength==6)
{
if(dayofweek==1)
imagenumber=1;
else if(dayofweek==2)
imagenumber=2;
else if(dayofweek==3)
imagenumber=3;
else if(dayofweek==4)
imagenumber=4;
else if(dayofweek==5)
imagenumber=5;
else if(dayofweek==6)
imagenumber=6;
else if(dayofweek==7)
imagenumber=1;
}
else if(arraylength==7)
imagenumber = dayofweek;
else if((arraylength>7 && (arraylength<15))
imagenumber=dayofweek+7;
else
imagenumber=arraylength;


I am manually checking array length and assigning the image. I need some dynamic calculation for assigning the image. Also this code works only upto 14 images. I need to display even the array length is 25, 50 etc...

Kindly check it out and give me a solution.

ever heard of division?

Hai jwenting!

If you know the logic, Post it...
Dont critize the one who is asking for help.

Thendral, in my opinion you are complicating your work. Why not to have 7 images in array and then you have no need for additional calculations. Just find out which day of week is currently and call array position to retrive image.

If you still feel funky then you can setup 31 images and acording to day in the month you call relevant image. Then you have image for each day

How about something like: imageId = imageArray.length % dayOfMonth; Change the formula to suit your needs but I hope you get the logic right.

Thank you Peter_Budo and ~S.O.S~...

This is my Project requirement. I cant change the array length as Static one ie 7.

I tried with division and modulo operator. but it is not working. Do you have any other calculations? If so kindly help me out.

The usual "this doesn't work" is not helpful at all. You need to tell us 'what' is not working and what you have tried out. Post the most recent relevant code which shows the concerned logic and then we can work out something.

hai..

Here is the code..


Servlet:


ResultSet rsid = null;
ResultSet rsw = null;
ResultSet rscount = null;
int arraylen = -1;
String dbimage = null;
Calendar cal = Calendar.getInstance();
int dayOfWeek = cal.get(cal.day_OF_WEEK);
int daycount = -1;
String sql = "select count(*) as count from banner where flag="+0;
stmt = con.prepareStatement(sql);
rscount = stmt.executeQuery();
while (rscount.next())
{
arraylen = rscount.getInt("count");
}
int[] array = new int[20];
String sql2 = "select imageid from banner where flag="+0;
stmt = con.prepareStatement(sql2);
rsid = stmt.executeQuery();
int loop = 1;
while(rsid.next())
{
array[loop++] = rsid.getInt("imageid");
}

if(arraylen==2)
{
if(dayofweek==1)
daycount=1;
else if(dayofweek==2)
daycount=2;
else if(dayofweek==3)
daycount=1;
else if(dayofweek==4)
daycount=2;
else if(dayofweek==5)
daycount=1;
else if(dayofweek==6)
daycount=2;
else if(dayofweek==7)
daycount=1;
}
else if(arraylen==3)
{
if(dayofweek==1)
daycount=1;
else if(dayofweek==2)
daycount=2;
else if(dayofweek==3)
daycount=3;
else if(dayofweek==4)
daycount=1;
else if(dayofweek==5)
daycount=2;
else if(dayofweek==6)
daycount=3;
else if(dayofweek==7)
daycount=1;
}
else if(arraylen==4)
{
if(dayofweek==1)
daycount=1;
else if(dayofweek==2)
daycount=2;
else if(dayofweek==3)
daycount=3;
else if(dayofweek==4)
daycount=4;
else if(dayofweek==5)
daycount=1;
else if(dayofweek==6)
daycount=2;
else if(dayofweek==7)
daycount=3;
}
else if(arraylen==5)
{
if(dayofweek==1)
daycount=1;
else if(dayofweek==2)
daycount=2;
else if(dayofweek==3)
daycount=3;
else if(dayofweek==4)
daycount=4;
else if(dayofweek==5)
daycount=5;
else if(dayofweek==6)
daycount=1;
else if(dayofweek==7)
daycount=2;
}
else if(arraylen==6)
{
if(dayofweek==1)
daycount=1;
else if(dayofweek==2)
daycount=2;
else if(dayofweek==3)
daycount=3;
else if(dayofweek==4)
daycount=4;
else if(dayofweek==5)
daycount=5;
else if(dayofweek==6)
daycount=6;
else if(dayofweek==7)
daycount=1;
}
else if(arraylen==7)
daycount = dayofweek;
else
daycount=arraylen;

int imageid = array[daycount];
String sql3 = "select image1 from image where imageid=?";
stmt = con.prepareStatement(sql3);
stmt.setInt(1, imageid);
rsw = stmt.executeQuery();
while(rsw.next())
{
dbimage = rsw.getString("image1");
}


Display(JSP):

<html>
<head>
Displaying the Image
</head>
<body bottommargin="0" topmargin="0" rightmargin="0" leftmargin="0" >
<center>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="center">
<table width="164" height="198" border="1" cellpadding="0" cellspacing="0" bordercolor="#009933">
<tr >
<td height="28" align="center" bordercolor="#FFFFFF" bgcolor="#ff9c18" class="steps1" >&nbsp;</td>
</tr>
<tr>
<td align="center" valign="middle" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
<p><img src="../../resource/img/<%=dbimage%>" width="130" height="150" border="0"></a> </p>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
</tr>
</table>
</center>
</body>
</html>

Here i need to change the red colored code. Here i checked for the array length 2,3,4,5,6,&7 and assigning the imageid. I need this to be done dynamically for array length n(1 to n). Some calculation is needed. i used modulo. it is not working. Also i used division. it is not working. Kindly help me for getting some other calculation.

Thanking you..

Why doesn't this work in your case:

dayCount = (dayOfMonth - 1) % arraySize;
imageId = myImageArray[dayCount];

Oh and BTW, your logic is wrong, the array indexing starts at 0 and not at 1. So your mesh of 'if' statements which use 1 as the starting index is wrong, unless you have something different planned out.

Plus you are using the wrong method to get the day. You should get the day of month, not the day of week otherwise you would always access the first 7 images since DAY_OF_WEEK ranges between 1 to 7. Use this:

GregorianCalendar c = new GregorianCalendar();
int day = c.get(Calendar.DAY_OF_MONTH);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.