WEB DATABASE REMOTE ACCESS

We assume in this example a Linux host with Ubuntu 20.04 LTS, but the process is similar with other versions of Linux, or Mac, or Windows.

The prerequisite is the installation of a LAMP (Linux) or MAMP (MacOS, Windows) server, and this is not treated in this article.

We must have a database to connect with, or we can create a database with phpMyAdmin, or another database management tool.

We assume to have a database, with the name mydatabase, and a table with the name mytable.

Grocery CRUD version 4 is a beta version, we will use the version 3, that is a stable version. Consequently we will use CodeIgniter 3.

CodeIgniter can be downloaded from codeigniter.com, it’s a powerful PHP framework to create web applications.

After downloading CodeIgniter we can extract the files from the zip package, and we will have a directory like bcit-ci-codeigniter….

The installation of CodeIgniter is very simple: we can copy this directory with all subfolders in /var/www.

Then rename the directory: for example we assume the new name myci.

The folder will be /var/www/myci. We can execute:

$sudo su
#cd /var/www
#chown -R www-data myci
#chmod -R 775 myci

In the folder /etc/apache2/sites-available we have to create a configuration file like this:

<VirtualHost *:80>
 ServerName ourwebservername
 DocumentRoot /var/www/myci
 <Directory /var/www/myci/>
  Options +FollowSymLinks
  AllowOverride All
  Order allow,deny
  allow from all
 </Directory>
 ErrorLog /var/log/apache2/codeigniter-error_log
 CustomLog /var/log/apache2/codeigniter-access_log common
</VirtualHost>

Substitute ourwebservername with the name in /etc/hosts, for the external access to the webserver

We can save the file with the name myci.conf.

Then from the console view:

#a2ensite myci.conf
#systemctl reload apache2

Now we must consider the file config.php in /var/www/mycy/config.

There is a row with the base URL, we change the empty string with:

$config[‘base_url’] = ‘http://ourwebservername/’;

And we save the changed file.

In the same folder there is the file database.php.

In this file we must compile the server name (‘ourwebservername’), the database user (for example ‘myuser’), the password, and the database name.

For example:

‘hostname’ => ‘ourwebservername‘,
‘username’ => ‘myuser’,
‘password’ => ‘mypass’,
‘database’ => ‘mydatabase’,

Save the file.

We can check if all is OK, executing in the browser:

http://ourservername/index.php

We must view a page like this:

Now we can add Grocery CRUD to the installation.

The first step is to download grocery CRUD version 3 from the site www.grocerycrud.com

Unzip the downloaded file, and the resulting folder will be something like grocery-crud-1.6.3.

In the folder of the downloaded file execute:

#mv grocery-crud-1.6.3 myci
#cp myci /var/www

We have merged in the same folder myci the files of CodeIgniter and the files of Grocery CRUD.

Now we can use an editor to create, or copy and modify php files.

A pratical editor is Geany, another is Visual Studio Code.

In /var/www/myci/application/controllers we can write our controller, with the name Main.php.

Here the file Main.php in controllers folder.

<?php  if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);

class Main extends CI_Controller {

    function __construct()

    {

        parent::__construct();

        $this->load->database();

        $this->load->helper(‘url’);

        $this->load->library(‘grocery_CRUD’);

    }

    public function index()

    {

        echo “<h1>Welcome to the world of Codeigniter</h1>”;//Just an example to

 ensure that we get into the function

        die();

    }

public function put here your table name()

    {

        $crud = new grocery_CRUD();

        $crud->set_table(‘put here your table name’);

        $output = $this->grocery_crud->render();

        $this->_example_output($output);   

    }

    function _example_output($output = null)

    {

        $this->load->view(‘our_template.php’,$output);    

    }

 }

In the folder /var/www/myci/application/views we can write our view, with the name our_template.php.

Here the file.

<!DOCTYPE html>

<html lang=”en”>

<head>

    <meta charset=”utf-8″ />

<?php 

foreach($css_files as $file): ?>

    <link type=”text/css” rel=”stylesheet” href=”<?php echo $file; ?>” />

<?php endforeach; ?>

<?php foreach($js_files as $file): ?>

    <script src=”<?php echo $file; ?>”></script>

<?php endforeach; ?>

<style type=’text/css’>

body

{

    font-family: Arial;

    font-size: 14px;

}

a {

    color: blue;

text-decoration: none;

    font-size: 14px;

}

a:hover

{

    text-decoration: underline;

}

</style>

</head>

<body>

<!– Beginning header –>

    <div>

        <a href='<?php echo site_url(‘Main/put here your table name’)?>’>Put here your table name</a> | 

     </div>

<!– End of header–>

    <div style=’height:20px;’></div>  

    <div>

        <?php echo $output; ?>

    </div>

<!– Beginning footer –>

<div>Footer</div>

<!– End of Footer –>

</body>

</html>

Et voilà !

We can go to http://yourservername/index.php/Main/yourtablename and you can create, read, update, delete the records, and filter them.

Leave a Reply