update column with aggregate function in codeigniter - php

I want to change this format to codeigniter format which is like
this->db->where('','')
This is my code
$data = "UPDATE dbhpl.hplpb SET hplpb.PEL_4 = (select COUNT(*) FROM pelayanan
WHERE pelayanan.ID_AREA=hplpb.ID_AREA AND pelayanan.ID_VERIFIKASI='4'
AND pelayanan.TRANSAKSI='PENYAMBUNGAN BARU' AND pelayanan.ESTIMASI='4'
AND pelayanan.ID_STATUS='1')";
$this->db->query($data);
How can I change it?

You might want to try this.
$data = array(
'ID_VERIFIKASI' => '4',
'TRANSAKSI' => 'PENYAMBUNGAN BARU',
'ESTIMASI' => '4',
'ID_STATUS' => '1'
);
$this->db->where($data)
->from('pelayanan')
->join('hplpb', 'hplpb.ID_AREA = pelayanan.ID_AREA', 'inner');
$count = $this->db->count_all_results();
$data = array(
"PEL_4" => $count
);
$this->db->update('hplpb', $data);

Related

Pagination count wrong Cakephp 1.2

I had auction bid list (debit for each auction was equal 1). I changed View code for "Debit" for showing "Auction Id" count. (you can see pic.).
But now I have problem with pagination. It has to show 26 but you can see 93.
I want to show result like in picture but with correct pagination.
Here is my code
In Controller:
$this->paginate = array(
'conditions' => array('Bid.user_id' => $user_id),
'limit' => 100,
'order' => array('Bid.created' => 'desc'),
'contain' => array('Auction' => array('Product'))
);
$this->set('bids', $this->paginate());
In Model: override paginateCount()
function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$sql = "
SELECT user_id,
auction_id,
description,
debit,
COUNT( * ) AS my_auct,
`credit`,
`created`
FROM `bids`
WHERE `user_id` = {$this->User->id}
GROUP BY auction_id, credit
";
$this->recursive = $recursive;
$results = $this->query($sql);
return count($results);
}
How can fix this issue only in controller (not using paginateCount) and put this query result into View (now I made calculating in View).
Thanks in advance!
I solved my problem. Here my code in controller
$this->paginate = array('conditions' => array('Bid.user_id' => $user_id),'fields' => array('COUNT(auction_id) as my_auct', 'Bid.*'), 'group' => array('Bid.auction_id', 'Bid.credit'), 'limit' => 10000, 'order' => array('Bid.created' => 'desc'), 'contain' => array('Auction' => array('Product')));
I found paginateCount function here and added in model:
public function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$conditions = compact('conditions');
if ($recursive != $this->recursive) {
$conditions['recursive'] = $recursive;
}
unset( $extra['contain'] );
$count = $this->find('count', array_merge($conditions, $extra));
if (isset($extra['group'])) {
$count = $this->getAffectedRows();
}
return $count;
}
Only thing I can't show in "Credit" column are all values (when I grouped by auction_id and credit, it shows to me only one 15 and 100). I want to show all values in credit column
Any ideas?

How to create multiple conditioner login in codeigniter

my model is here.....but i need to select status of admin ....... but
i m new in codeigniter....and don't no how to select... my need is...
select admin whole detail from table on condition admin status =
active and id=1...
my model is :
public function login($value) {
$query = $this->db->get_where('tbl_admin', $value, 1, 'active');
if ($query->num_rows() > 0) {
$row = $query->row_array();
$sess_arr = array(
'admin_user' => $row['fld_admin_username'],
'adm_key' => $row['fld_admin_key'],
'admin_type' => $row['fld_admin_type'],
'admin_id' => $row['fld_admin_id'],
'admin_logged_in' => TRUE
);
$this->session->set_userdata($sess_arr);
//echo "<pre>";print_r($this->session->all_userdata());exit;
}
else{
$this->session->set_flashdata('error', 'Invalid username/password');
redirect('adminzone');
}
}
The correct syntax for the first line would be:
$query = $this->db->get_where('tbl_admin', array('id' => 1, 'status' => 'active'));
I.e. The second parameter to get_where is an associative array of fields and their values..
Edit: Or perhaps it should be
$query = $this->db->get_where('tbl_admin', array('id' => $value, 'status' => 'active'));
(I am not sure what the $value variable is for here).

Update Query in codeigniter model is not working

In the function update_status the 2nd update query is not working. The values in array are properly getting and correct. Can somebody please help me with that?
Here my code:
function update_status($appno,$value,$user_note,$date)
{
$status = array(
'app_status' => $value, );
$this->db->where('app_no',$appno);
$this->db->update('tbl_application', $status);
$today = date('Y-m-d');
$emp = $this->session->userdata('username');
$this->db->select('*');
$this->db->from('tbl_employee');
$this->db->where('Name', $emp);
$query = $this->db->get();
foreach($query->result() as $row)
{ $emp_code = $row->EmployeeCd; }
$data = array(
'app_no' => $appno,
'office_code' => 'KL08',
'verifi_status' => $value,
'view_status' => 1,
'view_by' => $emp_code,
'first_view' => $date,
'last_view' => $today,
'modification' => $user_note,
'modifi_status' => '1',
'notes' => 'NA'
);
$this->db->update('tbl_appverification', $data);
$this->db->where('app_no',$appno);
}
Where clause must be written before udpate.
$this->db->where('app_no',$appno);
$this->db->update('tbl_appverification', $data);
Please write the Where clause before the Update query, as you have written in your first update.

