I send data via ajax in controller. I create form dynamically in jquery, that's why i have array in my post request. It look likes this
array (size=3)
'login' =>
array (size=2)
0 => string 'trololo' (length=7)
1 => string 'trololo2' (length=8)
'pass' =>
array (size=2)
0 => string 'trololo' (length=7)
1 => string 'trololo2' (length=8)
'email' =>
array (size=2)
0 => string 'trololo#gmail.com' (length=17)
1 => string 'trololo#gmail.com2' (length=18)
In my controller I want persist it in my db. I do that like:
$data = $request->request->all();
$i = 0;
foreach($data as $ud)
{
$user[] = new User();
$user->setLogin($ud['login[$i]']);
$user->setPass($ud['pass[$i]']);
$user->setEmail($ud['email[$i]']);
$em->persit($user[$i])
$em->flush();
$em->clear();
$i++
}
But it does not working. Please, help me to solve this problem
You are mixed $user as array as as object. Also input $data contain arrays to save, but you do not need foreach for $data itself.
Try change it to:
$data = $request->request->all();
foreach($data['login'] as $i => $login)
{
$user = new User();
$user->setLogin($login);
$user->setPass($data['pass'][$i]);
$user->setEmail($data['email'][$i]);
$em->persit($user);
$em->flush();
$em->clear();
}
Related
With my code:
$sql = "SELECT number.phone_number FROM number, ordered_number WHERE ordered_number.number_id=number.id AND ordered_number.order_id=$id";
$connection = \Yii::$app->getDb();
$command = $connection->createCommand($sql);
$numery = $command->queryAll();
I get array that looks like this:
array (size=3)
0 =>
array (size=1)
'phone_number' => string '546732354' (length=9)
1 =>
array (size=1)
'phone_number' => string '565345456' (length=9)
2 =>
array (size=1)
'phone_number' => string '456557546' (length=9)
I want to get simple array, where the first element is just the number (here - the string), without name 'phone_number' and additional 1-element arrays inside the main array. When I try to do foreach on this array, it tells me that I use "Illegal offset type". I found that it means I'm using object, instead of an array, but that's an array, not an object and I have no idea what to do.
Even simplier (but for php5.5 and php7):
$numery = array_column(
$command->queryAll(),
'phone_number'
);
Use below loop to get desired result
$numery = $command->queryAll();
$number_arr = array();
foreach($numery as $number)
{
array_push($number_arr,$number['phone_number']);
}
print_r($number_arr);
my controller function:
function view_story($story_id)
{
$this->load->model('m_signup');
$data['template_data'] = $this->m_signup->get_template($story_id);
var_dump($data);
// here i want 'template_name' so that it can save in variable.
}
my output
array (size=1)
'template_data' =>
array (size=1)
0 =>
array (size=3)
'bg_image' => string 'assets/img/marine/bubbles1.png'
'bg_sound' => string 'assets/img/old/old.mp3' (length=22)
'template_name' => string 'Marine' (length=6)
You want to like this try...
function view_story($story_id)
{
$this->load->model('m_signup');
$template_data= $this->m_signup->get_template($story_id)->result_array();
print_r($template_data);
}
I've got a form which users can add new rows by clicking on a button and automatically, it adds +1 on the field's name.
So for example, I've got my train_id_1, train_type_1 and my user wants to add a new one, so now I've got train_id_2 and train_type_2.
In order to save this in my database, I would like to sort and seperate train_type_1 / train_type_2... to make a foreach and then to save in my database.
So, the var_dump of my $_POST looks like :
array (size=60)
'train_id_1' => string ' 07:36' (length=6)
'train_type_1' => string ' -Z' (length=3)
'user_id_1' => string 'CPN' (length=3)
'event_criter_1' =>
array (size=3)
0 => string 'test' (length=4)
1 => string '234' (length=3)
2 => string '532' (length=3)
'train_id_2' => string ' 08:32' (length=6)
'train_type_2' => string ' -X' (length=3)
'user_id_2' => string 'CPN' (length=3)
'event_criter_2' =>
array (size=3)
0 => string 'TESTG' (length=5)
1 => string 'GGG' (length=3)
2 => string 'AETG' (length=4)
'train_id_3' => string ' 08:36' (length=6)
'train_type_3' => string ' -Z' (length=3)
'user_id_3' => string 'CPN' (length=3)
'event_criter_3' =>
array (size=1)
0 => string '' (length=0)
'train_id_4' => string ' 09:04' (length=6)
'train_type_4' => string ' -X' (length=3)
'user_id_4' => string 'CPN' (length=3)
'event_criter_4' =>
array (size=1)
0 => string '' (length=0)
Do you know how I can make abcd_1 separate from abcd_2 to make my foreach (or another solution) ?
Thank you!
Loop through your array, matching _1 and insert the posted elements as one row. Then increment and match _2 etc..
$postedElements = $_POST;
$elementsPerRow = 4;
$numRows = count($postedElements)/$elementsPerRow;
// loop through the number of 'rows' to insert
for($i=1;$i<=$numRows;$i++){
// Build an array to store matched elements to insert
$elementsToInsert = array();
//process the complete _POST array each time...
foreach($postedElements as $name => $value){
// ...get the 'row' (the bit after the underscore)...
//list($value,$row) = explode('_',$name); // doesn't work for 2 underscores..
$row = end(explode($name)); // This will
if($row == $i){
// ...and add elements that match to an insert array
// $elementsToInsert[] = $name;
$elementsToInsert[$name] = $value;
}
}
// insert $elementsToInsert into DB
}
I think, at first You need to have a amount of records in array through count($arr). Then use usual for loop:
//output of field names
for ($i = 0; $i < count($arr); $i++){
//Work with array
}
Make a new array.
Then add a data from loop into new array.
I think it could help You.
And by which reason You need to sort them?
This question already has answers here:
Convert laravel object to array
(14 answers)
Closed 5 years ago.
i queried a DB like this which got me an array:
foreach($oid as $orderid) {
$orderdetailData[] = DB::table('order_details')
->join('orders', 'order_details.oid', '=', 'orders.oid')
->select('order_details.oid', 'orders.ostatus')
->where('order_details.oid', $orderid)->get();
}
$data = array_flatten($orderdetailData);
return $data;
This is the array i get
array (size=2)
0 =>
object(stdClass)[174]
public 'oid' => int 1
public 'ostatus' => string 'Placed' (length=6)
1 =>
object(stdClass)[158]
public 'oid' => int 2
public 'ostatus' => string 'Placed' (length=6)
I am trying to get this array in the form
array (size=2)
0 =>
array (size=2)
public 'oid' => int 1
public 'ostatus' => string 'Placed' (length=6)
1 =>
array (size=2)
public 'oid' => int 2
public 'ostatus' => string 'Placed' (length=6)
I tried doing this:
foreach($orderdetailData as $key => $value){
$data[] = array_flatten($orderdetailData[$key]);
}
But doing this gets me an array in this form:
array (size=2)
0 =>
array (size=1)
0 =>
object(stdClass)[174]
public 'oid' => int 1
public 'ostatus' => string 'Placed' (length=6)
1 =>
array (size=1)
0 =>
object(stdClass)[158]
public 'oid' => int 2
public 'ostatus' => string 'Placed' (length=6)
Which is not i what i am looking for. Can someone tell me what would be an easy way to do this ? Thanks
Using array_map and casting to an array should be sufficient:
$data = array_map(function($object){
return (array) $object;
}, $data);
I also wouldn't run queries inside a loop. You should try getting that data in one query from the db. Something like this could work:
$data = DB::table('order_details')
->join('orders', 'order_details.oid', '=', 'orders.oid')
->select('order_details.oid', 'orders.ostatus')
->whereIn('order_details.oid', $oid)->get();
Edit
As it has been mentioned in another answer shortly let me explain how you can accomplish the same by setting PDO to FETCH_ASSOC:
DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();
However this changes the fetch mode globally for the rest of the request (at least if you don't open a new connection). To be save you should change it back afterwards:
DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();
DB::setFetchMode(PDO::FETCH_CLASS);
Or even back it up first if you can't be sure what default is used:
$fetchModeBefore = DB::getFetchMode();
DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();
DB::setFetchMode($fetchModeBefore);
On the PDO/DB object you can set the return style to assoc.
I have an object with a property:
protected $recipients = array();
In one of my methods I set the contents of the $recipients property with an associative array of data using this method:
public function setRecipientData($recipientData){
foreach($recipientData as $key => $value){
$this->recipients[$key] = $value;
}
}
When I dump the contents of $this->recipients, the array ends up looking like this:
array (size=2)
0 =>
array (size=6)
'email' => string 'info#mycompany.com' (length=29)
'userEmail' => string 'xxx#yyy.com' (length=11)
'first_name' => string 'Test' (length=4)
'last_name' => string 'User' (length=4)
'jobTitle' => string 'Customer User' (length=13)
'phoneNumber' => string '123.456.7890' (length=12)
I also have a property that will extract only the email addresses from the property like this:
private function getRecipientEmails(){
$emails = array();
foreach($this->recipients as $recipient){
$emailPair = array('email' => $recipient['userEmail']);
$emails[] = $emailPair;
}
return $emails;
}
The problem that I'm running into is that when I run the method getRecipientsEmails() I get the error:
Undefined index: userEmail
I don't understand why it's not seeing the index 'userEmail'. If I dump the nested array it shows a value:
private function getRecipientEmails(){
$emails = array();
foreach($this->recipients as $recipient){
dd($emailPair = array('email' => $recipient['userEmail']));
$emails[] = $emailPair;
}
return $emails;
}
Result:
array (size=1)
'email' => string 'xxx#yyy.com' (length=20)
I've tried dumping the autoload with composer but it makes no difference.
Can anyone help point me in the right direction??
EDIT: Sorry, I realized that I wrote the wrong method into my original post. I've added the original getRecipientEmails() method and the dump that shows the data.