how to insert post in codeigniter - php

i want to ask. I made an automatic code that I will post to the database, but I have problems, the data does not enter the database.
here I make an automatic code for item code
$data['awb'] = $this->M_order->bikin_kode();
I want to enter the value into 'tracking_number'
$data['awb'] = $this->M_order->bikin_kode();
$resinya = $data['awb'];
$sheet = $loadexcel->getActiveSheet()->toArray(null, true, true ,true);
$data = array();
$numrow = 1;
foreach($sheet as $row){
if($numrow > 1){
array_push($data,
array(
'tracking_number' => $resinya['awb'],
)
);
}
$numrow++;
}
$this->M_order->insert_multiple($data);
when I insert the data it doesn't enter

I think you have passing data wrongly to array. Use $data['awb'] instead of $resinya['awb'] for 'tracking_number'.like this
array_push($data,
array(
'tracking_number' => $data['awb'],
)
);
Now, your code insert the data outside the foreach. so it will execute the last data into the database table.

Related

Getting a an array from for loop and use another for loop with that array in PHP

I have a for loop, and will form two arrays in the loo
foreach ($data as $key => $value) {
........
........
$user_insert[] = [
'keyy' => $value,
'key' => $value,
....
...
...
];
$someArray1[] = [
/*'user_id' => $insert_id,*/
'key1' => $value1,
'keyy' => $value,
.......
........
];
}
the count of $user_insert[] array is 4, the count of $someArray1 is 15.
after this for loop, I need to insert $user_insert array data to the database and use that inserted_id to insert next array $someArray1
foreach($user_insert as $insert_user){
$unique_user_insert = array_unique($insert_user);
//dd($unique_user_insert);
$insert_id = DB::table('users')->insertGetId($unique_user_insert);
foreach ($someArray1 as $someArray) {
$someArray['user_id'] = $insert_id;
DB::table('table_name')->insert($someArray);
}
}
So the problem here is the data in the second loop is inserting 60 times(4 * 15). I need to insert only 15 rows.
The data($someArray1) is coming from the first for loop, but I need to add a user_id to that array which I get after the insert operation in second for loop.
So how can i insert only 15 rows.
I'm going to assume that you are able to access your $someArray1 using the $insert_id value to find the appropriate user data.
foreach($user_insert as $insert_user){
$unique_user_insert = array_unique($insert_user);
$insert_id = DB::table('users')->insertGetId($unique_user_insert);
// Get the user information you need as $someArray1 should be user_id=>data
$userData = $someArray[$insert_id];
$userData['user_id'] = $insert_id;
// No need for an inner loop, just access the necessary properties of the loop you created earlier.
DB::table('table_name')->insert($userData);
}
Your tags indicate that you are using Laravel 5 too. If you are using the eloquent ORM, some of the insertion and ID retrieval can be cleaned up by creating Models for your DB tables.
Actually, each time you process a line from $user_insert, you loop over $someArray1 instead of fetching just the line you need.
The thing is to understand the line you need. As much as I can understand your piece of code, I would say the easiest (most readable) way of doing it is by using a for loop, not a foreach one :
for( $i = 0, $iMax = count( $user_insert ); $i < $iMax; ++$i ){
$insert_user = $user_insert[$i];
// Put your `$user_insert` insert code here
$someArray1[$i]['user_id'] = $insert_id;
DB::table('table_name')->insert( $someArray[$i] ); // Note the [$i] here
}
You also may do that with foreach by requesting indices :
foreach( $user_insert as $i => $insert_user ){
$unique_user_insert = array_unique($insert_user);
//dd($unique_user_insert);
$insert_id = DB::table('users')->insertGetId($unique_user_insert);
// Now use $i requested above :
$someArray1[$i]['user_id'] = $insert_id;
DB::table( 'table_name' )->insert( $someArray1[$i] );
}

Update One JSON Field in Database - CodeIgniter

I have a JSON field called 'spec' and there are about 10 other items in this in JSON format. I need to only update the quantity.
Although when I try this method, it deletes everything else in it and just sets spec = quantity.
Heres what I have so far.
$pass_coupon_id = $this->pass_coupon_id();
$coupon_array = $this->db->query("SELECT * FROM coupon WHERE coupon_id='$pass_coupon_id'")->result_array();
foreach ($coupon_array as $row) {
$spec = json_decode($row['spec'], true);
}
$quantity_new = $spec['quantity'] - 1;
$data2 = array(
'spec' => json_encode(array(
'quantity'=> $quantity_new
)));
$this->db->where('coupon_id', $pass_coupon_id);
$this->db->update('coupon', $data2);
You need to overrite only this one field and update whole field in query.
<?php
$pass_coupon_id = $this->pass_coupon_id();
$coupon_array = $this->db->query("SELECT * FROM coupon WHERE coupon_id='$pass_coupon_id'")->result_array();
// i don't know what you're using, but using foreach to extract single row isn't good solution. Look for sth like result_row() maybe.
$coupon = $coupon_array[0];
$spec = json_decode($coupon, true);
$new_quantity = $spec['quantity'] - 1;
$spec['quantity'] = $new_quantity;
$new_spec = json_encode($spec);
$this->db->where('coupon_id', $pass_coupon_id);
$this->db->update('coupon', $new_spec);
Depending on the database, the best solution would be using specific function to ommit updating whole structure - https://stackoverflow.com/a/34987329/2926214

PHP search results not inserting on my table

