how to sort before printing in yii - sorting

so I found this:
How to print directly from printer with Yii?
and the 2nd answer (the one with the jquery) was the one I felt was most suitable for me
but I just want to know, is there a way I can sort these out before printing? like for example In the model
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('name',$this->name,true);
$criteria->compare('date',$this->date,true);
$criteria->compare('department_id',$this->department_id);
$criteria->compare('section_id',$this->section_id);
$criteria->compare('team_id',$this->team_id);
$criteria->compare('created_date',$this->created_date,true);
$criteria->compare('updated_date',$this->updated_date,true);
$data = new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
'pagination'=>false,
));
$_SESSION['all'] = $data;
$data = new CActiveDataProvider(get_class($this), array(
'pagination'=>array('pageSize'=> Yii::app()->user->getState('pageSize',
Yii::app()->params['defaultPageSize']),),
'criteria'=>$criteria,
));
$_SESSION['limited'] = $data;
return $data;
}
is it possible to display this ordered by the name maybe or the date instead of just the id?

$data = new CActiveDataProvider(get_class($this), array(
'sort'=>array(
'defaultOrder'=>'name ASC',
)
));

Related

how to insert the data using codeiginter?

I am new to codeiginter,how to insert the data into table using codeiginter
my controller code;
$data = array(
'subgrpname' => $this->input->post('subgrpname'),
'grpname'=> $this->input->post('grpname'),
'pltype'=> $this->input->post('pltype')
);
It is simple form use internet to search it
$data = array(
'subgrpname' => $this->input->post('subgrpname'),
'grpname'=> $this->input->post('grpname'),
'pltype'=> $this->input->post('pltype')
);
$this->db->insert('tablename',$data);
I would like to share reusable code approach by following the MVC coding style
My Controller
$table = 'table_name';
$data = array(
'subgrpname' => $this->input->post('subgrpname'),
'grpname'=> $this->input->post('grpname'),
'pltype'=> $this->input->post('pltype')
);
$record_id = $this->Common_Model->insert_into_table($table, $data);
My Common_Model
function insert_into_table($table, $data) {
// echo "<pre>";print_r($data);exit;
$insert = $this->db->insert($table, $data);
$insert_id = $this->db->insert_id();
if ($insert) {
return $insert_id;
} else {
return false;
}
}
You can use this model function as much time as you want.

Codeigniter multiple parameter on update

I am copying this code in Codeigniter official docs, but I don't know why this will throw an error
public function acceptChangeRequest($id,$data1,$accept) {
$data = array(
'status' => $accept,
'approve_by' => $data1,
);
$this->db->where('id', $id);
$this->db->update('change_request',$data);
//return true;
}
Error Number: 1054
Unknown column 'Array' in 'field list'
UPDATE change_request SET status = 'Y', approve_by = Array WHERE id = '22'
It sounds wierd since this code works in my other function. Any idea?
Read The Documentation Clearly
Read About Update Records In Codeigniter
Here IS Demo Function For You
public function updateBasic($data,$user_id){
$userData=array(
'marital_status'=>$data['marital_status'],
'country'=>$data['stateofresidence'],
'city'=>$data['city'],
'caste'=>$data['caste'],
'residential_status'=>$data['residencystatus']
);
$this->db->WHERE('user_id',$user_id)->update('basic_profile',$userData);
return true;
}
You Must Check Through Print_r();what Is coming To your Function In Your Case $data1 Is Coming In the From OF array You Must Extract The Array To Update The Record
Use this function in your model to update the record.
public function acceptChangeRequest($where, $table, $data){
$this->db->where($where);
$this->db->update($table, $data);
return $this->db->affected_rows() > 0;
}
use this code in your controller.
$where = array('id'=>$id);
$data = array(
'status' => $accept,
'approve_by' => $data1,
);
$this->User_model->update($where,'table_name',$data);
What is in your $data1 variable? I can see that it's an array, and that is the problem.
If your $data1 stores indexes like in your table's row, than this is the possible solution for you:
public function acceptChangeRequest($id,$data1,$accept) {
$data1["status"] = $accept;
$this->db->where('id', $id);
$this->db->update('change_request', $data1);
return true;
}
But if your $data1 variable stores only the approve_by information, than your possible solution is:
public function acceptChangeRequest($id,$data1,$accept) {
$data = array(
'status' => $accept,
'approve_by' => $data1["approve_by"]
);
$this->db->where('id', $id);
$this->db->update('change_request', $data);
return true;
}
First of all, you need to know what you expect from $data1, than you can move forward to the solution.
You can see your $data1 variable in your Controller's function:
print "<pre>";
print_r($data1);
print "<pre>";
die();

filter data from complex values in magento grid

