href link in td of datatable when server-side processing

href link in td of datatable when server-side processing

faizalkvfaizalkv Posts: 1Questions: 1Answers: 0
edited April 2017 in Free community support

First of all, I am a beginner in coding......

I have a datatable (server-side processing) with a large number of rows....
Now, I want to add href link in td of all the tr

<a href="more_log.php?more=<?php echo $row['id']?>" class="glyphicon-plus-sign btn-success btn-block" >Add</a></td>

here $row['id'] is the first column/td in the datatable

Please help me in adding href linkin td/column of my datatable.

Code for the datatable server side is as follows

index.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>test</title>
    <link rel="stylesheet" href="//cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css"/>
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="//cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
</head>
<body>
<h1>Data Source : Server-Side processing using PHP&MySQL and Ajax</h1>
<table cellpadding="1" cellspacing="1" id="users" class="display" width="100%">
    <thead>
    <tr>
        <th>ID</th>
        <th>NAME</th>
        <th>No</th>
        <th>Rack</th>
        <th>PID</th>
        <th>PID</th>
    </tr>
    </thead>
    <tfoot>
    <tr>
        <th>ID</th>
        <th>NAME</th>
        <th>No</th>
        <th>Rack</th>
        <th>PID</th>
        <th>PID</th>
    </tr>
    </tfoot>
</table>

<script type="text/javascript">
    $(document).ready(function () {
        $('#users').DataTable({
            "columns": [
                {"data": "id"},
                {"data": "name"},
                {"data": "no"},
                {"data": "rackno"},
                {"data": "pid"},
                {"data": "pid"}
            ],
            "processing": true,
            "serverSide": true,
            "ajax": {
                url: 'demo2.php',
                type: 'POST'
            }
        });
    });
</script>
</body>
</html>

demo2.php

<?php
/* IF Query comes from DataTables do the following */
if (!empty($_POST) ) {

    /*
     * Database Configuration and Connection using mysqli
     */

    define("HOST", "localhost");
    define("USER", "root");
    define("PASSWORD", "root123");
    define("DB", "test");
    define("MyTable", "well");

    $connection = mysqli_connect(HOST, USER, PASSWORD, DB) OR DIE("Impossible to access to DB : " . mysqli_connect_error());

    /* END DB Config and connection */

    /*
     * @param (string) SQL Query
     * @return multidim array containing data array(array('column1'=>value2,'column2'=>value2...))
     *
     */
    function getData($sql){
        global $connection ;//we use connection already opened
        $query = mysqli_query($connection, $sql) OR DIE ("Can't get Data from DB , check your SQL Query " );
        $data = array();
        foreach ($query as $row ) {
            $data[] = $row ;
        }
        return $data;
    }

    /* Useful $_POST Variables coming from the plugin */
    $draw = $_POST["draw"];//counter used by DataTables to ensure that the Ajax returns from server-side processing requests are drawn in sequence by DataTables
    $orderByColumnIndex  = $_POST['order'][0]['column'];// index of the sorting column (0 index based - i.e. 0 is the first record)
    $orderBy = $_POST['columns'][$orderByColumnIndex]['data'];//Get name of the sorting column from its index
    $orderType = $_POST['order'][0]['dir']; // ASC or DESC
    $start  = $_POST["start"];//Paging first record indicator.
    $length = $_POST['length'];//Number of records that the table can display in the current draw
    /* END of POST variables */

    $recordsTotal = count(getData("SELECT * FROM ".MyTable));

    /* SEARCH CASE : Filtered data */
    if(!empty($_POST['search']['value'])){

        /* WHERE Clause for searching */
        for($i=0 ; $i<count($_POST['columns']);$i++){
            $column = $_POST['columns'][$i]['data'];//we get the name of each column using its index from POST request
            $where[]="$column like '%".$_POST['search']['value']."%'";
        }
        $where = "WHERE ".implode(" OR " , $where);// id like '%searchValue%' or name like '%searchValue%' ....
        /* End WHERE */

        $sql = sprintf("SELECT * FROM %s %s", MyTable , $where);//Search query without limit clause (No pagination)

        $recordsFiltered = count(getData($sql));//Count of search result

        /* SQL Query for search with limit and orderBy clauses*/
        $sql = sprintf("SELECT * FROM %s %s ORDER BY %s %s limit %d , %d ", MyTable , $where ,$orderBy, $orderType ,$start,$length  );
        $data = getData($sql);
    }
    /* END SEARCH */
    else {
        $sql = sprintf("SELECT * FROM %s ORDER BY %s %s limit %d , %d ", MyTable ,$orderBy,$orderType ,$start , $length);
        $data = getData($sql);

        $recordsFiltered = $recordsTotal;
    }

    /* Response to client before JSON encoding */
    $response = array(
        "draw" => intval($draw),
        "recordsTotal" => $recordsTotal,
        "recordsFiltered" => $recordsFiltered,
        "data" => $data
    );

    echo json_encode($response);

} else {
    echo "NO POST Query from DataTable";
}
?>

Answers

  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin

    If the a tag needs to be generated, I would suggest that you use a renderer to do it. See also columns.render which includes an example.

    Allan

  • aykin0691aykin0691 Posts: 1Questions: 0Answers: 0
    edited November 2018

    $columns = array(
    array(
    'db' => 'id',
    'dt' => 0,
    'formatter' => function( $d, $row ) {
    return '<a href="more_log.php?more=' . '$d' . '">' . $d . '</a>';
    }
    ),
    array( 'db' => 'another_column', 'dt' => 1 ),
    array( 'db' => 'another_column', 'dt' => 2 ),
    array( 'db' => 'another_column', 'dt' => 3 ),
    array( 'db' => 'another_column', 'dt' => 4 ),
    );

    i know its too late but give it a try

This discussion has been closed.