6

I have this code in 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 execute multiple queries. You notice in where statement the two have different condition. I just want to get the result for the two queries. I try the switch statement to execute both queries and it is not working. Help me.

3
  • what is your condition for this code? Commented Feb 19, 2014 at 2:25
  • How can I add condition or statement in this code?Can you help me?I'm new in codeigniter. My condition would be, if both queries is true both will get the result. Commented Feb 19, 2014 at 2:29
  • I don't understand why you cannot use WHERE dataSearch.street LIKE ? OR dataSearch.stripNameWOSpace LIKE ? and pass your two variables into the parameter array of a single query. Commented Apr 16 at 4:22

2 Answers 2

7

You can set the query to a variable to do your things with each query.

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 CodeIgniter User Guide - Model

Sign up to request clarification or add additional context in comments.

6 Comments

But how can I call the variables in controller.
I have updated my answer, check it out. And don't forget to take a look at the link.
How about if I want to get the result both queries eventhough it will get duplicate entry?
Try replace return array_merge($result1, $result2); with return $result1 + $result2;
how to get my qry1 value.?
|
1

I know...old, was looking for something else when I saw something that could be done better. the two select statements are identical except for the where statement. you can use "OR" to combine the two WHERE statements. use "DISTINCT" to avoid duplicate entries. "UNION" should be used if you really want duplicate entries.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.