PHP array presentation

I have an php array with presentation as follow:-
<?php
$ads = array();
$ads [] = array(
'name' => 'Apple',
'duration' => '3',
'price' => "$5"
);
$ads [] = array(
'name' => 'Orange',
'duration' => '2',
'price' => "$10"
);
$ads [] = array(
'name' => 'Banana',
'duration' => '5',
'price' => "$6"
);
and then, I would like to replace the static data with dynamic data from database:-
$sql = "SELECT * from tb_fruit order by fruit_id ASC";
$result = mysql_query($sql_approve, $conn_fruit);
while($record = mysql_fetch_array($result))
{
$fruit_id = $record['fruit_id'];
$fruit_name = $record['fruit_name '];
$fruit_price= $record['fruit_price'];
$fruit_duration= $record_approve['fruit_duration'];
}
Actually, how shall I combine the 2 presentations together? Thanks!
If you want to get a similar structure as the one that you have in the first example, you can modify your second to create a new array and append it to an existing $ads array.
$sql = "SELECT * from tb_fruit order by fruit_id ASC";
$result = mysql_query($sql_approve, $conn_fruit);
$ads = array();
while($record = mysql_fetch_array($result))
{
$ads[] = array(
'name' => $record['fruit_name'],
'price' => $record['fruit_price'],
'duration' => $record['fruit_duration']);
}
iteration over result can be modified (provided only the required attributes are fetched in the query)
$fruit = array();
while($record = mysql_fetch_array($result))
{
$fruit[] = $record;
}
may be you could use array_merge
$result = array();
$result = array_merge($ads,$fruit);
So, you want to populate your "ads" array with information from the database? It seems fairly counterintuitive seeing as mysql_fetch_array already returns a perfectly adequate associative array, but here you go:
$sql = "SELECT * from tb_fruit order by fruit_id ASC";
$result = mysql_query($sql_approve, $conn_fruit);
$ads = array();
while($record = mysql_fetch_array($result))
{
$ads[] = array (
'name' => $record['fruit_name'],
'price' => $record['fruit_price'],
'duration' => $record['fruit_duration']
);
}
If
the initial array is not just an example, but default data that you might overwrite with the db data, and
the fruit name is unique (a primary key of sorts)
then you should set the array key of the main array to that of the fruit name, so that if the db has a different value, it will automatically get overwritten but otherwise left alone. Like so:
$ads ['Apple'] = array(
'duration' => '3',
'price' => "$5"
);
$ads ['Orange'] = array(
'duration' => '2',
'price' => "$10"
);
$ads ['Banana'] = array(
'duration' => '5',
'price' => "$6"
);
$sql = "SELECT * from tb_fruit order by fruit_id ASC";
$result = mysql_query($sql_approve, $conn_fruit);
$ads = array();
while($record = mysql_fetch_array($result))
{
$ads[$record['fruit_name']]['price'] = $record['fruit_price'];
$ads[$record['fruit_name']]['duration'] = $record['fruit_duration'];
}
Now, if 'Apple' is in the db, the price and duration get overwritten, but otherwise, it stays where you set it before the query.
You could go a step further and have both the price and duration returned by the query checked for an empty value (Null, "", 0), and only set the value if it is not empty, etc. But that is more a of business logic decision.

sorting multi dimensional and dynamically created arrays

$feeds = array();
$query = "SELECT * FROM actions WHERE user_id = '$user_id'";
$result = mysql_query($query);
while ($info = mysql_fetch_array($result)) {
$feeds[][$info['date']] = array("feed" => array($info['ID'] => $user_id));
}
$query = "SELECT * FROM follows WHERE user_id = '$id'";
$result = mysql_query($query);
while ($info = mysql_fetch_array($result)) {
$feeds[][$info['date']] = array("follow" => $info['user_id']);
}
I would like to sort that $feeds array in date format (Y-m-d H:i:s) using [$info['date']] key
How can i do that ?
thanks
EDIT:
example of what i want to see as result is
$feeds = array(
0 => array(
'<date>' => array("feed" => array("feed_id" => "user_id"));
),
1 => array(
'<date>' => array("follow" => "user_id" );
),
);
I want to group/sort them in DATE key and do sth depends on if it is FEED or FOLLOW
The best way is to do it in database (faster)!
But if you want do it in PHP, your need uksort (for sorting) + foreach (for grouping) I think

Categories