In codeigniter, how to pass array to function parameter - arrays

As title, I have a library function $this->foo->bar(). How can I pass $upload_data array to $this->foo->bar() param?
I have this in my controller
$upload_data = $this->upload->data();
//$upload_data['file_name']
$this->foo->bar($upload_data);
Then I have this in my library
public function bar($arr){
$config['source_name'] = //How can I use $arr['file_name'] in here
}
And I got this error
Message: Illegal string offset 'file_name'
var_dump($upload_data)
array (size=14)
'file_name' => string '1358459309.JPG' (length=14)
'file_type' => string 'image/jpeg' (length=10)
'file_path' => string 'C:/wamp/www/foldering/assets/img/uploaded_photos/' (length=49)
'full_path' => string 'C:/wamp/www/foldering/assets/img/uploaded_photos/1358459309.JPG' (length=63)
'raw_name' => string '1358459309' (length=10)
'orig_name' => string '1358459309.JPG' (length=14)
'client_name' => string '200X CNY1.JPG' (length=13)
'file_ext' => string '.JPG' (length=4)
'file_size' => float 30.87
'is_image' => boolean true
'image_width' => int 640
'image_height' => int 480
'image_type' => string 'jpeg' (length=4)
'image_size_str' => string 'width="640" height="480"' (length=24)

My mistake, issue comes from another function

Related

Display array in view / controller codeigniter

I dont have the same result when i var_dump an array in my controller and in my view. This is my controller:
public function index()
{
$getFestivals = $this->festival_model->getAllFestivals();
if (!empty($getFestivals)) {
foreach ($getFestivals as $festival) {
$countFestivalEdition = $this->festival_model->countFestivalEditionByFestivalID($festival->fID);
$festival->fCountEdition = $countFestivalEdition;
}
}
$this->data['getFestivals'] = $getFestivals;
#var_dump($this->data['getFestivals']);die;
$this->render_layout('festivals/index', $this->data);
}
The var_dump (juste before last line) return me:
0 =>
object(stdClass)[24]
public 'fID' => string '6' (length=1)
public 'fName' => string 'Festival2' (length=17)
public 'fPicture' => string '3df2c152d9dec657380c53b679d0b92b.jpg' (length=36)
public 'fCity' => string 'City2' (length=9)
public 'fCountry' => string 'France' (length=6)
public 'fCountEdition' => int 2
1 =>
object(stdClass)[25]
public 'fID' => string '8' (length=1)
public 'fName' => string 'Festival1' (length=19)
public 'fPicture' => string '73a267e6c047d507b66b3a1ab1fc0059.jpg' (length=36)
public 'fCity' => string 'City1' (length=16)
public 'fCountry' => string 'France' (length=6)
public 'fCountEdition' => int 1
On this var_dump i have my 'fCountEdition'. When i make var_dump on my view, i have this (var_dump($getFestivals);die;):
0 =>
object(stdClass)[24]
public 'fID' => string '6' (length=1)
public 'fName' => string 'Festival2' (length=17)
public 'fPicture' => string '3df2c152d9dec657380c53b679d0b92b.jpg' (length=36)
public 'fCity' => string 'City2' (length=9)
public 'fCountry' => string 'France' (length=6)
1 =>
object(stdClass)[25]
public 'fID' => string '8' (length=1)
public 'fName' => string 'Festival1' (length=19)
public 'fPicture' => string '73a267e6c047d507b66b3a1ab1fc0059.jpg' (length=36)
public 'fCity' => string 'City1' (length=16)
public 'fCountry' => string 'France' (length=6)
Why he dont parse my data fCountEdition ?
For informations, my render_layout look like this:
$this->load->view($view, $this->data);
You may try this simple way to send array to view as give example bellow.
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
$data=$this->db->query("SELECT * FROM table_name");
$result=$data->result_array();
$headerData = array(
"pageTitle" => "Home",
"stylesheet" => array("home.css")
);
$footerData = array(
"jsFiles" => array("home.js")
);
$viewData = array(
"viewName" => "home",
"viewData" => array('result'=>$result),
"headerData" => $headerData,
"footerData" => $footerData
);
//Sendig all Result Data to view by Storing into array as $result
$this->load->view('template',$viewData);
}
View
<?php
var_dump($result);
?>
It's because you're not referencing $getFestivals, instead you are passing the value of it to $festival and trying to assign it a new variable.
Basically, passing the value allows you to 'copy' the array/object/etc without modifying the original.
Referencing the original allows to to modify it directly from within the loop.
Do do it the way you've written the code, you need to reference the original instead.
foreach ($getFestivals as &$festival) { <-- add ampersand before $
$countFestivalEdition = $this->festival_model->countFestivalEditionByFestivalID($festival->fID);
$festival->fCountEdition = $countFestivalEdition;
}

