| | |
how to display one image per day from an array?
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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.
Last edited by ~s.o.s~; May 18th, 2007 at 1:49 pm.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
Join Date: Feb 2007
Posts: 14
Reputation:
Solved Threads: 0
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" > </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..
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" > </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:
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:
java Syntax (Toggle Plain Text)
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:
java Syntax (Toggle Plain Text)
GregorianCalendar c = new GregorianCalendar(); int day = c.get(Calendar.DAY_OF_MONTH);
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
![]() |
Similar Threads
- Random image with text and link (JavaScript / DHTML / AJAX)
- Need help to display data with image into page (ASP.NET)
- How to load a JPEG image file, store it in array and then save it (Visual Basic 4 / 5 / 6)
- how to display an attached image file in email (ASP)
- display image if applet is "not inited" (Java)
- Monitor display fault (Monitors, Displays and Video Cards)
- looking for a free or low cost program to randomly display text and/or images (PHP)
Other Threads in the JSP Forum
- Previous Thread: Ask about JCS... :)
- Next Thread: how to write the textbox values into a file
| Thread Tools | Search this Thread |
Tag cloud for JSP
apache array backbutton combobox comma connection csv database development directorystructure dropdownlist dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 mysql netbeans network parameters passing ping printinserverinsteadofclient project read redirect request.getparameter response seperated servlet servletdopost()readxml sessions software sql ssl state_saving_method stocks sun tomcat tutorial update values video web write