I am creating a search module to show results from database, I am echoing out the data to check if I am receiving it from database.
here is my current output:
As you can see, the results are there but I wasn't able to display it on my table, and I also have that error for invalid argument for foreach(). Can anyone check what is the problem here?
echo form_open(site_url() . '/search/get_account',array('id' => 'formSearch'));
$data = array(
'name' => 'acctNum',
'id' => 'acctNum',
'type' => 'hidden',
'value' => set_value('acctNum',''),
);
$dataCertType = array($data);
$dataCertType[''] = '--';
if(! is_null($certType))
foreach($certType as $rowType)
$dataCertType[$rowType->certTypeId] = $rowType->certTypeName;
$formCertType = form_dropdown('certType', $dataCertType, set_value('certType'),'id="certType" class="dropdown"');
add brackets. echo out the form drop dropdown.
if(! is_null($certType)){
foreach($certType as $rowType){
$dataCertType[$rowType->certTypeId] = $rowType->certTypeName;
$formCertType = form_dropdown('certType', $dataCertType,set_value('certType'),'id="certType" class="dropdown"');
echo $formCertType ;
}
}
bonus points - do your is_null check in your controller, and then show an appropriate view.

phpexcel printing mysql data to excel( printing only 1 row of data on database to excel)

I want to use the 10autofilter.php from phpexcel, to our program.
But I want a code that will print on excel the datas on our database since its only prints row 1 and doesnt print all data on our mysql please help me you can see the code it only output 1 row.
I think there's a problem here but this works fine on php displaying on browser but just not in excel the output is 1 row.
I have used $i++ on $row as you can see i dont know what to do.
$res = mysql_query("select * from services");
$row = mysql_num_rows($res);
for($i=0; $i<$row; $i++)
{
$serviceid = mysql_result($res,$i,"serviceid");
$servicename = mysql_result($res,$i,"servicename");
$contactemail = mysql_result($res,$i,"contactemail");
$charge = mysql_result($res,$i,"charge");
$contactlastname = mysql_result($res,$i,"contactlastname");
$contactmiddlename = mysql_result($res,$i,"contactmiddlename");
$yearassistancereceived = mysql_result($res,$i,"yearassistancereceived");
$yearestablished = mysql_result($res,$i,"yearestablished");
$dataArray = array(
array(
$serviceid,
$servicename,
$contactemail,
$charge." ".$contactmiddlename." ".$contactlastname,
$yearassistancereceived,
$yearestablished
)
);
$objPHPExcel->getActiveSheet()->fromArray($dataArray, NULL, 'A2');
}
I also have the error:
Fatal error. Uncaught exception 'PHPExcel_WriterException' with message 'Invalid parameters passed'. why? at the bottom of the error is written C:\xampp\htdocs\DOSTPROJECT\classes\PHPExcel\Writer\Excel2007\ContentTypes.php on line 263.
This executes and excel file but I have said only 1 row data, I mean whats wrong?
If you write every row of data to spreadsheet row #2, then the rows will all overwrite each other. You want to write the first row of data to spreadsheet row #2, the second to spreadsheet row #3, etc
You used $i++ to get each row from the database, but you're not using it when writing to PHPExcel, you're just writing every row array at cell A2
for($i=0; $i<$row; $i++)
{
$serviceid = mysql_result($res,$i,"serviceid");
$servicename = mysql_result($res,$i,"servicename");
$contactemail = mysql_result($res,$i,"contactemail");
$charge = mysql_result($res,$i,"charge");
$contactlastname = mysql_result($res,$i,"contactlastname");
$contactmiddlename = mysql_result($res,$i,"contactmiddlename");
$yearassistancereceived = mysql_result($res,$i,"yearassistancereceived");
$yearestablished = mysql_result($res,$i,"yearestablished");
$dataArray = array(
array(
$serviceid,
$servicename,
$contactemail,
$charge." ".$contactmiddlename." ".$contactlastname,
$yearassistancereceived,
$yearestablished
)
);
$objPHPExcel->getActiveSheet()->fromArray($dataArray, NULL, 'A'.($i+2));
}

theme_table drupal development

update
I have made this table: I have 2 array's UGentArray and InternshipsArray. How can I set these in my table? instead of "Row column 1 data" and "Row column 2 data". My question is: I want the values from UgentArray and InternshipArray as 2 columns under Each title UGentID and Internships
enter code here// Make table and display on screen.
$tbl_header = array();
$tbl_header[] = array("data" => "UGentID");
$tbl_header[] = array("data" => "Internships");
$tbl_row = array();
foreach($studentUGentID as $key => $value) {
for($i = 0; $i<count($value); $i++) {
$tbl_row[] = $value[$i]['value'];
}
}
$tbl_row[] = "Row column 2 data";
$tbl_rows = array();
$tbl_rows[] = $tbl_row;
$html = theme_table($tbl_header,$tbl_rows);
return $html;![enter image description here][1]
Without having an example of the data you are starting with it is difficult to give you exact code to solve your problem.
In general the best way to built tables in Drupal is like this:
$table_headers = array(
t('UGentID'), // this is the header for the first column
t('Internships'), // this is the header for the second column
);
$table_rows = array()
foreach ($studentUGentID as $key => $value) {
$table_rows[] = array(
'column 1 data', // add all the data for the row one column
'column 2 data', // at a time
);
}
$html = theme('table', $table_headers, $table_rows);
As I said without knowing what is in $studentUGentID and $internships I cannot help further.
With sample data I could help you more.

Categories