Welcome Guest, Not a member yet? Register   Sign In
how to get the specific match results in for keyword search
#1

[eluser]ashutosh[/eluser]
i have written a method and model for keyword search. But unfortunately I am getting the output which are matched with my keyword and also which does not have any match with the keyword I typed.

eg. if i typed keyword like "grisham" as author last name then i am getting the all other data(`book_title,auth_firstname,isbn`) as output including "grisham"(i.e `auth_lastname`)


What i want output is:
-----------------------
If i enter the keyword which is matching with the table column
auth_lastname of a row, then i want to show only results of
auth_lastname
but currently it is showing
auth_lastname,auth_fristname,title,publisher_name of a matching
row. Why it is display all?
it should display column which has match with the keywords!





Please help me ,to solve my problem.


controller.php
Code:
function suggestions()
    {
            $this->load->model('Booksmodel');
            $term = $this->input->post('term',TRUE);
            $rows = $this->Booksmodel->GetAutocomplete(array('keyword' => $term));
            $json_array = array();
            foreach ($rows as $row)
            array_push($json_array, $row->book_title);
            array_push($json_array, $row->auth_firstname);
            array_push($json_array, $row->isbn);
            array_push($json_array, $row->auth_lastname);
            array_push($json_array, $row->publisher_name);

            echo json_encode($json_array);
    }

model.php

Code:
function GetAutocomplete($options = array())
      {
        $this->load->database();  
        $this->db->limit('10');  
        $this->db->select('book_title,auth_firstname,isbn,auth_lastname,publisher_name');  
        $this->db->like('book_title', $options['keyword']);
        $this->db->or_like('auth_firstname', $options['keyword']);
        $this->db->or_like('isbn', $options['keyword']);
        $this->db->or_like('auth_lastname', $options['keyword']);
        $this->db->or_like('publisher_name', $options['keyword']);

        $query = $this->db->get('bookdetails');
          return $query->result();
        }
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

Your foreach won't work as you expect, because it doesn't have any braces around your statement:

Code:
foreach ($rows as $row)
{
    array_push($json_array, $row->book_title);
    array_push($json_array, $row->auth_firstname);
    array_push($json_array, $row->isbn);
    array_push($json_array, $row->auth_lastname);
    array_push($json_array, $row->publisher_name);
}

Also, you can save yourself a little typing doing away with array_push():
Code:
foreach ($rows as $row)
{
    $json_array[] = $row->book_title;
    $json_array[] = $row->auth_firstname;
    $json_array[] = $row->isbn;
    $json_array[] = $row->auth_lastname;
    $json_array[] = $row->publisher_name;
}

I'm sure you could do this with a series of subqueries, but I'm not sure how. For now, you could have your model loop through the results, checking each column for a matching result, and then passing back an array of matching strings. You may also want to limit your results.
#3

[eluser]ashutosh[/eluser]
Sorry you are not clear with my question.

What i want output is:


If i enter the keyword which is matching with the table column
auth_lastname of a row, then i want to show only results of
auth_lastname
but currently it is showing
auth_lastname,auth_fristname,title,publisher_name of a matching
row. Why it is display all?
it should display column which has match with the keywords!
#4

[eluser]TheFuzzy0ne[/eluser]
I understand exactly what you're asking. I think you are misunderstanding me. Wink

In your query, you are specifying the columns you want returned, so it's returning all columns (which I would expect).

What I am suggesting is that you add a loop to the end of your model method, that loops through the results, figures out which column is matching the keyword, and then puts it into an array to pass back to your controller.

For example: Let's say that your keyword is "foo", and you might get the following result from your query:
Code:
$res = array(
    array(
        'title' => 'CodeIgniter for Smart People',
        'auth_firstname' => 'Foo',
        'auth_lastname' => 'Bar',
        'publisher_name' => 'CI publishing',
    ),
    array(
        'title' => 'How to Jump Off a Cliff',
        'auth_firstname' => 'Hugo',
        'auth_lastname' => 'First',
        'publisher_name' => 'Foo Publishings',
    ),
    array(
        'title' => 'The Girl Who Fell Off the Cliff',
        'auth_firstname' => 'Eilene',
        'auth_lastname' => 'Dover',
        'publisher_name' => 'Foo Publishings',
    ),
    array(
        'title' => 'I Pity the Foo!',
        'auth_firstname' => 'Mr.',
        'auth_lastname' => 'T',
        'publisher_name' => 'Scared Publishers',
    ),
);

Now you'd loop through those results, and figure out what columns match the query:
Code:
$ret = array();
foreach ($res as $row)
{
    foreach ($row as $val)
    {
        if (FALSE !== strpos($val, $keyword))
        {
            $ret[] = $val;
            break;
        }
    }
}

