Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post

    replace function doesn't work

    I have a database of table of url extensions. like this:
    ?Book=1&Chapter=21&Verse=all&Keyword=the%20everlasting%20god&Keywordb=&Keywordc=&Keywordd=&Keyworde= &Keywordf=

    I want to replace "?Book=1" with the name of the book. I'm doing this because THe replaced will appear in between the <a></a> whereas the href will be the url extension.

    Code:
    	<SCRIPT LANGUAGE="VBSCRIPT" RUNAT="SERVER">
    		Function stringReplace(strSearchWithin,strSearchFor)
    		Dim lngStartingPosition
    		Dim lngFoundPosition
    		Dim strReplaced
    		'Set the start position
    			lngStartingPosition=1
    			lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,strSearchFor,1)
    			do while lngFoundPosition > 0
    				'found
    				strReplaced=strReplaced & Mid(strSearchWithin,lngStartingPosition,lngFoundPosition-lngStartingPosition) & "<span style='font-weight:bold;'>Genesis " & mid(strSearchWithin,lngFoundPosition,len(strSearchFor)) & "</span>"
    				lngFoundPosition=lngFoundPosition+len(strSearchFor)
    				lngFoundPosition=InStr(lngStartingPosition,strSearchWithin,strSearchFor,1)
    			Loop 
    			stringReplace=strReplaced & Mid(strSearchWithin,lngStartingPosition) 'catch the last one
    		End Function
    	</SCRIPT>  
    	  <%
    		'OPTION EXPLICIT
    		Dim strSearchWithin,strSearchFor
    		strSearchWithin = Response.Write(x.value)
    		strSearchFor = "Book=1"
    
    		Response.Write stringReplace(strSearchWithin,strSearchFor)
    		%>

  • #2
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,065
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Why reinvent the wheel???

    VB has a Built in Function called Replace() and it will do exactly what you want.

    Replace(strToSearch,strToSearchFor,strToReplaceWith)

  • #3
    New Coder
    Join Date
    May 2007
    Location
    SF, CA
    Posts
    55
    Thanks
    0
    Thanked 4 Times in 4 Posts
    documentation for VBScript's Replace function below:

    http://www.w3schools.com/vbscript/func_replace.asp

    semper fi...

  • #4
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post
    I was looking into that. Do you put
    Code:
    dim txt
    txt="This is a beautiful day!"
    document.write(Replace(txt,"beautiful","horrible"))
    in script tags or what?

  • #5
    Regular Coder Baleric's Avatar
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    332
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    dim txt
    txt="This is a beautiful day!"
    document.write(Replace(txt,"beautiful","horrible"))
    document.write is javascript but u are using the replace command that is asp.

    try this.

    Code:
    <%dim txt
    txt="This is a beautiful day!"
    
    dim txtAfterReplace 
    txtAfterReplace = Replace(txt,"beautiful","horrible")
    %>
    <% = txtAfterReplace %>

  • #6
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post

    how to skip empty cells

    There are some empty cells in my database table which I want to skip. Because is gives error when the split function is at work. I tried something below but didn't work.
    Code:
    dim txt,a
    if x.value IsNull then
    	response.write("")
    else
    	txt= x.value
    	a=Split(txt, "&")
    	response.write(a(0) & "<br />")
    	'response.write(a(1))
    end if

  • #7
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post
    I'm trying to write the sql:
    Code:
    			'This SQL statement creates a list of books
    			SQL2 = "Select * from bible"
    
    			rs2.Open sql2,conn2, 1
    
    
    			i = 0
    			for each x in rs.Fields
    				if i = 0 then
    					i = 1
    				else
    					response.write("<th>" & x.name & "</th>")
    			  		' process the rest of the loop except the one ignored above
    				end if
     			 next%>
      </tr>
      <%do until rs.EOF%>
        <tr>
        <%			i = 0
    			for each x in rs.Fields
    				if i = 0 then
    					i = 1
    				else
    	%>
          <td><a href="showverse.asp?<%Response.Write(x.value)%>">
     
    	  <%
    		'OPTION EXPLICIT
    dim txt,a
    If IsNull(x.value) then
    	response.write("")
    else
    	txt= x.value
    	a=Split(txt, "&")
    	sql2 = sql2 & " where " & a(0) & " AND " & a(1)
    	'response.Write(rs2("book_title") & " " & rs2("chapter") )	
    	response.Write(sql2)
    	'response.write(a(0) & " ")
    	'response.write(a(1))
    end if	
    		%>
    
    	  
    </a></td>
    There are two sqls: sql and sql2. The first table has url extensions:
    Code:
    Book=1&Chapter=1&Verse=all&Keyword=make/man/image/like/creeping%20thing&Keywordb=god/creat&Keywordc=&Keywordd=&Keyworde=&Keywordf=
    And the second table has "book" and "chapter" among its fields. I was hoping that by adding the sql2 in the if statement I would get distinct results. But it gives the result of the first record to everything.

  • #8
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,065
    Thanks
    4
    Thanked 8 Times in 8 Posts
    In VBScript you can check for Null or Empty using IsNull() and IsEmpty() functions. You do know that there is a BIG difference between NULL and EMPTY correct? NULL means it doesn't exist Empty is where there is a variable assigned to EMPTY or it is not initialized. A zero-length string asssigned like so stringA = "" is Neither NULL nor Empty.

    Run the following code and see what you are returned

    Code:
    Dim stringA
    
    If IsNull(stringA) Then 
    	Response.Write "stringA is NULL"
    ElseIf isEmpty(StringA) Then
    	Response.Write "stringA is Empty"
    Else
    	Response.Write "stringA is neither NULL nor Empty"
    End If
    Response.Write "<br>--<br>"
    stringA = NULL
    If IsNull(stringA) Then 
    	Response.Write "stringA is NULL"
    ElseIf isEmpty(StringA) Then
    	Response.Write "stringA is Empty"
    Else
    	Response.Write "stringA is neither NULL nor Empty"
    End If
    stringA = EMPTY 
    Response.Write "<br>--<br>"
    If IsNull(stringA) Then 
    	Response.Write "stringA is NULL"
    ElseIf isEmpty(StringA) Then
    	Response.Write "stringA is Empty"
    Else
    	Response.Write "stringA is neither NULL nor Empty"
    End If
    stringA = " "
    Response.Write "<br>--<br>"
    If IsNull(stringA) Then 
    	Response.Write "stringA is NULL"
    ElseIf isEmpty(StringA) Then
    	Response.Write "stringA is Empty"
    Else
    	Response.Write "stringA is neither NULL nor Empty"
                 Response.Write "<br>The length of stringA is " & Len(stringA) & " characters.<br>"
    	Response.Write "<br>Trimming stringA makes the length of stringA " & Len(Trim(stringA)) & " characters."
    End If



    If this is a SQL Server DB then you can also use fieldName NOT IS NULL in your SQL Query to prevent displaying records with NULLS

    Like So

    Code:
    SELECT FieldName FROM TableName WHERE FieldName NOT IS NULL;
    
    or
    
    SELECT FieldName FROM TableName WHERE NOT FieldName IS NULL;

    BTW using SELECT * is a bad habit to get into. I see a lot of beginning coders that start to use it and then always use it even if they are only grabbing a handful of fields in a table with many fields. It really isn't noticed until the DB starts to grow and then when you have about 30k records in it you notice that your queries take longer and longer.


  •  

    Posting Permissions

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