CodeIgniter Forums
Message: reset() expects parameter 1 to be array, null given - 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: Message: reset() expects parameter 1 to be array, null given (/thread-68269.html)



Message: reset() expects parameter 1 to be array, null given - iridion2015 - 06-17-2017

Hi guys

I'm having this error below.
Code:
A PHP Error was encountered
Severity: Warning

Message: reset() expects parameter 1 to be array, null given

Filename: controllers/Language.php

Line Number: 89

Backtrace:

File: C:\xampp\htdocs\ci\ci3-codes-mastery4\application\controllers\Language.php
Line: 89
Function: reset

File: C:\xampp\htdocs\ci\ci3-codes-mastery4\index.php
Line: 315
Function: require_once

and the codes of language.php are these below,

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Language extends MY_Controller
{
   public $current_id = array();
   public $array_container = array();
   public $total_array = 0;
   public $result_msg = '';
   
   function __construct()
   {
       parent::__construct();
       $this->load->model('qa_mdl');
       $this->load->model('subject_mdl');
       $this->load->helper('array');
   }

   /***
   *  author: warren nazareno
   *  email: [email protected]
   *  purpose:
   *  Date created:
   ***/        
   public function index()
   {
       echo 'Index...';
   }    
   
   public function suggestion() {
       $this->dryview2($header='templates/header', $msg='Welcome back ', $menu='templates/menu', $content='forms/suggestion_form', $footer='templates/footer', $master='templates/landing_master');
   }    
   
   public function landing()
   {
       $language = $this->uri->segment(3, 0);
       $version = $this->subject_mdl->get_version($language);
       $this->set_button_clicks(); //initialize button clicks value to zeros
       
       $total_basic        = $this->qa_mdl->get_total_questions($language, $version, $topic="basic");
       $total_oop          = $this->qa_mdl->get_total_questions($language, $version, $topic="oop");
       $total_regex        = $this->qa_mdl->get_total_questions($language, $version, $topic="regex");
       $total_functions    = $this->qa_mdl->get_total_questions($language, $version, $topic="built-in functions");
       $total_patterns     = $this->qa_mdl->get_total_questions($language, $version, $topic="design patterns");
       $total_advance      = $this->qa_mdl->get_total_questions($language, $version, $topic="advance");
       $total_general      = $this->qa_mdl->get_total_questions($language, $version, $topic="general");
       $total_libraries    = $this->qa_mdl->get_total_questions($language, $version, $topic="libraries");
       $total_helper       = $this->qa_mdl->get_total_questions($language, $version, $topic="helpers");
       $total_database     = $this->qa_mdl->get_total_questions($language, $version, $topic="database");
       
       
       $content_data = array(
           "subject"           => $language,
           "version"           => $version,
           "total_basic"       => $total_basic,
           "total_oop"         => $total_oop,
           "total_regex"       => $total_regex,
           "total_functions"   => $total_functions,
           "total_patterns"    => $total_patterns,
           "total_advance"     => $total_advance,
           "total_general"     => $total_general,
           "total_libraries"   => $total_libraries,
           "total_helper"      => $total_helper,
           "total_database" => $total_database,
       );    
       
       $this->session->set_userdata('content_data', $content_data);
       
       $this->dryview2($header='templates/header', $msg='Welcome back ', $menu='templates/menu', $content='templates/landing_content', $footer='templates/footer', $master='templates/landing_master');
   }
   
   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);
       $topic2 = str_ireplace("_", " ", $topic);
       
       $this->session->set_userdata('init', $array_init);    
       
       //Begin initializing array...
       //do this once only.
       if($array_init === 'init') {
           $this->set_array_container($language, $version, $topic2);       //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);
           $condition = 'no';
           $this->save_end_of_array('no');
           $this->total_array = count($this->array_container);
           $this->session->set_userdata('total_array', $this->total_array);
           $this->set_button_clicks(); //initialize button clicks value to zeros

       }
       
       $this->total_array = $this->session->userdata('total_array');
       
       //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_end_of_array('no');

               }
               
               if($idx === $last_idx) {
                   $this->save_end_of_array('yes');
               }
               
               $this->save_idx_position($idx);  
             
               $idx = $this->get_idx_position();
               $condition = $this->get_end_of_array();
           }
       //End catch yes or no button
     
       //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);    

       #showArray($this->array_container);
       /*
       echo 'Index position: '. $idx;                      
       echo '<br>Topic: '. $topic;
       echo '<br>QA_ID: '. $this->array_container[$idx]->qa_id;
       echo '<br> End of Array: '. $condition;
       echo '<br>End of Array Segment: '. $array_init;
       echo '<br>Total array: '. $this->total_array .'<br>';
        */
       
       
       if(isset($_POST['yes']))
       {
           $this->record_button_clicks($button='yes');
       }
       
       if(isset($_POST['no']))
       {
           $this->record_button_clicks($button='no');
       }
       
       $button_clicks = $this->get_button_clicks();
       /*
       echo '<br><br><Br>';
       echo 'Button clicked - yes: '. $button_clicks['yes'];
       echo '<br>Button clicked - no: '. $button_clicks['no'];
        /*
        */
           
       $total_clicks = $button_clicks['yes'] + $button_clicks['no'];
       
       if($this->total_array === $total_clicks)
       {
           if($this->total_array === $button_clicks['yes'])
           {
               $this->result_msg = '<br>If that was an honest answers congrats!<br> Perfect answer.';
           }
           else
           {
               $this->result_msg = 'S C O R E  <br> Right Answer: '. $button_clicks['yes'] .' <br> Wrong Answer: '. $button_clicks['no'];
           }
       }
       
       $this->session->set_userdata('result_msg', $this->result_msg);
       
       //Get question, answer, reference through an array key.
       $qa_data = $this->qa_mdl->get_data_by_id($this->array_container[$idx]->qa_id);
       $qa_data['yes'] = $button_clicks['yes'];
       $qa_data['no'] = $button_clicks['no'];
       $this->session->set_userdata('qa_data', $qa_data);

       
       $this->dryview2($header='templates/header', $msg='Welcome back ', $menu='templates/menu', $content='templates/landing_qa', $footer='templates/footer', $master='templates/landing_master2');
   }
   
