akomaz Posted February 8, 2010 Share Posted February 8, 2010 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); ?> Quote Link to comment Share on other sites More sharing options...
schilly Posted February 8, 2010 Share Posted February 8, 2010 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. Quote Link to comment Share on other sites More sharing options...
akomaz Posted February 8, 2010 Author Share Posted February 8, 2010 Thanks that seems like a good solution but how do I assign each cell to its corresponding value? Quote Link to comment Share on other sites More sharing options...
schilly Posted February 8, 2010 Share Posted February 8, 2010 replace $cell with $row['column_name'] Quote Link to comment Share on other sites More sharing options...
akomaz Posted February 8, 2010 Author Share Posted February 8, 2010 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)) 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.