Home > database >  unable to insert multiple array data from dynamic generated field?
unable to insert multiple array data from dynamic generated field?

Time:12-19

I am unable to enter multiple data, it enter only single data. I have tried using for loop and then entering data, using 3 user and 2 task, there is an error previously offset. offseterror error recieved an array view

public function add($postData)
{
    // dd($postData);
    $c = count($postData['user_name']);
    $t = count($postData['task_name']);

    for ($i = 0; $i < $c; $i  ) {
        $user_name = $postData['user_name'][$i];
        $user_email = $postData['user_email'][$i];
      
        $data['insert']['user_name'] = $user_name;
        $data['insert']['user_email'] = $user_email;
               
    }

    for ($j = 0; $j < $t; $j  ) {
        $task_name = $postData['task_name'][$j];

        $data['insert']['task_name'] = $task_name;        
    }
    $data['insert']['name'] = $postData['name'];
    $data['insert']['description'] = $postData['description'];
    $data['insert']['customer_name'] = $postData['customer_name'];
    $data['insert']['billing_method'] = $postData['billing_method'];
    $data['insert']['dt_created'] = DT;
    $data['table'] = PROJECT;
    $result = $this->insertRecord($data);
    
    if ($result == true) {
        $response['status'] = 'success';
        $response['message'] = 'Project created';
    } else {
        $response['status'] = 'danger';
        $response['message'] = DEFAULT_MESSAGE;
    }
    return $response;
}

CodePudding user response:

As Per lack of question details attached, I am supposing that you want to insert multiple task entry with project name, description etc.

Here is updated code:

<?php
// dd($postData);
    $username = $postData['username'];
    $user_email = $postData['user_email'];
    $task_name = $postData['task_name'];
    foreach ($username as $key => $value) {
        $data['insert']['name'] = $postData['name'];
        $data['insert']['description'] = $postData['description'];
        $data['insert']['customer_name'] = $postData['customer_name'];
        $data['insert']['billing_method'] = $postData['billing_method'];
        $data['insert']['username'] = $value;
        $data['insert']['user_email'] = $user_email[$key];
        $data['insert']['task_name'] = $task_name[$key];
        $data['insert']['dt_created'] = DT;
        $data['table'] = PROJECT;
        $result = $this->insertRecord($data);    
    }

CodePudding user response:

I have used 2 more tables to add dynamically generated data, that is user_name,user_email as well as task_name.It's not in working condition.

 public function add($postData)
{
    // dd($postData);

    $data['insert'] = [
        'user_id' => $postData['user_id'],
        'name' => $postData['name'],
        'description' => $postData['description'],
        'cust_id' => $postData['cust_id'],
        'billing_method' => $postData['billing_method'],
        'dt_created' => DT
    ];
    $data['table'] = PROJECT;
    $result = $this->insertRecord($data);

    unset($data);
    $userCounted = count($postData['user_name']);

    for ($i = 0; $i < $userCounted; $i  ) {
        $user_name = $postData['user_name'][$i];
        $user_email = $postData['user_email'][$i];

        $data['insert'] = [
            'project_id' => $result,
            'user_name' => $user_name,
            'user_email' => $user_email,
            'dt_created' => DT
        ];
        $data['table'] = USER_PROJECT;
        $resdata = $this->insertRecord($data);
    }

    unset($data);
    $taskCounted = count($postData['task_name']);
    for ($i = 0; $i < $taskCounted; $i  ) {
        $task_name = $postData['task_name'][$i];
        $data['insert'] = [
            'task_name' => $task_name,
            'id_project' => $result,
            'dt_created' => DT
        ];
        $data['table'] = TASK_PROJECT;
        $res = $this->insertRecord($data);
    }

    if (count($resdata) != 0 && count($res) != 0) {
        $response['status'] = 'success';
        $response['message'] = 'Project created';
    } else {
        $response['status'] = 'danger';
        $response['message'] = DEFAULT_MESSAGE;
    }
    return $response;
}
  • Related