1

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I just hope the OP gives you more details :)
0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.