Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi all There is prob another way 1

Status
Not open for further replies.

UnfitElf

Programmer
Dec 3, 2002
24
NZ
Hi all

There is prob another way easier way to do what im trying to do but i cannot think of it ATM..

i want to get the comments of a that i have made chat room and then i want to write them out right?

well you can only have like 5 or so messages then the page goes all screwed from the frame thing that i am using.. you have to keep on scrolling down.. well i need to get the first 5 comments the when a sixth is entered i need to get rid of number1 and include number6... well this is the code i can up with....

If Request("BUTTON")="Submit" Then
If i <= 5 Then
Application(&quot;comments&quot;)=Application(&quot;comments&quot;) & &quot;<P>&quot; & Request(&quot;comments&quot;)
Application(&quot;comments&quot;& i)=Request(&quot;comments&quot;)
i=i+1
Else If i > 6 Then
Application(&quot;comments0&quot;)=Application(&quot;comments1&quot;)
Application(&quot;comments1&quot;)=Application(&quot;comments2&quot;)
Application(&quot;comments2&quot;)=Application(&quot;comments3&quot;)
Application(&quot;comments3&quot;)=Application(&quot;comments4&quot;)
Application(&quot;comments&quot;)=Application(&quot;comments0&quot;) & &quot;<p>&quot; & Application(&quot;comments1&quot;) & &quot;<p>&quot; & Application(&quot;comments2&quot;) & &quot;<p>&quot; & Application(&quot;comments3&quot;) & &quot;<p>&quot; & Request(&quot;comments&quot;)

Else Application(&quot;comments&quot;)= &quot;&quot;
End If
End If
End If

The prop is that i cannot get i to increment.. i gets to 1 then its like its resetting back to 0 and then incrementing to one again..

any help??

Thanks Heaps

PS.. if any body can think of a better way please say...
 
for i=i+1 U would have to put it in a loop i think. I is a serverside component that is reset everytime U press submit and would have to be saved clientside to get it to work...

Well.. I just woke up so I do not know if the info i correct :)
 
Why not create a custom object to handle the message storage?
If you could create something similar to a queue with some get methods then you could simply encapsulate all the functionality in the object and create it in your global.asa file with public access on the application level so that all the users could interact with the same object.

Here is a skeleton of what I had in mind:
Code:
<%
Class MessageQueue
	Dim topMessage
	Dim numMessages

	'initialize function
	Private Sub Class_Initialize
		'by default set max number of messages = 5
		numMessages = 5
	End Sub

	'propertries to set max length of queue
	Public Property Let MaxLength(ByVal newLength)
		numMessages = newLength
	End Let

	Public Property Get MaxLength
		MaxLength = numMessages
	End Get

	'function to add a message to the queue
	Public Function AddMessage(ByVal msgContent)
		Dim tMsg
		tMsg = New Message
		tMsg.Content = msgContent
		Push(tMsg)	'Push tMsg onto the front of the queue
	End Function

	'the push functions pushes the passed object onto the front of the queue, shifting everything else
	Private Function Push(newMsg)
		'shift the queue to prepare for new object
		topMessage.Shift
		'set the nextMessage for the new message equal to the current top message
		newMsg.SetNext topMessage
		'set the topMessage variable to point at the new object
		Set topMessage = newMsg
		'remove the last item (index = numMessages)
		topMessage.Remove(numMessages)
	End Function
	
	'a function to return an array of message strings with the oldest at the top of the array
	Public Function GetMessageArray
		Dim msgAry, msgCnt

		msgCnt = topMessage.Count
		Redim msgAry(msgCnt - 1)

		Dim i
		'loop through filling the array backwards
		For i = 0 to msgCnt - 1
			msgAry(msgCnt - 1 - i) = topMessage.GetContentByIndex(i)
		Next
		
		'return the array
		GetMessageArray = msgAry
	End Function
End Class

