2

I have this code that I got on a site and it works on pagination and search. What I want is to add an icon or button to a column that will link to the edit and delete function for the specific data that is selected in the datatable.

<?php
/* Database connection start */
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sample";

$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());

/* Database connection end */


// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;


$columns = array( 
// datatable column index  => database column name
 0 =>'id', 
 1 => 'facility',
 2=> 'price',
 3=> 'action'
 

);

// getting total number records without any search
$sql = "SELECT id, facility, price ";
$sql.=" FROM facilities";
$query=mysqli_query($conn, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.


$sql = "SELECT id, facility, price ";
$sql.=" FROM facilities WHERE 1=1";
if( !empty($requestData['search']['value']) ) {   // if there is a search parameter, $requestData['search']['value'] contains search parameter
 $sql.=" AND ( id LIKE '".$requestData['search']['value']."%' ";    
 $sql.=" OR facility LIKE '".$requestData['search']['value']."%' ";

 $sql.=" OR price LIKE '".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql);
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result. 
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc  */ 
$query=mysqli_query($conn, $sql);

$data = array();
while( $row=mysqli_fetch_array($query) ) {  // preparing an array
 $nestedData=array(); 

 $nestedData[] = $row["id"];
 $nestedData[] = $row["facility"];
 $nestedData[] = $row["price"];
 $nestedData[] = $row["id"];
 
 
 $data[] = $nestedData;
}



$json_data = array(
   "draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
   "recordsTotal"    => intval( $totalData ),  // total number of records
   "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
   "data"            => $data   // total data array
   );

echo json_encode($json_data);  // send data as json format

?>

I am having problem on this part of the code above, I don't know what should I put so that it will work for the edit and delete icon/button..

$nestedData[] = $row["id"]; - with this, it shows the id of the facility, I want it to be the two icon/button that is link to the function I want to perform.

user8540439
  • 47
  • 1
  • 9
  • Try this for your JSON: version PHP <= 5.4.0 should put : defined('JSON_UNESCAPED_UNICODE') or define('JSON_UNESCAPED_UNICODE', 256); json_encode($json_data,JSON_UNESCAPED_UNICODE); – Joel Garcia Nuño Sep 12 '17 at 06:55
  • You can do it with Ajax simply. [Check this link](https://stackoverflow.com/a/19323136/7189547) – proofzy Sep 12 '17 at 06:57

1 Answers1

1

You can change your $nestedData[] = $row["id"]; to

$nestedData[] = '<button type="button" class="btn btn-success" id="edit-'.$row["id"].'">Edit</button> <button type="button" class="btn btn-danger" id="delete-'.$row["id"].'">Delete</button>';

or

$nestedData[] = '<a href="edit.php" class="btn btn-success" id="edit-'.$row["id"].'">Edit</a> <button type="button" class="btn btn-danger" id="delete-'.$row["id"].'">Delete</button>';

You can add tag, if you want to redirect to edit page.

Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104