2017-09-12 13 views
2

私はこのコードをサイトに持っていて、ページネーションと検索で動作します。私が望むのは、データテーブルで選択されている特定のデータの編集と削除機能にリンクするアイコンまたはボタンを列に追加することです。PHPを使用して1つの列にのみ編集と削除ボタンを追加するには?

<?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 
 
\t 0 =>'id', 
 
\t 1 => 'facility', 
 
\t 2=> 'price', 
 
\t 3=> 'action' 
 
\t 
 

 
); 
 

 
// 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 
 
\t $sql.=" AND (id LIKE '".$requestData['search']['value']."%' ";  
 
\t $sql.=" OR facility LIKE '".$requestData['search']['value']."%' "; 
 

 
\t $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 */ \t 
 
$query=mysqli_query($conn, $sql); 
 

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

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

 

 

 
$json_data = array(
 
\t \t \t "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. 
 
\t \t \t "recordsTotal" => intval($totalData), // total number of records 
 
\t \t \t "recordsFiltered" => intval($totalFiltered), // total number of records after searching, if there is no searching then totalFiltered = totalData 
 
\t \t \t "data"   => $data // total data array 
 
\t \t \t); 
 

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

 
?>

私は私はそれが編集のために働くとアイコン/ボタンを削除するように置く必要があるかわからない、上記のコードのこの部分に問題が生じています。..

$ nestedData [] = $ row ["id"]; - これで、施設のIDが表示されます。これは、実行したい機能へのリンクである2つのアイコン/ボタンになります。

+0

バージョンPHP <= 5.4.0は、 を定義( 'JSON_UNESCAPED_UNICODE')するか、( 'JSON_UNESCAPED_UNICODE'、256)を定義する必要があります。 json_encode($ json_data、JSON_UNESCAPED_UNICODE); –

+0

あなたはAjaxだけでそれを行うことができます。 [このリンクをチェック](https://stackoverflow.com/a/19323136/7189547) – proofzy

答えて

1

あなたの$nestedData[] = $row["id"];ページを編集するリダイレクトしたい場合は、タグを追加することができます

$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>'; 

または

$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>'; 

に変更することができます。

+0

これは機能します。私はボタンのリンクを変更するだけです。ありがとうございました。それは多くの助け:) – user8540439

+0

あなたは大歓迎です:) –

関連する問題