Welcome Guest, Not a member yet? Register   Sign In
Parse error: syntax error, unexpected $end caused by foreach in view
#1

[eluser]swgj19[/eluser]
Codeigniter Vs. 2.0

Oh right. I basically queried my database in my model. Then in my controller I called my model. And then I called the string as $row in a foreach in my view. I am getting that basic old error and I cannot figure out why. I commented out the foreach on my view and the error goes away. So I know where the error is caused, but cannot figure out how to keep my foreach loop in my view without the error. Thanks guys..

Here is the full error:
Parse error: syntax error, unexpected $end in /home/content/70/6930870/html/tt/application/views/logged_in_area.php on line 28

Here is my model:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Membership_model extends CI_Model {


function validate()
{
  $this->db->where('username', $this->input->post('username'));  //validate username
  $this->db->where('password', md5($this->input->post('password'))); //validate password
  $query = $this->db->get('membership');        //get db table
  
  if($query->num_rows == 1)           //Make sure data in table exist
  {
   return true;             //if data in table, then return results
  }
}
function create_member()
  {
  $new_member_insert_data = array(         //if validation passed (line 57 controller/login.php), post array data to db
   'first_name' => $this->input->post('first_name'),
   'last_name' => $this->input->post('last_name'),
   'email_address' => $this->input->post('email_address'),
   'username' => $this->input->post('username'),
   'password' => md5($this->input->post('password'))  //make sure password is md5 or same in db
   );
  
   $insert = $this->db->insert('membership', $new_member_insert_data); //insert (insert is new_member_insert_data array) into membership table
   return $insert;
  }
  function retrieve()
   {
    $q = $this->db->get('membership');
    $this->db->where('email_address', $this->input->post('email_address'));  //validate username
    if($q->num_rows() > 0) {
     foreach($q->result() as $row) {
      $data[] = $row;
     }
     return $data;
    }
  

    
  }

}


/*if($q->num_rows() > 0) {
     foreach ($q->result() as $row) {
      $data[] = $row->row;    
    }
    return $data;
   }*/

Here is my controller
Code:
<?php

class Site extends CI_Controller
{
function __construct()
{
  parent::__construct();
  $this->is_logged_in();
}

function members_area()
{
  $this->load->model('Membership_model');
  $data['records'] = $this->Membership_model->retrieve();
  
  //$data['t'] = $this->db->get('membership','first_name');    //Need to have result before converting to a string.
  $this->load->view('logged_in_area', $data);
  
  
}
function account()
{
  redirect('site/members_area');
}

function another_page() // just for sample
{
  echo 'good. you\'re logged in.';
}

function is_logged_in()
{
  $is_logged_in = $this->session->userdata('is_logged_in');
  if(!isset($is_logged_in) || $is_logged_in != true)
  {
   echo 'You don\'t have permission to access this page.';
   echo anchor('login/index', 'Login');
   die();  
   //$this->load->view('login_form');
  }  
}

}

Here is my view
Code:
<!DOCTYPE html>

&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;title&gt;untitled&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<h2>Welcome Back, &lt;?php echo $this->session->userdata('username'); ?&gt;!</h2>
     <p>If you are a member you can stay. If not, then get out!</p>
    
<h4>
  &lt;?php echo anchor('site/members_area','Profile');?&gt; |
  &lt;?php echo anchor('login/logout', 'Logout');?&gt; |
  &lt;?php echo anchor('site/account', 'Account');?&gt;
  
</h4>



&lt;?php
foreach($records as $row):
  echo $row->first_name;

  ?&gt;

&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]InsiteFX[/eluser]
Missing endforeach.
Code:
&lt;?php
foreach($records as $row):
  echo $row->first_name;

  endforeach;
?&gt;

Example:
Code:
&lt;!-- Xajax pagination Table --&gt;
<div id="xajax-grid">

<table border='2' cellpadding='0' cellspacing='0' id='paged-table'>

  <thead>
   <tr>
    <th align="left">ID</th>
    <th align="left">Name</th>
   </tr>
  </thead>

  <tbody>
   &lt;?php $i = 0; ?&gt;
   &lt;?php foreach ($grid_list as $item): ?&gt;
    <tr class="&lt;?php echo ($i % 2 == 0) ? "even" : "odd"; ?&gt;">
     <td>&lt;?php echo $item->id; ?&gt;</td>
     <td>&lt;?php echo $item->name; ?&gt;</td>
     &lt;!--<td> any function like edit, delete, etc </td>--&gt;
    </tr>
    &lt;?php $i++; ?&gt;
   &lt;?php endforeach; ?&gt;
  </tbody>

