Tagged: ,

Viewing 7 reply threads
  • Author
    Posts
    • #1905
      egnsh98
      Member

      Hello,

      I know this is more of a general PHP question than it is Piklist, but I figured I would post it here anyways.

      The Setup

      Example settings setup

      What I am trying to do

      On each iteration, grab the value of each field for that particular group. For example the first group box:

      • Category => Anime
      • Time => 01:00
      • Format => woot

      and so on…

      Actual Behaviour

      On each iteration, the same fields in the different group boxes are being stored together, rather than apart. I’m posting the formatted output so you can see what I mean:

      a:8:{
      
      	s:11:"group";
      
      	a:3:{
      
      		s:9:"cat";
      
      		a:2:{
      
      			i:0;
      			s:4:"3722";
      
      			i:1;
      			s:2:"20";
      
      		}
      
      		s:10:"time";
      
      		a:2:{
      
      			i:0;
      			s:1:"1";
      
      			i:1;
      			s:1:"2";
      
      		}
      
      		s:12:"output";
      
      		a:2:{
      
      			i:0;
      			s:4:"woot";
      
      			i:1;
      			s:5:"hello";
      
      		}
      
      	}
      
      }

      And this is how I am trying to retrieve them…

      // Loop through options
      foreach ( $options as $field_group ) {
      
          // Check type
          if ( is_array( $field_group ) ) {
      
              // Loop through field groups
              foreach ( $field_group as $field_arr ) {
                  
                  // Check type
                  if ( is_array( $field_arr ) ) {
                      
                      // Loop through fields
                      foreach ( $field_arr as $field ) {
                          
                          // Print results
                          echo $field;
      
                      }
      
                  }
      
              }
      
          }
      
      }

      The output from my above loops looks like this:

      37222012woothello

      I need to access and store each field from the first group before moving onto the second group.

      Would anyone be able to give me some advice on how to go about this?

      Thanks

    • #1906
      Steve
      Keymaster

      Due to the nature of the Add-mores and how powerful they are, we had to use an unconventional array structure. But don’t worry, Piklist helps you get the data out.

      Check out the docs on Add-More’s. It’s explained in detail.

      Let us know if you still need help.

    • #1907
      egnsh98
      Member

      Should have checked that out first. Thanks a lot for the reference, I’ll have a look over it now 🙂

    • #1908
      egnsh98
      Member

      Steve,

      I’ve managed to get the values outputting on the screen for test purposes as I had wanted. Only issue is the values are being output in the add_more template file, not in the main class file that I am working with. How should I access those variables, will I need to create getters and setters to move that data between files or is there a better way?

      The structure for this data in my main class file looks something like this:

      // Get the schedule group information
      $schedule_group = $options['sched_group'];
      
      // 1. Display the data using the file schedule_template.php.
      // 2. Assign your $sched_cat parameter (from above) to 'data'.
      // 3. Loop through 'data'.
      
      piklist('schedule_template', array('data' => $schedule_group, 'loop' => 'data'));
      
      $sched_category_id = $data['sched_cat'];
      $sched_timestamp = $data['sched_time'];
      $sched_format = $data['sched_format'];
      
      var_dump ( $sched_category_id . $sched_timestamp . $sched_format );

      Regards.

    • #1909
      Steve
      Keymaster

      Working with multi-dimensional arrays is never fun, that’s why we made it easy to output data. However, if you want to access the variables and work with them you are going to have to create your own methods.

      The array is structure so all the data is in order. All key [1] is related to the same add-more for example.

    • #1910
      egnsh98
      Member

      Okay thanks. Would I be better off going my initial route when I first opened this topic, or can I still use the piklist functions to help breakdown the array? I have trouble wrapping my head around some of this stuff.

    • #1911
      Steve
      Keymaster

      Since it’s only a single level add-more it might be easier to just loop through it the way you originally suggested.

    • #1951
      egnsh98
      Member

      You are right, it was much easier. This is resolved now.

Viewing 7 reply threads
  • The topic ‘Settings Array’ is closed to new replies.