Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Insert multiple arrays
#1

[eluser]RobertB.[/eluser]
Hello,
Trying to insert different data, types an array and a non array

$interval is not an array
$services is an array

$interval needs to repeat as long as there are items in the service array

db table(services2intervals)
The serviceId column is where the $service[] array data will go
The make2intervalId column is where the $interval data will go
Code:
id | serviceId | make2intervalId | isActive
1  |     1     |       6         |    1
2  |     2     |       6         |    1
3  |     3     |       6         |    0

Sending the data like this
Code:
<input type="hidden" name="interval" value="' . $interval . '">
<input type="checkbox" name="service[]" value="' . $service -> service_id . '">
CONTROLLER
Code:
function update() {        
        $interval = $this -> input -> post('interval');
        $services = $this -> input -> post('service');

        $data['output'] = $this -> services -> updateServices($interval, $services);

        //echo var_dump($interval);
    }

Example var_dump
Code:
($services)
array(2) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "3"
}

($interval)
string(1) "6"

MODEL
Code:
function ifRecordExist() {
        $this -> db -> distinct();
        $this -> db -> select('s2i.serviceId, s2i.make2intervalId');
        $this -> db -> from('services2intervals s2i');

        $query = $this -> db -> get();
        if($query -> num_rows() > 0) {
            foreach($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

function updateServices($interval, $services) {

        $reset = array('isActive' => 0);
        $active = array('isActive' => 1);
        
        foreach($services as $key => $service) {
            $newRecord['serviceId'][$key] = $service;
        }

        if($interval) {
            /** Reset isActive to '0' WORKS **/
            $this -> db -> where('make2intervalId', $interval);
            $this -> db -> update('services2intervals', $reset);
            
            /** Set isActive to '1' WORKS **/
            foreach($services as $key => $service) {
                $this -> db -> where('make2intervalId', $interval);
                $this -> db -> where('serviceId', $service);
                $this -> db -> update('services2intervals', $active);
            }
            //Checks if row exist WORKS
            $data['records'] = $this -> ifRecordExist();
            foreach($services as $key => $service) {
                if($data['records']) {
                    foreach($data['records'] as $record) {
                        if($record -> serviceId === $service && $record -> make2intervalId === $interval) {
                            //Do Nothing
                        } else {
                           // Insert records if they don't exist DON'T WORK
                            $this -> db -> insert('services2intervals', $services, $interval);
                        }
                    }
                }
            }
        }
        return true;
    }

I will like to know how to insert the data and also if there is a better way of doing the whole thing "Best Practice"
Thanks




Theme © iAndrew 2016 - Forum software by © MyBB