yii activerecord return object

I am using using yii2 to in basic template with MySQL database
Why This code returns an object instead of an array of selected records from Database
when i use var_damp($rooms) the output is seems an object and not an array of selected record in array format;
any body can help
public function actionIndexFiltered()
{
$query = Room::find();
$searchFilter = [
'floor' => ['operator' => '', 'value' => ''],
'room_number' => ['operator' => '', 'value' => ''],
'price_per_day' => ['operator' => '', 'value' => ''],
];
if(isset($_POST['SearchFilter']))
{
$fieldsList = ['floor', 'room_number', 'price_per_day'];
foreach($fieldsList as $field)
{
$fieldOperator = $_POST['SearchFilter'][$field]['operator'];
$fieldValue = $_POST['SearchFilter'][$field]['value'];
$searchFilter[$field] = ['operator' => $fieldOperator, 'value' => $fieldValue];
if( $fieldValue != '' )
{
$temp1=$query->andWhere([$fieldOperator, $field, $fieldValue]);
}
}
}
$room1=$temp1->all();
$rooms = $query;
return $this->render('indexFiltered', [ 'rooms' => $rooms, 'searchFilter' => $searchFilter,'room1'=>$room1 ]);
}
Output is like this and this shows that this code return a query object and not an array of query execution on database ,
so i checked the code using var_dump function the results is that it is returning a object not array
object(yii\db\ActiveQuery)[70]
public 'sql' => null
public 'on' => null
public 'joinWith' => null
public 'select' => null
public 'selectOption' => null
public 'distinct' => null
public 'from' =>
array (size=1)
0 => string 'room' (length=4)
public 'groupBy' => null
public 'join' => null
public 'having' => null
public 'union' => null
public 'params' =>
array (size=0)
empty
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
public 'where' =>
array (size=3)
0 => string '=' (length=1)
1 => string 'room_number' (length=11)
2 => string '3' (length=1)
public 'limit' => null
public 'offset' => null
public 'orderBy' => null
public 'indexBy' => null
public 'emulateExecution' => boolean false
public 'modelClass' => string 'app\models\Room' (length=15)
public 'with' => null
public 'asArray' => null
public 'multiple' => null
public 'primaryModel' => null
public 'link' => null
public 'via' => null
public 'inverseOf' => null
This code return an a dataProvider .. alias the code for get models /active record
$query = Room::find();
for obtain the all the models you can use
$roomModels= Room::find()->all();
in this the result is a collection of obejct (models) of class Room
if you need an array you can use
$roomArray = Room::find()->asArray()->all();
In your code $temp1 is the same as $query. andWhere() method returns $this which means that
$temp1 = $query->andWhere([$fieldOperator, $field, $fieldValue]);
makes $temp1 to be the same as $query which is object of yii\db\ActiveQuery.
Now you are calling this:
$room1 = $temp1->all(); // the same as $room1 = $query->all();
and this holds array of Room objects while
$rooms = $query;
is just another unnecessary assignment because $rooms is the same as $query which is object of yii\db\ActiveQuery.
Finally i found the reason for this problem is that
I must call asArray() member function of ActiveRecord class to generate an array output
$room1=$temp1->all(); will changed to $room1=$temp1->asArray()->all();

Laravel Sqlsrv eloquent query error

