Welcome Guest, Not a member yet? Register   Sign In
Returning an array into a table
#1

[eluser]maddtechwf[/eluser]
I have a model setup that pulls the data that I want. I then want to take that data and display it on a table on a view.

Can anyone help me with this?

Model
Code:
public function get_company_info($company_id)
{
  $q_company = $this
     ->db
     ->where('id', $company_id)
     ->get('company');
  if ( $q_company->num_rows > 0) {
   return $q_company->row();
  }
  return false;
}

Controller
Code:
public function company_info()
{
  $data['admin_content'] = "company_info";
    
  $this->load->model('admin_model');
  $company = $this
     ->admin_model
     ->get_company_info(
      $this->session->userdata('company_id')
     );
  $this->load->view('includes/template_admin', $data);
  
  if ( $company !== false) {
   return $company;
  }
}

l would appreciate any help.
#2

[eluser]micfai[/eluser]
I would use a view to do this. Here is an example of what I do when needing this.

Code:
<?php $columns = 6; ?>
<div class="admin-box">
<h3>Contact</h3>

&lt;?php echo form_open(current_url()) ;?&gt;
<table class="table table-striped">
  <thead>
   <tr>
    &lt;?php if(has_permission('Contact.Content.Delete')):?&gt;
     &lt;?php $columns++; ?&gt;
    <th class="column-check">&lt;input class="check-all" type="checkbox" /&gt;&lt;/th>
    &lt;?php endif;?&gt;
    <th>&lt;?php echo lang('contact_form_name');?&gt;</th>
    <th>&lt;?php echo lang('contact_form_email');?&gt;</th>
    <th>&lt;?php echo lang('contact_form_phone');?&gt;</th>
    <th>&lt;?php echo lang('contact_form_subject');?&gt;</th>
    <th>&lt;?php echo lang('contact_form_message');?&gt;</th>
    <th>&lt;?php echo lang('contact_form_created');?&gt;</th>
   </tr>
  </thead>
&lt;?php if (isset($records) && is_array($records) && count($records)) : ?&gt;
  <tfoot>
  &lt;?php if(has_permission('Contact.Content.Delete')):?&gt;
   <tr>
    <td colspan="&lt;?php echo $columns?&gt;">
     &lt;?php echo lang('bf_with_selected') ?&gt;
     &lt;input type="submit" name="submit" class="btn-danger" id="delete-me" value="&lt;?php echo lang('bf_action_delete') ?&gt;"  confirm('&lt;?php echo lang('pages_delete_multiple_confirm'); ?&gt;')"&gt;
    </td>
   </tr>
  &lt;?php endif; ?&gt;
  </tfoot>
&lt;?php endif; ?&gt;
  <tbody>
  &lt;?php if (isset($records) && is_array($records) && count($records)) : ?&gt;
   &lt;?php foreach ($records as $record) : ?&gt;
   <tr>
   &lt;?php if(has_permission('Contact.Content.Delete')):?&gt;
    <td>&lt;input type="checkbox" name="checked[]" value="&lt;?php echo $record-&gt;contact_id ?&gt;" /&gt;&lt;/td>
   &lt;?php endif;?&gt;
    <td>&lt;?php echo anchor(SITE_AREA .'/content/contact/view/'. $record->contact_id, $record->name) ?&gt;</td>
    <td>&lt;?php echo $record->email_address;?&gt;</td>
    <td>&lt;?php echo $record->phone;?&gt;</td>
    <td>&lt;?php echo $record->subject;?&gt;</td>
    <td>&lt;?php echo $record->message;?&gt;</td>
    <td>&lt;?php echo $record->created_on;?&gt;</td>
   </tr>
   &lt;?php endforeach; ?&gt;
  &lt;?php else: ?&gt;
   <tr>
    <td colspan="&lt;?php echo $columns?&gt;">No items were found that match your selection.</td>
   </tr>
  &lt;?php endif; ?&gt;
  </tbody>
</table>

<div class="well">
  &lt;?php echo $total_records." records found"; ?&gt;
</div>
&lt;?php echo form_close(); ?&gt;

