0

so, i have this

Array
(
    [ModuleCode] => Array
        (
            [0] => MD001
            [1] => MD002
            [2] => MD004
            [3] => MD005
        )

    [MD001] => Array
        (
            [insert] => on
            [edit] => on
            [delete] => on
        )

    [MD002] => Array
        (
            [insert] => on
            [edit] => on
            [delete] => on
        )

    [MD005] => Array
        (
            [insert] => on
            [edit] => on
            [delete] => on
            [access_edit] => on
        )

)

as you can see there are an array with ModuleCode as key. After some try i can get this

MD001
insert => 1
edit => 1
delete => 1
access_edit => 0 

MD002
insert => 1
edit => 1
delete => 1
access_edit => 0

MD004
insert => 0
edit => 0
delete => 0
access_edit => 0

MD005
insert => 1
edit => 1
delete => 1
access_edit => 1

with this script

$dataModul = $this->input->post('ModuleCode');
        $field = array ("insert","edit","delete","access_edit");
        for($x=0;$x<count($dataModul);$x++){
            echo "<pre>".$dataModul[$x] . "<br>";
                for($a=0;$a<count($field);$a++){
                    $subcheck = (isset($this->input->post($dataModul[$x])[$field[$a]])) ? 1 : 0;
                    echo $field[$a]. " => " . $subcheck . "<br>" ;
                }
            echo "<pre>";
        }

Ok, here is what i want to achieve . from this part (for an example)

 MD001
 insert => 1
 edit => 1
 delete => 1
 access_edit => 0 

i want to make something like this

Update TableName set insert = 1, edit = 1, delete = 1 , access_edit = 0 where ModuleCode = 'MD001'

How can i achieve that ? thanks in advance

YVS1102
  • 2,658
  • 5
  • 34
  • 63

2 Answers2

0

You can try this code:

You can call use the function as echo generate_query_string('MD004', $modules); where the first parameter is the module code and the second the whole array.

<?php

function generate_query_string( $module, $module_arr ) {

    if( !isset( $module_arr[$module] ) ) { return false; } // return false if module does not exist

    // set default values
    $defaults = array(
        'insert' => 0,
        'edit' => 0,
        'delete' => 0,
        'access_edit' => 0
    );

    $settings = array_merge( $defaults, $module_arr[$module] ); // merge default values and the actual values
    $settings = array_filter( $settings );  // remove items with 0 value since we don't need to include them on the query string

    // render the query string values
    $values = [];   
    foreach ($settings as $key => $value) {
        $value = ( $value == 'on' )? 1 : 0;
        $values[] = $key. ' = '. $value;
    }
    $values = implode(', ', $values); 

    return 'Update TableName set '. $values .' where ModuleCode = '. $module;
}

?>
Paul Janubas
  • 136
  • 4
0

I found the solustion. Here is what i do

First. i add a custom function ( ref : https://stackoverflow.com/a/42052020/6354277 )

    function custom_function($input_array)
        {
            $output_array = array();
            for ($i = 0; $i < count($input_array); $i++) {
                for ($j = 0; $j < count($input_array[$i]); $j++) {
                    $output_array[key($input_array[$i])] = $input_array[$i][key($input_array[$i])];
                }
            }
            return $output_array; 
}

then i change my code to this.

function updateaccess(){
    $dataModul = $this->input->post('ModuleCode');
    $field = array ("insert","edit","delete","access_edit");
    for($x=0;$x<count($dataModul);$x++){
            for($a=0;$a<count($field);$a++){
                $subcheck[$a] = (isset($this->input->post($dataModul[$x])[$field[$a]])) ? 1 : 0;
                $mynewarray[$dataModul[$x]][] = array($field[$a] => $subcheck[$a]);
            }

        foreach ($mynewarray as $key => $value) {
                  $forSave[$dataModul[$x]] = $this->custom_function($value);
        }
    }

    foreach ($forSave as $key2 => $values2) {
            $this->mainmodel->updateRow(array("ModuleCode" => $key2),"user_modules", $values2 );
    }
}
YVS1102
  • 2,658
  • 5
  • 34
  • 63