-2

Environment: MySQL Db, 3 columns ID, Subject, URL

Page I am Creating is a PHP page.

My HTML Code is:-

Additional Links (Under Development)

<div style="visibility:visible;">
    <?php
    $mysql_hostname = "localhost";
    $mysql_user     = "user";
    $mysql_password = "password";
    $mysql_database = "MyDB";
    $bd             = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Oops some thing went wrong");
    mysql_select_db($mysql_database, $bd) or die("Oops some thing went wrong");// we are now connected to database

    $result = mysql_query("SELECT * FROM `Colorado` WHERE `id` ORDER BY RAND() LIMIT 10");  // selecting data through mysql_query()

    echo '<table border=0px>';  // opening table tag
    echo'<th>Subject</th><th>Url</th>'; //table headers

    while($data = mysql_fetch_array($result))
    {
    echo'<tr>'; // printing table row
    echo '<td>'.$data['Subject'].'</td><td><a href="'.$data['Url'].'">'.$data['Url'].'</a></td></td> '; // we  are looping all data to be printed till last row in the table
    echo'</tr>'; // closing table row
    }
    echo '</table>';  //closing table tag
    ?>

This results in producing a two column table with the Subject Column on the left and the Url Column on the right. Perfect! So far...

What I cannot find out is how to make the Url Column a Hyperlink. There seems to be a lot of suggestions out there, most far more complicated than I need. Can anyone show me where and what code I am missing? Or, is there a better way to call the table data and make the second column a hyper column?

Please show how you would do it!

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98

1 Answers1

0

Change

echo '<td>'.$data['Subject'].'</td><td>'.$data['Url'].'</td></td> ';

to

echo '<td>'.$data['Subject'].'</td><td><a href="'.$data['Url'].'">'.$data['Url'].'</a></td></td> ';

Interpolating the string would make it a little more readable:

echo "<td>{$data['Subject']}</td><td><a href='{$data['Url']}'>{$data['Url']}</a></td></td>";

Note that this will make the Url a hyperlink, but the table column itself will not be clickable; only the text. You can make the cell clickable with a little CSS; refer to this question: Making a TD clickable

Community
  • 1
  • 1
mlg
  • 1,447
  • 15
  • 19