Hi I have added a new column in a shipment grid as below
$this->addColumn('telephone', array(
'header' => Mage::helper('sales')->__('Billing Phone'),
'index' => 'telephone',
'renderer'=> new OSP_Adminhtml_Block_Customer_Renderer_BillingPhone()
));
in this i am using a renderer to show custom values as below
public function render(Varien_Object $row) {
$customer_id = $row->getData('customer_id');
if( $customer_id > 0 ) {
// get member_id (club canon)
$customer = Mage::getModel('customer/customer')->load($customer_id);
if( is_object( $customer ) ) {
$value = $customer->getData('mobile');
}
}else{
$id = $row->getData('order_increment_id');
$order = Mage::getModel('sales/order')->loadByIncrementId($id);
$value = $order->getBillingAddress()->getTelephone();
}
return $value;
}
which is working fine and it shows data properly on the basis of
condition in renderer.
But the problem is now I need to filter the data also which is not
working as it looks for data in only one column as telephone or mobile
I have read about filter_condition_callback but unable to make the work . Can you please suggest me how can I make this work.
Thanks in advance
1.Add line filter_condition_callback to column, like that:
$this->addColumn('telephone', array(
'header' => Mage::helper('sales')->__('Billing Phone'),
'index' => 'telephone',
'filter_condition_callback' => array($this, '_someCallBackFunction'),
'renderer'=> new OSP_Adminhtml_Block_Customer_Renderer_BillingPhone()
));
After you added this line in your column, Magento will call this callback function, look app/code/core/Mage/Adminhtml/Block/Widget/Grid.php in 468 line.
2.In your block grid file create _someCallBackFunction function, which will take two parameters, $collection - collection object not loaded, and $column Mage_Adminhtml_Block_Widget_Grid_Column
protected function _someCallBackFunction($collection, $column)
{
$value = $column->getFilter()->getValue();
if ($value === null) { //here check if filter is not null
return $this;
}
/**
* Here you can add filter to collection
* or do other manipulations with collection.
* As example you can check filter value and filter collection.
*/
if ($value != 0) {
$collection->addFieldToFilter('telephone', array('neq' => 0));
}
return $this;
}

CakePHP 2.0: How can i work with own functions

I work with CakePHP 2.0. I made a new function sort and had no problem, if I take the table fields. But if I want to calculate something and give the result (method: 'Participant.year'=>'calculateyear' ) in my sort method, I have no result on my view.
function sort() {
$participants = $this->Participant->find('all', array(
'conditions'=>array('Participant.sex'=>'m','Participant.year'=>'calculateyear'),
'order'=>array('Participant.communitieRank'=>'ASC','Communitie.name'=>'ASC')
));
$this->set('participants', $participants);
}
function calculateyear ($jahr) {
$jahr = '2000';
return $jahr;
}
assuming you are in your PartecipantsController I guess what you want to do is:
function sort() {
$participants = $this->Participant->find('all', array(
'conditions'=>array(
'Participant.sex'=>'m',
'Participant.year'=> $this->calculateyear(2000)
),
'order'=>array('Participant.communitieRank'=>'ASC','Communitie.name'=>'ASC')
));
$this->set('participants', $participants);
}
but your question is not clear at all
thats my solution, it works :)
function calculateyear ($year) {
$today = date("Y");
$year = $today - $year;
return $year;
}
function kat1w() {
$participants = $this->Participant->find('all', array(
'conditions'=>array('Participant.sex'=>'f',
'Participant.year >='=>$this->calculateyear(9)),
'order'=>array('Participant.communitieRank'=>'ASC','Communitie.name'=>'ASC')
));
$this->set('participants', $participants,$this->Paginator->paginate());
}

How to get a value from the last inserted row using Codeigniter

Is there any codeigniter function to do this?
try this code:
$this->db->insert_id();
Refer:
https://www.codeigniter.com/user_guide/database/helpers.html
There is no special function for that. But you can get the id of row that you've just inserted and then get it's data:
$this->db->insert('table_name', $data);
$id = $this->db->insert_id();
$q = $this->db->get_where('table_name', array('id' => $id));
return $q->row();
I have tips for you. When you want to insert data, please return your data array and you can edit it.
public function set_alb_photos() {
$data = array(
'alp_title' => $this->input->post('alp_title'),
'alp_file_location' => $this->input->post('alp_file_location'),
'alp_album_id' => $this->input->get('alb_id'),
'alp_created_at' => date("Y-m-d H:i:s"),
'alp_updated_at' => date("Y-m-d H:i:s")
);
$this->db->insert('alb_photos', $data);
return $data;
}
I hope this help.
Use Active Record method after insertion. return last inserted id
function insert($data)
{
$this->db->insert('tablename', $data);
return $this->db->insert_id();
}
Here is reference
This will help you to get last inserted row from the table.
$last = $this->db->order_by('id',"desc")->limit(1)->get('tablename')->row();
print_r($last);
You can get inserted data by below code,
$query = $this->db->get_where('table1', array('id' => $id)); //get inserted data
return $query->result_array();
Refer:https://www.codeigniter.com/user_guide/database/query_builder.html
What about :
$this->db->get('mytable', 1,0)
Try this only with active record of codeigniter. This code gives you the last row of your table.
$this->db->limit(1);
$this->db->order_by('id','desc');
$query = $this->db->get('tablename');
return $query->result_array();
Try this depending on the options and return :
$data = {} //your array;
// now if your $data array contained all the values of the new row just do this
function yourFunction(){
// insert into array into db
$this->db->insert('table_name', $data);
// get created rows id
$id = $this->db->insert_id();
// update your $data array with the new generated id and return it
$data['id'] = $id;
return $data;
}
// Option 2: if row has fields that auto populates (example current timestamp)
function yourFunctionAlt(){
// insert into array into db
$this->db->insert('table_name', $data);
// get created rows id
$id = $this->db->insert_id();
// get row as a array
$_rowArray = $this-db->get_where('table_name',array('id'=>$id)->row_array();
return $_rowArray;
// or get as a object
$_Object = $this->db->get_where('table_name',array('id'=>$id);
return $_Object;
}

Resources