PDA

View Full Version : Returning a field as hyperlink


Seanus
03-15-2005, 11:32 PM
Please could someone help me - I am quite new to ASP and have a databse with urls in which I want to display on the page as a hyperlink. My code is as follows. I do not know how to get it to write an active, clickable link.

Code:

<%
'Dimension variables
Dim adoCon 'Holds the Database Connection Object
Dim rsimsref 'Holds the recordset for the records in the database
Dim strSQL 'Holds the SQL query for the database

'Create an ADO connection object
Set adoCon = Server.CreateObject("ADODB.Connection")

'Set an active connection to the Connection object using a DSN-less connection
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("imsref.mdb")

'Create an ADO recordset object
Set rsimsref = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "select referral.URL, referral.Description FROM referral;"

'Open the recordset with the SQL query
rsimsref.Open strSQL, adoCon

'Loop through the recordset
Do While not rsimsref.EOF

'Write the HTML to display the current record in the recordset


Response.Write ("</a>")
Response.Write ("<br>")
Response.write (rsimsref("URL"))
Response.Write ("</a>")
Response.Write ("<br>")
Response.Write (rsimsref("Description"))
Response.Write ("</a>")
Response.Write ("<br>")
Response.Write ("a href=""pullinfo.asp?ID="(rsimsref,"URL") & "URL")


'Move to the next record in the recordset
rsimsref.MoveNext

Loop

'Reset server objects
rsimsref.Close
Set rsimsref = Nothing
Set adoCon = Nothing
%>

Tha last response.write line is one of many that I have tried. The URLS are in a Column named URL

Please help!! :confused:

glenngv
03-16-2005, 05:11 AM
This is basically how a link looks like:

<a href="theURL">theDescription</a>

Having said that, we can now proceed to the solution.

...
'Loop through the recordset
Do While not rsimsref.EOF

'Write the HTML to display the current record in the recordset
Response.Write ("<a href=""" & rsimsref("URL") & """>" & rsimsref("Description") & "</a> <a href=""pullinfo.asp?ID=" & Server.URLEncode(rsimsref("URL")) & """>More info</a>")

'Move to the next record in the recordset
rsimsref.MoveNext
Loop
...

Seanus
03-16-2005, 10:33 AM
Thanks for the feedback - but I am getting this error when I use this code. The error is:

Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'URLEncode'
/www.dads-haven.co.uk/test/pullinfo.asp, line 55


Any more suggestions?

Thanks

glenngv
03-17-2005, 01:14 AM
That happens because the URL in the db is null. Is URL only optional in the db?
dim url

'Loop through the recordset
Do While not rsimsref.EOF
url = rsimsref("URL")
if Not IsNull(url) then
'Write the HTML to display the current record in the recordset
Response.Write ("<a href=""" & url & """>" & rsimsref("Description") & "</a> <a href=""pullinfo.asp?ID=" & Server.URLEncode(url) & """>More info</a>")
end if
'Move to the next record in the recordset
rsimsref.MoveNext
Loop
...

Seanus
03-30-2005, 09:39 AM
Thanks, I finally got it working by playing around with that code a bit.

In the table I have the full url of the sites which I want the link to - it is like this:

http://www.sitename.co.uk for insatnce. Is showing on the page as this too (which is correct as well). My question is: Ho do you get it to display only the patrt of the hyperlink (sitename.co.uk) and still get the link to go to that site. when I try do it, it tries to open tha page that is in my domain name and not on www. Is it possible to get it to display like this?

Your help is greatly appreciated.


:thumbsup:

glenngv
03-31-2005, 08:02 AM
dim url

'Loop through the recordset
Do While not rsimsref.EOF
url = rsimsref("URL")
if Not IsNull(url) then
'Write the HTML to display the current record in the recordset
Response.Write ("<a href=""" & url & """>" & replace(url, "http://www.", "") & "</a> <a href=""pullinfo.asp?ID=" & Server.URLEncode(url) & """>More info</a>")
end if
'Move to the next record in the recordset
rsimsref.MoveNext
Loop

Seanus
03-31-2005, 10:43 AM
Thanks - it has worked! Do you have a website?

:D

glenngv
03-31-2005, 11:45 AM
I have web sites but I haven't got time to really update it and show it to the world. :)

Seanus
03-31-2005, 01:01 PM
If you want, send me an e-mail to submit@dads-haven.co.uk and I may feature them on my site. Thanks