Chris
Chris

Reputation: 1129

PHP - Adding multiple database entries

I have a function that adds an entry to the database, the code i have at the moment is as follows:

    function create_internal_role($rolename, $rolekey)
{
    $data = array(
        'name'  => $rolename,
        'key'   => $rolekey.'1'
    );
    if (!is_null($res = $this->ci->internal_roles->create_role($data))) {
        return $data;
    }

    return NULL;
}

What i want to do, is using the same function add another 2 data arrays with a 2 and 3 behind the $rolekey, so that with the one function, it adds 3 lots of data, rolekey1, rolekey2 and rolekey3

How would i go about doing that?

Upvotes: 1

Views: 196

Answers (2)

Nick G
Nick G

Reputation: 960

Perhaps something like this will work:

function create_internal_role($rolename, $rolekey)
{
    // define role key indecies
    $indexArray = array(1,2,3);
    $ret = array(); // return array

    // create roles
    for(i=0; i<count($indexArray); $i++){
        $data = array(
            'name'  => $rolename,
            'key'   => $rolekey.indexArray[$i]
        );

        if (!is_null($res = $this->ci->internal_roles->create_role($data))) {
            $ret[] = $data;
        }else{
            $ret[] = null;
        }
    }

    return $ret;
}

Upvotes: 0

Daniel Bingham
Daniel Bingham

Reputation: 12914

With out knowing about your structure and from the current phrasing of your question, the obvious answer would seem to be this:

function create_internal_role($rolename, $rolekey)
{
    $ret = array();
    $data = array(
        'name'  => $rolename,
        'key'   => $rolekey.'1'
    );
    if (!is_null($res = $this->ci->internal_roles->create_role($data))) {
        $ret[] = $data;
    }
$data = array(
            'name'  => $rolename,
            'key'   => $rolekey.'2'
        );
        if (!is_null($res = $this->ci->internal_roles->create_role($data))) {
            $ret[] = $data;
        }
$data = array(
            'name'  => $rolename,
            'key'   => $rolekey.'3'
        );
        if (!is_null($res = $this->ci->internal_roles->create_role($data))) {
            $ret[] = $data;
        }

        return $ret;
    }

If you give more detail in your question, I may be able to give you a better answer.

Upvotes: 1

Related Questions