Get Data

Get Data is most basic example to illustrate how framework getting data from database.

Get data is part of CRUD method, called Retrieve. CRUD is shortname of Create Retrieve Update and Delete.

In this tutorial you will learn about how to:

  • Setup the database by creating the database and importing the SQL file.
  • Check connection and configuration from framework.
  • Create a model.
  • Modify controller and view.

Prerequisites

For best result of this tutorial, you have to setup or installed the followings item:

Okay, lets get started!

Setup the database

Before we proceed to code with Seme Framework, we have to setup the database for working connection between Seme Framework and Database.

Create the database using PhpMyAdmin

PhpMyAdmin is web based database management interface.

This tool allowed you to do almost anything to your MySQL or MariaDB powered database.

If you have run the XAMPP, then you can open http://localhost/phpmyadmin/.

You have to create a database, named seme_framework.

Import the example database

After create a database, you have to import the database.

We have already save some sql files in your sql directory.

Before continuing the export process you have to select the database seme_framework which is you have created it before.

Navigate to export and choose seme_framework.sql file in sql directory.

After importing SQL, you can see new tables already added to database.

The Codes

After setup the database, its time for coding. First thing we have to recheck the configuration file located at app/config/development.php. Please make sure the database connection configuration is working.

Create Model

The model on MVC framework used for bridging framework and database table. And then if we have separated the model, the rates of reusability increased because a model can be used repeatedly multiple times on many controllers.

Lets created new file named a_apikey_model.php for table a_apikey under app/model/, and then put this codes to your file.

<?php
class A_ApiKey_Model extends SENE_Model
{
    public $tbl = 'a_apikey';
    public $tbl_as = 'ak';

    public function __construct()
    {
        parent::__construct();
        $this->db->from($this->tbl, $this->tbl_as);
    }
    public function set($di)
    {
        return $this->db->insert($this->tbl,$di);
    }
    public function update($id,$du)
    {
        $this->db->where('id',$id);
        return $this->db->update($this->tbl,$du);
    }
    public function del($id,$du)
    {
        $this->db->where('id',$id);
        return $this->db->delete($this->tbl);
    }
    public function get()
    {
        return $this->db->get();
    }
    public function getById($id)
    {
        $this->db->where('id',$id);
        return $this->db->get_first();
    }
}

Edit Controller

From the first tutorial, we have created a controller located at app/controller/home.php. For achieving this tutorial, we have to modified the codes.

First, we have to load new file that we created before a_apikey_model. After that, we have to get the data and put into a variable named $data['aakm'].

<?php
class Home extends SENE_Controller
{
  public function __construct()
  {
    parent::__construct();
    $this->setTheme('front');
    $this->load("a_apikey_model", "aakm");
  }
  public function index()
  {
    $data = array();
    $this->setTitle('Get The Data!');
    $this->setDescription("Here is the example for getting the data.");
    $this->setKeyword('Seme Framework');
    $this->setAuthor('Seme Framework');

    $data['aakm'] = $this->aakm->get();

    $this->putThemeContent("home/home",$data); //pass data to view
    $this->putJsContent("home/home_bottom",$data); //pass data to view

    $this->loadLayout("col-1",$data);
    $this->render();
  }
}

Edit View

From the first tutorial, we have created view located at app/view/front/home/home.php. For achieving this tutorial, we have to modified the codes for viewing the data properly.

First, we have to iterating the array of $aakm and put result on card type html.

<div class="container">
  <div class="row">
    <?php foreach($aakm as $aak) { ?>
    <div class="col m12 s12">
      <div class="card rounded preload-any">
        <div class="card-content ">
          <span class="card-title">[<?=$aak->code?>] <?=$aak->name?></span>
          <p><small><?=$aak->cdate?></small></p>
          <p><?=$aak->name.' ID: '.$aak->id?></p>
          <br>
          <div class="btn-group">
            &nbsp;
          </div>
        </div>
      </div>
    </div>
    <?php } ?>
  </div>
</div>

Result

If you open the http://localhost/seme_framework and view same as screenshot before, so you are achieved this tutorial successfully.