I am inserting multiple data from multiple forms into database but encounter an error at column photo
Unknown column 'Array' in 'field list'
Controller
if(!empty($_FILES['user_profile_file']['name'])){
$filesCount = count($_FILES['user_profile_file']['name']);
for ($i = 0; $i < $filesCount; $i++) {
$studentImg = '';
$_FILES['userFile']['name'] = $_FILES['user_profile_file']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['user_profile_file']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['user_profile_file']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['user_profile_file']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['user_profile_file']['size'][$i];
$config['upload_path'] = FCPATH."uploadfiles/users/student-img";
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 0;
$studentImgNew = uniqueId();
$config['file_name'] = $studentImgNew;
$this->load->library('upload', $config);
if($this->upload->do_upload('userFile')){
$fileData = $this->upload->data();
$studentImg[$i]['photo'] = $fileData['file_name'];
$studentData[] = array(
'name' => $_POST['user_name'][$i],
'nric' => $_POST['user_nric'][$i],
'gender' => $_POST['user_gender'][$i],
'photo' => $studentImg,
'email' => $_POST['user_email'][$i],
'password' => md5($_POST['user_password'][$i]),
'is_active' => '0',
);
}
}
}
$this->User_account_model->create($studentData)
Model
function create($studentData){
foreach ($studentData as $key => $studentDatas) {
$insertStudentData[$key] = array(
'parent_id' => $parent_id,
'email' => $studentDatas['email'],
'password' => $studentDatas['password'],
'name' => $studentDatas['name'],
'nric' => $studentDatas['nric'],
'gender' => $studentDatas['gender'],
'photo' => $studentDatas['photo'],
'is_active' => $studentDatas['is_active'],
);
$this->db->insert('users_student', $insertStudentData[$key]);
}
if($this->db->affected_rows() != 1){
return false;
} else {
return true;
}
}
When I do print_r() I can get the exact data to be inserted into the database.
Array
(
[0] => Array
(
[parent_id] => 11
[email] => testing.mael#gmail.com
[password] => e02cb962ac59075b964b07152d234b70
[name] => testing
[nric] => 123123
[gender] => 0
[photo] => Array
(
[0] => Array
(
[photo] => 5955cac09b8f71.jpg
)
)
[is_active] => 0
)
[1] => Array
(
[parent_id] => 11
[email] => testing.mael#gmail.com
[password] => e13cc542ac59075b9643s5152d234g59
[name] => testing sister
[nric] => 123123
[gender] => 0
[photo] => Array
(
[0] => Array
(
[photo] => 5955cac09b8f72.jpg
)
)
[is_active] => 0
)
)
You are trying to insert an array inside the for insertStudentData[key].
Try This:
$insertStudentData[$key] = array(
'parent_id' => $parent_id,
'email' => $studentDatas['email'],
'password' => $studentDatas['password'],
'name' => $studentDatas['name'],
'nric' => $studentDatas['nric'],
'gender' => $studentDatas['gender'],
'photo' => $studentDatas['photo'],
'is_active' => $studentDatas['is_active']['0']['photo'],
);
$this->db->insert('users_student', $insertStudentData[$key]);
if you are trying to insert multiple rows, then you can use insert_batch
I have tried your code on my side and it gets perfectly correct. it added the multiple rows to the table. This is what I have done.
function create($studentData){
$insertStudentData = ''; //Create a Variable
foreach ($studentData => $studentDatas) {
$insertStudentData[] = array(
'parent_id' => $parent_id,
'email' => $studentDatas['email'],
'password' => $studentDatas['password'],
'name' => $studentDatas['name'],
'nric' => $studentDatas['nric'],
'gender' => $studentDatas['gender'],
'photo' => $studentDatas['photo']['0']['photo'],
'is_active' => $studentDatas['is_active']
);
}
$this->db->insert_batch('users_student', $insertStudentData[$key]);
if($this->db->affected_rows() != 1){
return false;
} else {
return true;
}
}
Finally I solved my issue. From my controller I changed the assigning array like this :
if($this->upload->do_upload('userFile')){
$fileData = $this->upload->data();
$studentImg[$i]['photo'] = $fileData['file_name'];
$studentData[] = array(
'name' => $_POST['user_name'][$i],
'nric' => $_POST['user_nric'][$i],
'gender' => $_POST['user_gender'][$i],
'photo' => $studentImg[$i]['photo'], // Edited row
'email' => $_POST['user_email'][$i],
'password' => md5($_POST['user_password'][$i]),
'is_active' => '0',
);
}
And my foreach model I changed to this :
foreach ($studentData as $studentDatas) {
$insertStudentData[] = array(
'parent_id' => $parent_id,
'email' => $studentDatas['email'],
'password' => $studentDatas['password'],
'name' => $studentDatas['name'],
'nric' => $studentDatas['nric'],
'gender' => $studentDatas['gender'],
'photo' => $studentDatas['photo'],
'is_active' => $studentDatas['is_active'],
);
}
$this->db->insert_batch('users_student', $insertStudentData);
And the output of those array :
Array
(
[0] => Array
(
[parent_id] => 11
[email] => testing.mael#gmail.com
[password] => 202cb962ac59075b964b07152d234b70
[name] => testing
[nric] => 123123
[gender] => 0
[photo] => 5955cac09b8f71.jpg
[is_active] => 0
)
[1] => Array
(
[parent_id] => 11
[email] => testing.mael#gmail.com
[password] => e13cc542ac59075b9643s5152d234g59
[name] => testing sister
[nric] => 123123
[gender] => 0
[photo] => 5955cac09b8f72.jpg
[is_active] => 0
)
)
By the way, thank you for everyone that trying to help! I appreciate it.
Related
CakePHP 3
My Model
Users has Many Badges
Titles has Many Badges
Badges belongs to Titles
Badges belongs to Users
In my Users View, I want to access the value of Titles but I don't know how.
**Users Controller**
public function view($id = null)
{
$this->viewBuilder()->layout('admin');
TableRegistry::get('Users');
$user = $this->Users->get($id, [
'contain' => ['Badges', 'Projects', 'Reports', 'Rewards']
]);
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
UsersTable.php
$this->hasMany('Badges', [
'foreignKey' => 'user_id'
]);
TitleTable.php
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Titles', [
'foreignKey' => 'title_id',
'joinType' => 'INNER'
]);
BadgesTable.php
$this->hasMany('Badges', [
'foreignKey' => 'title_id'
]);
App\Model\Entity\User Object
(
[id] => 1
[firstname] => Jl
[lastname] => C
[email] => m
[username] => admin
[password] =>
[profile_pic] => /img/noimage.jpg
[report_count] => 0
[role] => admin
[reputation] => 999999
[account_status] => Active
[confirmation] => 1
[site_name] => C
[site_email] => mi
[created] => Cake\I18n\FrozenTime Object
(
[time] => 2017-02-27T13:46:51+08:00
[timezone] => Asia/Manila
[fixedNowTime] =>
)
[modified] => Cake\I18n\FrozenTime Object
(
[time] => 2017-03-01T10:37:50+08:00
[timezone] => Asia/Manila
[fixedNowTime] =>
)
[badges] => Array
(
[0] => App\Model\Entity\Badge Object
(
[id] => 1
[user_id] => 1
[title_id] => 11
[created] => Cake\I18n\FrozenTime Object
(
[time] => 2017-02-28T15:16:08+08:00
[timezone] => Asia/Manila
[fixedNowTime] =>
)
[modified] => Cake\I18n\FrozenTime Object
(
[time] => 2017-02-28T15:16:08+08:00
[timezone] => Asia/Manila
[fixedNowTime] =>
)
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[invalid]] => Array
(
)
[[repository]] => Badges
)
)
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[repository]] => Users
)
What I want is to get the data from title table (i.e name)
[badges] => Array
(
[0] => App\Model\Entity\Badge Object
(
[id] => 1
[user_id] => 1
[title_id] => 11
[created] => Cake\I18n\FrozenTime Object
In your contain, you can use 'Model.Model' to get further related data like so :
$user = $this->Users->get($id, [
'contain' => ['Badges.Titles', 'Projects', 'Reports', 'Rewards']
]);
How can I insert array data to mysql using code igniter ??
I tried example from CI documentation like this
$data = array(
'id_kls' => 'id_kls',
'fk__id_kls' => 'fk__id_kls',
'id_reg_pd' => 'id_reg_pd',
'nm_pd' => 'nm_pd',
'asal_data' => 'asal_data',
'nilai_angka' => 'nilai_angka',
'nilai_huruf' => 'nilai_huruf',
'nilai_indeks' => 'nilai_indeks',
);
$this->db->insert('master_nilai', $data);
// Executes: REPLACE INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
But not working ..
I have array data like this
Array
(
[error_code] => 0
[error_desc] =>
[result] => Array
(
[0] => Array
(
[id_kls] => f77294f7-2a5a-4876-860b-824d227d5b19
[fk__id_kls] => 02
[id_reg_pd] => 001be76b-4e58-4cea-96cf-fee2d8e0abdc
[nm_pd] => SUYATNO
[asal_data] => 9
[nilai_angka] =>
[nilai_huruf] => B
[nilai_indeks] => 3.00
)
Make sure you've enable config/autoload.php/$autoload['libraries'] = array('database');
and config/database.php/your hostname,username,dbname!
This code
global $wpdb;
$bankaccountinfo = get_option( 'va_options' );
$arr = array($bankaccountinfo);
print_r(array_values($arr));
Provides me with the array below
Array
(
[0] => Array
(
[geocoder] => google
[geocoder_settings] => Array
(
[google] => Array
(
[geo_region] => us
[geo_language] => en
[geo_unit] => mi
[api_key] =>
)
)
[map_provider] => google
[map_provider_settings] => Array
(
[google] => Array
(
[geo_region] => us
[geo_language] => en
[geo_unit] => mi
[api_key] =>
)
)
[geo_unit] => mi
[currency_code] => EUR
[currency_identifier] => symbol
[currency_position] => left
[thousands_separator] => .
[decimal_separator] => ,
[tax_charge] => 21
[color] => blue
[admin_security] => manage_options
[wp_login] =>
[listing_price] => 0
[listing_charge] => no
[listing_duration] => 30
[moderate_listings] => no
[moderate_claimed_listings] => yes
[included_categories] => 0
[listings_per_page] => 10
[default_listing_sort] => default
[default_listing_home_sort] => default
[listings_featured_sort] => random
[editor_listing] => default
[default_geo_search_sort] => distance
[default_search_sort] => rating
[default_radius] =>
[addons] => Array
(
[featured-home] => Array
(
[enabled] => 1
[price] => 9.99
[duration] => 30
[period] => 30
[period_type] => D
)
[featured-cat] => Array
(
[enabled] => 1
[price] => 4.99
[duration] => 30
[period] => 30
[period_type] => D
)
)
[category_surcharges] => Array
(
[software] => Array
(
[surcharge] => 0
)
)
[categories_menu] => Array
(
[count] =>
[depth] => 3
[sub_num] => 3
[hide_empty] =>
)
[categories_dir] => Array
(
[count] =>
[depth] => 3
[sub_num] => 3
[hide_empty] =>
)
[events_enabled] =>
[default_event_geo_search_sort] => distance
[default_event_search_sort] => event_date
[default_event_radius] =>
[event_charge] => no
[event_price] => 0
[event_featured-home_enabled] => 1
[event_featured-home_price] => 0
[event_featured-cat_enabled] => 1
[event_featured-cat_price] => 0
[event_included_categories] => 0
[event_expiration] => 30
[moderate_events] => no
[events_per_page] => 10
[default_event_sort] => default
[default_event_home_sort] => default
[events_featured_sort] => random
[editor_event] => default
[listing_permalink] => fotografen
[edit_listing_permalink] => wijzigen
[renew_listing_permalink] => vernieuwen
[claim_listing_permalink] => claimen
[purchase_listing_permalink] => kopen
[listing_cat_permalink] => specialisme
[listing_tag_permalink] => zoekwoord
[event_permalink] => evenementen
[edit_event_permalink] => edit
[purchase_event_permalink] => purchase
[event_cat_permalink] => categorie
[event_tag_permalink] => zoekwoord
[event_day_permalink] => dag
[dashboard_events_permalink] => evenementen
[dashboard_event_comments_permalink] => reacties
[dashboard_event_favorites_permalink] => evenementen-favorieten
[dashboard_events_attending_permalink] => deelnemen
[dashboard_permalink] => dashboard
[dashboard_listings_permalink] => vermeldingen
[dashboard_claimed_permalink] => geclaimd
[dashboard_reviews_permalink] => beoordelingen
[dashboard_faves_permalink] => favorieten
[gateways] => Array
(
[enabled] => Array
(
[bank-transfer] => 1
[paypal] => 1
[pronamic_ideal] => 1
)
[bank-transfer] => Array
(
[message] => Bankinfo
)
[pronamic_ideal] => Array
(
[config_id] => 440
)
[paypal] => Array
(
[email_address] => *protected email*
[business_account] =>
[sandbox_enabled] =>
[pdt_enabled] =>
[pdt_key] => G9YvVFpThi0bNAu6wYQe4unz_ReqUttrcHWXHDvPA0pbdCQ3ky63CWFvGfu
[ipn_enabled] =>
)
)
[listing_sharethis] => 0
[event_sharethis] => 0
[blog_post_sharethis] => 0
[page_template_updates_1_2] => 1
[default_user_role_update_1_3_2] => 1
)
)
Now i would like to get and return the value of '[bank-transfer] => Array ( [message]' which is 'Bankinfo'. I am not sure how to do this with multiple (sub)arrays. Tried the code below which is not doing anything.
echo $arr['bank-transfer']['message'];
Can someone help me out?
This is my database:
week, subbizname, devicetype
20141203, common, PC
20141203, unknown, PC
20141210, KRsinKD, SP
20141210, unknown, PC
20141217, Unknown, SP
20141217, Chintai, TAB
....
I am trying to get the number of records for each unique couple devicetype/week.
Ex:
array(
20141203 => array(
'PC'=>2,
'TAB'=>0,
'SP'=>0
),
20141210 => array(
'PC'=>1,
'TAB'=>0,
'SP'=>1
),
...
.....
)
UPDATE:
I have used query:
$data = $this->Test->find('all', array(
'conditions'=>array(
'OR'=>array(
array('devicetype'=>'PC'),
array('devicetype'=>'SP'),
array('devicetype'=>'TAB'),
)
),
'fields'=>"devicetype,week,COUNT(devicetype) AS countDevice",
'group'=>"week,devicetype"
));
Thing is, it returns something like this:
(int) 0 => array(
'Test' => array(
'devicetype' => 'PC',
'week' => '20141126'
),
(int) 0 => array(
'countDevice' => '34844'
)
),
(int) 1 => array(
'Test' => array(
'devicetype' => 'SP',
'week' => '20141126'
),
(int) 0 => array(
'countDevice' => '32401'
)
),
(int) 2 => array(
'Test' => array(
'devicetype' => 'TAB',
'week' => '20141126'
),
(int) 0 => array(
'countDevice' => '4256'
)
),
(int) 3 => array(
'Test' => array(
'devicetype' => 'PC',
'week' => '20141203'
),
(int) 0 => array(
'countDevice' => '96564'
)
),
(int) 4 => array(
'Test' => array(
'devicetype' => 'SP',
'week' => '20141203'
),
(int) 0 => array(
'countDevice' => '97450'
)
),
But I do not manage to get the expected result.
Surely there must be a better way.
How can I fix this?
Using your $data variable, you can do:
$d = array();
foreach ($data as $value) {
$week = $value['Test']['week'];
if (isset($d[$week])) {
$d[$week][$value['Test']['devicetype']] = $value[0]['countDevice'];
} else {
$d[$week] = array($value['Test']['devicetype'] => $value[0]['countDevice']);
}
}
print_r($d); // $d will contain the data in your required format
I have following code:
$yt_profiles = $youtube->channels->listChannels('brandingSettings', array(
'mine' => 'true',
));
That returns the following output:
Google_Service_YouTube_ChannelListResponse Object
(
[collection_key:protected] => items
[etag] => "WFPuK6TsnblcGPcnMex79s42ynQ/sTnuE1bHO-tokx_mFFDt1ybN90g"
[eventId] =>
[itemsType:protected] => Google_Service_YouTube_Channel
[itemsDataType:protected] => array
[kind] => youtube#channelListResponse
[nextPageToken] =>
[pageInfoType:protected] => Google_Service_YouTube_PageInfo
[pageInfoDataType:protected] =>
[prevPageToken] =>
[tokenPaginationType:protected] => Google_Service_YouTube_TokenPagination
[tokenPaginationDataType:protected] =>
[visitorId] =>
[modelData:protected] => Array
(
[pageInfo] => Array
(
[totalResults] => 1
[resultsPerPage] => 1
)
[items] => Array
(
[0] => Array
(
[kind] => youtube#channel
[etag] => "WFPuK6TsnblcGPcnMex79s42ynQ/ecOcHFmWyWQ7ToCD7-B1L36b4L4"
[id] => UCQO6uXy5maTpYvSa_yM--Bw
[brandingSettings] => Array
(
[channel] => Array
(
[title] => Vasim Padhiyar
[showRelatedChannels] => 1
[featuredChannelsTitle] => Featured Channels
[featuredChannelsUrls] => Array
(
[0] => UCw-TnDmYDQyjnZ5qpVWUsSA
)
[profileColor] => #000000
)
[image] => Array
(
[bannerImageUrl] => http://s.ytimg.com/yts/img/channels/c4/default_banner-vfl7DRgTn.png
)
[hints] => Array
(
[0] => Array
(
[property] => channel.featured_tab.template.string
[value] => Everything
)
[1] => Array
(
[property] => channel.banner.image_height.int
[value] => 0
)
[2] => Array
(
[property] => channel.modules.show_comments.bool
[value] => True
)
)
)
)
)
)
[processed:protected] => Array
(
)
)
I want to loop through modelData:protected variable to get the list of channels and its items data. Its json object so $yt_profiles->modelData:protected not working while accessing. Please help me to solve this issue.
Thanks in advance
You can reach it as in an array:
print_r ($yt_profiles['modelData']);
Your request:
$yt_profiles = $youtube->channels->listChannels('brandingSettings', array(
'mine' => 'true',
));
To acces to values Channel title for example:
$titleChannel = $yt_profiles["items"][0]["brandingSettings"]["channel"]["title"];
To acces to values bannerImageUrl for example:
$banner = $yt_profiles["items"][0]["brandingSettings"]["image"]["bannerImageUrl"];
Does this help you?
$yt_profiles = $youtube->channels->listChannels('id, brandingSettings', array('mine' => 'true',));
foreach ($yt_profiles['modelData']['items'] as $searchResult) {
switch ($searchResult['kind']) {
case 'youtube#channel':
$channels[]= array('id'=> $searchResult['id'],
'brandingSettings'=> $searchResult['brandingSettings']);
break;
}
}