Problem in php array is not work with while - arrays

I have a php code when I want to show info to the browser it show me : Notice: Array to string conversion in
Can You Help Me Please I want to show this information from function because I don't want to create new while one every request.
function get_school($school_code){
global $conn,$Lang;
$school_select = $conn->query("SELECT * FROM schools WHERE code='$school_code'");
$school_exs = $school_select->num_rows;
if ($school_exs != 0) {
$show = array();
while ($school_info = mysqli_fetch_array($school_select)) {
$nestedData = array();
$nestedData[] = $school_info['id'];
$nestedData[] = $school_info['name'];
$nestedData[] = $school_info['code'];
$nestedData[] = $school_info['type'];
$nestedData[] = $school_info['wilaya'];
$nestedData[] = $school_info['baladiya'];
$nestedData[] = $school_info['capacity'];
$nestedData[] = $school_info['phone'];
$nestedData[] = $school_info['email'];
$nestedData[] = $school_info['adress'];
$nestedData[] = $school_info['arabic_name'];
$nestedData[] = $school_info['direction'];
$nestedData[] = $school_info['language'];
$show[] = $nestedData;
}
}else{
}
return $show;
}
echo get_school($admin_school_code);

You are returning an array therefore you cannot use echo.
Try to var_dump(get_school($admin_school_code)) or print_r(get_school($admin_school_code))

Related

Multiple if statement to update the filed same model or change the records to another model using Cakephp

I am getting array value from the request and my first if statement works great to me, in the case of second if statement its migrating only one row to my salereturn table but i want all the record in the request to be migrate to the salereturn table.
if ($this->request->is('post')) {
$Productsalesrec = !empty($this->request->data['Productsales']) ? $this->request->data['Productsales'] : "";
if (!empty($Productsalesrec)) {
foreach ($Productsalesrec as $Productsales) {
if ($Productsales['status'] == 'MoveToShipment') {
$this->Productsales->id = $Productsales['id'];
$this->request->data['Productsales']['status'] = $Productsales['status'];
$this->Productsales->save($this->request->data);
}
if ($Productsales['status'] == 'Returned') {
$productsalesretArr = array();
$productsalesre = $this->Productsales->find('all', array('conditions' => array('Productsales.product_sales_slno' => $id)));
$this->request->data['Salesreturn']['sales_order_date'] = $productsalesre['Productsales']['sales_order_date'];
$this->request->data['Salesreturn']['product_sale_id'] = $productsalesre['Productsales']['id'];
$this->request->data['Salesreturn']['sales_date'] = $productsalesre['Productsales']['expected_delivery_date'];
$this->request->data['Salesreturn']['product_sales_slno'] = $productsalesre['Productsales']['product_sales_slno'];
$this->request->data['Salesreturn']['price_per_unit_order'] = $productsalesre['Productsales']['sales_price_per_unit_order'];
$this->request->data['Salesreturn']['total_amount'] = $productsalesre['Productsales']['sales_price_per_unit_order'] * $Productsales['tot_unit'];
$this->request->data['Salesreturn']['total_unit'] = $Productsales['tot_unit'];
$this->request->data['Salesreturn']['product_id'] = $Productsales['product_id'];
$this->request->data['Salesreturn']['amount_returned'] = 0;
$this->request->data['Salesreturn']['status'] = 'Returned';
$this->request->data['Salesreturn']['payment_method'] = 'Cash on Delivery';
$this->request->data['Salesreturn']['created_date'] = date('Y-m-d H:i:s');
$this->request->data['Salesreturn']['created_by'] = $this->Auth->user('id');
$this->Salesreturn->save($this->request->data['Salesreturn']);
if ($Productsales['total_unit'] == $Productsales['tot_unit']) {
$this->Productsales->delete($Productsales['id']);
} elseif ($Productsales['total_unit'] >= $Productsales['tot_unit']) {
$this->Productsales->id = $Productsales['id'];
$this->request->data['Productsales']['total_unit'] = $Productsales['total_unit'] - $Productsales['tot_unit'];
$this->Productsales->save($this->request->data);
}
$prodtype = $this->Producttype->find('first', array('conditions' => array('Producttype.id' => $productsalesre['Productsales']['product_type_id'])));
$this->Producttype->id = $prodtype['Producttype']['id'];
$prodquantity = $prodtype['Producttype']['quantity'] + ($Productsales['total_unit'] - $Productsales['tot_unit']);
$prodtotstck = $prodtype['Producttype']['total_unit_stock'] + ($Productsales['total_unit'] - $Productsales['tot_unit']);
$this->Producttype->saveField('total_unit_stock', $prodtotstck);
$this->Producttype->saveField('quantity', $prodquantity);
}
}
$this->redirect(array('action' => 'index'));
}
}
I'm not sure to really understand the problem...
Do you mean this part of code hasn't the wanted behaviour ?
if($Productsales['status'] == 'Returned') {
$this->request->data['Salesreturn']['sales_order_date']=$productsalesre['Productsales']['sales_order_date'];
...
$this->Salesreturn->save($this->request->data['Salesreturn']);
}
Does your condition " $Productsales['status'] == 'Returned' " is the problem ?
may be only one row of your post array has this status.

