【发布时间】:2019-09-10 17:24:26
【问题描述】:
我正在尝试创建一个从 SQL 数据库中提取数据的表,并且表上的每一行都是指向其相应页面的可点击链接,具体取决于数据库中的第一列数据。
我已经设法分别完成了这两个部分,即使用 SQL 数据创建了一个表,并且我已经成功地创建了一个表,其中每一行都是可点击的链接,但是我正在努力将两者结合起来。
我正在使用 .js 来监听点击并根据“tr data-href='url'”将您发送出去。
我创建表格 SQL 数据的 php 脚本首先创建“tr”,然后用每个单元格填充行,关闭“/tr”并为所有行重复。
如何让 php 写入“tr data-href='url'”,其中 'url' 与每一行的第一列数据相同?
下面是我的PHP脚本,后面是五行JS。
<?php
$host = "localhost";
$user = "user";
$pass = "pass";
$db_name = "FFD";
//create connection
$connection = mysqli_connect($host, $user, $pass, $db_name);
//test if connection failed
if(mysqli_connect_errno()){
die("connection failed: "
. mysqli_connect_error()
. " (" . mysqli_connect_errno()
. ")");
}
//get results from database
$result = mysqli_query($connection,"SELECT id,name,location,plots,blurb FROM Sites");
$all_property = array(); //declare an array for saving property
//showing property
echo '
<div class="content-container">
<h1>Table heading</h1>
<section>
<div class="tbl-header">
<table cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>'; //initialize table tag
while ($property = mysqli_fetch_field($result)) {
echo '<th>' . $property->name . '</th>'; //get field name for header
array_push($all_property, $property->name); //save those to array
}
echo '</tr>
</thead>
</table>
</div>
<div class="tbl-content">
<table cellpadding="0" cellspacing="0" border="0">
<tbody>';
//end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
foreach ($all_property as $item) {
echo '<td>' . $row[$item] . '</td>'; //get items using property value
}
echo '</tr>';
}
echo '</tbody></table></div></section></div>';
?>
$(document).ready(function(){
$('tr[data-href]').on("click", function() {
document.location = $(this).data('href');
});
});
我可能只是太厚了,错过了一些简单明了的东西。
提前致谢!
【问题讨论】:
标签: javascript php html sql