</table>

<br />

&lt;!-- Create the Xajax Pagination Links... --&gt;
&lt;?php echo $pager_links; ?&gt;

</div>
&lt;!-- End of Xajax pagination! --&gt;
#3

[eluser]swgj19[/eluser]
Thanks Ray. That was a simple mistake. I am a little rusty.

I have a couple more questions. I am trying to break each query result into $row->field_name so I can echo the first name, last name, and email in the user's profile seperately. The way I have it set up now in my query returns every first name from my membership table. So could I do this without a foreach loop?
Also, How would I change my query in my model to retrieve only the first name of the user's session, who is logged in in my members area?
#4

[eluser]InsiteFX[/eluser]
1) first name, last name and email
Code:
function retrieve()
   {
    $this->db->where('email_address', $this->input->post('email_address'));  //validate username
    $q = $this->db->get('membership');

    if($q->num_rows() > 0) {
      // return a single db row array $row['name'];
      return $q->row_array();
      // return a single db object $row->name;
      // return $q->row;      
     }
     return FALSE;
    }
  }

2) Using the query above you already have the first_name
Code:
$data = $this->model_name->retrive();
// array
$first_name = $data['first_name'];
// object
$first_name = $data->first_name;
#5

[eluser]swgj19[/eluser]
Now I am getting each string from the controller and my first and last name our showing, but I am getting an error: Trying to get property of non-object. I am getting this error for every string I echo in my view which is currently the $first_name and $last_name,


Code:
function members_area()
{
  $this->load->model('Membership_model');
  $data = $this->Membership_model->retrieve();
  $first_name = $data['first_name'];
  $first_name = $data->first_name;
  $last_name = $data['last_name'];
  $last_name = $data->last_name;
  //$data['t'] = $this->db->get('membership','first_name');    //Need to have result before converting to a string.
  $this->load->view('logged_in_area', $data);

Code:
function retrieve()
   {
    $this->db->where('email_address', $this->input->post('email_address'));  //validate username
    $q = $this->db->get('membership');

    if($q->num_rows() > 0) {
      // return a single db row array $row['name'];
      return $q->row_array();
      // return a single db object $row->name;
      // return $q->row;      
     }
     return FALSE;

  
    
  }

}
#6

[eluser]InsiteFX[/eluser]
Code:
function members_area()
{
  $this->load->model('Membership_model');
  $data = $this->Membership_model->retrieve();
  $this->load->view('logged_in_area', $data);

Just pass the $data to your view then echo
Code:
&lt;?php echo $first_name; ?&gt;
#7

[eluser]swgj19[/eluser]
That is exactly what I did and I still get the error.
I am stating the $first_name etc. in my controller then passing $data in my view in my controller. Then I am simply echoing the $first_name in the view. The queried data passed to the string in the controller is showing in the view. So on my page I am seeing the first name and last name from the database, but with the above error for every string echoed in the view.
#8

[eluser]weboap[/eluser]
what @InsiteFX meaned
take off this part from your controller
Code:
$first_name = $data['first_name'];
  $first_name = $data->first_name;
  $last_name = $data['last_name'];
  $last_name = $data->last_name;

and leave the function just like
Code:
//load model
  $this->load->model('Membership_model');
//pull data
  $data = $this->Membership_model->retrieve();
//load the view and pass the data to it
  $this->load->view('logged_in_area', $data);

then in your view
Code:
&lt;?php echo $first_name; ?&gt;
#9

[eluser]InsiteFX[/eluser]
That sounds werid,

Show me your controller, model and view code that you have now!

If you did not change this it should be
Code:
function members_area()
{
  $this->load->model('Membership_model');
  $data = $this->Membership_model->retrieve();
  
  //$data['t'] = $this->db->get('membership','first_name');    //Need to have result before converting to a string.
  $this->load->view('logged_in_area', $data);
  
  
}
#10

[eluser]swgj19[/eluser]
Thanks for the clarification. I did what ya said and it worked. Thanks

Now this is returning the first row in the table for first name and last name. How would I return the table and row for corresponding logged in session. If you look at my code, I returned the session username when user logs in to my member's area. But how would I return the session profile info for the session being used?




Theme © iAndrew 2016 - Forum software by © MyBB