• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

please help me sort out this logic

Hi all. I can't work this out. Please Help.

I have the following jsp file, what I'm trying to do is write out items in a vector in groups based on their session number. I need to somehow check whether the current iteration is the same as the previous one. You'll see in my current code, it will always be true. ....

so in other words, if they all have the same session number, put them in the same table cell, in not create a new cell, with a heading, message time and message body.

the heading and message time must appear once only, here's my code bit.

<logic:present name="logsRetrieved" scope="request">
<br />
<table width="690" border="0" cellspacing="0" cellpadding="0">
<tr>
<td div id="TOPLEFT">Log</td>
 </tr>
<%--define our vector containing the logs as a bean--%>
<%--Iterate throught the available logs for chat sessions--%>
<%
Vector vcrUnityChatLogVector = (Vector)request.getAttribute("vcrUnityChatLogVector");
%>
<logic:iterate collection="<%=vcrUnityChatLogVector%>" id="chatLog" name="logs" type="com.ro.unity.UnityLogsBean"><tr>
<td div id="SUBWHOLE" > <%--Write the Room Name--%>
<strong><bean:write name="chatLog" property="roomName"/></strong>
<%--write the time of the chat, smaller--%>
<br />
Message Time:&nbsp;<bean:write name="chatLog" property="messageTime" />
<br />
<%
// define variables
int temp = chatLog.getSession();

out.println(temp);
// compare strings
if (chatLog.getSession() == temp)
{
%>
Message:&nbsp;<bean:write name="chatLog" property="message" />
<br />
<%
}
else
{
out.println("draw line!");
%>
Message:&nbsp;<bean:write name="chatLog" property="message" />
<br />
</td>
</tr>
<%
}
%>
<%--end display of individual chat log--%>
<%
 temp++;
out.println(temp);
%>
</logic:iterate> <%--End iteration through log vector--%>
</table>
</logic:present>

thanks.
blu.
0
blue-genie
Asked:
blue-genie
  • 11
  • 8
  • 8
2 Solutions
 
suprapto45Commented:
Hi,

>>"the heading and message time must appear once only, here's my code bit."
Have you achieved it? From your codes, I can see that it will be iterated again and again.

I am still reading the logic. However, can you answer my previous question?

Regards
Dave
0
 
suprapto45Commented:
Secondly,

Why do you need temp++; as temp contains the chatLog.getSession()?

Regards
Dave
0
 
bloodredsunCommented:
You need to create a temporary variable out side of the iteration that keeps track of the session String. Your current one is inside the loop and so can't keep state information about the loop as it's instatntiated each time.

Somehting like this psuedocode

String sessionTemp = "" ;
<logic:iterate ///blah
<%
String sessionCurrent =  chatLog.getSession() ;
if (sessionCurrent .equals(sessionTemp)){
//same session stuff
}else{
//new session stuff
}
sessionTemp = sessionCurrent ; //important bit where we reset the sessionTemp variable to be the last session
%>
</logic:iterate>

Can I rewrite this in JSTL !!! :-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
blue-genieAuthor Commented:
it must appear once per group of items with same session number i.e once per cell only.

make sense?
0
 
suprapto45Commented:
Hehe bloodredsun,

Now we can see why JSTL makes things clearer :)

Regards
Dave
0
 
bloodredsunCommented:
Is this an extension to a current question? I'd rather not tread on Dave's toes if he's in the process of answering this one...
If it is then feel free to tell me to hang back and I'll only answer further if requested.
0
 
suprapto45Commented:
Hi bloodredsun,

No, please keep posting your feedback. I will be happy if we can both solve his/her problem. In addition, I will have a short meeting soon so I could leave it to you :).

Regards
Dave
0
 
blue-genieAuthor Commented:
bloodredsun - no its not an extension

both of u - i have no idea what JSTL is, so I'll have to say NO you can't rewrite in JSTL as this is part of a bigger project.

