I'm currently working on a very important project and I'm almost done.
But I'm stuck on the final part of the core function of the website... I want to know how to retrieve data from three columns: "Title", "Description", "Link" from my database and print the "Title" and "Description" rows to a <div></div> and i want to use the $("#anchor").attr("href", link) to the link attribute <div><a href = "" id = "anchor"></a></div> my problem is that it only executes the Javascript code on the first row in the database table.
<html>
<head>
<title>test001</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<style>
#load1 {
background-color: green;
height: 100px;
width: 100px;
}
#load2 {
background-color: green;
height: 100px;
width: 100px;
}
</style>
<div id="divone">
<div id="load">
<div id="load1">
</div>
<div id="load2">
</div>
<div>
<a href="" id="load3" target="_ blank">
Test
</a>
</div>
</div>
</div>
<script>
function clone() {
var div = document.getElementById("load");
var cln = div.cloneNode(true);
document.getElementById("divone").appendChild(cln);
}
</script>
<?php
$conn = mysqli_connect("localhost", "id5757217_testsql01", "9977553311", "id5757217_testsql1");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM insertion";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ( $row = $result->fetch_assoc() ) {
$array[] = $row;
$id = $row['ID'];
$tle = $row['Title'];
$des = $row['Description'];
$lnk = $row['Link'];
echo "<script type = text/javascript>clone()</script>";
}
} else { echo "0 results"; }
$conn->close();
?>
<script>
var id = "<?php echo $id; ?>";
var title = "<?php echo $tle; ?>";
$("#load1").text(title);
var desc = "<?php echo $des; ?>";
$("#load2").text(desc);
var lnk = "<?php echo $lnk; ?>";
$("#load3").attr("href", lnk);
</script>
</body>
</html>
clone()function is creating duplicate IDs. IDs have to be unique.