I want to check whether there are more than three teachers for a single subject. So I am trying to select ids from the table for some conditions. the fact is that I have more than one or two classes and indices. so how to check for all classes and indices? I couldn't make it, how can it possible, I am not familiar with CodeIgniter
controller
$checkr = $this->class_model->check($class_data,$subject);
model
function check($class_data,$subject)
{
foreach($class_data as $key => $value)
{
$this->db->select('s_id');
$this->db->from('table1');
$this->db->where('type',2);
$this->db->where('subject',$subject);
$this->db->where('status',1);
$condition= array(
'class' => $key,
'stream_index' => $value
);
$this->db->where($condition);
}
$rows = $this->db->get();
$count= $rows->num_rows() ;
return $count
}
I think this will help you..
function check($class_data,$subject){
$class_teacher_count = array();
foreach($class_data as $key => $value){
$this->db->select('s_id');
$this->db->from('table1');
$this->db->where('type',2);
$this->db->where('subject',$subject);
$this->db->where('status',1);
$this->db->where('class', $key);
$this->db->where('stream_index',$value);
$query = $this->db->get();
$rows = $query->result();
$count = count($rows);
$class_teacher_count[$key] = $count;
}
return $class_teacher_count;
}
function check($class_data,$subject){
foreach($class_data as $key => $value){
$this->db->select('s_id');
$conditions = array(
'type' => 2,
'subject' => $subject,
'status' => 1,
'class' => $key,
'stream_index' => $value
);
$teacher_count[$key] = $this->db->get_where('table1',$conditions)->num_rows();
}
return $teacher_count;
}
Related
I'm trying to insert data in DB with three entries. But only one row insert in DB with this code.
$parent_id = $object->id;
$amounts = $request->amount;
$payess = $request->add_payee;
dd($amounts,$payess);
foreach ($payess as $key => $ids) {
App\Payees_amount::create([
'add_payee' => $ids[$key],
'building_id' => $parent_id,
'payee_amount' => $amounts[$key],
]);
}
This arrays I want in DB against one id :
use array_map with array_combine :
$parent_id = $object->id;
$amounts = $request->amount;
$payess = $request->add_payee;
$res = array_map(null, $payess , $amounts );
$keys = array("pay", "amount");
$res = array_map(function ($e) use ($keys) {return array_combine($keys, $e);}, $res);
foreach ($res as $key => $value) {
App\Payees_amount::create([
'add_payee' => $value['pay'],
'building_id' => $parent_id,
'payee_amount' => $value['amount'],
]);
}
how to update plan with vendor_plan_task_status_mapp table.
the model for plan update is
public function updatePlanData(){
$planId = $this->input->post('plan_id');
$data = array(
'plan_title' => $this->input->post('plan_title'),
'plan_price' => $this->input->post('plan_price'),
'plan_desc' => $this->input->post('plan_desc')
);
$this->db->where('plan_id', $planId);
$this->db->update('tbl_plan', $data);
$this->db->where('plan_id',$planId);
$this->db->delete('plan_task_mapping');
foreach ($this->input->post('task_id') as $key => $value)
{
$data2 = array(
'plan_id' => $planId,
'task_id' => $value
);
// echo "Index {$key}'s value is {$value}.";
$this->db->insert('plan_task_mapping', $data2);
}
//-------- HEAR I NEED A CODE TO UPDATE The V_T_S_M table-----------
}
after 1st table update i want to update the data in vendr_task_status_mapping table?????
IN THIS ANSWER YOU GET AN ERROR IF YOU CHANGE THE PLAN BUT USING THIS CODE YOU HAVE TO SIMPLY EDIT AND UPDATE IT ANGIN AND THIS MODEL IS CREATE A NEW ID FOR YOUR TASK AND INSERT IT AGAIN.
public function vendorUpdateModel($data)
{
$vendor_id = $this->input->post('vendor_id');
$data = array(
'category_id' => $this->input->post('category_id'),
'plan_id' => $this->input->post('plan_id'),
'city_id' => $this->input->post('city_id'),
'business_name' => $this->input->post('business_name'),
'owner_name' => $this->input->post('owner_name'),
'contact_no1' => $this->input->post('contact_no1'),
'contact_no2' => $this->input->post('contact_no2'),
'vendor_email' => $this->input->post('vendor_email'),
'subscription_date' => $this->input->post('subscription_date'),
'vendor_description' => $this->input->post('vendor_description'),
'vendor_address' => $this->input->post('vendor_address')
);
$this->db->where('vendor_id', $vendor_id);
$this->db->update('vendor',$data);
$this->db->where('vendor_id',$vendor_id);
$this->db->delete('vendor_task_status_mapping');
$this->db->select('task_mapp_id');
$this->db->where('plan_id',$this->input->post('plan_id'));
$query = $this->db->get('plan_task_mapping');
foreach($query->result() as $row)
{
$data2 = array(
'task_mapp_id' => $row->task_mapp_id,
'vendor_id' => $vendor_id,
'task_status' => '0'
);
$this->db->insert('vendor_task_status_mapping', $data2);
}
return;
}
If you added one field in plan_task_mapping table for add unique number then... As you mention your code:
$unique=array();
foreach ($this->input->post('task_id') as $key => $value)
{
$no=rand(0, 15);
$data2 = array(
'unique_no'=>$no,
'plan_id' => $planId,
'task_id' => $value
);
$unique[] = $no; //store no in array to use it.
// echo "Index {$key}'s value is {$value}.";
$this->db->insert('plan_task_mapping', $data2);
}
Before deleting plan_task_mapping table data. fetch that data.
function select()
{
$this->db->where('plan_id',$planId);
$Q = $this->db->get('plan_task_mapping');
if($Q->num_rows() > 0)
{
foreach ($Q->result_array() as $row)
{
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
Then delete the data as you mention in you code:
$this->db->where('plan_id',$planId);
$this->db->delete('plan_task_mapping');
Then delete that old data from VTSM table:
$this->db->where('vendor_plan_task_mapping_id',$data[0]['id']); //this data[0]['id'] come form select(). change field name if required.
$this->db->delete('VTSM');
Here fetch that new inserted data by that unique no: //which we stored it in array.
foreach($unique as $u_no)
{
$this->db->where('unique_no',$u_no);
$Q = $this->db->get('plan_task_mapping');
if($Q->num_rows() > 0)
{
foreach ($Q->result_array() as $row1)
{
$plan[] = $row1;
}
}
$Q->free_result();
return $plan;
}
In above code we have fetched that new inserted data to get their id to insert status.
Now inserting status:
foreach($plan as $a)
{
$Sdata=array(
"plan_task_mapping_id"=>$a['id'], //this is new id change name if required
"status"="your new status");
$this->db->insert('VTSM',$Sdata);
}
Use $this->db->insert_batch(); instead of inserting in foreach loop.
Check at this link how to use it.
Hi I'm a newbie of FuelPHP. I'm makin' a demo use ACL. I have fetched all roles from database with format as the code below
$data = array(
array('admin'=>array(
'none' => array(
'crudform' => array('create','index')
)
)),
array('admin'=>array(
'none' => array(
'cruddept' => array('create','view')
)
)),
);
And now I want to convert that array to format as
$data = array(
'admin' => array(
'none'=>array(
'crudform' => array(
'create',
'index'
),
'cruddept'=>array(
'create',
'view'
)
)
)
)
How I can do that ?
After you retreive your data from database, you can convert your array using a function just like this one
function change_array($data)
{
$result = array('admin' => array('none' => array()));
foreach ($data as $key => $value)
foreach ($data[$key]['admin']['none'] as $key_child => $value_child)
$result['admin']['none'][$key_child] = $value_child;
return $result;
}
All you need to do, is to use it like this
$data = change_array($data);
Thanks Mr Khalid, your idea has provide a way for me how to resolve my solution. I have found the way to build array as I want. This is my code
function build_role_array($roles){
$final = array();
foreach($roles as $row){
foreach($row as $role=>$value){
if(!isset($final[$role])){
$final[$role] = array();
}
foreach($value as $module=>$area){
if(!isset($final[$role][$module])){
$final[$role][$module] = array();
}
foreach($area as $controller=>$rights){
$final[$role][$module][$controller] = $rights;
}
}
}
}
return $final;
}
Thanks for support
This question already has answers here:
Sort array using array_multisort() with dynamic number of arguments/parameters/rules/data
(5 answers)
Closed 2 years ago.
I have the problem with sort direction. I try to sort multi-dimensional array with direction. I can't use array_multisort() directly, because I don't know how many parametrs will be. I use call_user_func_array('array_multisort', $params); And it works, but I can't set sort direction (SORT_ASC,SORT_DESC). How can I set sort direction for call_user_func_array('array_multisort', $params);?
Here is my code, you can try it
function get_fields($data, $order_by) {
$order_row = preg_split("/[\s,]+/", $order_by);
for ($i=0;$i<count($order_row);$i++) {
foreach ($data as $key => $row) {
$tmp[$i][$key] = $row[$order_row[$i]];
}
}
return $tmp;
}
function ordering($data, $order_by) {
$tmp = get_fields($data, $order_by);
$params = array();
foreach($tmp as &$t){
$params[] = &$t;
}
$params[1] = array("SORT_DESC","SORT_DESC","SORT_DESC","SORT_DESC"); // like that no warning but no sorting
$params[] = &$data;
call_user_func_array('array_multisort', $params);
return array_pop($params);
}
$data = array (
array('id' => 1,'name' => 'Barack','city' => 9),
array('id' => 7,'name' => 'boris','city' => 2),
array('id' => 3,'name' => 'coris','city' => 2),
array('id' => 3,'name' => 'coris','city' => 2)
);
$order_by = "city desc, name";
echo "<br>ORDER BY $order_by<br>";
$ordered = ordering($data, $order_by);
echo "<pre>";
var_dump($ordered);
echo "</pre>";
I want to do a sort like MySQL ORDER BY city DESC, name. It's my goal.
To be able to sort an array multiple times and achieve a result like ORDER BY city DESC, name ASC you need a function that does a stable sort.
As far as I know PHP doesn't have one so you have to sort it once with a comparator function like this
$data = array (
array('id' => 3,'name' => 'coris','city' => 2),
array('id' => 1,'name' => 'Barack','city' => 9),
array('id' => 7,'name' => 'boris','city' => 2),
array('id' => 3,'name' => 'coris','city' => 2),
);
$order_by = array(
'city' => array('dir' => SORT_DESC, 'type' => SORT_NUMERIC),
'name' => array('dir' => SORT_ASC, 'type' => SORT_STRING),
);
function compare($row1,$row2) {
/* this function should determine which row is greater based on all of the criteria
and return a negative number when $row1 < $row2
a positive number when $row1 > $row2
0 when $row1 == $row2
*/
global $order_by;
foreach($order_by as $field => $sort) {
if($sort['type'] != SORT_NUMERIC) {
// strings are compared case insensitive and assumed to be in the mb_internal_encoding
$cmp = strcmp(mb_strtolower($row1[$field]), mb_strtolower($row2[$field]));
} else {
$cmp = doubleval($row1[$field]) - doubleval($row2[$field]);
}
if($sort['dir'] != SORT_ASC) $cmp = -$cmp;
if($cmp != 0) return $cmp;
}
return 0;
}
usort($data,'compare');
I had the same problem. It seems that call_user_func_array() can't handle the constants.
I've solved this problem by dynamically building an argument string and evaluating this string:
$args = array($arr1, $arr2);
$order = array(SORT_ASC, SORT_DESC);
$evalstring = '';
foreach($args as $i=>$arg){
if($evalstring == ''){ $evalstring.= ', '; }
$evalstring.= '$arg';
$evalstring.= ', '.$order[$i];
}
eval("array_multisort($evalstring);");
I know eval() is evil and this is not a clean way, but it works ;-)
It is working for me :
$arrayThatNeedToSort = array('data..');
$multiSortprop = array(['data.....']=> SORT_DESC,['data.....'] => SORT_ASC)
$properties = array();
foreach ($multiSortprop as $sortArr => $sortArg) {
array_push($properties,$sortArr);
array_push($properties,$sortArg);
}
array_push($properties,$arrayThatNeedToSort);
array_multisort(...$properties);
var_dump(end($properties));
I have a function that adds an entry to the database, the code i have at the moment is as follows:
function create_internal_role($rolename, $rolekey)
{
$data = array(
'name' => $rolename,
'key' => $rolekey.'1'
);
if (!is_null($res = $this->ci->internal_roles->create_role($data))) {
return $data;
}
return NULL;
}
What i want to do, is using the same function add another 2 data arrays with a 2 and 3 behind the $rolekey, so that with the one function, it adds 3 lots of data, rolekey1, rolekey2 and rolekey3
How would i go about doing that?
With out knowing about your structure and from the current phrasing of your question, the obvious answer would seem to be this:
function create_internal_role($rolename, $rolekey)
{
$ret = array();
$data = array(
'name' => $rolename,
'key' => $rolekey.'1'
);
if (!is_null($res = $this->ci->internal_roles->create_role($data))) {
$ret[] = $data;
}
$data = array(
'name' => $rolename,
'key' => $rolekey.'2'
);
if (!is_null($res = $this->ci->internal_roles->create_role($data))) {
$ret[] = $data;
}
$data = array(
'name' => $rolename,
'key' => $rolekey.'3'
);
if (!is_null($res = $this->ci->internal_roles->create_role($data))) {
$ret[] = $data;
}
return $ret;
}
If you give more detail in your question, I may be able to give you a better answer.
Perhaps something like this will work:
function create_internal_role($rolename, $rolekey)
{
// define role key indecies
$indexArray = array(1,2,3);
$ret = array(); // return array
// create roles
for(i=0; i<count($indexArray); $i++){
$data = array(
'name' => $rolename,
'key' => $rolekey.indexArray[$i]
);
if (!is_null($res = $this->ci->internal_roles->create_role($data))) {
$ret[] = $data;
}else{
$ret[] = null;
}
}
return $ret;
}