dave, in terms of why I used temp++ , I don't know, I figured I need some way to increment it (As i would in a for loop) -

both of u, and anyone else - disregard any wierd stuff I might be doing, but basically, this is what I'm trying to achieve.

1. I have a vector.
2. I'm iterating through it, and writing out items in a table.
3. Each table cell needs a heading, message time, and message body.
4. Each message body that has the same session ID as the previous one needs to go into the same table cell, without repeating the heading and message time.
5. Once the session time is changed (ie. different from the previous one) - I need to write out a new table row, with a heading and message time.

thanks again.

blu.
0
 
suprapto45Commented:
Please use the pseudocode from bloodredsun. If there is still an error, please do not hesitate to contact me.

I will go for 15 mins to meeting.

Regards
Dave
0
 
suprapto45Commented:
Mm.......

"5. Once the session time is changed (ie. different from the previous one) - I need to write out a new table row, with a heading and message time."

Are the data in the vector itself is sorted by session number? It means that it would be much easier if the data is sorted by session number.

Regards
Dave
0
 
blue-genieAuthor Commented:
ok, i implemented the psuedo code, but now the output is as follows:

the first item is being written out, in the table.
the next lot of items in the same group (with same session) - are being written out on top of the table, while the next items are working fine.
If that makes any sense.

Might be something minor, I'll have a look and post again.

they arent' sorted in the vector, but are ordered in the dbase. (if that makes any sense).

blu.
0
 
bloodredsunCommented:
So somehting like this:

<table width="690" border="0" cellspacing="0" cellpadding="0">
<tr>
      <td div id="TOPLEFT">Log</td>
</tr>
      <% Vector vcrUnityChatLogVector = (Vector)request.getAttribute("vcrUnityChatLogVector"); %>
      
      <% int lastSession = 0 ;%>
      
      <logic:iterate collection="<%=vcrUnityChatLogVector%>" id="chatLog" name="logs" type="com.ro.unity.UnityLogsBean"><tr>
      <td div id="SUBWHOLE" > 
      
      <%
      //check for same sessionId
        if (chatLog.getSession() == lastSession ){
              lastSession = chatLog.getSession() ;
      %>
            Message:&nbsp;<bean:write name="chatLog" property="message" /><br />
      <%
        }else{
              lastSession = chatLog.getSession() ;
      %>
      <tr>
            <td>
      <%--Room Name--%><strong><bean:write name="chatLog" property="roomName"/></strong><br />
      <%--time of the chat--%>Message Time:&nbsp;<bean:write name="chatLog" property="messageTime" />      <br />
            </td>
      </tr>
      <tr>
            <td>Message:&nbsp;<bean:write name="chatLog" property="message" /><br />
      <%}%>
      </logic:iterate> <%--End iteration through log vector--%>
      
            </td>
      </tr>
</table>
</logic:present>

You need to be careful with your formatting of the tr and td tags. The logic is simple but you need to make sure that each row is openend and closed correctly.

If you haven't come across JSTL, you should give it a go. I understand that this is a projcet already underway so you can't change it, but once you've tried JSTL you'll see why.
0
 
bloodredsunCommented:
In the code above I haven't allowed for the opening a closing of the td/tr tags, but that should be pretty simple by checking for the lastSession variable to see whether it's 0 to print out the closing tags.
If it is 0 then it's the first iteration and you can output no tags. If it's not 0 then you can close the previous table cell and open a new one.
The last closing tags should be outside the iteration so that they execute when your vector has been iterated over.
0
 
blue-genieAuthor Commented:
ok, that's slightly better, but the formatting is odd.

go have a look here...

http://www.geocities.com/bluegenie_sa/ee/table.htm

a screen shot of whats currently happening and what it should be.

thanks.

Blu :-)
0
 
