0

I'm learning the ways off writing code in OOP, but i have a file where i do not know how to change it in oop. I have written php code before, but dont know how to convert this file in OOP cant somebody help me with a example of my code or just convert it? It's converted into a datables table with ajax and json, so everytime if i build a class around this code i get a invalid json response This is the code what i want to convert in OOP with 1 class. like a Class project.

  <?php
// Database details
$db_server   = 'localhost';
$db_username = 'mydetails';
$db_password = 'mydetails';
$db_name     = 'mydetails';

// 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
  if ($project == 'get_projecten'){

    // Get projecten
    $query = "SELECT * FROM projects ORDER BY projectid";
    $query = mysqli_query($db_connection, $query);

    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

        );
      }
    }

  } elseif ($project == 'get_project'){

    if ($id == ''){
      $result  = 'error';
      $message = 'id missing';
    } else {
      $query = "SELECT * FROM projects WHERE projectid = '" . mysqli_real_escape_string($db_connection, $id) . "'";
      $query = mysqli_query($db_connection, $query);
      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']
          );
        }
      }
    }

  } elseif ($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';
    }

  } 

    elseif ($project == 'form_tasks'){
      // Add project task

    $tasknaam = $_GET['tasknaam'];
    $taakomschrijving =$_GET['taakomschrijving'];
    $datum =$_GET['datum'];

    $sql = "INSERT INTO tasks (tasknaam, taakomschrijving, datum) VALUES (".$tasknaam.", '".$taakomschrijving."', '".$datum."')";

    $query = mysqli_query($db_connection, $sql);    
    if (!$query){
      $result  = 'error';
      $message = 'query error';
    } else {
      $result  = 'success';
      $message = 'query success';
    }

  } 
    elseif ($project == 'form_projectusers'){
      // Add project task

    $fullname = $_GET['fullname'];
    $sql = "INSERT INTO projectuser (fullname) VALUES (".$fullname.")";

    $query = mysqli_query($db_connection, $sql);    
    if (!$query){
      $result  = 'error';
      $message = 'query error';
    } else {
      $result  = 'success';
      $message = 'query success';
    }

  } 


  elseif ($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';
      }
    }

  } elseif ($project == 'delete_project'){

    // Delete project
    if ($id == ''){
      $result  = 'error';
      $message = 'id missing';
    } else {
      $query = "DELETE FROM projects WHERE projectid = '" . mysqli_real_escape_string($db_connection, $id) . "'";
      $query = mysqli_query($db_connection, $query);
      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;    
?> 
2
Contributors
1
Reply
24
Views
1 Year
Discussion Span
Last Post by rubberman
0

OOP - object-oriented programming is just that, programming with objects. The code you show is function-oriented. For OOP you need classes with member variables and methods (functions that work on the data of a particular class). That encapsulates all that code and variables in a single object (or multiple objects of the same class but with different values). There are tonnes of tutorial documentation on OOP on the web, including examples for PHP on the www.php.net web site.

Edited by rubberman

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.