Welcome Guest, Not a member yet? Register   Sign In
Parsing arrays with arrays
#1

[eluser]_Smilie_[/eluser]
Hi,
So I'm trying to parse an Array to my view file, and that array contains another array.

Controller:
Code:
function view(){
  $form_id = mysql_real_escape_string($this->uri->segment(3));
  $row = $this->db
    ->where('quest_form =', $form_id) //don't really need the =, that is the default
    ->get('form_question')
    ->row();
   $data['question_list'] = array();
   foreach($row as $row2){
    array_push($data['question_list'], $this->convertQuest($row2->quest_type));
    //$data['question_list'] array($this->convertQuest($row2->quest_type));
   }
  
  $this->load->view('global/header');
  $this->load->view('global/userinfo');
  $this->load->view('form_view', $data);
}

function convertQuest($quest_type){
  if($quest_type == 1){
   return '
     <div class="mws-form-row">
                        <label>Title</label>
                         <div class="mws-form-item large">
                             &lt;input type="text" name="host_title" class="mws-textinput required" /&gt;
                      </div>
                    </div>';
  }
}

My view:
Code:
&lt;?php foreach ($question_list as $question):
                              echo $question;
                              endforeach;?&gt;

Alright, so this produces a total of 3 errors:

Code:
Message: Trying to get property of non-object

Filename: controllers/form.php

Line Number: 19

Line 19:
array_push($data['question_list'], $this->convertQuest($row2->quest_type));

Dunno why it produces 3, as I only have one entry in my database.. Also I don't know why this error occurs at all ^^
#2

[eluser]Aken[/eluser]
You're ending your database query with row(), which is returning a single row object, not an array of results. So when you are doing foreach ($row as $row2), you are looping the properties of that object, not an array of data.




Theme © iAndrew 2016 - Forum software by © MyBB