There are plenty of frameworks in PHP such as Laravel, Symfony, CodeIgniter, Zend, Cake and others. Also PHP is currently one of the most popular web frameworks all over the world. There are many developers writing code in PHP, and developers like new tools and methods that make their work easier. Zontroy can generate code in PHP as well as in other languages used in programming such as Javascript, Typescript, Html etc. You are going to get more productive while using Zontroy as a PHP Code Generator.
We can make a simple application of generating code as example and give some details about PHP code generation. We need a data model to begin.
We have used well-known Employees database and you can download it from official MySQL website or you can use your own model.
Creating a Zontroy Project
Create a new empty Zontroy Project.
Enter data source and project information properly.
After you click Create button, your project is going to be created and opened.
On the left side, there is Entity Window, and database tables are listed here as Entities. On the right side, there is Project Window and project files are viewed here. We created an empty project so there is just Zontroy Project file in our project. We will go on with adding a template file to generate code files.
Template File
Add a Zontroy Repeating File (zref) to your project.
Select zref in New File window and name it as below. This file name means that it will create a PHP file with code for each entity and name the file same as the entity.
Click Add File and double click on the file. In editor window write the code below:
```php<?phpclass [[[zg-entity...zg-name]]]{ private $servername = "localhost"; private $username = "root"; private $password = "root"; private $database = "php_curd"; public $con; // Database Connection public function __construct() { $this->con = new mysqli($this->servername, $this->username,$this->password,$this->database); if(mysqli_connect_error()) { trigger_error("Failed to connect to MySQL: " . mysqli_connect_error()); }else{ return $this->con; } } // Insert customer data into customer table public function insertData($post) { zg-for(((zg-item:::[[[zg-entity...zg-fields]]]))){{{ $[[[zg-item...zg-name]]] = $this->con->real_escape_string($_POST['[[[zg-item...zg-name]]]']);}}} $query = "INSERT INTO [[[zg-entity...zg-name]]](zg-for(((zg-item:::[[[zg-entity...zg-fields]]]))){{{[[[zg-item...zg-name]]],}}}) VALUES(zg-for(((zg-item:::[[[zg-entity...zg-fields]]]))){{{'$[[[zg-item...zg-name]]]',}}})";
$sql = $this->con->query($query); if ($sql==true) { header("Location:index.php?msg1=insert"); }else{ echo "Registration failed try again!"; } }}?>```
Generate Code
Click Generate button and see the result.
Generated files will be viewed in Project Window.
We have generated code files for each entity in our database. If we wanted, we could exclude some of them. In this case, no code would be generated for those entities.
The code in each generated file is as below:
DEPARTMENTS:
```php<?phpclass departments{ private $servername = "localhost"; private $username = "root"; private $password = "root"; private $database = "php_curd"; public $con; // Database Connection public function __construct() { $this->con = new mysqli($this->servername, $this->username,$this->password,$this->database); if(mysqli_connect_error()) { trigger_error("Failed to connect to MySQL: " . mysqli_connect_error()); }else{ return $this->con; } } // Insert customer data into customer table public function insertData($post) { $dept_no = $this->con->real_escape_string($_POST['dept_no']); $dept_name = $this->con->real_escape_string($_POST['dept_name']); $query = "INSERT INTO departments(dept_no,dept_name,) VALUES('$dept_no','$dept_name',)"; $sql = $this->con->query($query); if ($sql==true) { header("Location:index.php?msg1=insert"); }else{ echo "Registration failed try again!"; } }}?>```
DEPT_EMP:
```php<?phpclass dept_emp{ private $servername = "localhost"; private $username = "root"; private $password = "root"; private $database = "php_curd"; public $con; // Database Connection public function __construct() { $this->con = new mysqli($this->servername, $this->username,$this->password,$this->database); if(mysqli_connect_error()) { trigger_error("Failed to connect to MySQL: " . mysqli_connect_error()); }else{ return $this->con; } } // Insert customer data into customer table public function insertData($post) { $emp_no = $this->con->real_escape_string($_POST['emp_no']); $dept_no = $this->con->real_escape_string($_POST['dept_no']); $from_date = $this->con->real_escape_string($_POST['from_date']); $to_date = $this->con->real_escape_string($_POST['to_date']); $query = "INSERT INTO dept_emp(emp_no,dept_no,from_date,to_date,) VALUES('$emp_no','$dept_no','$from_date','$to_date',)";
$sql = $this->con->query($query); if ($sql==true) { header("Location:index.php?msg1=insert"); }else{ echo "Registration failed try again!"; } }}?>```
DEPT_MANAGER:
```php<?phpclass dept_manager{ private $servername = "localhost"; private $username = "root"; private $password = "root"; private $database = "php_curd"; public $con; // Database Connection public function __construct() { $this->con = new mysqli($this->servername, $this->username,$this->password,$this->database); if(mysqli_connect_error()) { trigger_error("Failed to connect to MySQL: " . mysqli_connect_error()); }else{ return $this->con; } } // Insert customer data into customer table public function insertData($post) { $emp_no = $this->con->real_escape_string($_POST['emp_no']); $dept_no = $this->con->real_escape_string($_POST['dept_no']); $from_date = $this->con->real_escape_string($_POST['from_date']); $to_date = $this->con->real_escape_string($_POST['to_date']); $query = "INSERT INTO dept_manager(emp_no,dept_no,from_date,to_date,) VALUES('$emp_no','$dept_no','$from_date','$to_date',)";
$sql = $this->con->query($query); if ($sql==true) { header("Location:index.php?msg1=insert"); }else{ echo "Registration failed try again!"; } }}?>```
EMPLOYEES:
```php<?phpclass employees{ private $servername = "localhost"; private $username = "root"; private $password = "root"; private $database = "php_curd"; public $con; // Database Connection public function __construct() { $this->con = new mysqli($this->servername, $this->username,$this->password,$this->database); if(mysqli_connect_error()) { trigger_error("Failed to connect to MySQL: " . mysqli_connect_error()); }else{ return $this->con; } } // Insert customer data into customer table public function insertData($post) { $emp_no = $this->con->real_escape_string($_POST['emp_no']); $birth_date = $this->con->real_escape_string($_POST['birth_date']); $first_name = $this->con->real_escape_string($_POST['first_name']); $last_name = $this->con->real_escape_string($_POST['last_name']); $gender = $this->con->real_escape_string($_POST['gender']); $hire_date = $this->con->real_escape_string($_POST['hire_date']); $query = "INSERT INTO employees(emp_no,birth_date,first_name,last_name,gender,hire_date,) VALUES('$emp_no','$birth_date','$first_name','$last_name','$gender','$hire_date',)";
$sql = $this->con->query($query); if ($sql==true) { header("Location:index.php?msg1=insert"); }else{ echo "Registration failed try again!"; } }}?>```
SALARIES:
```php<?phpclass salaries{ private $servername = "localhost"; private $username = "root"; private $password = "root"; private $database = "php_curd"; public $con; // Database Connection public function __construct() { $this->con = new mysqli($this->servername, $this->username,$this->password,$this->database); if(mysqli_connect_error()) { trigger_error("Failed to connect to MySQL: " . mysqli_connect_error()); }else{ return $this->con; } } // Insert customer data into customer table public function insertData($post) { $emp_no = $this->con->real_escape_string($_POST['emp_no']); $salary = $this->con->real_escape_string($_POST['salary']); $from_date = $this->con->real_escape_string($_POST['from_date']); $to_date = $this->con->real_escape_string($_POST['to_date']); $query = "INSERT INTO salaries(emp_no,salary,from_date,to_date,) VALUES('$emp_no','$salary','$from_date','$to_date',)";
$sql = $this->con->query($query); if ($sql==true) { header("Location:index.php?msg1=insert"); }else{ echo "Registration failed try again!"; } }}?>```
TITLES:
```php<?phpclass titles{ private $servername = "localhost"; private $username = "root"; private $password = "root"; private $database = "php_curd"; public $con; // Database Connection public function __construct() { $this->con = new mysqli($this->servername, $this->username,$this->password,$this->database); if(mysqli_connect_error()) { trigger_error("Failed to connect to MySQL: " . mysqli_connect_error()); }else{ return $this->con; } } // Insert customer data into customer table public function insertData($post) { $emp_no = $this->con->real_escape_string($_POST['emp_no']); $title = $this->con->real_escape_string($_POST['title']); $from_date = $this->con->real_escape_string($_POST['from_date']); $to_date = $this->con->real_escape_string($_POST['to_date']); $query = "INSERT INTO titles(emp_no,title,from_date,to_date,) VALUES('$emp_no','$title','$from_date','$to_date',)";
$sql = $this->con->query($query); if ($sql==true) { header("Location:index.php?msg1=insert"); }else{ echo "Registration failed try again!"; } }}?>```
You can generate thousands of code files and thousands of code lines in a few minutes. It is the power of PHP Code Generator. You can get rid of boring tasks with this way and you can focus on architectural part of programming more.
This is just a simple example. You can generate all parts of your project as adding multiple template files.
Good to know: You can generate PHP code using Zontroy.