Jump to content

building table with details hyperlinks


akomaz

Recommended Posts

Hi there!  I am running a test table that pulls values from mysql and I am having a lot of trouble trying to form a dynamic link with my table values. 

 

First column is primary key.  I am looking to enable the text values of the second column to be hyperlinked to an automated details page instead of creating a separate "VIEW DETAILS" column in the table. 

 

Since I currently have all cell values processed in one line they are all hyperlinked.  Can anyone advise on how to construct my table by allowing only the values in the second column to be hyperlinked to my details page?  Thanks!

 

<?php
$db_host = 'localhost';
$db_user = '****';
$db_pwd = '****';
$database = 'beta';
$table = 'dbtst';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Unable to connect to database");

if (!mysql_select_db($database))
    die("No database was selected");

$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
    die("Query failed");
}

$fields_num = mysql_num_fields($result);

echo "<h1>Table Build Test</h1>";
echo '<table STYLE="border-collapse: collapse; border: 1px solid #aaaaaa; font: normal 80%/140% arial, helvetica, sans-serif;
color: #555; background: #dfdfdf; margin: 10px" cellpadding="6px"><thead>';

for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</thead>\n";

while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    foreach($row as $cell)
	echo '<td><a href="details.php">' .  $cell  . '</a></td>';

    echo "</tr>\n";

}

mysql_free_result($result);
?>

Link to comment
https://forums.phpfreaks.com/topic/191399-building-table-with-details-hyperlinks/
Share on other sites

do this instead:

echo "<tr>";

echo '<td><a href="details.php">' .  $cell1  . '</a></td>';
echo '<td>' . $cell2  . '</td>';
echo '<td>' .  $cell3  . '</td>';
echo '<td>' .  $cell4  . '</td>';
echo '<td>' .  $cell5  . '</td>';
echo '<td>' .  $cell6  . '</td>';

    echo "</tr>\n";

 

that way you can pick which cell gets the link.

 

Thanks for your help Schilly.

 

I was able to get it working by switching the $cell variables to $row['column'] as you suggested. 

 

For those with similar issues I also needed to remove the foreach clause and change the while statement to fetch an array instead of row to have it display correctly.

 

while($row = mysql_fetch_row($result))

 

changed to:

 

while($row = mysql_fetch_array($result))

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.