I have the following query generated by laravel:
insert into [web_cmdelt] (
[id],
[list_num],
[is_main],
[published_at],
[list_title],
[artdes],
[unitdes],
[unitprix],
[detail_qty],
[catdes],
[souscatdes],
[art_origine],
[art_label],
[art_transport],
[art_culture],
[art_annexe5],
[art_annexe6],
[artcateg],
[artsouscateg],
[hash]
) values (4728, 1004, 1, 2015-02-12 10:33:02.000, COMPANY NAME, AUBERGINE "P"ESP , Kg, 5.80, , LEGUMES, LEGUMES, ESP, , , , , , 1, 7, 1619328673)
from this object:
object(Ronin\Entities\OrderItem)[230]
protected 'connection' => string 'webcmd' (length=6)
protected 'table' => string 'web_cmdelt' (length=10)
protected 'primaryKey' => string 'cmdelt_id' (length=9)
public 'timestamps' => boolean false
protected 'fillable' =>
array (size=0)
empty
protected 'perPage' => int 15
public 'incrementing' => boolean true
protected 'attributes' =>
array (size=20)
'id' => string '4728' (length=4)
'list_num' => string '1004' (length=4)
'is_main' => string '1' (length=1)
'published_at' => string '2015-02-12 10:33:02.000' (length=23)
'list_title' => string 'COMPANY NAME' (length=18)
'artdes' => string 'AUBERGINE "P"ESP ' (length=17)
'unitdes' => string 'Kg' (length=2)
'unitprix' => string '5.80' (length=4)
'detail_qty' => null
'catdes' => string 'LEGUMES' (length=7)
'souscatdes' => string 'LEGUMES' (length=7)
'art_origine' => string 'ESP' (length=3)
'art_label' => null
'art_transport' => null
'art_culture' => null
'art_annexe5' => null
'art_annexe6' => null
'artcateg' => string '1' (length=1)
'artsouscateg' => string '7' (length=1)
'hash' => string '1619328673' (length=10)
...
The problem is that SQL Server raise this error (sql editor error):
ErrorCode: -2146232060
[.Net SqlClient Data Provider]
Number: 102, Class: 15, State: 1, Line: 1
ErrorMessage: Incorrect syntax near '10'.
I see many problems here:
the date isn't quoted, so the space between date and time breaks the query
same for any other string field (why they aren't quoted ??)
the NULL values are inserted as '' (but without quotes)
Can someone point me to the right direction ?
This raw format should fix the issue.
DB::insert("insert into users (id, name) values (1,'Kamaro Lambert')");

Cakephp - SQL not being generated for all records being inserted

I have the following code in my controller to insert records.
//Perform Inserts
$success = true;
foreach($toinsertrecords as $toinsertrecord) {
$this->BillingCenterDetail->create();
if (!$this->BillingCenterDetail->save($toinsertrecord)) {
$success = false;
echo "insert fail";
} else {echo "insert success";}
}
if ($troubleshoot) {
$log = $this->BillingCenterDetail->getDataSource()->getLog(false, false);
echo "<pre>Output from Datasource Log";
var_dump($log);
echo "</pre>";
}
The array being looped at, has the following contents (Note that the last record has the field order slightly switched, but as this is an associative array, i thought it should not matter)
array (size=3)
0 =>
array (size=1)
'BillingCenterDetail' =>
array (size=12)
'startdate' => string '2014-03-10' (length=10)
'enddate' => string '2014-03-10' (length=10)
'billing_center_id' => string '50' (length=2)
'isactive' => boolean false
'addr1' => string 'melbourne' (length=9)
'addr2' => string 'melbourne' (length=9)
'addr3' => string 'melbourne' (length=9)
'city' => string 'melbourne' (length=9)
'postcode' => string '777' (length=3)
'country' => string 'aus' (length=3)
'email' => string 'a#a.com' (length=7)
'phone' => string '1234' (length=4)
1 =>
array (size=1)
'BillingCenterDetail' =>
array (size=12)
'startdate' => string '2014-03-12' (length=10)
'enddate' => string '2028-12-10' (length=10)
'billing_center_id' => string '50' (length=2)
'isactive' => boolean false
'addr1' => string 'melbourne' (length=9)
'addr2' => string 'melbourne' (length=9)
'addr3' => string 'melbourne' (length=9)
'city' => string 'melbourne' (length=9)
'postcode' => string '777' (length=3)
'country' => string 'aus' (length=3)
'email' => string 'a#a.com' (length=7)
'phone' => string '1234' (length=4)
2 =>
array (size=1)
'BillingCenterDetail' =>
array (size=12)
'billing_center_id' => string '50' (length=2)
'startdate' => string '2014-03-11' (length=10)
'enddate' => string '2014-03-11' (length=10)
'isactive' => string '1' (length=1)
'addr1' => string 'test' (length=4)
'addr2' => string 'test' (length=4)
'addr3' => string 'test' (length=4)
'city' => string 'test' (length=4)
'postcode' => string 'test' (length=4)
'country' => string 'test' (length=4)
'email' => string 'test#test' (length=9)
'phone' => string 'test' (length=4)
This is the output from my getDataSource()->getLog statement
Output from Datasource Log
array (size=3)
'log' =>
array (size=4)
0 =>
array (size=5)
'query' => string 'SELECT `BillingCenterDetail`.`id`, `BillingCenterDetail`.`startdate`, `BillingCenterDetail`.`enddate`, `BillingCenterDetail`.`billing_center_id`, `BillingCenterDetail`.`isactive`, `BillingCenterDetail`.`addr1`, `BillingCenterDetail`.`addr2`, `BillingCenterDetail`.`addr3`, `BillingCenterDetail`.`city`, `BillingCenterDetail`.`postcode`, `BillingCenterDetail`.`country`, `BillingCenterDetail`.`email`, `BillingCenterDetail`.`phone`, `BillingCenterDetail`.`created`, `BillingCenterDetail`.`modified` FROM `bm`.`bil'... (length=718)
'params' =>
array (size=0)
...
'affected' => int 1
'numRows' => int 1
'took' => float 0
1 =>
array (size=5)
'query' => string 'BEGIN' (length=5)
'params' =>
array (size=0)
...
'affected' => int 1
'numRows' => int 1
'took' => float 0
2 =>
array (size=5)
'query' => string 'INSERT INTO `bm`.`billing_center_details` (`startdate`, `enddate`, `billing_center_id`, `isactive`, `addr1`, `addr2`, `addr3`, `city`, `postcode`, `country`, `email`, `phone`, `modified`, `created`) VALUES ('2014-03-10', '2014-03-11', 50, '0', 'melbourne', 'melbourne', 'melbourne', 'melbourne', '777', 'aus', 'a#a.com', '1234', '2014-03-11 15:47:15', '2014-03-11 15:47:15')' (length=374)
'params' =>
array (size=0)
...
'affected' => int 1
'numRows' => int 1
'took' => float 0
3 =>
array (size=5)
'query' => string 'INSERT INTO `bm`.`billing_center_details` (`startdate`, `enddate`, `billing_center_id`, `isactive`, `addr1`, `addr2`, `addr3`, `city`, `postcode`, `country`, `email`, `phone`, `modified`, `created`) VALUES ('2014-03-13', '2028-12-10', 50, '0', 'melbourne', 'melbourne', 'melbourne', 'melbourne', '777', 'aus', 'a#a.com', '1234', '2014-03-11 15:47:15', '2014-03-11 15:47:15')' (length=374)
'params' =>
array (size=0)
...
'affected' => int 1
'numRows' => int 1
'took' => float 0
'count' => int 4
'time' => float 0
You can clearly see that it has only tried to insert the first 2 records, but for some reason it hasn't inserted the 3rd record. Based on the log, it also didn't generate the 3rd Insert SQL statement?
By the time it hits the 3rd loop, the "Save" function fails, this causes $success = false in the end.
Can anyone guess why this is happening?
My guess is the data in the third entry isn't passing validation.
Possibly your model has the postcode field as numeric only? In that case this data will fail:
'postcode' => string 'test' (length=4)
To debug this, I'd add this line inside your existing failure routine:
var_dump($this->BillingCenterDetail->invalidFields());

Codeigniter upload multiply files with name length 32

I faced an issue that in /uploads appear files with name like 69398f524b17741a34ee3f77234797e2.jpg etc. It seems that file is encoded as random MD5.
In library Upload.php I found that MD5 used for adding an number to end file name if it found that file with equal name already exist.
In model I have such code:
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '1000';
$config['max_width'] = '2024';
$config['max_height'] = '2024';
$config['encrypt_name'] = TRUE;
//var_dump($config);
$this->load->library('upload', $config);
$temp_files = $_FILES;
// var_dump($temp_files);
// echo "files";
// var_dump($_FILES);
$count = count ($_FILES['userfile']['name']);
for ($i=0; $i<=$count-1; $i++){
$_FILES['file'] = array (
'name'=>$temp_files['userfile']['name'][$i],
'type'=>$temp_files['userfile']['type'][$i],
'tmp_name'=>$temp_files['userfile']['tmp_name'][$i],
'error'=>$temp_files['userfile']['error'][$i],
'size'=>$temp_files['userfile']['size'][$i]);
unset($_FILES['userfile']);
var_dump($_FILES);
if ( ! $this->upload->do_upload('file'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('insert', $error);
}
else
{
$this->upload->do_upload('file');
$tmp_data = $this->upload->data();
echo "<hr>";
var_dump($tmp_data);
}//end else
}//end for
}//end do_upload
var_dump($_FILES) return
array (size=1)
'file' =>
array (size=5)
'name' => string 'Desert.jpg' (length=10)
'type' => string 'image/jpeg' (length=10)
'tmp_name' => string 'C:\Windows\Temp\php3776.tmp' (length=27)
'error' => int 0
'size' => int 845941
var_dump($tmp_data) return
array (size=14)
'file_name' => string '69398f524b17741a34ee3f77234797e2.jpg' (length=36)
'file_type' => string 'image/jpeg' (length=10)
'file_path' => string 'D:/server/www/melok/uploads/' (length=28)
'full_path' => string 'D:/server/www/melok/uploads /69398f524b17741a34ee3f77234797e2.jpg' (length=64)
'raw_name' => string '69398f524b17741a34ee3f77234797e2' (length=32)
'orig_name' => string 'Desert.jpg' (length=10)
'client_name' => string 'Desert.jpg' (length=10)
'file_ext' => string '.jpg' (length=4)
'file_size' => float 826.11
'is_image' => boolean true
'image_width' => int 1024
'image_height' => int 768
'image_type' => string 'jpeg' (length=4)
'image_size_str' => string 'width="1024" height="768"' (length=25)
Intead of $config['encrypt_name'] = TRUE; write $config['encrypt_name'] = FALSE;

Resources