Class Message
	Dim strContent
	Dim nextMessage
	Dim messageIndex

	'an initialize sub
	Private Sub Class_Initialize
		messageIndex = 0
		strContent = &quot;&quot;
	End Sub

	'Some properties to allow you to get/set the content of a message
	Public Property Get Content
		Content = strContent
	End Get

	Public Property Let Content(ByVal str)
		strContent = str
	End Let

	'a property to get the index of a message
	Public Property Get Index
		Index = messageIndex
	End Get

	'property to set the nextMessage
	Public Property Set SetNext(msgObj)
		Set nextMessage = msgObj
	End Set

	'a shift function to push messages down the queue
	Public Function Shift
		If isEmpty(messageIndex) Then
			'raise an error, this object hasn't been created by a MessageQueue object
		End If
		'increment my position in the queue
		messageIndex = messageIndex + 1
		If Not IsEmpty(nextMessage) Then
			nextMessage.Shift
		End If
	End Function

	'a function to remove a message by index
	Public Function Remove(ByVal index)
		'If I am the object before the specified index and the next one isn't empty
		If messageIndex = index - 1 And Not IsEmpty(nextMessage) Then
			nextMessage = Empty
		'otherwise if the one to be removed is further down, pass it down the line
		ElseIf messageIndex < index - 1 Then
			nextMessage.Remove(index)
		End If
	End Function

	'a function to return a count from this index on, semi-recursively
	Public Function Count
		Dim cnt
		If IsEmpty(nextMessage) Then
			cnt = 1
		Else
			cnt = 1 + nextMessage.Count
		End if
		Count = cnt
	End Function

	'a function to return the content of a message by it's index
	Public Function GetContentByIndex(ByVal index)
		If messageIndex = index Then
			GetContentByIndex = strContent
		ElseIf index > messageIndex Then
			If isEmpty(nextMessage) Then
				'raise an index out of bounds error
			Else
				GetContentByIndex = nextMessage.GetContentByIndex(index)
			End If
		End If
	End Function
End Class
%>

It appears I got carried away again and wrote most of the code. I did put commenting in so it should be fairly easy to work with. This is completely untested and written on the fly (though I will name and save the file I was writing this in as soon as I finish posting). Hopefully I didn't confuse you by writing so much of the code myself.

In order to get a better understanding of how the object works, here is how you would use the messageQueue object:
Code:
<%
Dim myMsgQ

'Setting up the object
Set myMsgQ = New MessageQueue
myMsgQ.MaxLength = 3 'or however many you want

'Adding messages to the object
myMsgQ.AddMessage &quot;This is the first message I added&quot;
myMsgQ.AddMessage &quot;This is the second message I added&quot;
myMsgQ.AddMessage &quot;This is the third message I added&quot;
myMsgQ.AddMessage &quot;This is the fourth message I added&quot;

'Getting an array of the messages back in descending order
Dim msgAry
msgAry = myMsgQ.GetMessageArray

'Now all you have to do is loop through that array for iu = 0 to UBound(msgAry)
%>

I guarantee there are bugs or minor rough spots in that code, and hope they won't be to bad, but it may help you to understand the process I used a little better if you have to debug a few minor issues. For example, after writing the little example above this I realized I missed a Set call at one point in the code. If you have any major problems, or decide to write your own based on what I have above, feel free to ask should any questions arise,

-Tarwn ________________________________________________
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
Hal the Tarwn for he is our ASP god. [lol]

Excellent post!!!!
_________________________________________________________
for the best results to your questions: FAQ333-2924
Is your question a most FAQ?? Find out here FAQ333-3048
 
WOW!!!!!!!!!!!

OK.. that is a lot of code 4 what i thought was a quite simple thing...

Im a beginner at asp so it might take me a while to get throught that lot.. But thanks anyway.. I would also just like to know if it would be possible to do it the way i was going to do it?? and how i would get the incrementing to work??

Thanks Heaps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top