Welcome Guest, Not a member yet? Register   Sign In
How to execute 2 queries with the same settings?
#1

[eluser]chefnelone[/eluser]
Hello

I need to make 2 queries with the same settings:

If I do this:
Code:
$this->db->order_by('order', 'asc'); //SETTING FOR THE QUERY
$this->db->like('product_name', 'shoes'); //SETTING FOR THE QUERY

$data['total_products'] = $this->db->get('products')->result(); // FIRST REQUEST

$this->db->limit(10);
$data['products'] = $this->db->get('products')->result(); // SECOND REQUEST

the FIRST REQUEST takes the setting of the query: order_by and like
but then the SECOND REQUEST doesn't take those settings of the query again.


I mean:
the request: $this->db->count_all_results('products'); generate this query:
Code:
SELECT COUNT(*)  FROM (`products`) WHERE `product_name` LIKE '%shoes%'  ORDER BY `orden` asc;
but the request: $this->db->get('products')->result(); generate this query:
Code:
SELECT * FROM (`products`) LIMIT 10;
And I need it to take the order_by and like in the same way than the FIRST REQUEST:
Code:
SELECT * FROM (`products`)WHERE `product_name` LIKE '%shoes%'  ORDER BY `orden` asc; LIMIT 10

It's like I need to set up again order_by and like after a request.??
#2

[eluser]n0xie[/eluser]
That's because you have to set it up again.
#3

[eluser]chefnelone[/eluser]
[quote author="n0xie" date="1280844413"]That's because you have to set it up again.[/quote]
and is there any way to save the query and use it again using actives records?
#4

[eluser]Clooner[/eluser]
[quote author="chefnelone" date="1280844641"]and is there any way to save the query and use it again using actives records?[/quote]
Isn't that why they invented procedures, functions and methods... Big Grin

Code:
function basicquerysettings()
{
  $this->db->order_by('order', 'asc'); //SETTING FOR THE QUERY
  $this->db->like('product_name', 'shoes'); //SETTING FOR THE QUERY
}

function gettotal()
{
  $this->basicquerysettings();
  // rest of your query?
}

function getproducts()
{
  $this->basicquerysettings();
  // rest of your query?
}
Or am I not fully understanding the question?
#5

[eluser]n0xie[/eluser]
Or use named scopes as Jamie has named them.
#6

[eluser]Clooner[/eluser]
[quote author="n0xie" date="1280845541"]Or use named scopes as Jamie has named them.[/quote]
yes, using named scopes is the way to go!
#7

[eluser]chefnelone[/eluser]
[quote author="n0xie" date="1280845541"]Or use named scopes as Jamie has named them.[/quote]
This seems to be a good one to me.
thanks
#8

[eluser]mddd[/eluser]
CI has its own "caching" in the active record class. It is not real caching, but more like remembering of settings. Exactly what you are looking for.
Code:
$this->db->start_cache();
$this->db->order_by('order', 'asc'); //SETTING FOR THE QUERY
$this->db->like('product_name', 'shoes'); //SETTING FOR THE QUERY
$this->db->from('products');
$this->db->stop_cache();

// now, if you do a query, the above settings will be used. every time, until you call $this->db->flush_cache();
$data['total_products'] = $this->db->get()->result(); // FIRST REQUEST -- table name not necessary because it's already in the cached part

$this->db->limit(10);
$data['products'] = $this->db->get()->result(); // SECOND REQUEST
#9

[eluser]chefnelone[/eluser]
[quote author="mddd" date="1280847738"]CI has its own "caching" in the active record class. It is not real caching, but more like remembering of settings. Exactly what you are looking for.
Code:
$this->db->start_cache();
$this->db->order_by('order', 'asc'); //SETTING FOR THE QUERY
$this->db->like('product_name', 'shoes'); //SETTING FOR THE QUERY
$this->db->from('products');
$this->db->stop_cache();

// now, if you do a query, the above settings will be used. every time, until you call $this->db->flush_cache();
$data['total_products'] = $this->db->get()->result(); // FIRST REQUEST -- table name not necessary because it's already in the cached part

$this->db->limit(10);
$data['products'] = $this->db->get()->result(); // SECOND REQUEST
[/quote]

you're right moddd this is what I was looking for. Many thanks.
#10

[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...ecord.html

Quote:$this->db->count_all_results();

Permits you to determine the number of rows in a particular Active Record query. Queries will accept Active Record restrictors such as where(), or_where(), like(), or_like(), etc. Example:

Code:
echo $this->db->count_all_results('my_table');
// Produces an integer, like 25

$this->db->like('title', 'match');
$this->db->from('my_table');
echo $this->db->count_all_results();
// Produces an integer, like 17




Theme © iAndrew 2016 - Forum software by © MyBB