我需要将超链接添加到我的动态数据表中

时间:2018-11-19 02:00:39

标签: php ajax datatables jquery-datatables-editor

我有一个表,该表从数据库中提取数据并从数据库表中显示数据。我想将超链接添加到狗名。我发现的所有文档都显示了如何使用静态数据而不是下面的动态示例:

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;
         }
      } 
   ]
});

我需要能够选择数据并将其转到我的链接。这是我当前的代码。

$(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);

任何见解都会有所帮助,我对使用数据表还是很陌生的。

1 个答案:

答案 0 :(得分:0)

我认为您需要在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;
}