Multiple column ORDERing in ZF? - mysql

i got this code
$select
->from(array("e" => "embarcacoes"))
->join(array("i" => "imagens"), 'e.id = i.barcoId')
->where("e.tipo = '{$this->view->tipoEmbarcacao}'")
->group("i.barcoId")
->limitPage($paginaAtual, $porPagina)
->order('e.prioridade DESC');
it works well, if i change the ->order('prioridade DESC'); line to ->order('id DESC'); it still works well, but if i try:
->order('prioridade DESC, id DESC'); or ->order(array('prioridade DESC','id DESC'));
it doesnt work. What is the correct syntax to make multiple orderign in zend framework? Thanks.

Got the solution,
i have to use like this
->order(array('e.prioridade DESC','e.id DESC'));
oh christ, such a beginner mistake.

Related

How to get random row from MYSQL and how to write it in active records?

This is my query
"SELECT * FROM package_info ORDER BY RAND() LIMIT 0,3;"
I try to write it in active records like this.
$this->db->select('*');
$this->db->from('package_info');
$this->db->order_by("id", "random");
$this->db->limit(0, 3);
$result = $this->db->get();
But it is not work. How to write this in active record?
Use Below code it will work fine -
$this->db->select('*');
$this->db->from('package_info');
$this->db->order_by("id", "random");
$this->db->limit(3, 0);
$result = $this->db->get()->result();
// shows last executed query
echo $this->db->last_query();
// shows data fetched
echo "<pre>";
print_r( $result );
echo "</pre>";
You can visit on this link to view how queries are used in Codeigniter.
https://www.codeigniter.com/userguide2/database/active_record.html
CodeIgniter does not mandate that you provide 2 arguments for most Query Builder statements, you can do a whole WHERE by doing ->where('1=1') and its perfectly fine.
I'm surprised how many people don't understand method chaining, but I'll show it in my example it is just nicer...
$result = $this->db->select('*')
->from('package_info')
->order_by('rand()')
->limit(0, 3)
>get();
As per above, if you don't have 2 parameters in your original query, dont feel compelled to add two.
Another thing you can do with basic queries like these, is omit the from('package_info') entirely and stick the table name in the ->get('package_info')
If you cant be bothered with query builder you don't need to use it either. I don't for some things (you cannot use UNION with them for one). In this case just use
$result = $this->db->query("SELECT * FROM package_info ORDER BY RAND() LIMIT 0,3;");

convert from mysqli to pdo

I'm a rookie in MYSQL PDO, and I'm trying to convert from SQLi to PDO.
I have solved most of the line but one of the line I'm still getting errors after I changed to PDO.
please see code....
<?php session_start(); include_once("../cs/db.php");
//echo 'lll'.$_SESSION['user']; if(isset($_SESSION['user_id'])){
if(isset($_SESSION['cart_id'])){
$cart_id_session =
$_SESSION['cart_id'];
}else{
$q_fetch_cart_id = $conn->query("select cart_id from cart where user_id =
'".$_SESSION['user_id']."'");
$cart_id_set = $q_fetch_cart_id ->fetch(PDO::FETCH_ASSOC);
$cart_id_session = $cart_id_set[0];
$_SESSION['cart_id'] = $cart_id_session;}}
?>
As you can see on last three lines, I changed to (PDO::FETCH_ASSOC); and that line have solved but then next line where it said
$cart_id_session = $cart_id_set[0];
I'm getting errors said
Undefined offset: 0
Is this has to do with PDO! if so what is correct code is!
Many thanks for your help.
AM

Laravel count where row value > 0

I've been trying to find a solution for my query for some time now. But i haven't been able to solve it yet. Most of it works nicely, but the count part does not work like i intend it to.
My query looks like this:
$years = Sample::whereCostumerId($id)
->groupBy('year')
->orderBy('year', 'DESC')
->get(array(
DB::raw('year'),
DB::raw('COUNT(id) as antalProver'),
DB::raw('MIN(provnr) AS minProvnr'),
DB::raw('MAX(provnr) AS maxProvnr'),
DB::raw('count(P_HCl) AS numP_HCl'),
DB::raw('count(Total_lerhalt) AS numLerhalt'),
DB::raw('ROUND(AVG(pH),1) AS avgpH'),
DB::raw('ROUND(AVG(P_AL),1) AS avgP_AL'),
DB::raw('ROUND(AVG(K_AL),1) AS avgK_AL'),
DB::raw('AVG(X) AS coordExist')
));
The issue here is that many of the rows in the column P_HCl and Total_lerhalt contains zero. And i don't want to count these. I only want to count where value is greater than zero.
Im shure there's some nice solution for this.
If you guys have any other solution for the query all together id be happy to see it.
Thanks
You‘re most of the way there—you just need to add a where clause to your query:
$years = Sample::whereCostumerId($id)
->groupBy('year')
->orderBy('year', 'DESC')
->where('year', '>', 0)
->get(array(
DB::raw('year'),
DB::raw('COUNT(id) as antalProver'),
DB::raw('MIN(provnr) AS minProvnr'),
DB::raw('MAX(provnr) AS maxProvnr'),
DB::raw('count(P_HCl) AS numP_HCl'),
DB::raw('count(Total_lerhalt) AS numLerhalt'),
DB::raw('ROUND(AVG(pH),1) AS avgpH'),
DB::raw('ROUND(AVG(P_AL),1) AS avgP_AL'),
DB::raw('ROUND(AVG(K_AL),1) AS avgK_AL'),
DB::raw('AVG(X) AS coordExist')
));

Delete from db multiple rows Drupal

i have a problem i need to delete from cache table specific rows. I have like 3 rows to delete by one submit.
cache_73_content
cache_73_work
cache_73_header
I tried like this :
$cache_delete = sprintf('cache_%s', $form_state['values']['node_tid']) . '%';
db_delete('cache')
->condition('cid', $cache_delete)
->execute();
But didn't work. Please need your help
It would make more sense to use the API:
$cache_delete = sprintf('cache_%s', $form_state['values']['node_tid']);
cache_clear_all($cache_delete, 'cache', TRUE);
If you must use the database directly:
$cache_delete = sprintf('cache_%s', $form_state['values']['node_tid']) . '%';
db_delete('cache')
->condition('cid', $cache_delete, 'LIKE')
->execute();

Code Igniter - not showing the entry I need

I have the following code to get one line for each MAC with the LATEST state. The problem I have is that I get one line but not with the latest state but rather with the earliest.
function get_active_devices($min_duration, $max_duration)
{
//get all active devices DESC order
$this->db->distinct();
$this->db->group_by('mac');
$this->db->order_by("id", "desc");
$this->db->select('data.mac, state, time, iot_bo.notified, iot_bo.op_state, iot_bo.Name');
$this->db->where('time >', time()-$max_duration);
$this->db->where('time <', time()-$min_duration);
$this->db->join('iot_bo', 'iot_bo.mac = data.mac');
$this->db->where('iot_bo.op_state', '1');
$query = $this->db->get();
return $query;
}
Have you tried the query without the distinct and groupBy first? May be the result you want isn't in the total result set to begin with. Because there doesn't seem to be anything wrong with your use of db methods as it is.