Tutorial CI-03: How To Make a Search Engine in CodeIgniter

in #utopian-io7 years ago (edited)

Cover 3.png

What Will I Learn?


  • You will learn How to Make and Preparing database in CodeIgniter
  • You will learn How to Use MVC method with CodeIgniter
  • You will learn How to make a search Engine box in CodeIgniter

Requirements


Hardware

  • A Computer with required system

Software

  • Xampp or WAMP Preconfigured
  • CodeIgniter 3.1.7
  • Sublime, Notepad++ or any other text editor/IDE
  • A willingness to study hard

Dificulty

  • Intermediate

What is CodeIgniter ?


Framework is a collection of instructions collected in a function and class with a function to facilitate the developer in the call without having to write the same program syntax over and over again. Source code will look cleaner and more structured.

Codeigniter is a php framework that is open source and certainly free for every use, The purpose behind the development of a framework to facilitate the programmer in building a web-based applications. The method used is MVC (Model, View, Controller). Webserver Packages (AppServ, XAMPP, EasyPHP, WAMP, etc) & Code Igniter Codeigniter (CI) is a PHP framework, aimed at people who want to build websites using PHP. Using a view-controller-model architecture that separates the logic and display sections of the program, CI is quite "fun" to use. Not difficult especially you have mastered the basic principles of OOP in PHP.

The concept that separates every major component into 3 MVC components is

  1. Model is the intended part for processing or manipulation database.
  2. View is a part of the home screen at the page that will appear to the user.
  3. Controller is the final layer that there is a collection of instructions commands connecting model and view.

Database is a set of data that has been arranged in such a way with the provisions or certain rules that are mutually related between several classes and components so as to facilitate the user in managing it also makes it easier to obtain information. In addition, those that define the database as a collection of interconnected files, tables, or archives stored in electronic media. Some of the benefits of the database is one of them is the database has the ability in selecting data to become a group that sorted quickly. This is what ultimately can produce the information needed quickly too.

STEP 1 : DATABASE


  • The first step to load is to create a new database by enabling the apache and mysql service on Xampp then the browser's face and then typing in the address bar localhost/phpmyadmin. Create a new database name to your liking, click new at the right corner of the phpmyadmin view.
  • Prepare table with name tb_book (field: id_book, title_book, stok_book).
CREATE TABLE IF NOT EXISTS tb_book 
(id_book varchar (10) NOT NULL, 
title_book varchar (50) DEFAULT NULL, 
stock_book year (4) DEFAULT NULL, 
PRIMARY KEY (id_book )) 
ENGINE = MyISAM DEFAULT CHARSET = latin1;

Here is a little analysis of the three SQL commands "create table" before the following is an explanation of the attributes - supporting attributes that follow the main command is :

Not Null is a field property that provides a condition for the field to be null or non-existent.

Auto_increment is a special field property for Primary Key that causes the field to have the ability to increase automatically continuously as the number of data stored in the table increases.

Default Null is a field property that gives a condition to the field may be null or can not be entered.

Primary Key is a field that specifically becomes a priority or identifies each record line contained in a table.

Key is a field that becomes a reference search data in a database table, where the field is also stored in the database index so that easier search.

Engine is a table property that defines the type of storage used to store data for each table.

Default Charset is a character type that is configured against the table.

  • Insert some sample data into the table. The following sql query program down below.
INSERT INTO `tb_book` 
(`id_book`, `Judul_book`, `Stok_book`) 
VALUES ('14', 'BOLA.net', '2008'), ('22', 'Travelling', '2010');
  • Ok, we've finished creating a simple database!

STEP 2 : CONFIGURE SEARCHING SCRIPT


Model

  • Create a file on the model with the name Test_model.php
<?php
class Test_model extends CI_Model {
    public function __construct()
    {
        $this->load->database();
    }
    public function cariOrang()
    {
        $cari = $this->input->GET('cari', TRUE);
        $data = $this->db->query("SELECT * from dborang where Name like '%$cari%' ");
        return $data->result();
    }
}
  • Explanation of the code program
