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
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.

 

Link to comment
Share on other sites

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))

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.