Codeigniter 3 Transaction

In my code the transaction is not rolling even when i have database error.... here is the code...
Even when insert in trial or register fails the login user inserts the data...
$this->db->trans_start();
$logindata = $this->_get_login_data();
$logindata['is_deleted'] = 0;
$this->db->insert('user_login',$logindata);
//insert login data end
//insert register data begin
$registerdata = $this->_get_register_data();
$registerdata['is_active'] = 1;
$registerdata['user_login_id'] = $this->db->insert_id();
$registerdata['subscription_id'] = null;
$registerdata['login_other_accounts_id'] = null;
$registerdata['is_first_time_login'] = 1;
$registerdata['default_language_id'] = 1;
$registerdata['created_on'] = date('Y-m-d H:i:s');
$registerdata['is_deleted'] = 0;
//insert trial period begin
$trialdata['trial_period_day'] = 10;
$trialdata['is_publish'] = 0;
$trialdata['is_deleted'] = null;
$this->trial_period_model->insert('trial_period',$trialdata);
//insert trial period end
if($registerdata['is_company']==null)
$registerdata['is_company'] = 0;
$registerdata['trial_period_id'] = $this->user_model->get_last_inserted_id();
$this->upload_image();
$imagename = $this->upload->data();
$registerdata['photo_logo'] = $imagename['file_name'];
$this->user_model->insert('general_reg_info',$registerdata);
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
echo "failed";
}
else
{
$this->db->trans_commit();
echo "success";
}
//insert register data end
Your database type might be 'MyISAM' change the database type to 'InnoDB'

Add database in codeigniter dynamically

I am working on codeigniter app to allow user to add edit and modify database.
I want to repeat this code by loop:
$db['db1']['hostname'] = 'localhost';
$db['db1']['username'] = 'ts4l_wp13';
$db['db1']['password'] = 'O&3D6c(0zD70.^9';
$db['db1']['database'] = 'ts4l_wp13';
$db['db1']['dbdriver'] = 'mysql';
$db['db1']['dbprefix'] = '';
$db['db1']['pconnect'] = FALSE;
$db['db1']['db_debug'] = TRUE;
$db['db1']['cache_on'] = FALSE;
$db['db1']['cachedir'] = '';
$db['db1']['char_set'] = 'utf8';
$db['db1']['dbcollat'] = 'utf8_general_ci';
$db['db1']['swap_pre'] = '';
$db['db1']['autoinit'] = TRUE;
$db['db1']['stricton'] = FALSE;
I do this:
#$get_data = mysql_query("SELECT * FROM `database_name`");
while($getdata = mysql_fetch_assoc($get_data)){
$id='db'.$getdata['id'];
$iid="$id";
$db["$iid"]['hostname'] = 'localhost';
$db["$iid"]['username'] = $getdata['username'];
$db["$iid"]['password'] = $getdata['password'];
$db["$iid"]['database'] = $getdata['name'];
$db["$iid"]['dbdriver'] = 'mysql';
$db["$iid"]['dbprefix'] = '';
$db["$iid"]['pconnect'] = FALSE;
$db["$iid"]['db_debug'] = TRUE;
$db["$iid"]['cache_on'] = FALSE;
$db["$iid"]['cachedir'] = '';
$db["$iid"]['char_set'] = 'utf8';
$db["$iid"]['dbcollat'] = 'utf8_general_ci';
$db["$iid"]['swap_pre'] = '';
$db["$iid"]['autoinit'] = TRUE;
$db["$iid"]['stricton'] = FALSE;
}
and this block.
// Loading db and running query.
$CI = &get_instance();
$this->db1 = $CI->load->database('db1', TRUE);
$this->db1->set($data);
$this->db1->insert($this->_table_name);
$id=$this->db1->insert_id();
I do this:
$this->load->model('mdb_m');
$dbms =$this->mdb_m->get();
if (count($dbms)) {
foreach ($dbms as $dbm) {
$mid=$dbm->id;
$dbc='db'.$mid;
$CI = &get_instance();
$this->db.$mid = $CI->load->database( "$dbc" , TRUE);
$this->db.$mid->set($data);
$this->db.$mid->insert($this->_table_name);
}
}
But it only insert at first database in the array then stop and retrive this error "You have specified an invalid database connection group."
you can get brief information of using multiple database from here.As you have mentioned.
https://ellislab.com/codeigniter/user-guide/database/connecting.html
for better approach you can make a loader file to load a database.I was in same problem and got the clear visualization from here
https://coderwall.com/p/_kyjvg/codeigniter-loading-up-multiple-databases
refer here and if still confused please revert back to me thankyou.

