我正在尝试将我的PHP代码转换为OOP。我正在使用带有jquery en ajax + JSON的Datatables将项目加载到表中。但我想将此代码转换为OOP,但我总是得到一个无效的json响应。我是新写的OOP,所以任何人都可以帮我解决我的代码错误吗?我试图围绕这个编码构建一个类。任何人都可以为我帮助或制作骨架,所以我知道我做错了什么? 这是代码:
/**
* Class project
*/
class Project {
private $sql
// Get project (and id)
$project = '';
$id = '';
if (isset($_GET['project'])){
$project = $_GET['project'];
if ($project == 'get_projecten' ||
$project == 'get_project' ||
$project == 'add_project' ||
$project == 'edit_project' ||
$project == 'delete_project'){
if (isset($_GET['id'])){
$id = $_GET['id'];
if (!is_numeric($id)){
$id = '';
}
}
} else {
$project = '';
}
}
// Prepare array
$mysql_data = array();
// Valid job found
if ($project != ''){
// Connect to database
$db_connection = mysqli_connect($db_server, $db_username, $db_password, $db_name);
if (mysqli_connect_errno()){
$result = 'error';
$message = 'Failed to connect to database: ' . mysqli_connect_error();
$project = '';
}
// Execute job
public function get_projecten ($project == 'get_projecten'){
// Get projecten
$sql= "SELECT * FROM projects ORDER BY projectid";
$query = mysqli_query($db_connection, $sql);
if (!$query){
$result = 'error';
$message = 'query error';
}
else {
$result = 'success';
$message = 'query success';
while ($project = mysqli_fetch_array($query)){
$test = '<a href="tasks.php" data-name="' . $project['projectnaam'] . '">' . $project['projectnaam'] . '</a>';
$functions = '<div class="function_buttons"><ul>';
$functions .= '<li class="function_edit"><a data-id="' . $project['projectid'] . '" data-name="' . $project['projectnaam'] . '"><span>Edit</span></a></li>';
$functions .= '<li class="function_delete"><a data-id="' . $project['projectid'] . '" data-name="' . $project['projectnaam'] . '"><span>Delete</span></a></li>';
$functions .= '</ul></div>';
$mysql_data[] = array(
"projectid" => $project['projectid'],
"projectnaam" => $test,
"startdatum" => $project['startdatum'],
"einddatum" => $project['einddatum'],
"omschrijving" => $project['omschrijving'],
"functions" => $functions
);
}
}
} public function get_project ($project == 'get_project'){
if ($id == ''){
$result = 'error';
$message = 'id missing';
} else {
$sql = "SELECT * FROM projects WHERE projectid = '" . mysqli_real_escape_string($db_connection, $id) . "'";
$query = mysqli_query($db_connection, $sql);
if (!$query){
$result = 'error';
$message = 'query error';
} else {
$result = 'success';
$message = 'query success';
while ($project = mysqli_fetch_array($query)){
$mysql_data[] = array(
"projectid" => $project['projectid'],
"projectnaam" => $project['projectnaam'],
"startdatum" => $project['startdatum'],
"einddatum" => $project['einddatum'],
"omschrijving" => $project['omschrijving']
);
}
}
}
} public function add_project ($project == 'add_project'){
// Add project
$projectid = $_GET['projectid'];
$projectnaam =$_GET['projectnaam'];
$startdatum =$_GET['startdatum'];
$einddatum =$_GET['einddatum'];
$omschrijving =$_GET['omschrijving'];
$sql = "INSERT INTO projects (projectid, projectnaam, startdatum, einddatum, omschrijving) VALUES (".$projectid.",'".$projectnaam."', '".$startdatum."', '".$einddatum."', '".$omschrijving."')";
$query = mysqli_query($db_connection, $sql);
if (!$query){
$result = 'error';
$message = 'query error';
} else {
$result = 'success';
$message = 'query success';
}
}
public function edit_project ($project == 'edit_project'){
// Edit project
if ($id == ''){
$result = 'error';
$message = 'id missing';
} else {
$projectid = $_GET['projectid'];
$projectnaam =$_GET['projectnaam'];
$startdatum =$_GET['startdatum'];
$einddatum =$_GET['einddatum'];
$omschrijving =$_GET['omschrijving'];
$sql = "UPDATE projects SET projectnaam='".$projectnaam."', startdatum='".$startdatum."', einddatum='".$einddatum."', omschrijving='".$omschrijving."' WHERE projectid =".$projectid;
$query = mysqli_query($db_connection, $sql);
if (!$query){
$result = 'error';
$message = 'query error';
} else {
$result = 'success';
$message = 'query success';
}
}
}
public function delete_project ($project == 'delete_project'){
// Delete project
if ($id == ''){
$result = 'error';
$message = 'id missing';
} else {
$sql = "DELETE FROM projects WHERE projectid = '" . mysqli_real_escape_string($db_connection, $id) . "'";
$query = mysqli_query($db_connection, $sql);
if (!$query){
$result = 'error';
$message = 'query error';
} else {
$result = 'success';
$message = 'query success';
}
}
}
// Close database connection
mysqli_close($db_connection);
}
// Prepare data
$data = array(
"result" => $result,
"message" => $message,
"data" => $mysql_data
);
// Convert PHP array to JSON array
$json_data = json_encode($data);
echo $json_data;
?>