return $ret;

Hope this helps.
#5

[eluser]ashutosh[/eluser]
Sounds good.
But i am confuse where to put it exactly.

I tried keeping below code. But i am not getting any output for my auto-completer textbox.

controller.php
Code:
function suggestions()
{
$this->load->model('Booksmodel');
$term = $this->input->post('term');
         $rows = $this->Booksmodel->GetAutocomplete($term);
$json_array = array();
foreach ($rows as $row)
        {
            $json_array[] = $row->book_title;
            $json_array[] = $row->auth_firstname;
            $json_array[] = $row->isbn;
            $json_array[] = $row->auth_lastname;
            $json_array[] = $row->publisher_name;
        }

echo json_encode($json_array);
}

model.php

Code:
function GetAutocomplete($keyword)
    {
    $this->load->database();  
    $this->db->limit('10');  
    $this->db->select('book_title,auth_firstname,isbn,auth_lastname,publisher_name');  
    $this->db->like('book_title', $keyword);
    $this->db->or_like('auth_firstname', $keyword);
    $this->db->or_like('isbn', $keyword);
    $this->db->or_like('auth_lastname', $keyword);
    $this->db->or_like('publisher_name', $keyword);
      
    $query = $this->db->get('bookdetails');
    $ret = $query->result();
        foreach ($res as $row)
        {
            foreach ($row as $val)
            {
                if (FALSE !== strpos($val, $keyword))
                {
                    $ret[] = $val;
                    break;
                }
            }
        }
        return $ret;          
    }
#6

[eluser]TheFuzzy0ne[/eluser]
It would go at the end of your model method:

Code:
function GetAutocomplete($options = array())
{  
    $this->db->limit('10');  
    $this->db->select('book_title,auth_firstname,isbn,auth_lastname,publisher_name');  
    $this->db->like('book_title', $options['keyword']);
    $this->db->or_like('auth_firstname', $options['keyword']);
    $this->db->or_like('isbn', $options['keyword']);
    $this->db->or_like('auth_lastname', $options['keyword']);
    $this->db->or_like('publisher_name', $options['keyword']);

    $query = $this->db->get('bookdetails');
    $ret = array();
    foreach ($res->result_array() as $row)
    {
        foreach ($row as $val)
        {
            if (FALSE !== strpos($val, $keyword))
            {
                $ret[] = $val;
                break;
            }
        }
    }

    return $ret;
}

I'd also suggest you either load your database automatically, or put the call to $this->load->database() in your model constructor.

It's untested, and it also returns an array. If you want an object, I'll leave it to you to make the necessary modifications. Smile
#7

[eluser]ashutosh[/eluser]
i updated my second post. Please have a look. Still i am not getting any result to be display.

How i am calling an action:
i have j query auto-completer in view.php, from ajax i am call my controller action , Then finally trying to display match results.
#8

[eluser]TheFuzzy0ne[/eluser]
Since you're now building your JSON array in your model, and returning that, you controller method should now be something like this:
Code:
function suggestions()
{
    $this->load->model('Booksmodel');
    $term = $this->input->post('term');
    $json_array = $this->Booksmodel->GetAutocomplete($term);
    echo json_encode($json_array);
}

You should be able to call upon that method directly from your address bar, to see the output. You'll just need to hardcode the $term variable for testing:
Code:
function suggestions()
{
    $this->load->model('Booksmodel');
    $term = $this->input->post('term');
    $term = 'whatever'; // Remove this when you're done testing.
    $json_array = $this->Booksmodel->GetAutocomplete($term);
    echo json_encode($json_array);
}

You may also need to enable error reporting. Put this at the beginning of your index.php file, and don't forget to remove it before your app goes live.

Code:
error_reporting(E_ALL);
ini_set('display_errors', '1');

It's up to you to debug your code. I understand you're new to PHP, but you have to learn some time. I cannot write your app for you, but I'm perfectly happy to help if you get stuck.
#9

[eluser]ashutosh[/eluser]
I hardcoded for testing but still having problem.
After running this:->http://localhost/site/search/suggestions into browser i am getting the below error


A PHP Error was encountered

Severity: Notice

Message: Undefined variable: res

Filename: models/Booksmodel.php

Line Number: 899


Fatal error: Call to a member function result_array() on a non-object in D:\xampp\htdocs\site\application\models\Booksmodel.php on line 899
#10

[eluser]TheFuzzy0ne[/eluser]
That's because I assigned the return result to a variable called $query, and then went on to call $res->result_array() (and $res is undefined) instead of $query->result_array(). Substitute $res for $query, and all should be well.




Theme © iAndrew 2016 - Forum software by © MyBB