Welcome Guest, Not a member yet? Register   Sign In
Searching with serialized id's
#1

(This post was last modified: 10-25-2016, 05:24 AM by wolfgang1983.)

When the user types in the search PHP it should find the questions that have category id of 1

I store my category id in a column in question called "tags" it's serialized of couse because the user may have chosen multiple tags / categories.

The category table name is category and columns "category_id" "name" "status"

a:2:{i:0;s:1:"1";i:1;s:1:"4";}

I can search for questions fine but cannot search if it is a category they are searching for.

Any idea's and examples thanks for your time

PHP Code:
public function get_questions($search) {
    $this->db->select('*');
    $this->db->from($this->db->dbprefix 'question');
    $this->db->like('title'$search);
    $query1 $this->db->get();

    if ($query->num_rows() > 0) {

        foreach ($query->result_array() as $key) {
            $data[] = array(
                'question_id' => $key['question_id'],
                'title' => $key['title'],
                'votes' => $key['votes'],
                'answers' => $this->answer_model->count_answers($key['question_id']),
                'views' => ''
            );
        }
    }

    return $data;



Controller



PHP Code:
<?php 

class Search extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('user_agent');

        if ($this->input->get('q') == FALSE) {
            redirect($this->agent->referrer());
        }

        $this->load->model('answers/answer_model');
    }

    public function index() {
        if ($this->input->post('search')) {

            $q str_replace('+',  ' '$this->input->get('q'));

            $data['title'] = 'Questions ' "'" $q "'";

            $question_results $this->get_questions($q);

            $data['questions'] = array();

            if (isset($question_results)) {
                foreach ($results as $result) {
                    $data['questions'][] = array(
                        'question_id' => $result['question_id'],
                        'title' => $result['title'],
                        'votes' => $result['votes'],
                        'answers' => $this->answer_model->count_answers($result['question_id']),
                        'views' => ''
                    ); 
                }
            }

            $data['page'] = 'search/search_results';

            $this->load->view($this->config->item('config_template') . '/template/template_view'$data);

        }
    }



Attached Files
.php   Search.php (Size: 1.59 KB / Downloads: 60)
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply


Messages In This Thread
Searching with serialized id's - by wolfgang1983 - 10-25-2016, 03:39 AM
RE: Searching with serialized id's - by Narf - 10-25-2016, 07:02 AM
RE: Searching with serialized id's - by JayAdra - 10-25-2016, 05:03 PM
RE: Searching with serialized id's - by d4jk4 - 10-25-2016, 08:05 PM



Theme © iAndrew 2016 - Forum software by © MyBB