Welcome Guest, Not a member yet? Register   Sign In
query database with contitions (with parenthesis)
#1

[eluser]tolinho[/eluser]
Hello.
I have a form that lists pages on my site with the folowing code:

Code:
$q = $this->db->select('pages.*, p.slug as parent_slug, p.title as parent_title')
                ->join('pages as p', 'pages.parent_id=p.id', 'left')
                ->from('pages')
                ->limit($limit, $offset)          
                ->order_by('pages.parent_id', 'asc')
                ->order_by('pages.order', 'asc');

I also have a little filter on the page and the code that follows:

Code:
if (strlen($query_array['title'])) {
            $q->like('pages.title', $query_array['title']);
            $q->or_like('p.title', $query_array['title']);
        }              
        
        if (strlen($query_array['order'])) {
            $q->like('pages.order', $query_array['order']);
        }        
        
        if (strlen($query_array['active'])) {        
            $q->where("pages.active", $query_array['active']);
        }
        
        if (strlen($query_array['main'])) {        
            $q->where("pages.main", $query_array['main']);
        }        

        $ret['rows'] = $q->get()->result();

The end result query is this:
Code:
SELECT `pages`.*, `p`.`slug` as parent_slug, `p`.`title` as parent_title
FROM (`pages`)
LEFT JOIN `pages` as p ON `pages`.`parent_id`=`p`.`id`
WHERE `pages`.`active` =  '0'
AND  `pages`.`title`  LIKE '%sobre%'
OR  `p`.`title`  LIKE '%sobre%'
ORDER BY `pages`.`parent_id` asc, `pages`.`order` asc
LIMIT 100

My trouble here is that mysql is not filtering because of missing parenthesis. If should be:

Code:
SELECT `pages`.*, `p`.`slug` as parent_slug, `p`.`title` as parent_title
FROM (`pages`)
LEFT JOIN `pages` as p ON `pages`.`parent_id`=`p`.`id`
WHERE `pages`.`active` =  '0'
AND  (`pages`.`title`  LIKE '%sobre%'
OR  `p`.`title`  LIKE '%sobre%')
ORDER BY `pages`.`parent_id` asc, `pages`.`order` asc
LIMIT 100

Can someone please point out the changes I must make in my code to get the query looking like this?
#2

[eluser]tolinho[/eluser]
Hi again.

Seems I was not the first to come across this problem.

Found the solution here:
http://stackoverflow.com/questions/65528...cord-query

It mite help some else Wink
#3

[eluser]ivantcholakov[/eluser]
If you switch to CodeIgniter 3, within its query builder you will find the methods group_start(), *_group_start(), group_end(), they are exactly for controlling parentheses.
#4

[eluser]tolinho[/eluser]
[quote author="ivantcholakov" date="1402933826"]If you switch to CodeIgniter 3, within its query builder you will find the methods group_start(), *_group_start(), group_end(), they are exactly for controlling parentheses.[/quote]

Hello ivantcholakov.
Codeigniter 3?! Smile

I just checked
http://ellislab.com/codeigniter
and to my suprise there is a new release (2.2.0) I have 2.1.4

Anyway, were can I checkout coeigniter 3?
Can you provide any additional information on the version? Is it beta?

Thanks
#5

[eluser]ivantcholakov[/eluser]
I would qualify the status of CodeIgniter 3 as "almost ready".

Here is the repository:
https://github.com/EllisLab/CodeIgniter - checkout or download from "develop" branch.

Here is the source of documentation (branch "develop" again):
https://github.com/EllisLab/CodeIgniter/..._guide_src

There is one task to be finished before the release:
https://github.com/EllisLab/CodeIgniter/issues/3073 A new session library is under testing, its code at the moment is in the branch "feature/session". Apparently the new session library will be merged with the "develop" branch in the near future and it will appear in the final release.

There is no a release date yet, but I can see wish CodeIgniter 3 to be released soon. Please, consider this information as informal.
#6

[eluser]ivantcholakov[/eluser]
And here https://github.com/EllisLab/CodeIgniter/...uilder.php you can find the query builder methods that are interesting for you. Search the source for "group_start" and "group_end".

The new "query builder" is what in the past was known as "active record". The name changes for not being misleading.

Here are the upgrade notes, if you need them: https://github.com/EllisLab/CodeIgniter/...de_300.rst

Edit: I have corrected the links.
#7

[eluser]tolinho[/eluser]
I was not aware of any of it.
Troth be told, I'm currently making my very first website with codeigniter.
First time using a framework by the way.

It has taken me a while to get in to the coding process has I'm just a "sort of a beginner" Smile

I have not changed any of the system files.
Do you perhaps know if what I have done until now in codeigniter 2.1.4 "should work" in codeigniter 3?
I know the only way to be certain is to test, and I will as soon has I have a chance.
What i'm asking is if there are any major changes like how to call or load CI or stuff like that.

Sorry to who ever reads this, its now kind of a off topic but with interesting stuff!

#8

[eluser]CroNiX[/eluser]
A lot has changed between 2.x and 3. Check the upgrade guide (last link) that ivantcholakov posted in his last post.
#9

[eluser]tolinho[/eluser]
Will do.
In fact I was reading it when I figured it was actually the answer to my question Smile

Thanks




Theme © iAndrew 2016 - Forum software by © MyBB