我需要将超链接添加到我的动态数据表
I need to add hyperlinks to my Dynamic DataTables
我有一个 table 从我的数据库中提取数据并从数据库 table 中显示它。我想在狗名中添加 hyperlinks。我找到的所有文档都显示了如何使用静态数据而不是下面的动态示例:
var responseObj = [
{ "information": "A1", "weblink": "http://www.microsoft.com" },
{ "information": "A2", "weblink": "http://www.yahoo.com" },
{ "information": "A3", "weblink": "http://www.google.com" },
{ "information": "A4", "weblink": "http://www.duckduckgo.com" }
];
$('#example').dataTable({
"data": responseObj,
"columns": [
{ "data": "information" },
{
"data": "weblink",
"render": function(data, type, row, meta){
if(type === 'display'){
data = '<a href="' + data + '">' + data + '</a>';
}
return data;
}
}
]
});
我需要能够 select 数据并将其转到我的 link。这是我当前的代码。
$(document).ready( function () {
var dataTable=$('#example').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
url:"fetch.php",
type:"post"
}
});
} );
</script
在我的 fetch.php 我有这个。
$request = $_REQUEST;
$col = array (
1 => 'Dog_Name',
0 => 'Dog_Number',
2 => 'Breed',
3 => 'Sex'
);
$sql ="SELECT * FROM dog_profiles";
$query=mysqli_query($conn,$sql);
$totalData=mysqli_num_rows($query);
$totalFilter=$totalData;
//Search
$sql =("SELECT * FROM dog_profiles");
if(!empty($request['search']['value'])){
$sql.=" WHERE (Dog_Number Like '".$request['search']['value']."%' ";
$sql.=" OR Dog_Name Like '".$request['search']['value']."%' ";
$sql.=" OR Breed Like '".$request['search']['value']."%' ";
$sql.=" OR Sex Like '".$request['search']['value']."%' )";
}
$query=mysqli_query($conn,$sql);
$totalData=mysqli_num_rows($query);
//Order
$sql.=" ORDER BY ".$col[$request['order'][0]['column']]." ".$request['order'][0]['dir']." LIMIT ".
$request['start']." ,".$request['length']." ";
$query=mysqli_query($conn,$sql);
$data = array();
while($row=mysqli_fetch_array($query)){
$subdata=array();
$subdata[]=$row[0]; //dog number
$subdata[]=$row[1]; //name
$subdata[]=$row[2]; //breed
$subdata[]=$row[3]; //sex
$data[]=$subdata;
}
$json_data=array(
"draw" => intval($request['draw']),
"recordsTotal" => intval($totalData),
"recordsFiltered" => intval($totalFilter),
"data" => $data
);
echo json_encode($json_data);
任何见解都会有所帮助我对使用 Datatables 很陌生。
我认为您需要更新 while 循环中的代码。
试试下面的代码,
while($row=mysqli_fetch_array($query)){
$subdata=array();
$subdata[]=$row[0]; //dog number
$subdata[]="<a href='".$row[1]."'>".$row[1]."</a>";
$subdata[]=$row[2]; //breed
$subdata[]=$row[3]; //sex
$data[]=$subdata;
}
我有一个 table 从我的数据库中提取数据并从数据库 table 中显示它。我想在狗名中添加 hyperlinks。我找到的所有文档都显示了如何使用静态数据而不是下面的动态示例:
var responseObj = [
{ "information": "A1", "weblink": "http://www.microsoft.com" },
{ "information": "A2", "weblink": "http://www.yahoo.com" },
{ "information": "A3", "weblink": "http://www.google.com" },
{ "information": "A4", "weblink": "http://www.duckduckgo.com" }
];
$('#example').dataTable({
"data": responseObj,
"columns": [
{ "data": "information" },
{
"data": "weblink",
"render": function(data, type, row, meta){
if(type === 'display'){
data = '<a href="' + data + '">' + data + '</a>';
}
return data;
}
}
]
});
我需要能够 select 数据并将其转到我的 link。这是我当前的代码。
$(document).ready( function () {
var dataTable=$('#example').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
url:"fetch.php",
type:"post"
}
});
} );
</script
在我的 fetch.php 我有这个。
$request = $_REQUEST;
$col = array (
1 => 'Dog_Name',
0 => 'Dog_Number',
2 => 'Breed',
3 => 'Sex'
);
$sql ="SELECT * FROM dog_profiles";
$query=mysqli_query($conn,$sql);
$totalData=mysqli_num_rows($query);
$totalFilter=$totalData;
//Search
$sql =("SELECT * FROM dog_profiles");
if(!empty($request['search']['value'])){
$sql.=" WHERE (Dog_Number Like '".$request['search']['value']."%' ";
$sql.=" OR Dog_Name Like '".$request['search']['value']."%' ";
$sql.=" OR Breed Like '".$request['search']['value']."%' ";
$sql.=" OR Sex Like '".$request['search']['value']."%' )";
}
$query=mysqli_query($conn,$sql);
$totalData=mysqli_num_rows($query);
//Order
$sql.=" ORDER BY ".$col[$request['order'][0]['column']]." ".$request['order'][0]['dir']." LIMIT ".
$request['start']." ,".$request['length']." ";
$query=mysqli_query($conn,$sql);
$data = array();
while($row=mysqli_fetch_array($query)){
$subdata=array();
$subdata[]=$row[0]; //dog number
$subdata[]=$row[1]; //name
$subdata[]=$row[2]; //breed
$subdata[]=$row[3]; //sex
$data[]=$subdata;
}
$json_data=array(
"draw" => intval($request['draw']),
"recordsTotal" => intval($totalData),
"recordsFiltered" => intval($totalFilter),
"data" => $data
);
echo json_encode($json_data);
任何见解都会有所帮助我对使用 Datatables 很陌生。
我认为您需要更新 while 循环中的代码。
试试下面的代码,
while($row=mysqli_fetch_array($query)){
$subdata=array();
$subdata[]=$row[0]; //dog number
$subdata[]="<a href='".$row[1]."'>".$row[1]."</a>";
$subdata[]=$row[2]; //breed
$subdata[]=$row[3]; //sex
$data[]=$subdata;
}