blue-genieAuthor Commented:
i have a query about this bit


  <%
     //check for same sessionId
       if (chatLog.getSession() == lastSession){
            lastSession = chatLog.getSession() ;
     %>

my chatLog sessions always start at 1, not 0, so they will never be 0, does this mean I should change my initial value of last session to 1?

blu.
0
 
bloodredsunCommented:
>> i have a query about this bit
Basically that bit is to find out do you need to create a new table row for a new session group.

>>my chatLog sessions always start at 1
Good. It means the first time that you try it, the test will be false, therefore it's a "new session group" so that you can create a new row.

>> does this mean I should change my initial value of last session to 1?
Nope. Reasons as above.
0
 
bloodredsunCommented:
Here's some revised code:

<logic:present name="logsRetrieved" scope="request">
<br />

<table width="690" border="0" cellspacing="0" cellpadding="0">
<tr>
      <td div id="TOPLEFT">Log</td>
</tr>
      <% Vector vcrUnityChatLogVector = (Vector)request.getAttribute("vcrUnityChatLogVector"); %>
      
      <% int lastSession = 0 ;%>
      
      <logic:iterate collection="<%=vcrUnityChatLogVector%>" id="chatLog" name="logs" type="com.ro.unity.UnityLogsBean"><tr>
      <td div id="SUBWHOLE" > 
      
      <%
      //check for same sessionId
        if (chatLog.getSession() == lastSession ){
              lastSession = chatLog.getSession() ;
      %>
            Message:&nbsp;<bean:write name="chatLog" property="message" /><br />
      <%
        }else{
              if(lastSession == 0 ){ //if start of page, just open a table cell
        %>
              <tr>
                    <td>
              
        <%      }else{  //if this is not the first cell, close the previous one and open a new one %>
                    </td>
              </tr>
              <tr>
                    <td>
        <%
              }
              lastSession = chatLog.getSession() ;
      %>
            <strong><bean:write name="chatLog" property="roomName"/></strong><br />
            Message Time:&nbsp;<bean:write name="chatLog" property="messageTime" />      <br />
            Message:&nbsp;<bean:write name="chatLog" property="message" /><br />
      <%}%>
      </logic:iterate> <%--End iteration through log vector--%>
      
            </td>
      </tr>
      
</table>
</logic:present>

You can now see that the code to open and close cells is in it's own area (with the final finishing one done at the end)
0
 
blue-genieAuthor Commented:
i managed to get to the same result, though not quite as tidy, but, what's happening is after every entry, its creating a new <tr> - it musn't, only if its a new session.
0
 
bloodredsunCommented:
>>only if its a new session.

you mean only if a session is not the same number as the previous one, correct?
0
 
bloodredsunCommented:
I would tag the <tr> and<td> tags with <!-- td 1,2 etc--> comments so that you can see which ones are slipping into your code...
0
 
blue-genieAuthor Commented:
sorted!!!

thanks much to both of you.
its become a lot clearer, it sudenly hit me like a bolt of light that obviously, where i was writing out the room name, it would need a new <Tr> tag

:-)

0
 
bloodredsunCommented:
Great!

And thanks for the point and the grade
0
 
suprapto45Commented:
:)

Thanks for the point. However, I think that it is bloodredsun's answer which is accepted and not assisted because he helped you more than me.

Howerver, thanks so much

Regards
Dave
0
 
bloodredsunCommented:
very kind of you to say so Dave but it makes no real difference :-)
0
 
blue-genieAuthor Commented:
sorry, after i pressed the btn i noticed it did that. but s/he still gets the more pts.

thank u both.
0
 
suprapto45Commented:
Yes,

I am just afraid that bloodredsun feels unfair :). However, he does not. Thank you too.

Regards
Dave
0
 
bloodredsunCommented:
lol, no it makes no difference to me. As long as the problem was solved and the right people get the right points, then (as we say in London) "it's all good"!

Hope both of you have a good week, in Jo'burg and where ever you are Dave.

;-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 11
  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now