Need to create an href for a table data which is dynamically retrieved
I'm new to PHP, and I have retrieved data to a dynamically created table from a MySQL database. And I need to place a href to the first row value of the table.
For example, assume a person is named John. And once I click on the href of John, I开发者_JAVA百科 need to see his other details. I have no idea about doing it because all the columns & rows are created dynamically.
Simply do this:
$sql = mysql_query($query);
echo '<table>';
while($result = mysql_fetch_attay($sql) ) {
echo '<tr><td><a herf ="#">'.$result['first_name'].'</td></tr>';
}
echo '</table>';
So you have the first row with the name of a person and the next rows are his details.
Well, you will have to check in PHP while you loop the table if the data is the person's details and then hide it with CSS (display: none
).
Then add an ID to the row of the person name and the ID to the hidden fields.
Example:
name row: name_row
detail row: name_row_detail1, name_row_detail2, ...
Then you write JavaScript code to show those hidden details (you should use some kind of JavaScript framework; I recommend jQuery).
You add a JavaScript call in the onclick event on that row with the person's name (name_row). And in this JavaScript function you check if name_row_* are hidden or displayed, and depending on that you change its CSS to hidden or displayed.
If you are a novice, this will take a while to figure out...
You have to choices:
During the generation of the table simply add an additional output to the detail page s.th. Like:
<td><a href="/user/detail/44">John</a></td>
Or if you can't touch this part, use JavaScript. For example, with jQuery it would be something like (I am not sure if clicking simply works on a td
field):
$('#table_id > td').each( function(){
$(this).click( function(){
// Do something like retrieving an id and perform a post...
});
});
精彩评论