I am getting the array value like below
Array
(
[0] => Array
(
[upload_label] => label 1
[upload_name] => 100-best_1940225136.png
[society_id] => 57
)
[1] => Array
(
[upload_label] => label 2
[upload_name] => 150x150_2147441709.png
[s_id] => 57
)
)
Now I have to insert the data into the database. So I used below inert code
$sqlUpload="INSERT INTO `tbl_uploadAll`(upload_label,upload_name,s_id) VALUES (:upload_label,:upload_name,:s_id)";
$stmt= $pdo->prepare($sqlUpload);
$stmt->execute($dataupload);
when I hit the button then am getting 500 error in the network tab
My full code here
if (isset($_POST['send'])) {
$total = count($_FILES['docUpload']['name']);
$dataupload=array();
for($i=0; $i < $total; $i++) {
if(isset($_FILES['docUpload']['name'][$i]) && $_FILES['docUpload']['name'][$i] != "")
{
$foldername="profile";
$uploadLabel=$_POST['docUploadLabel'][$i];
$image1 = $_FILES['docUpload']['name'][$i];
$filename = basename($image1);
$onlyfile = pathinfo($filename, PATHINFO_FILENAME);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$file = mt_rand();// random number
$newname = $onlyfile.'_'.$file.'.'.$extension;
//$location='images/'.$foldername.'/'.$newname;
$location='../assets/images/uploads/'.$foldername.'/'.$newname;
if($extension=='png' || $extension=='jpg' || $extension=='jpeg') {
compressImage($_FILES['docUpload']['tmp_name'][$i],$location,60);
}
else{
move_uploaded_file($_FILES['docUpload']['tmp_name'][$i], $location);
}
$uploadDoc=$newname;
$dataupload[]=array(
'upload_label'=>$uploadLabel,
'upload_name'=>$uploadDoc,
's_id'=>$last_id,
);
}
}
echo"<pre>";
print_r($dataupload);
$sqlUpload="INSERT INTO `tbl_uploadAll`(upload_label,upload_name,s_id) VALUES (:upload_label,:upload_name,:s_id)";
$stmt= $pdo->prepare($sqlUpload);
$stmt->execute($dataupload);
}
You are passing array of array
$stmt->execute($dataupload);
execute expects an array of key => value where key should be named placeholders. Like
$data = [
'name' => $name,
'surname' => $surname,
'sex' => $sex,
];
So for your case, you should execute inside a foreach
foreach ($dataupload as $row)
{
$stmt->execute($row);
}
Related
I am using Spout Excel reader to read Excel files from php code and saving into a multidimensional array in PHP variable,Array looks like this
$array = [
[
'id[0]' => 'BX-78',
'Name[0]' => 'XXX',
'Address[0]' => 'YUUSATD'
],
[
'id[1]' => 'BX-79',
'Name[1]' => 'YYY',
'Address[1]' => 'DHJSHDJGY'
],
[
'id[2]' => 'BX-80',
'Name[2]' => 'ZZZ',
'Address[2]' => 'DDSDSDA'
]
[
'id[3]' => 'BX-78',
'Name[3]' => 'AAA',
'Address[3]' => 'FSDSDS'
][
'id[4]' => 'BX-81',
'Name[4]' => 'XXX',
'Address[4]' => 'DSDSDSD'
]];
Now i want to show duplicate data from above array using two keys ['id'] and ['name'] if id repeats show as duplicate data,
If name repeats show that row as duplicate data if both are duplicate show as again duplicate row
Otherwise it is unique row.
I have tried using multidimensional array sorting but it is using only one key to match data in rows.
foreach ($arrExcelData as $v) {
if (isset($arrExcelData[$v[0]])) {
// found duplicate
continue;
}
// remember unique item
$arrExcelData3[$v[0]] = $v;
}
// if you need a zero-based array, otheriwse work with $_data
$arrExcelData2 = array_values($arrExcelData3);
Edited : Expected Output Result :
Matching Rows:
Id Name Address
-------------------------
BX-78 XXX YUUSATD
BX-78 AAA DDSDSDA
BX-81 XXX DSDSDSD`
If you want to list the duplicate values, I think the address of the second match should be FSDSDS as there is not item with name AAA and value DDSDSDA:
BX-78 AAA FSDSDS
If that is the case, what you could do is to first use a double foreach to mark the arrays that contain a duplicate id or name by for example adding a property named id and name except when the array is itself in the second loop.
After this loop, you can tell which arrays are the duplicate ones. Instead of using a corresponding index 0 as in id[0], I have used reset and next so it is not tied to these indexes.
To get the filtered result you could use array_reduce to check for the array keys and unset them.
For example:
foreach ($array as $index => $a) {
foreach ($array as $v) {
if ($v === $a) continue;
if (reset($v) === reset($a)) $array[$index]["id"] = "duplicate";
if (next($v) === next($a)) $array[$index]["name"] = "duplicate";
}
}
$array = array_reduce($array, function($carry, $item) {
if (array_key_exists("id", $item) || array_key_exists("name", $item)) {
unset($item["id"], $item["name"]);
$carry[] = $item;
}
return $carry;
}, []);
print_r($array);
Result
Array
(
[0] => Array
(
[id[0]] => BX-78
[Name[0]] => XXX
[Address[0]] => YUUSATD
)
[1] => Array
(
[id[3]] => BX-78
[Name[3]] => AAA
[Address[3]] => FSDSDS
)
[2] => Array
(
[id[4]] => BX-81
[Name[4]] => XXX
[Address[4]] => DSDSDSD
)
)
See a php demo
I've this very pragmatic approach:
$spout_output = [
[
'id[0]' => 'BX-78',
'Name[0]' => 'XXX',
'Address[0]' => 'YUUSATD'
],
[
'id[1]' => 'BX-79',
'Name[1]' => 'YYY',
'Address[1]' => 'DHJSHDJGY'
],
[
'id[2]' => 'BX-80',
'Name[2]' => 'ZZZ',
'Address[2]' => 'DDSDSDA'
],
[
'id[3]' => 'BX-78',
'Name[3]' => 'AAA',
'Address[3]' => 'FSDSDS'
],
[
'id[4]' => 'BX-81',
'Name[4]' => 'XXX',
'Address[4]' => 'DSDSDSD'
]];
// store id to row, and name to row mappings.
// id and name will be keys, value will be an array of indexes of the array $spout_output
$id_to_rows = array();
$name_to_rows = array();
$duplicate_ids = array();
$duplicate_names = array();
foreach($spout_output as $row => $data)
{
$key_id = 'id['.$row.']';
$key_name = 'Name['.$row.']';
if(!isset($data[$key_id]))
continue;
$value_id = $data[$key_id];
$value_name = $data[$key_name];
if(!isset($id_to_rows[$value_id]))
{
$id_to_rows[$value_id] = array();
}
else
{
if(!isset($duplicate_ids[$value_id]))
{
$duplicate_ids[$value_id] = $id_to_rows[$value_id];
}
$duplicate_ids[$value_id][] = $row;
}
if(!isset($name_to_rows[$value_name]))
{
$name_to_rows[$value_name] = array();
}
else
{
if(!isset($duplicate_names[$value_name]))
{
$duplicate_names[$value_name] = $name_to_rows[$value_name];
}
$duplicate_names[$value_name][] = $row;
}
$id_to_rows[$value_id][] = $row;
$name_to_rows[$value_name][] = $row;
}
echo 'Duplicates:';
echo '<br>';
$shown_rows = array();
foreach($duplicate_ids as $id => $rows)
{
foreach($rows as $nr)
{
echo $id . '|' . $spout_output[$nr]['Name['.$nr.']'] . '|' . $spout_output[$nr]['Address['.$nr.']'];
echo '<br>';
$shown_rows[] = $nr;
}
}
foreach($duplicate_names as $name => $rows)
{
foreach($rows as $nr)
{
// if already shown above, skip this row
if(in_array($nr, $shown_rows))
continue;
echo $spout_output[$nr]['id['.$nr.']'] . '|' . $spout_output[$nr]['Name['.$nr.']'] . '|' . $spout_output[$nr]['Address['.$nr.']'];
echo '<br>';
$shown_rows[] = $nr;
}
}
Outputs:
Duplicates:
BX-78|XXX|YUUSATD
BX-78|AAA|FSDSDS
BX-81|XXX|DSDSDSD
I think your 'wanted output' contains an error in the address?
Anyway, with my code above I think you'll have enough mapped data to produce the output you want.
You could do something like this:
$dupes = [];
$current = [];
foreach ($array as $index => $entry) {
$idKey = "id[$index]";
$nameKey = "Name[$index]";
if (array_key_exists($entry[$idKey], $current)) {
$dupes[] = [$entry, $current[$entry[$idKey]]];
}
elseif (array_key_exists($entry[$nameKey], $current)) {
$dupes[] = [$entry, $current[$entry[$nameKey]]];
}
else {
$current[$entry[$idKey]] = $current[$entry[$nameKey]] = $entry;
}
}
print_r($dupes);
Which results in an array containing each set of duplicates (array of arrays):
Array
(
[0] => Array
(
[0] => Array
(
[id[3]] => BX-78
[Name[3]] => AAA
[Address[3]] => FSDSDS
)
[1] => Array
(
[id[0]] => BX-78
[Name[0]] => XXX
[Address[0]] => YUUSATD
)
)
[1] => Array
(
[0] => Array
(
[id[4]] => BX-81
[Name[4]] => XXX
[Address[4]] => DSDSDSD
)
[1] => Array
(
[id[0]] => BX-78
[Name[0]] => XXX
[Address[0]] => YUUSATD
)
)
)
Demo here: https://3v4l.org/JAtNU
In case someone of you are searching unique values by key.
function unique_multidim_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
This function just takes multidimensional array and key value of field you need.
Then takes value of given array one by one (smaller arrays).
Then traverses given array and looking if taken key-value pair matches with given key.
After that if taken key-value pair matches with given key function just inserts smaller array in temporary array (array with unique values).
Don't forget to increment indexes of arrays ($i).
Then return array you got (with unique values) after function ends work.
so, i have this
Array
(
[ModuleCode] => Array
(
[0] => MD001
[1] => MD002
[2] => MD004
[3] => MD005
)
[MD001] => Array
(
[insert] => on
[edit] => on
[delete] => on
)
[MD002] => Array
(
[insert] => on
[edit] => on
[delete] => on
)
[MD005] => Array
(
[insert] => on
[edit] => on
[delete] => on
[access_edit] => on
)
)
as you can see there are an array with ModuleCode as key.
After some try i can get this
MD001
insert => 1
edit => 1
delete => 1
access_edit => 0
MD002
insert => 1
edit => 1
delete => 1
access_edit => 0
MD004
insert => 0
edit => 0
delete => 0
access_edit => 0
MD005
insert => 1
edit => 1
delete => 1
access_edit => 1
with this script
$dataModul = $this->input->post('ModuleCode');
$field = array ("insert","edit","delete","access_edit");
for($x=0;$x<count($dataModul);$x++){
echo "<pre>".$dataModul[$x] . "<br>";
for($a=0;$a<count($field);$a++){
$subcheck = (isset($this->input->post($dataModul[$x])[$field[$a]])) ? 1 : 0;
echo $field[$a]. " => " . $subcheck . "<br>" ;
}
echo "<pre>";
}
Ok, here is what i want to achieve . from this part (for an example)
MD001
insert => 1
edit => 1
delete => 1
access_edit => 0
i want to make something like this
Update TableName set insert = 1, edit = 1, delete = 1 , access_edit = 0 where ModuleCode = 'MD001'
How can i achieve that ? thanks in advance
You can try this code:
You can call use the function as echo generate_query_string('MD004', $modules); where the first parameter is the module code and the second the whole array.
<?php
function generate_query_string( $module, $module_arr ) {
if( !isset( $module_arr[$module] ) ) { return false; } // return false if module does not exist
// set default values
$defaults = array(
'insert' => 0,
'edit' => 0,
'delete' => 0,
'access_edit' => 0
);
$settings = array_merge( $defaults, $module_arr[$module] ); // merge default values and the actual values
$settings = array_filter( $settings ); // remove items with 0 value since we don't need to include them on the query string
// render the query string values
$values = [];
foreach ($settings as $key => $value) {
$value = ( $value == 'on' )? 1 : 0;
$values[] = $key. ' = '. $value;
}
$values = implode(', ', $values);
return 'Update TableName set '. $values .' where ModuleCode = '. $module;
}
?>
I found the solustion. Here is what i do
First. i add a custom function ( ref : https://stackoverflow.com/a/42052020/6354277 )
function custom_function($input_array)
{
$output_array = array();
for ($i = 0; $i < count($input_array); $i++) {
for ($j = 0; $j < count($input_array[$i]); $j++) {
$output_array[key($input_array[$i])] = $input_array[$i][key($input_array[$i])];
}
}
return $output_array;
}
then i change my code to this.
function updateaccess(){
$dataModul = $this->input->post('ModuleCode');
$field = array ("insert","edit","delete","access_edit");
for($x=0;$x<count($dataModul);$x++){
for($a=0;$a<count($field);$a++){
$subcheck[$a] = (isset($this->input->post($dataModul[$x])[$field[$a]])) ? 1 : 0;
$mynewarray[$dataModul[$x]][] = array($field[$a] => $subcheck[$a]);
}
foreach ($mynewarray as $key => $value) {
$forSave[$dataModul[$x]] = $this->custom_function($value);
}
}
foreach ($forSave as $key2 => $values2) {
$this->mainmodel->updateRow(array("ModuleCode" => $key2),"user_modules", $values2 );
}
}
How to get the first value of element of array in php.
My story board is like this:
I have an array like this:
(
[0] => Array
(
[ID] => 68
[MATERIAL] => I have
[AC] => Try
)
[1] => Array
(
[ID] => 69
[MATERIAL] => It
[AC] => No Surrender
)
)
I want to update some record on my database like this,
foreach element of array,
UPDATE MY TABEL SET MATERIAL = [MATERIAL], AC = [AC] where id= [id]
this is the model named m_admin :
public function update_eir_to_cost($id, $material, $ac) {
$data = array(
"MATERIAL" => $material,
"AC" => $ac);
$this->db->trans_start();
$this->db->where($id);
$this->db->update('tb_repair_detail', $data);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE) {
// generate an error... or use the log_message() function to log your error
echo "Error Updating";
} else {
echo "Alhamdulillah";
}
}
This is the controller :
public function update_json_detail() {
$post_data = $this->input->post("POST_ARRAY");
$execute = array();
foreach ($post_data as $data) {
$execute[] = array(
'ID'=> $data['0'],
'MATERIAL' => $data['7'],
'AC' => $data['8']
);
}
echo "<pre>";
print_r($execute); // return an array like above.
/*forech element
update table using model
*/
}
This will solve your problem:
public function update_json_detail() {
$post_data = $this->input->post("POST_ARRAY");
$execute = array();
foreach ($post_data as $data) {
$execute[] = array(
'ID'=> $data['0'],
'MATERIAL' => $data['7'],
'AC' => $data['8']
);
}
echo "<pre>";
print_r($execute); // return an array like above.
$this->load->model('m_admin');
foreach ($execute as $row) {
$this->m_admin->update_eir_to_cost($row['ID'], $row['MATERIAL'], $row['AC']);
}
}
I am trying to build a multi dimentional array of students and their data from my database table using mysqli and php.
I want my array to look something like this
Array #$adult array
(
[626] => Array #student no 626 data
(
[name] => emily,
[age] => 43,
[height] => 156,
)
[627] => #student no 627 data
(
[name] => luke,
[age] => 31,
[height] => 176,
)
)
the number being the members id followed by their data.
So i have tried the following
$sql = "SELECT * FROM pzgym_waitinglist WHERE seen = 0 GROUP BY gym_discipline, school_yr, id";
$result = $db->query($sql);
if ($result->num_rows > 0)
{
#set up array
$adult = array();
while($row = $result->fetch_array())
{
$id = $row["id"];
$name = $row["name"];
$age= $row["age"];
$height = $row['height'];
if($row['gym_discipline'] == "Adult Gymnastics")
{
$adult[$id] = "['name'] => $name, ['age'] => $age, ['height'] => $height";
}
}
}
but this isnt producing the correct results, so im guessing my array building sucks :( here is what i am getting.
Array
(
[626] => ['name'] => Emily, ['age'] => 43, ['height'] => 156
[627] => ['name'] => Luke, ['age'] => 31, ['height'] => 176
)
Could someone help me please to build a successful multi dimentional array from the data im my database
Many Thanks
You need a second level when you create the array, instead of just simply adding keys and data to the array.
So, first create an index (key) with the student ID, then the value of that new index will be a sub-array with all the data for that student.
Then next loop it will do the same for the next student.
Something like:
while($row = $result->fetch_array())
{
$id = $row["id"];
$name = $row["name"];
$age= $row["age"];
$height = $row['height'];
if($row['gym_discipline'] == "Adult Gymnastics")
{
$adult[$id] = array(
"name" => $name,
"age" => $age,
"height" => $height,
);
}
}
Use the array construct:
$adult[$id] = array('name' => $name, 'age' => $age, 'height' => $height);
You need to do in following manner--
$adult[$id]['name'] = $name;
$adult[$id]['age'] = $age;
$adult[$id]['name'] = $height;
Note:- if condition is not required because your array is created through id.
I have two string values namely $late_array and $wrong_array. The values are comma delimited.
What I would like to do is compare the two arrays and if the first two elements are the same add the value to the end else make it zero. The arrays I have:
$late_array = array(
[0] => 140610d,Richard,12
[1] => 140610a,Dave,22
[2] => 140610n,Noddy,121
[3] => 140610a,Nick,15
)
$wrong_array = array(
[0] => 140610d,Richard,2
[1] => 140610d,Mary,60
[2] => 140610a,Dave,11
[3] => 140610n,Noddy,90
)
The end result should be:
$combined_array = array(
[0] => 140610d,Richard,12,2
[1] => 140610d,Mary,0,60
[2] => 140610a,Dave,22,11
[3] => 140610a,Nick,15,0
[4] => 140610n,Noddy,121,90
)
I have so far formed a foreach and used the '===' operators to check if the date and name match then output as I want but I have not been able to get it to work if the name is not present in one array but another to make the value zero.
EDIT: Just to clear it up, hopefully. If the value is present in both arrays then the date,name,late value,wrong value should show. But if the value is present in late only then the value for wrong should be 0, same visa versa. Added "Nick" to try and explain a bit better.
This is what I did to solve the problem so far:
$wrong_val = array();
foreach($out as $wrong_value) {
$wrong_tosearch[] = substr($wrong_value,0,strrpos($wrong_value,","));
$w_id = substr($wrong_value,0,strrpos($wrong_value,","));
$wrong_val[$w_id] = substr($wrong_value,strrpos($wrong_value,",")+1,strlen($wrong_value));
}
foreach($sql_late_array as $late_value) {
$late_tosearch[] = substr($late_value,0,strrpos($late_value,","));
$l_id = substr($late_value,0,strrpos($late_value,","));
$late_val[$l_id] = substr($late_value,strrpos($late_value,",")+1,strlen($late_value));
}
$merge = array_merge($wrong_tosearch,$late_tosearch);
$sort = array_values(array_unique($merge));
$combined_array = array();
foreach ($sort as $search_val) {
if (array_key_exists($search_val,$wrong_val) !== FALSE) {
foreach ($wrong_val as $w_key=>$w_val) {
$combined_array[$w_key]['late'] = "0";
$combined_array[$w_key]['wrong'] = $w_val;
}
}
if (array_key_exists($search_val,$late_val) !== FALSE) {
foreach ($late_val as $l_key=>$l_val) {
$combined_array[$l_key]['wrong'] = "0";
$combined_array[$l_key]['late'] = $l_val;
}
}
}
print_r($combined_array);
Check if below code solves your problem.
$late_array = array(
"0" => "140610d,Richard,12",
"1" => "140610a,Dave,22",
"2" => "140610n,Noddy,121"
);
$wrong_array = array(
"0" => "140610d,Richard,2",
"1" => "140610d,Mary,60",
"2" => "140610a,Dave,11",
"3" => "140610n,Noddy,90"
);
foreach($wrong_array as $wv)
{
$tosearch = substr($wv,0,strrpos($wv,",")-1);
$valtoadd = substr($wv,strrpos($wv,",")+1,strlen($wv));
$added=false;
foreach($late_array as $lv)
{
if(strstr($lv, $tosearch) !== false)
{
$combined_array[] = $lv.",".$valtoadd;
$added=true;
break;
}
}
if(!$added)
$combined_array[] = $tosearch.",0,".$valtoadd;
}
$tcombined_array = $combined_array;
foreach($late_array as $wv)
{
$added = false;
foreach($tcombined_array as $cv)
if(strstr($cv,$wv))
$added = true;
if(!$added) $combined_array[] = $wv.",0";
}
print_r($combined_array);
May be big but works
<?php
$late_array = array(
0 => "140610d,Richard,12",
1 => "140610a,Dave,22",
2 => "140610n,Noddy,121",
);
$wrong_array = array(
0 => "140610d,Richard,2",
1 => "140610d,Mary,60",
2 => "140610a,Dave,11",
3 => "140610n,Noddy,90"
);
$pattern = "/[0-9]*[a-zA-Z]*,[0-9]*[a-zA-Z]*,/";
$combined_array = $late_array;
foreach($wrong_array as $wrong_index => $wrong_value){
foreach($late_array as $late_index => $late_value){
preg_match_all($pattern, $late_value, $late_matches);
preg_match_all($pattern, $wrong_value, $wrong_matches);
if($late_matches[0] == $wrong_matches[0]){
$explode = explode(',',$wrong_value);
$combined_array[$late_index] = $combined_array[$late_index].','.$explode[2];
$matchedValues[] = $wrong_index;
}
}
}
$unmatched_values = array_diff(array_keys($wrong_array), array_values($matchedValues));
foreach($unmatched_values as $key => $value){
$combined_array[] = $wrong_array[$value].',0';
}
echo '<pre>';
print_r($combined_array);
?>