taneya Posted July 13, 2007 Share Posted July 13, 2007 I am a real novice w/ PHP. If I have a field in my database table that is a link (for example - http://www.phpfreaks.com), is there a way to use PHP to turn that URL into a link when displayed on my php page? I'd prefer not to have to the a href tag and info around the link itself into the database table. I certainly welcome any suggestions. Taneya Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 13, 2007 Share Posted July 13, 2007 <a href="<?=page.php?>">your data</a> or <a href="<?=$varibla?>">your data</a> or echo '<a href="page.php">your data</a>'; or echo '<a href="'.$variable.php.'">your data</a>'; hmm does it make sense Quote Link to comment Share on other sites More sharing options...
Glyde Posted July 13, 2007 Share Posted July 13, 2007 Take this example script: <?php $_socket = mysql_connect('server', 'username', 'password'); mysql_select_db('database'); $result = mysql_query('your select query'); while ($row = mysql_fetch_assoc($result)) { print '<a href="' . $result['yourDatabaseLinkColumn'] . '">' . $result['yourDatabaseLinkColumn'] . '</a>'; } ?> Simply change yourDatabaseLinkColumn to the name of the column in your database that corresponds to the link field. Quote Link to comment Share on other sites More sharing options...
taneya Posted July 14, 2007 Author Share Posted July 14, 2007 Thank you both for the suggestions! I plan to get back to this tonight and will try it out. May I ask another question though? Let's say one field is site name and another field is URL. What would the code look like if I wanted to show the text of the site name, but make that text a link using the URL in the other field of the table? Quote Link to comment Share on other sites More sharing options...
porta325 Posted July 14, 2007 Share Posted July 14, 2007 echo "<a href=\"$url\">$title</a>" this is how the echo should look like $url should be the URL you have in the database $title it's obvious Quote Link to comment Share on other sites More sharing options...
porta325 Posted July 14, 2007 Share Posted July 14, 2007 Make sure in the database the link containst http://www. Quote Link to comment Share on other sites More sharing options...
chigley Posted July 14, 2007 Share Posted July 14, 2007 <?php $query = mysql_query("SELECT name, url FROM table") or die(mysql_error()); while(list($name, $url) = mysql_fetch_row($query)) { echo "<a href=\"{$url}\">{$name}</a><br />\n"; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.