For a new page, fetch only the fields you need with PDO and escape database text before writing it into HTML. Native <details> and <summary> elements can provide an expandable record interface without custom show/hide JavaScript.
<?php
$stmt = $pdo->query(
'SELECT id, name, class, mark, sex FROM student ORDER BY id LIMIT 10'
);
foreach ($stmt as $row) {
$name = htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8');
$class = htmlspecialchars($row['class'], ENT_QUOTES, 'UTF-8');
$sex = htmlspecialchars($row['sex'], ENT_QUOTES, 'UTF-8');
echo "<details>";
echo "<summary>{$name}</summary>";
echo "<p>Class: {$class}; Mark: " . (int) $row['mark'] . "; Sex: {$sex}</p>";
echo "</details>";
}
?>
The original div-and-JavaScript version remains below because it demonstrates manual toggling and DOM updates. For production output, avoid unquoted array keys such as $row[id] and escape user/database text before rendering it.
<style type="text/css">
div {
display: none;
}
</style>
function display_detail(id){
var sid='s'+id;
if( document.getElementById(id).style.display == 'inline' ){
If the details layer is not displayed then we will display the layer by managing style.display property. We will also change the symbol to - giving option to user to close the opened details.
document.getElementById(id).style.display = 'inline';// show details
If details are displayed then we will hide the div layer by managing the style display property. function display_detail(id){
var sid='s'+id;
if( document.getElementById(id).style.display == 'inline' ){
document.getElementById(id).style.display = 'none'; // Hide the details div
document.getElementById(sid).innerHTML = '+'; // Change the symbol to +
}else {
document.getElementById(id).style.backgroundColor = '#ffff00'; // Add different color to background
document.getElementById(id).style.display = 'inline'; // show the details
document.getElementById(sid).innerHTML = '-'; //Change the symbol to -
} // end of if else
} // end of function
require "config.php"; // MySQL connection string
You can get full details of MySQL PDO connection string here.
$count="SELECT name,id,class,mark,sex FROM student LIMIT 10";
Now we will execute the query and display the records. Note each record has two div layers and as per the style property all layers are hidden. So we have given special inline style property to display our symbols.
echo "<table>";
foreach ($dbo->query($count) as $row) {
$sid='s' . $row[id];
echo "<tr ><td><div id='$sid' style="display:inline" onclick=display_detail($row[id])> + </div> $row[name]</td></tr>";
echo "<tr ><td><div id=$row[id]><b>Class</b> : $row[class] <b> Mark </b>: $row[mark] <b>SEX</b> : $row[sex]</div></td></tr>";
}
echo "</table>";
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| koushik pramanik | 22-11-2014 |
| great thanks to solve different type of problems. i am grateful of this site | |