Running multiple queries in a model in codeigniter

I have this code in a model in codeigniter:

<?php
Class Mymodel Extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function search($textinput)
    {
        $street = "select street from dataSearch;";
        $stripNameWOSpace = "select stripNameWOSpace FROM dataSearch;";
        $vowels = array('a','e','i','o','u',';','/','-', ' ');
        $string = strtolower($textinput);
        $strippedVowels = mysql_real_escape_string(str_replace($vowels, '', $string));
            $this->db->query("select dataSearch.id,
                dataSearch.name,
                dataSearch.street,
                dataSearch.city,
                dataSearch.lat,
                dataSearch.lng,
                category.asiatype from dataSearch join category on dataSearch.cat = category.id
                where dataSearch.street like '%".$textinput."%'");
            $this->db->query("select dataSearch.id,
                dataSearch.name,
                dataSearch.street,
                dataSearch.city,
                dataSearch.lat,
                dataSearch.lng,
                category.asiatype from dataSearch join category on dataSearch.cat = category.id
                where dataSearch.stripNameWOSpace like '%".$strippedVowels."%'");
            $query = $this->db->get();
            $query->result();

    }
}
?> 

I just want to complete some queries. You notice that the two statements have different conditions. I just want to get the result for two queries. I am trying to execute a switch statement to execute both requests and it is not working. Help me.

+4
source share
2 answers

You can set a variable request to perform your tasks with each request.

Like this:

Class Mymodel Extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function search($textinput) {
        $query1 = $this->db->query("YOUR QUERY");
        $query2 = $this->db->query("YOUR SECOND QUERY");

        $result1 = $query1->result();
        $result2 = $query2->result();

        return array_merge($result1, $result2); // If you want to merge both results
    }
}

In your controller:

$this->load->model('Mymodel');
$searchresult = $this->Mymodel->search($textinput);

For more information, you should read the CodeIgniter User Guide - Model

+4
source

... , - , -, . select , where. "" WHERE. "DISTINCT", . "UNION" , .

+1

Source: https://habr.com/ru/post/1527621/


All Articles