CodeIgniter Forums
Help me.. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Help me.. (/thread-64023.html)

Pages: 1 2


Help me.. - ngangchill - 01-04-2016

someone help me.. i just stucked and cant get any idea how too do it.


Source array:
 

     ["data"] => array(10) {
         ["type"] => string(10) "controller"
         ["name"] => string(3) "eg1"
         ["description"] => string(5) "desc1"
         ["type2"] => string(8) "function"
         ["name2"] => string(2) "c2"
         ["controller2"] => string(4) "welcome"
         ["description2"] => string(6) "desc 2"
         ["type3"] => string(10) "controller"
         ["name3"] => string(2) "c3"
         ["description3"] => string(8) "c3 descr"
       }


from the above array how can i get new array like:

      array(
        array(
          'type' => 'controller',
          'name' => 'eg1',
          'description' => 'desc1'
        ),
         array(
          'type' => 'function',
          'controller' => 'welcome',
          'name' => 'c2',
          'description' => 'desc 2'
        ),
        array(
          'type' => 'controller',
          'name' => 'eg1',
          'description' => 'desc1'
        ),
        array(
          'type' => 'controller',
          'name' => 'c3',
          'description' => 'c3 descr'
        )
      );


data is submitted through a https://github.com/tristandenyer/Clone-section-of-form-using-jQuery


thanks for help Shy


RE: Help me.. - Diederik - 01-04-2016

Why not use array input fileds like <input type="text" name="type[]" />? Then you will get post data wich hold 4 arrays, one for each fieldname (type,name etc).
The number of entries in those arrays is the same as the amount of times the form hs been cloned and the keys between the arrays will corresponed so you can link them easely to create the array type you need.


RE: Help me.. - Aendawan - 01-04-2016

(01-04-2016, 03:52 PM)Diederik Wrote: Why not use array input fileds like <input type="text" name="type[]" />? Then you will get post data wich hold 4 arrays, one for each fieldname (type,name etc).
The number of entries in those arrays is the same as the amount of times the form hs been cloned and the keys between the arrays will corresponed so you can link them easely to create the array type you need.

Do you have an exemple for me Smile


RE: Help me.. - PaulD - 01-05-2016

Or loop through your data in php. Use a counter to add the 1,2,3 to the end of the default type, name, description variables, and create your array from the data you have.
PHP Code:
<?php
     $source_data 
= array (
 
         "type1" => "controller",
 
         "name1" => "eg1",
 
         "description1" => "desc1",
 
         "type2" => "function",
 
         "name2" => "c2",
 
         "description2" => "desc 2",
 
         "type3" => "controller",
 
         "name3" => "c3",
 
         "description3" => "c3 descr",
 
    );

 
    $count 1;
 
    $checking TRUE;
 
    $output_data = array();
 
    
     
while ($checking)
 
        
          $temp_data 
= array();
 
         
          if 
