Results 1 to 16 of 16

Thread: Inserting at Position

  1. #1

    Thread Starter
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Inserting at Position

    This to me seems like it should be an almost simple solution, but for some reason, I'm having a brain fart.

    I am building an extensive content management system for asp, complete with plugins and modules. The issue I'm running into is finding an efficient way of replacing the placeholder with the asp include.

    Let me give you an example, in my past implementation, I had an extensive if... elseif... else statement that delimited the data with split command that would have to be hard coded into the index of the site.

    My new solution is a little more elegant in theory, but I'm having issues implementing it.

    The entire site is database driven, with records created for every page, there are tons of different modules scripted. These modules are held in another table on the database mapping out which include is needed for each different placeholder.

    For example, I may have placeholders that look similar to
    Code:
    [$Calendar$]
    [$Users$]
    [$Blog Post$]
    [$FacebookComments$]
    etc
    In the page table within the database, it may look similar to:
    Code:
    This is an example page created to showcase the power of the plugins. Below you'll find the gallery as well as a comment system.
    [$Gallery$]
    If you have any comments please post.
    [$Comments$]
    Thank you for your continued support
    [$FacebookLike$]
    As you can see from the example, I have 3 separate plugins in this single page, and my idea was something similar to:

    Get position of placeholder
    At placeholder position insert asp include
    Continue page.
    Repeat

    Can anyone think of an elegant way of accomplishing this?

    EDIT

    Ok guys, I'm pretty sure I've managed to get this working. I don't wish to mark this resolved yet because I believe there may be a few bugs still. In case anyone was wondering, the method I chose was creating a custom method to run through the page, eliminate the placeholders, and in their place, use the execute command to execute functions that I had stored within the database. All the plugins have their own set of includes that are added to the index when they're installed.

    This is fairly sloppy, so if anyone can think of some revisions for it I'd really appreciate it.

    Code:
    <% 
    
    Function InsertPlugins(sData)
    	Dim strData: strData=sData
    	Dim lngUpper: lngUpper = 0
    	Dim strPosition()
    	Dim start
    	Dim strPlaceholder
    	Dim strPlugin
    	Dim i
    
    [DATABASE CONNECTION STRING HERE]
    	
    	Do Until rs2.EOF
    		start = 1
    		strPlaceholder = rs2.fields("pName")
    		Do Until start = 0
    			start = instr(start, strData, strPlaceholder)
    			If start> 0 then
    				Redim Preserve strPosition(10000,lngUpper)
    				strPosition(0, lngUpper) = start
    				strPosition(1, lngUpper) = strPlaceholder
    				start=start+len(strPlaceholder)
    				lngUpper = lngUpper + 1
    			End If
    		Loop
    		rs2.movenext
    	Loop
    	rs2.close
    	if int(lngUpper)>0 then
    		for ii=0 to lngupper - 1
    			temp1 = strPosition(0,ii)
    			temp2 = strPosition(1,ii)
    			strPosition(0,ii) = temp2
    			strPosition(1,ii) = temp1
    		next
    		strPosition2 = arraySort(strPosition, 1, "Number")
    		for ii=0 to lngupper - 1
    			temp1 = strPosition2(0,ii)
    			temp2 = strPosition2(1,ii)
    			strPosition2(0,ii) = temp2
    			strPosition2(1,ii) = temp1
    		next
    		response.write(left(strData,strPosition2(0,0)-1))
    		For i=0 to lngUpper - 1
    			if strPosition2(0, i) > 1 Then
    				If i<lngUpper - 1 Then
    					strPlugin = strPosition2(1,i)
    					rs2.open "SELECT * FROM tblPlugins WHERE pName='" & strPlugin & "'",conn
    					rs2.movefirst
    					execute(rs2.fields("pFunction"))
    					response.write(mid(strdata,strPosition2(0,i)+len(strPlugin), strPosition2(0,i+1)-strPosition2(0, i)-len(strPosition2(1, i))))
    					rs2.close					
    				else
    					strPlugin = strPosition2(1,i)
    					rs2.open "SELECT * FROM tblPlugins WHERE pName='" & strPlugin & "'",conn
    					rs2.movefirst
    					execute(rs2.fields("pFunction"))
    					rs2.close
    				End if
    			else
    				strPlugin = strPosition2(1, i)
    				rs2.open "SELECT * FROM tblPlugins WHERE pName='" & strPosition2(1,i) & "'",conn
    				rs2.movefirst
    				execute(rs2.fields("pFunction"))
    				rs2.close
    				response.write(mid(strdata,strPosition2(0,i)+len(strPlugin), strPosition2(0,i+1)-strPosition2(0, i)-len(strPosition2(1, i))))
    			End If
    		Next
    		response.write(mid(strData, strPosition2(0, lngUpper - 1) + len(strPosition2(1,lngUpper - 1))))
    	end if
    	InsertPlugins = lngUpper
    end Function 
    
    function arraySort(arToSort, sortBy, compareType)
    	Dim c, d, e, smallestValue, smallestIndex, tempValue
    
    	For c = 0 To uBound( arToSort, 2 ) - 1
    		smallestValue = arToSort( sortBy, c )
    		smallestIndex = c
    
    		For d = c + 1 To uBound( arToSort, 2 )
    			if compareType = "Text" then
    				if strComp( arToSort( sortBy, d ), smallestValue ) < 0 Then
    					smallestValue = arToSort( sortBy, d )
    					smallestIndex = d
    				End if
    			elseif compareType = "Date" then
    				if not isDate( smallestValue ) then
    					arraySort = arraySort( arToSort, sortBy, false)
    					exit function
    				else
    					if dateDiff( "d", arToSort( sortBy, d ), smallestValue ) > 0 Then
    						smallestValue = arToSort( sortBy, d )
    						smallestIndex = d
    					End if
    				end if
    			elseif compareType = "Number" then
    				if cint( arToSort( sortBy, d ) ) < cint(smallestValue) Then
    					smallestValue = arToSort( sortBy, d )
    					smallestIndex = d
    				End if
    			end if
    		Next
    		if smallestIndex <> c Then 'swap
    			For e = 0 To uBound( arToSort, 1 )
    				tempValue = arToSort( e, smallestIndex )
    				arToSort( e, smallestIndex ) = arToSort( e, c )
    				arToSort( e, c ) = tempValue
    			Next
    		End if
    	Next
    	arraySort = arToSort
    end function
    
    %>
    Last edited by Mxjerrett; Jan 26th, 2012 at 01:16 AM.

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  2. #2

    Thread Starter
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: Inserting at Position

    I'm fairly sure I've figured it out. After several hours of tinkering and research, I believed it's fool proof. At least I hope.


    One request, if anyone can help me with a more efficient method of sorting my array, I'd be incredibly grateful.
    Code:
    <% 
    
    Function InsertPlugins(sData)
    	Dim strData: strData=sData
    	Dim lngUpper: lngUpper = 0
    	Dim strPosition()
    	Dim start
    	Dim strPlaceholder
    	Dim strPlugin
    	Dim i
    
    [DATABASE CONNECTION STRING HERE]
    	
    	Do Until rs2.EOF
    		start = 1
    		strPlaceholder = rs2.fields("pName")
    		Do Until start = 0
    			start = instr(start, strData, strPlaceholder)
    			If start> 0 then
    				Redim Preserve strPosition(10000,lngUpper)
    				strPosition(0, lngUpper) = start
    				strPosition(1, lngUpper) = strPlaceholder
    				start=start+len(strPlaceholder)
    				lngUpper = lngUpper + 1
    			End If
    		Loop
    		rs2.movenext
    	Loop
    	rs2.close
    	if int(lngUpper)>0 then
    		for ii=0 to lngupper - 1
    			temp1 = strPosition(0,ii)
    			temp2 = strPosition(1,ii)
    			strPosition(0,ii) = temp2
    			strPosition(1,ii) = temp1
    		next
    		strPosition2 = arraySort(strPosition, 1, "Number")
    		for ii=0 to lngupper - 1
    			temp1 = strPosition2(0,ii)
    			temp2 = strPosition2(1,ii)
    			strPosition2(0,ii) = temp2
    			strPosition2(1,ii) = temp1
    		next
    		response.write(left(strData,strPosition2(0,0)-1))
    		For i=0 to lngUpper - 1
    			if strPosition2(0, i) > 1 Then
    				If i<lngUpper - 1 Then
    					strPlugin = strPosition2(1,i)
    					rs2.open "SELECT * FROM tblPlugins WHERE pName='" & strPlugin & "'",conn
    					rs2.movefirst
    					execute(rs2.fields("pFunction"))
    					response.write(mid(strdata,strPosition2(0,i)+len(strPlugin), strPosition2(0,i+1)-strPosition2(0, i)-len(strPosition2(1, i))))
    					rs2.close					
    				else
    					strPlugin = strPosition2(1,i)
    					rs2.open "SELECT * FROM tblPlugins WHERE pName='" & strPlugin & "'",conn
    					rs2.movefirst
    					execute(rs2.fields("pFunction"))
    					rs2.close
    				End if
    			else
    				strPlugin = strPosition2(1, i)
    				rs2.open "SELECT * FROM tblPlugins WHERE pName='" & strPosition2(1,i) & "'",conn
    				rs2.movefirst
    				execute(rs2.fields("pFunction"))
    				rs2.close
    				response.write(mid(strdata,strPosition2(0,i)+len(strPlugin), strPosition2(0,i+1)-strPosition2(0, i)-len(strPosition2(1, i))))
    			End If
    		Next
    		response.write(mid(strData, strPosition2(0, lngUpper - 1) + len(strPosition2(1,lngUpper - 1))))
    	end if
    	InsertPlugins = lngUpper
    end Function 
    
    function arraySort(arToSort, sortBy, compareType)
    	Dim c, d, e, smallestValue, smallestIndex, tempValue
    
    	For c = 0 To uBound( arToSort, 2 ) - 1
    		smallestValue = arToSort( sortBy, c )
    		smallestIndex = c
    
    		For d = c + 1 To uBound( arToSort, 2 )
    			if compareType = "Text" then
    				if strComp( arToSort( sortBy, d ), smallestValue ) < 0 Then
    					smallestValue = arToSort( sortBy, d )
    					smallestIndex = d
    				End if
    			elseif compareType = "Date" then
    				if not isDate( smallestValue ) then
    					arraySort = arraySort( arToSort, sortBy, false)
    					exit function
    				else
    					if dateDiff( "d", arToSort( sortBy, d ), smallestValue ) > 0 Then
    						smallestValue = arToSort( sortBy, d )
    						smallestIndex = d
    					End if
    				end if
    			elseif compareType = "Number" then
    				if cint( arToSort( sortBy, d ) ) < cint(smallestValue) Then
    					smallestValue = arToSort( sortBy, d )
    					smallestIndex = d
    				End if
    			end if
    		Next
    		if smallestIndex <> c Then 'swap
    			For e = 0 To uBound( arToSort, 1 )
    				tempValue = arToSort( e, smallestIndex )
    				arToSort( e, smallestIndex ) = arToSort( e, c )
    				arToSort( e, c ) = tempValue
    			Next
    		End if
    	Next
    	arraySort = arToSort
    end function
    
    %>
    Last edited by Mxjerrett; Jan 26th, 2012 at 01:13 AM. Reason: Updated for accuracy

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  3. #3
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Inserting at Position

    I haven't looked closely at the code but a quick glance appear that you know the name of your placeholder ahead of time and you're using that to find the position of where you want to do your insert. If that is the case couldn't you just use the Replace function instead of all the string manipulation?

  4. #4

    Thread Starter
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: Inserting at Position

    Quote Originally Posted by MarkT View Post
    I haven't looked closely at the code but a quick glance appear that you know the name of your placeholder ahead of time and you're using that to find the position of where you want to do your insert. If that is the case couldn't you just use the Replace function instead of all the string manipulation?
    Yes and no, replace would be able to remove the function, but it isn't going to find the location of where I have to insert the calls to the appropriate function.

    Example
    Code:
    strData=replace(strData,Placeholder,FunctiontoExecute)
    If I were to simply replace the placeholder with the Function, it would just write the function to the page, instead of executing it.

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  5. #5
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Inserting at Position

    Couldn't you call the function, place the return in a variable and use that in the replace call. I may not be seeing the bigger picture here.
    Code:
    Ret = FunctiontoExecute ' call the function
    strData=replace(strData,Placeholder,Ret )

  6. #6

    Thread Starter
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: Inserting at Position

    MarkT, I actually just attempted what you said and it doesn't seem to worth. It throws a mismatch error.

    The reason the example you showed doesn't work is some of these plugins' functions have extensive methods within them. Some of them writing to the form several times.

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  7. #7

    Thread Starter
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: Inserting at Position

    Well, this is somewhat frustrating. After rethinking it with a well rested and clear mind. I think I may have managed to restructure the functions to be able to handle piping the information over to the Plugin Function.

    Here is what it looks like now

    Code:
    Function InsertPlugins(sData)
    [DATABASE QUERY]
    	
    	strData=sData
    	do until rs2.eof
    		execute("funct =" & rs2.fields("pFunction"))
    		strData=replace(strData,rs2.fields("pName"),funct)
    		rs2.movenext
    	loop
    	if strData<>sData then 
    		InsertPlugins2 = 1
    		response.write(strData)
    	end if
    	rs2.close
    End Function
    Originally the subroutines my other code called were writing to the form within their own function. This new way should work, but I'll keep the old code just incase.

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  8. #8
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Inserting at Position

    Just wondering why you use the replace() function instead of insert()? I only vaguely read through your code, still a little confused.

    vb Code:
    1. Dim str As String = "this is string value"
    2. str = str.Insert(str.IndexOf("string"), "my ")

    edit: Disregard my post... I didn't see this was for asp.net, I assumed some kind of vb script.
    Last edited by AceInfinity; Jan 27th, 2012 at 12:20 AM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  9. #9

    Thread Starter
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: Inserting at Position

    Quote Originally Posted by AceInfinity View Post
    Just wondering why you use the replace() function instead of insert()? I only vaguely read through your code, still a little confused.

    vb Code:
    1. Dim str As String = "this is string value"
    2. str = str.Insert(str.IndexOf("string"), "my ")

    edit: Disregard my post... I didn't see this was for asp.net, I assumed some kind of vb script.
    Mine is asp, are you saying your code is asp.net?

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  10. #10
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Inserting at Position

    Quote Originally Posted by Mxjerrett View Post
    Mine is asp, are you saying your code is asp.net?
    No, and that's why I mentioned in my post to disregard my answer.

    Quote Originally Posted by AceInfinity View Post
    edit: Disregard my post... I didn't see this was for asp.net, I assumed some kind of vb script.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  11. #11
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482

    Re: Inserting at Position

    Just curious why you want the code in a DB. Seems that would be a PITA to update the code. Why not just put the code Server Side Include files and then include them where you want?

  12. #12

    Thread Starter
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: Inserting at Position

    Quote Originally Posted by Maverickz View Post
    Just curious why you want the code in a DB. Seems that would be a PITA to update the code. Why not just put the code Server Side Include files and then include them where you want?
    I do have all the functions within includes, it's a module based system, when they want to execute the code they use the CMS and put something like [$ContactF$] then it reads the plugin table, when it finds the appropriate plugin, it executes the appropriate function found in the SSI

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  13. #13
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482

    Re: Inserting at Position

    Quote Originally Posted by Mxjerrett View Post
    I do have all the functions within includes, it's a module based system, when they want to execute the code they use the CMS and put something like [$ContactF$] then it reads the plugin table, when it finds the appropriate plugin, it executes the appropriate function found in the SSI
    To me the easiest thing would be to just have them use SSI format wherever they want the plugin to go:
    <!--#include virtual="/includes/myFunction.inc" -->

    However if you really want to use the [$ContactF$] format, it might make more sense to write an ISAPI filter to preprocess the document before IIS parses it. Have the ISAPI filter replace [$ContactF$] with <!--#include virtual="/includes/myFunction.inc" --> then have IIS parse it and the includes should work as normal.

    So the flow would be as follows:
    1. Customer requests somepage.asp from IIS
    2. IIS sees that your ISAPI filter is to be used for .asp extentions and passes the request to the ISAPI filter
    3. The ISAPI filter retrieves the document requested and replaces the Text as needed.
    4. The ISAPI filter returns the document to IIS for processing.
    5. IIS executes the code in the includes as normal and returns the HTML to the customer.

  14. #14

    Thread Starter
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: Inserting at Position

    Quote Originally Posted by Maverickz View Post
    To me the easiest thing would be to just have them use SSI format wherever they want the plugin to go:
    <!--#include virtual="/includes/myFunction.inc" -->

    However if you really want to use the [$ContactF$] format, it might make more sense to write an ISAPI filter to preprocess the document before IIS parses it. Have the ISAPI filter replace [$ContactF$] with <!--#include virtual="/includes/myFunction.inc" --> then have IIS parse it and the includes should work as normal.

    So the flow would be as follows:
    1. Customer requests somepage.asp from IIS
    2. IIS sees that your ISAPI filter is to be used for .asp extentions and passes the request to the ISAPI filter
    3. The ISAPI filter retrieves the document requested and replaces the Text as needed.
    4. The ISAPI filter returns the document to IIS for processing.
    5. IIS executes the code in the includes as normal and returns the HTML to the customer.
    I wouldn't know of a decent way of deploying this in mass. This is a deployable CMS system that allows for quick installation on someone's server. I'm not sure how I could make it modify ISAPI filters directly.

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  15. #15
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482

    Re: Inserting at Position

    If you use an installer to deploy, it should be able to configure this. If you just want to unzip and go then yeah not so easy.

  16. #16

    Thread Starter
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: Inserting at Position

    Quote Originally Posted by Maverickz View Post
    If you use an installer to deploy, it should be able to configure this. If you just want to unzip and go then yeah not so easy.
    Yeah, you just move the files to the server and run the initialing page.

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width