...

View Full Version : replace function doesn't work



gilgalbiblewhee
06-19-2007, 10:03 PM
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.


<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)
%>

miranda
06-19-2007, 11:36 PM
Why reinvent the wheel???

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

Replace(strToSearch,strToSearchFor,strToReplaceWith)

javabits
06-20-2007, 12:10 AM
documentation for VBScript's Replace function below:

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

semper fi...

gilgalbiblewhee
06-20-2007, 01:43 AM
I was looking into that. Do you put

dim txt
txt="This is a beautiful day!"
document.write(Replace(txt,"beautiful","horrible"))

in script tags or what?

Baleric
06-20-2007, 11:22 AM
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.



<%dim txt
txt="This is a beautiful day!"

dim txtAfterReplace
txtAfterReplace = Replace(txt,"beautiful","horrible")
%>
<% = txtAfterReplace %>

gilgalbiblewhee
06-20-2007, 02:37 PM
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.

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

gilgalbiblewhee
06-20-2007, 03:46 PM
I'm trying to write the sql:

'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:

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.

miranda
06-20-2007, 06:59 PM
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



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



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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum