user8054284
user8054284

Reputation:

I'm trying to make a function CodeIgniter framework so I can search products through my database

Good day! I'm trying to make a function on the CodeIgniter framework so I can search products through my phpMyAdmin database but I'm getting an error.

Some database info:

db name: kadokado
db table: products
db table column name: product_naam

My view file (result_view.php):

    <h1>Zoek een cadeau</h1>
<form class="navbar-form" role="search" action=" {{ base_url }}search/search_keyword" method = "post">
    <div class="input-group">
        <input type="text" class="form-control" placeholder="Search" name = "keyword"size="30px; ">
        <div class="input-group-btn">
            <button class="btn btn-default " type="submit" value = "Search"><i class="glyphicon glyphicon-search"></i></button>
        </div>
    </div>
</form>

<div class="clearfix"></div>

My controller file (SearchController.php) :`

<?php


class SearchController extends CI_Controller {


    public function index()
    {
         $this->load->view('result_view',$data);
    }

    function search_keyword()
{
    $keyword    =   $this->input->post('keyword');
    $data['results']    =   $this->mymodel->search($Keyword);
}


}`

My model file (Keyword.php) :`

<?php



class Keyword extends CI_model  {

    function search($keyword)
{
    $this->db->like('product_naam',$keyword);
    $query  =   $this->db->get('products');
    return $query->result();
}





}


?>
`

The PHP error that I'm receiving when I load the view:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: controllers/SearchController.php

Line Number: 9

Backtrace:

File: /home/ubuntu/workspace/application/controllers/SearchController.php
Line: 9
Function: _error_handler

File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once

And this is the error when i click on my search button:

404 Page Not Found

The page you requested was not found.

I hope someone can help me so this function can work properly. Thanks!

Upvotes: 0

Views: 45

Answers (2)

Muhammad Usman
Muhammad Usman

Reputation: 1423

you have not defined the $data variable in your controller

class SearchController extends CI_Controller {

  public function index() {
    $this->load->view('result_view');
  }

  function search_keyword() {
    $this->load->model('Search_model');
    $keyword = $this->input->post('keyword');
    $data['results'] = $this->Search_model->search($Keyword);
    print_r($data);
  }
}

Your Model

class Search_model extends CI_model  {

 function search($keyword) {
   $this->db->like('product_naam', $keyword);
   $query = $this->db->get('products');
   return $query->result();
 }
}

View

<form class="navbar-form" role="search" action="<?php echo base_url('SearchController/search_keyword'); ?>" method = "post">
<div class="input-group">
  <input type="text" class="form-control" placeholder="Search" name = 
  "keyword"size="30px; ">
<div class="input-group-btn">
    <button class="btn btn-default " type="submit" value = "Search"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>

Upvotes: 0

Alex Mac
Alex Mac

Reputation: 2993

You have not defined $data variable in controller.

public function index(){
         $data = array();
         $this->load->view('result_view',$data);
    }

In your controller page please load model first than use model function.

function search_keyword()
{
    $this->load->model('Keyword');
    $keyword    =   $this->input->post('keyword');
    $data['results']    =   $this->Keyword->search($Keyword);
}

In your model part check function describe below.

function search($keyword)
{
    $this->db->select('product_naam');
    $this->db->like('product_naam',$keyword);
    $query  =   $this->db->get('products');
    return $query->result();
}

Upvotes: 1

Related Questions