javascript - 如何使用php在一列中添加编辑和删除按钮?

标签 javascript php ajax

我有从网站上获得的这段代码,它适用于分页和搜索。我想要的是将一个图标或按钮添加到一个列,该列将链接到数据表中选定的特定数据的编辑和删除功能。

<?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

?>

我在上面的这部分代码上遇到问题,我不知道应该放什么,以便它可以用于编辑和删除图标/按钮。

$nestedData[] = $row["id"]; - 有了这个,它显示了设施的 ID,我希望它是链接到我要执行的功能的两个图标/按钮。

最佳答案

您可以将您的 $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>';

如果你想重定向到编辑页面,你可以添加标签。

关于javascript - 如何使用php在一列中添加编辑和删除按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46169751/

相关文章:

javascript - 如何集成动态倒计时计数器,从 WordPress 中的自定义字段获取日期值?

javascript - 使用 socket.io 解析 cookie

php - 如何一次更新 2 个 mysql 行?

php - jQuery 自动建议

javascript - 如何在ajax请求成功回调中获取自动parseJSON数据?

javascript - 这个gpu操作的算法?

javascript - floatThead 和 bootstrap downdrop

php - Phing,调用命令将其输出放入属性中

php - 将 PHP 脚本转换为可执行文件

javascript - form.serializeArray() 的自定义键