Search Multidimensional Array ActionScript 3

If I have two arrays like the example below, how can I search the first part of each node in the 'score' array with the values in the 'search' array and return the value that is the second part of each node in the 'score' array? Basically in this case I'd want to get 5 and 7.
var score:Array = new Array();
score[0] = ["cat", "3"];
score[1] = ["dog", "5"];
score[2] = ["fish", "0.5"];
score[3] = ["bird", "0.25"];
score[4] = ["horse", "10"];
score[5] = ["cow", "15"];
score[6] = ["iguana", "7"];
var search:Array = ["dog", "iguana"];
Try with this code (using Dictionary):
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private var score:Dictionary = new Dictionary();
private var search:Array = ["dog", "iguana"];
public function init():void{
score["cat"] = "3";
score["dog"] = "5";
score["fish"] = "0.5";
score["bird"] = "0.25";
score["horse"] = "10";
score["cow"] = "15";
score["iguana"] = "7";
var t1:String = score[search[1]];
var t2:String = score[search[0]];
Alert.show(t1 + ' ' + t2); //prints 5 7
}
]]>
</mx:Script>
</mx:Application>
Or you can do this too:
var t1:String = score["dog"];
var t2:String = score["iguana"];
Or without Dictionary:
<?xml version="1.0" encoding="utf-8"?>
public function init():void{
score[0] = ["cat","3"];
score[1] = ["dog","5"];
score[2] = ["fish","0.5"];
score[3] = ["bird","0.25"];
score[4] = ["horse","10"];
score[5] = ["cow","15"];
score[6] = ["iguana","7"];
for(var j:int = 0;j<search.length;j++){
for(var i:int = 0;i<score.length;i++){
var temp:Array = score[i] as Array;
if(temp[0] == search[j]){
Alert.show(temp[1]);
}
}
}
}
]]>
</mx:Script>
Also you have not used an appropriate way to declare arrays, I recommend using my method or changing the array declaration.

How to return value of 2 data in foreach which each data has 5 rows in model of codeigniter to view

I have a similar problem that the function only return the first loop data.
The result should be 2 loops and each loop has rows data.
For example, The first loop is A and has 5 rows data and second loop is B and has 5 rows.
The return value only A with 5 rows, it sould be 10 rows. 5 rows A + 5 rows B.
How is the code should be?
This function in model is bellow
function getDataForm($data_search)
{
$i=0;
if(!empty($data_search['arr_idacl']))
{
foreach($data_search['arr_idacl'] as $idx => $val)
{
if(!empty($operatingHour))
{
$sql = "SELECT be_acl_resources.description as description,DATE_FORMAT(tbl_camera_detail.start_time,'%d %M %Y ') as datadate, SUM(IF(tbl_consolid_detail.operation = 'plus' AND tbl_consolid_detail.direction = 'IN',tbl_camera_detail.enters,0))+SUM(IF(tbl_consolid_detail.operation = 'minus' AND tbl_consolid_detail.direction = 'IN',tbl_camera_detail.enters*(-1),0))+SUM(IF(tbl_consolid_detail.operation = 'plus' AND tbl_consolid_detail.direction = 'OUT',tbl_camera_detail.exits,0))+SUM(IF(tbl_consolid_detail.operation = 'minus' AND tbl_consolid_detail.direction = 'OUT',tbl_camera_detail.exits*(-1),0)) as total_enter FROM tbl_consolid JOIN tbl_consolid_detail ON tbl_consolid.id = tbl_consolid_detail.consolid_id JOIN tbl_config_data ON tbl_consolid_detail.config_data_id = tbl_config_data.id JOIN tbl_camera ON tbl_config_data.id_acl = tbl_camera.id_acl JOIN tbl_camera_detail ON tbl_camera.id = tbl_camera_detail.camera_id JOIN be_acl_resources ON tbl_consolid.id_acl = be_acl_resources.id WHERE tbl_camera.date >= '".$data_search['start_date']."' AND date_format(tbl_camera_detail.start_time, '%H, %i %S') >= '".$startOpratingHour."' AND date_format(tbl_camera_detail.start_time, '%H, %i %S') <= '".$operatingHour['finish_time']."' AND tbl_camera.date <= '".$data_search['end_date']."' AND tbl_consolid.id_acl = '".$val."' GROUP BY tbl_camera.date";
echo $sql;
$query = $this->db->query($sql);
$result_data[] = $query->result();
//return $result;
}
$i++;
}
return $result_data;
}
}
Declare $result_data = array(); just before if(!empty($data_search['arr_idacl'])) condition.
Replace $result_data[] = $query->result(); with $result_data[$i] = $query->result();
Update:
Just close to the answer. :) Try this:
Replace $result_data[] = $query->result(); with $result_data[$i][] = $query->result();

Resources