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

2
  • Sorry, but your question is so unclear.. convert array to jquery php?? did you mean convert an array from php to json? Commented Aug 6, 2018 at 2:42
  • @PaulJanubas my bad, i have edit my question . I hope you get it. Commented Aug 6, 2018 at 2:48

2 Answers 2

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

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

2 Comments

thanks, but what is the example values of $modules ?
hey sorry, $module value would be example: MD005 and the module_arr would be the whole array
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 );
    }
}

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.