Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-12-2012, 03:22 PM   PM User | #1
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
Results in a link question

Hello...I'm new to programming with PHP, but as I'm going through tutorials and reading books I've come across a question I have not been able to figure out.

I have a table with 2 fields....one field is just text and the other is for a URL.

I want to have a link created that uses the url for the link and the text field for the text.

Here is the code I have so far, but I cannot get it to work:

Code:
<html>
<head>
<title>Test Page</title>
</head>
<body>
<?php
mysql_connect('hostname','username','password');
mysql_select_db('db_name') or die (mysql_error());
$result = mysql_query("SELECT * from patterns");
$row = mysql_fetch_array($result);
echo <a href="$row[lnktxt]">$row[dsptxt]</a>;
}
?>

</body>
</html>
Any help would be appreciated
mharrison is offline   Reply With Quote
Old 12-12-2012, 04:00 PM   PM User | #2
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 892
Thanks: 4
Thanked 206 Times in 205 Posts
tracknut is an unknown quantity at this point
Looks like just the echo is wrong. It should be:
PHP Code:
echo "<a href=".$row['lnktxt'].">".$row['dsptxt']."</a>"
There are lots of ways to deal with the various quotes. Keep in mind just output text (like <a href) needs quotes around it (i.e. to make it a string).

Dave
tracknut is offline   Reply With Quote
Old 12-12-2012, 04:04 PM   PM User | #3
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
Hi Dave,

Thanks for the reply. When I updated my code and try to view it, all it shows me in the web browser is this:

"$row['dsptxt'].""; } ?>
mharrison is offline   Reply With Quote
Old 12-12-2012, 04:08 PM   PM User | #4
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 892
Thanks: 4
Thanked 206 Times in 205 Posts
tracknut is an unknown quantity at this point
Could you copy in the code you're using again, including a few lines before and after this particular one?

Thanks
Dave
tracknut is offline   Reply With Quote
Old 12-12-2012, 04:11 PM   PM User | #5
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
Code:
<?php
mysql_connect('hostname','username','password');
mysql_select_db('db_name') or die (mysql_error());
$result = mysql_query("SELECT * from patterns");
$row = mysql_fetch_array($result);
echo "<a href=".$row['lnktxt'].">".$row['dsptxt']."</a>";  

}
?>
mharrison is offline   Reply With Quote
Old 12-12-2012, 04:21 PM   PM User | #6
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 892
Thanks: 4
Thanked 206 Times in 205 Posts
tracknut is an unknown quantity at this point
I forgot the quotes around the URL, try this:
PHP Code:
echo "<a href=\"".$row['lnktxt']."\">".$row['dsptxt']."</a>"
tracknut is offline   Reply With Quote
Users who have thanked tracknut for this post:
mharrison (12-12-2012)
Old 12-12-2012, 04:24 PM   PM User | #7
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
Same thing....when I replace the exact line from the code I showed you with what you gave me, the webpage only displays:

".$row['dsptxt'].""; } ?>
mharrison is offline   Reply With Quote
Old 12-12-2012, 04:35 PM   PM User | #8
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
Actually, I think I got it figured out. I renamed the page to .php instead of .html and removed the } before the ?> and it worked. Just have to add a loop into it so it displays all of the table records. Thanks a bunch for your help!
mharrison is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:05 AM.


Advertisement
Log in to turn off these ads.