&lt;?php echo $this->pagination->create_links(); ?&gt;

</div> &lt;!-- Contact Editor --&gt;
#3

[eluser]maddtechwf[/eluser]
Thanks for the quick reply. How would I modify the above code to work for when I'm returning a single record.
#4

[eluser]Vheissu[/eluser]
[quote author="maddtechwf" date="1368586455"]Thanks for the quick reply. How would I modify the above code to work for when I'm returning a single record.[/quote]

A foreach will loop over one or more results, so it doesn't matter if you only have one as the loop should only repeat once.
#5

[eluser]maddtechwf[/eluser]
So this is what my view looks like now.
Code:
&lt;?php echo form_open(current_url()) ;?&gt;
        &lt;?php foreach ($companies as $company) : ?&gt;
         <div class="row">
          <div class="large-8 columns">
           <label>Company Name</label>
           &lt;input type="text" value="&lt;?php echo $company-&gt;name; ?&gt;" /&gt;
          </div>
         </div>
         <div class="row">
          <div class="large-8 columns">
           <label>Address 1</label>
           &lt;input type="text" value="&lt;?php echo $company-&gt;address1; ?&gt;" /&gt;
          </div>
         </div>
         <div class="row">
          <div class="large-8 columns">
           <label>Address 2</label>
           &lt;input type="text" value="&lt;?php echo $company-&gt;address2(); ?&gt;" /&gt;
          </div>
         </div>
         <div class="row">
          <div class="large-8 columns">
           <label>City</label>
           &lt;input type="text" value="&lt;?php echo $company-&gt;city(); ?&gt;" /&gt;
          </div>
         </div>
         <div class="row">
          <div class="large-8 columns">
           <label>State</label>
           &lt;input type="text" value="&lt;?php echo $company-&gt;state(); ?&gt;" /&gt;
          </div>
         </div>
         <div class="row">
          <div class="large-8 columns">
           <label>Country</label>
           &lt;input type="text" value="&lt;?php echo $company-&gt;country(); ?&gt;" /&gt;
          </div>
         </div>
         <div class="row">
          <div class="large-8 columns">
           <label>Phone</label>
           &lt;input type="text" value="&lt;?php echo $company-&gt;phone(); ?&gt;" /&gt;
          </div>
         </div>
         <div class="row">
          <div class="large-8 columns">
           <label>Fax</label>
           &lt;input type="text" value="&lt;?php echo $company-&gt;fax(); ?&gt;" /&gt;
          </div>
         </div>
         <div class="row">
          <div class="large-8 columns">
           <label>URL</label>
           <div class="row collapse">
           <div class="small-3 large-2 columns">
              <span class="prefix">http://</span>
           </div>
           <div class="small-9 large-10 columns">
              &lt;input type="text" placeholder="Enter your URL..." value="&lt;?php echo $company-&gt;url(); ?&gt;"&gt;
           </div>
         </div>
          </div>
         </div>
        &lt;?php endforeach; ?&gt;
       &lt;?php echo form_close(); ?&gt;

After saving and going to view the page, I get the following error.
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: company

Filename: admin/company_info.php

Line Number: 6
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: admin/company_info.php

Line Number: 6
#6

[eluser]micfai[/eluser]
Change this line in your controller to the following:

Code:
this->load->view('includes/template_admin', $companies);
#7

[eluser]maddtechwf[/eluser]
But if I change that then I won't be able to specify my view to pull for the content.
Code:
$data['main_content'] = "admin/users";

Is there a way to pass both the above content and the content of $company?
#8

[eluser]micfai[/eluser]
Change your view then from $companies to $data and see what happens.
#9

[eluser]maddtechwf[/eluser]
When I do the following, I get a blank page.

Code:
public function company_info()
{
  $data['admin_content'] = "admin/company_info";
    
  $this->load->model('admin_model');
  $company = $this
     ->admin_model
     ->get_company_info(
      $this->session->userdata('company_id')
     );
  $data['companies'] = $company;
  $this->load->view('includes/template_admin', $data);
}




Theme © iAndrew 2016 - Forum software by © MyBB