PHP Code Generator - Zontroy

PHP Code Generator

Generate Code in PHP and Produce More Code

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 programming languages such as Javascript, Typescript, Html etc. You are going to get productive while using Zontroy as a PHP Code Generator.

We can make a simple example to generate code and we are going to give details about PHP code generation. We need a data model to begin.

Data Model

We have used well-known Employees database and you can download it from official MySQL website or you can use your own model.

If you have not installed Zontroy, you can download free version.

Zontroy Project

Create a new empty Zontroy Project.

Enter data source and project information properly.

After 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 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 create a PHP file with code for each entity and name the file same as entity.

Click Add File and double click on the file. In editor window write the code below:

<?php

    class [[[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 you want you can exclude some of them while generation.

The code lines in each generated file is as below:

departments.php

<?php

    class 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

<?php

    class 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

<?php

    class 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

<?php

    class 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

<?php

    class 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

<?php

    class 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 hundreds 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 via this way and you can more focus on architectural part of programming.

It is just a simple example and you can generate all parts of your project as adding multiple template files.

You can find more about file types like Zontroy Repeating File on our DOCS.

Comments

  • Nilton Oliveira

    17 May 2022

    I did the whole process, but when running it doesn’t work.
    Error: Invalid characters in path

    reply
    • Zontroy

      18 May 2022

      Hi Nilton,

      I actually reproduced the error you had and you are right there is an error while generating if you copy the file name [[[zg-entity…zg-name]]].php.zref from post. Our blog page automatically converts the three dots (…) to ellipsis character and when paste the file name to generator it displays such an error.

      To avoid getting this error:
      1. We have updated our blog posts.
      2. Replace the ellipsis character with three dots in already entered file names.

      Thank you for making such a useful comment.

      Best Regards.

      reply

Post a Comment