CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Results in a link question (http://www.codingforums.com/showthread.php?t=284033)

mharrison 12-12-2012 03:22 PM

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

tracknut 12-12-2012 04:00 PM

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

mharrison 12-12-2012 04:04 PM

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'].""; } ?>

tracknut 12-12-2012 04:08 PM

Could you copy in the code you're using again, including a few lines before and after this particular one?

Thanks
Dave

mharrison 12-12-2012 04:11 PM

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>"; 

}
?>


tracknut 12-12-2012 04:21 PM

I forgot the quotes around the URL, try this:
PHP Code:

echo "<a href=\"".$row['lnktxt']."\">".$row['dsptxt']."</a>"


mharrison 12-12-2012 04:24 PM

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 12-12-2012 04:35 PM

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!


All times are GMT +1. The time now is 03:36 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.