Reading Data as a link

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
flash_trader
Forum Newbie
Posts: 3
Joined: Mon Mar 07, 2005 3:09 am

Reading Data as a link

Post by flash_trader »

Hello there, I have a question regarding data within the database and html links.

I want to read part of the data that is in a field as a html link when I display it on my website.

What is the best way to do this?

For example lets say I have a companie name:
Microsoft

I want there that to be linked to http://www.mircosoft.com when it is read on the website.

Do I have to also store http://www.mircosoft.com in another table field called URL? or is there another way to link data when it comes out of the database and read on the website?

thanks and advance
Flash
User avatar
Jean-Yves
Forum Contributor
Posts: 148
Joined: Wed Jul 02, 2003 2:13 pm
Location: West Country, UK

Post by Jean-Yves »

Try this:

Code: Select all

$query = "SELECT CompanyName, URLName FROM MyTable WHERE MyID = $id";

$result = mysql_query($query) or die(mysql_error());

if(mysql_num_rows == 0)
  echo "No record found for that ID";
else{
  $dbRow = mysql_fetch_array($result);
  echo "<a href='" . $dbRow&#1111;"URLName"] . "'>" . $dbRow&#1111;"CompanyName"] . "</a>\n";
&#125;

Btw, I always use mysql_fetch_array(), although I have a feeling that it's probably not the most efficient of the mysql_fetch_xxx functions?
flash_trader
Forum Newbie
Posts: 3
Joined: Mon Mar 07, 2005 3:09 am

My current Code

Post by flash_trader »

Here is my current code: How can I work what you had into this:

Code: Select all

$result = mysql_query("SELECT *,((priceasof - profileprice)/profileprice * 100) AS percent FROM test");
mysql_close();

echo "<table border='1' style='font-family: verdana; font-size: 11px; border-style: soild' cellpadding='0' cellspacing='1'>";
echo "<tr> <th style='font-family: verdana';>Name</th> <th>Symbol</th> <th>Date</th> <th>Profiled Price</th> <th>Price as of 02-28-05</th> <th>Change</th></tr>";

while($row = mysql_fetch_array( $result )) &#123;

echo "<tr><td width=18%>";
echo $row&#1111;'name'];
echo "</td><td width=8%>";
echo $row&#1111;'symbol'];
echo "</td><td width=10%>";
echo $row&#1111;'date'];
echo "</td><td width=8%>";
echo $row&#1111;'profileprice'];
echo "</td><td width=10%>";
echo $row&#1111;'priceasof'];
echo "</td><td width=10%>";
        $font = "<font color=#000000>";
        if($row&#1111;'percent'] < 0) &#123;
        $font = "<font color=#ff0000>";
    &#125;
         if($row&#1111;'percent'] > 0) &#123;
        $font = "<font color=#00ff00>";
    &#125;
echo $font;
echo round($row&#1111;'percent'], 2);
echo "%</td></tr>";

&#125;
echo "</table>"; 


?>
Thanks a lot for your help
User avatar
Jean-Yves
Forum Contributor
Posts: 148
Joined: Wed Jul 02, 2003 2:13 pm
Location: West Country, UK

Post by Jean-Yves »

First, you need to move the mysql_close() to after the mysql_fetch_array() while() loop.

Then, simply add another <th> and another <td> set of tags, as you currently have. You will of course need to add the URL field into your table first.

You can use the code that I detailed above for your <td> tag - so basically the part where you construct the <a> tag would be within the new <td> tag.
flash_trader
Forum Newbie
Posts: 3
Joined: Mon Mar 07, 2005 3:09 am

<td> tag example

Post by flash_trader »

Would you be able to show me a full example of the new td and th tags, I am not clear on what it should look like.

I did move the mysql close(); thanks for pointing that out!

Thanks
Post Reply