CodeIgniter Forums
$idx not incrementing? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: $idx not incrementing? (/showthread.php?tid=64921)



$idx not incrementing? - iridion2015 - 04-11-2016

Hi guys,

I have two buttons on the form 'yes' or 'no' button.

now when these button are clicked it will call the show_qa() methods below,



Code:
   public function show_qa()
   {
       $content = $this->session->userdata('content_data');
       #showArray($content);    
       
       //Get language, version and topic
       $language = $content['subject'];
       $version = $content['version'];
       $topic = $this->uri->segment(3, 0);
       $array_init = $this->uri->segment(4, 0);
       
       //Begin initializing array...
       //do this once only.
       if($array_init == 'init') {
           $this->set_array_container($language, $version, $topic);       //do only once
           reset($this->array_container); //set the array pointer in the very first index which is zero.
           $idx = key($this->array_container);  
           $this->save_idx_position($idx);    
       }
       
       //Begin catch yes or no button
       if (isset($_POST['yes']) || isset($_POST['no'])) {
               #$this->array_container = $this->fetch_array_from_session();
           
               $last_idx = $this->get_last_idx_key();
               $idx = $this->get_idx_position();
               
               if($idx < $last_idx) {
                   $idx++;
               }
               
               $this->save_idx_position($idx);    
               
               $idx = $this->get_idx_position();
           }
       //End catch yes or no button

       showArray($this->array_container);
       echo '<b>Index position: '. $idx . '</b></br></br>';              
       
       //replace blank space with underscore
       $topic_underscore = str_ireplace(" ", "_", $topic);
       $this->session->set_userdata('topic', $topic_underscore);    

       #get current url and store to session
       $segments = array('language', 'show_qa', $topic_underscore);
       $qa_url = site_url($segments);        
       $this->session->set_userdata('qa_url', $qa_url);    
       
       $this->qa_ids = $this->qa_mdl->get_qa_ids($language, $version, $topic);
       
       #echo '<br>value: '. $current_idx->qa_id;
       
       $this->dryview2($header='templates/header', $msg='Welcome back ', $menu='templates/menu', $content='templates/landing_qa', $footer='templates/footer', $master='templates/landing_master');
   }

when you click either 'yes' or 'no' for the first time it increments the $idx from 0 to 1.
and when you click again those 'yes' or 'no' buttons the 2nd, 3rd, 4th etc... times...
It will not increments anymore.

Any suggestions, please?

Thanks in advance.


RE: $idx not incrementing? - iridion2015 - 04-11-2016

Okay no worries guys,
I already solved this.

here is the modified codes,

if($array_init === 'init') {
$this->set_array_container($language, $version, $topic); //do only once
reset($this->array_container); //set the array pointer in the very first index which is zero.
$idx = key($this->array_container);
$this->save_idx_position($idx);
echo '<br>Array_init: '. $array_init;
}