class Test_model extends CI_Model

to serves classify the Test_Model class based on CI_Model

The construction method

is a function of contruct that will first be executed during operation.

$this->load->database();

To explain that we load a database file

public function cariOrang()

to declare a class function

$cari = $this->input->GET('cari', TRUE);

a boolean value as the second parameter

$data = $this->db->query("SELECT * from dborang where Name like '%$cari%' ");

to call the database with the selection of queries and search by name

return $data->result(); 

The value is returned using an optional return statement. If the return is omitted then the NULL value will be returned.

Controller

  • Create a new controller file with the name Test.php, following the program code
<?php
class Test extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->helper("url");
        $this->load->model('Test_model');
    }
    public function cari() 
    {
        $this->load->view('search');
    }
    public function hasil()
    {
        $data2['cari'] = $this->Test_model->cariTest();
        $this->load->view('result', $data2);
    }
}
  • Explanation of the code program
parent::__construct();

serves to prepare the needs of the 'parent' class first, for example the car class. car class requires a brand name or type.

$this->load->helper("url");

Performing loading on every controller that will use helper, Helper also serves to help developers build applications more quickly and efficiently.

$data2['cari'] = $this->Test_model->cariTest();

is the program code in hasil function to declare the Test_model object in the search operation

  • Then create the file search.php so there will be css display when we run localhost, follow a script down below
<!DOCTYPE html>
<html lang="">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Pencarian dengan CodeIgniter 3 &raquo; </title>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
        <div class="container">
            <h3>Pencarian</h3
>           <hr>
            <form action="<?php echo base_url('index.php/orang/hasil')?>" action="GET">
                <div class="form-group">
                    <label for="cari">data yang dicari</label>
                    <input type="text" class="form-control" id="cari" name="cari" placeholder="cari">
                </div>
                <input class="btn btn-primary" type="submit" value="Cari">
            </form>
        </div>
    </body>
</html>

  • Then create the result.php file to show the search results from the file we are looking for, more or less the program code as follows
<!DOCTYPE html>
<html lang="">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Pencarian dengan CodeIgniter 3 &raquo; Jaranguda.com</title>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
    <div class="container">
    <h3>Hasil Pencarian</h3>
    <hr>
        <?php
        if(count($cari)>0)
        {
            foreach ($cari as $data) {
            echo $data->Name . " => " . $data->Country ."<br>";
            }
        }
        else
        {
            echo "Data tidak ditemukan";
        }
    ?>
    </div>
    </body>
</html>
  • And then we looking for the result, localhost/CodeIgniter/index.php
    CI3.png

CONCLUSION


  • Generally there are many libraries that can be used on CodeIgniter framework. But for the initial stage of the library above that must be known because the library is generally used.
  • In the Database Configuration section used multidimensional array Because it is more simple and can store more than one connection configuration.
  • The search box on a web page displays the same functionality as Google that allows users to find specific content on the website. Having a Search Box is an absolute necessity for most websites and is needed to increase the convenience of website users.
  • The CodeIgniter framework also supports many types of databases eg MySQL, PostGre SQL, Oracle and many more.

Curriculum




Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules, and is considered as plagiarism. Plagiarism is not allowed on Utopian, and posts that engage in plagiarism will be flagged and hidden forever.

Plagiarised from here.

You can contact us on Discord.
[utopian-moderator]

Hey @amosbastian, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

greet, yed mampir ketempat aku y hehe @mrword

oke broo maksih

You can also integrate elasticsearch in this codeigniter based search engine to make it faster. Integrating elasticsearch is pretty easy. You can create the environment using elasticsearch class and create indexes.

Thank you very much because your post helped me incredibly in order to do this. I'll tell you even more because your post encouraged me to study this industry. I started watching video tutorials and studying various courses, for example CodeIgniter Tutorial Videos CI Version 4 . By the way, I can highly recommend it to you.

  1. Have a table in your database storing all the entries you want the search engine to crawl in
  2. Create a web service in CodeIgniter which executes an SQL query with the LIKE operator to search for relevant results