/**************************************************
* PHP Array Driver methods
**************************************************/    
   
   //define the array_container once.
   public function set_array_container($language, $version, $topic)
   {
       $this->array_container = $this->qa_mdl->get_qa_ids($language, $version, $topic);
       $this->save_array_2_session();
       return;
       #showArray($this->fetch_array_from_session());
   }
   
   //save array_container to session
   public function save_array_2_session()
   {
       $this->session->set_userdata('array_set', $this->array_container);
       return;
   }
   
   //retrieve array_container from session
   public function fetch_array_from_session()
   {
       return $this->session->userdata('array_set');
   }
   
   //Count all elements in an array, or something in an object
   public function a_count($array_name)
   {
       $result = count($array_name);
       //return $result;
       echo 'total indexes'. $result;
   }
   
   #get the last index key
   public function get_last_idx_key()
   {
       end($this->array_container);
       return key($this->array_container);          
   }
   
   //save index position into session
   public function save_idx_position($idx)
   {
       $this->session->set_userdata('idx_key', $idx);
       return;
   }
   
   //retrieve index position from session
   public function get_idx_position()
   {
       $idx_key = $this->session->userdata('idx_key');
       return $idx_key;
   }    
   
   //save counter position into session
   public function save_end_of_array($condition)
   {
       $this->session->set_userdata('condition', $condition);
       return;
   }
   
   //retrieve counter position from session
   public function get_end_of_array()
   {
       $condition = $this->session->userdata('condition');
       return $condition;
   }    
   
   //Initialize button clicks value to zeros
   public function set_button_clicks()
   {
       $button_clicks = array(
               'yes'  => 0,
               'no'     => 0,
       );        
       
       $this->session->set_userdata('button_clicks', $button_clicks);
   }
   
   //record button clicks.
   public function record_button_clicks($button)
   {
       //get button clicks from session first.
       $button_clicks = $this->get_button_clicks();
       
       //determine what button was clicked.
       if($button === 'yes')
       {
           $button_clicks['yes'] += 1;
       }
       
       if($button === 'no')
       {
           $button_clicks['no'] += 1;
       }
       
       //save click to array as session
       $this->session->set_userdata('button_clicks', $button_clicks);
   }
   
   //ret nittpm c;ocls.
   public function get_button_clicks()
   {
       return $this->session->userdata('button_clicks');
   }
   
/**************************************************
* These methods below are for testing purpose only
**************************************************/
   public function ver()
   {
       echo CI_VERSION;
   }
   
   public function replace_word()
   {
       $topic = 'built-in functions';
       $word_replaced = str_ireplace(" ", "_", $topic);
       echo $word_replaced;
   }
   
   //Will get the cuurrent Q&A in an array
   public function current_qa($current_array, $current_pointer)
   {
       //Get the current array
       //Get the current pointer (beginning)
       //Store the $current_pointer into session
       //if the user click the next button, pointer will also cal next() method
       //then store the $current_pointer into session again.
       //do this until the end of the array index/value
       
   }
   
   public function dialog()
   {
       
       $this->load->view('dialog');
   }
   


}




Thanks in advnce


RE: Message: reset() expects parameter 1 to be array, null given - iridion2015 - 06-17-2017

by the way I'm using version 3.0.6

I hope someone will help me.


RE: Message: reset() expects parameter 1 to be array, null given - InsiteFX - 06-18-2017

It's saying that the $this->array_container is null and does not have a value assigned to it.

echo out the array_container or var_dump it to see if it is null.


RE: Message: reset() expects parameter 1 to be array, null given - iridion2015 - 06-18-2017

thanks will try to fix it.


RE: Message: reset() expects parameter 1 to be array, null given - iridion2015 - 06-18-2017

Okay, it's NULL.

I don't understand it, even though $array_container is already declared as an array.
Anyone would, like to help?

Thanks in advance.


RE: Message: reset() expects parameter 1 to be array, null given - pdthinh - 06-18-2017

(06-18-2017, 04:36 PM)iridion2015 Wrote: Okay, it's NULL.

I don't understand it, even though $array_container is already declared as an array.
Anyone would, like to help?

Thanks in advance.

PHP is a loosely type language, so type of a variable can be changed if you want.
You should check set_array_container() method to see if $array_container is set correctly.