(isset($source_data['type'.$count]))
 
         {
 
              $temp_data['type'] = $source_data['type'.$count];     
          
         
          if 
(isset($source_data['name'.$count]))
 
         {
 
              $temp_data['name'] = $source_data['name'.$count];
 
                  
          if 
(isset($source_data['description'.$count]))
 
         {
 
              $temp_data['description'] = $source_data['description'.$count];
 
         }
 
         
          if 
(count($temp_data) > 0)
 
         {
 
              $output_data[] = $temp_data;
 
         }
 
         else
          
{
 
              $checking FALSE;
 
         }
 
         
          
++$count;
 
         
     
}
 
    
     
echo print_r($output_data);
 
    
?>



RE: Help me.. - mr_pablo - 01-05-2016

(01-05-2016, 06:50 AM)PaulD Wrote: Or loop through your data in php. Use a counter to add the 1,2,3 to the end of the default type, name, description variables, and create your array from the data you have.
PHP Code:
<?php
     $source_data 
= array (
 
         "type1" => "controller",
 
         "name1" => "eg1",
 
         "description1" => "desc1",
 
         "type2" => "function",
 
         "name2" => "c2",
 
         "description2" => "desc 2",
 
         "type3" => "controller",
 
         "name3" => "c3",
 
         "description3" => "c3 descr",
 
    );

 
    $count 1;
 
    $checking TRUE;
 
    $output_data = array();
 
    
     
while ($checking)
 
        
          $temp_data 
= array();
 
         
          if 
(isset($source_data['type'.$count]))
 
         {
 
              $temp_data['type'] = $source_data['type'.$count];     
          
         
          if 
(isset($source_data['name'.$count]))
 
         {
 
              $temp_data['name'] = $source_data['name'.$count];
 
                  
          if 
(isset($source_data['description'.$count]))
 
         {
 
              $temp_data['description'] = $source_data['description'.$count];
 
         }
 
         
          if 
(count($temp_data) > 0)
 
         {
 
              $output_data[] = $temp_data;
 
         }
 
         else
          
{
 
              $checking FALSE;
 
         }
 
         
          
++$count;
 
         
     
}
 
    
     
echo print_r($output_data);
 
    
?>

Sorry but this is a horrible idea.


RE: Help me.. - PaulD - 01-05-2016

Why? It turns his array into the array he wanted?

I was just trying to help... but out of interest, why is it horrible?

Paul.


RE: Help me.. - mr_pablo - 01-05-2016

(01-05-2016, 08:35 AM)PaulD Wrote: Why? It turns his array into the array he wanted?

I was just trying to help... but out of interest, why is it horrible?

Paul.

It's a lot of code for something can that be fixed by using POST arrays.


RE: Help me.. - ngangchill - 01-05-2016

(01-05-2016, 06:50 AM)PaulD Wrote: Or loop through your data in php. Use a counter to add the 1,2,3 to the end of the default type, name, description variables, and create your array from the data you have.
PHP Code:
<?php
     $source_data 
= array (
 
         "type1" => "controller",
 
         "name1" => "eg1",
 
         "description1" => "desc1",
 
         "type2" => "function",
 
         "name2" => "c2",
 
         "description2" => "desc 2",
 
         "type3" => "controller",
 
         "name3" => "c3",
 
         "description3" => "c3 descr",
 
    );

 
    $count 1;
 
    $checking TRUE;
 
    $output_data = array();
 
    
     
while ($checking)
 
        
          $temp_data 
= array();
 
         
          if 
(isset($source_data['type'.$count]))
 
         {
 
              $temp_data['type'] = $source_data['type'.$count];     
          
         
          if 
(isset($source_data['name'.$count]))
 
         {
 
              $temp_data['name'] = $source_data['name'.$count];
 
                  
          if 
(isset($source_data['description'.$count]))
 
         {
 
              $temp_data['description'] = $source_data['description'.$count];
 
         }
 
         
          if 
(count($temp_data) > 0)
 
         {
 
              $output_data[] = $temp_data;
 
         }
 
         else
          
{
 
              $checking FALSE;
 
         }
 
         
          
++$count;
 
         
     
}
 
    
     
echo print_r($output_data);
 
    
?>

many many thanks.....


RE: Help me.. - PaulD - 01-05-2016

Ah, ok, it is, and I would not have a clunky solution like that in my code.

But I got the impression from the question that he was using an external system to generate the array, that I guessed he was having trouble with as Diederik's answer was good.

So, if you have a messy array, and need it in another format, and was not sure how to do it, or alter how you are getting the array in the first place, I think my answer is a good possible solution for the problem described.

Yes it is a lot of code, with flags, little error checking etc. But if a rejig of an array gets you out of a tricky corner how else can you do it.

Anyway, I think 'horrible' made me think you thought I had done something wrong in the code. Perhaps next time you call someones code 'horrible' you could either temper your vocabulary or add some qualification to it.

As I said, was just offering an answer to someone who said they were stuck, in the hope of helping, not proposing super efficient power coding as an entry into a coding competition.

Best wishes,

Paul :-(


RE: Help me.. - PaulD - 01-05-2016

(01-05-2016, 08:49 AM)ngangchill Wrote: many many thanks.....

You are welcome :-)