0

I am new to CodeIgniter and have a problem I have been unable to figure out. Here is my model class (filename = tenant.php):

<!-- language php --> class tenant extends CI_Model { function getTenants() { $this->db->select()->from('hrs_tenants'); $query=$this->db->get(); return $query->result_array(); } } 

my controller class (filename = tenants.php):

<!-- language php --> class tenants extends CI_Controller { function index() { $this->load->model('tenant'); $data['tenants']= $this->tenant->getTenants(); echo "<pre>"; print_r($data['tenants']); echo "</pre>"; $this->load->view('tenants', $data); } } 

and finally my view file (tenants.php):

<!-- language php --> <html> <head> <title>Tenants Listing</title> </head> <body> <h1>Tenants Listing</h1> <?php if(!isset($tenants)) { ?> <p>There are no Tenants to List</p> <?php } else { foreach($tenants as $row){?> <h2><a href="<?php base_url()?>tenants/tenant<?php $row['T_ID']?>"><?php $row['T_Name']?></a></h2> <p>Mobile : <?php $row['T_Mobile']?></p> <?php } } ?> </body> </html> 

Now coming back to the problem - it should display the tenant name and tenant mobile no., but the view doesn't display it. Instead it shows the static HTML view. But some how it do repeat the tags. Here is the html output/ rendered HTML for the view file:

<pre>Array ( [0] => Array ( [T_ID] => 1 [T_Name] => John Doe [T_Mobile] => 030112345678 ) [1] => Array ( [T_ID] => 2 [T_Name] => Haider Hassan [T_Mobile] => 033412345678 ) ) </pre><html> <head> <title>Tenants Listing</title> </head> <body> <h1>Tenants Listing</h1> <h2><a href="tenants/tenant"></a></h2> <p>Mobile : </p> <h2><a href="tenants/tenant"></a></h2> <p>Mobile : </p> </body> </html> 

My database is connected fine, as I also did an echo directly in the controller file and it is also generated in the HTML file within pre tag.

Did I forgot to add something, why am I running into this issue?

7
  • Have you tried it without the foreach loop? Try just doing $tenants['T_ID'] directly and see if that works. Commented Mar 10, 2013 at 10:26
  • when the data is returned from the model test it in the controller first to make sure your model is returning the desired data Commented Mar 10, 2013 at 10:31
  • did you autoload the database helper. if not then you need to load $this->load->database() in your model. You may also want to run $query->result() outside the model Commented Mar 10, 2013 at 10:34
  • @dcd018 Tried it directly as you said. it gave error. Severity: Notice Message: Undefined index: T_ID Commented Mar 10, 2013 at 10:36
  • @MuhammadNasir i think model is returning data or that array with in the <pre> tag wouldnt have been generated.. Commented Mar 10, 2013 at 10:38

2 Answers 2

1

Controller file:

class tenants extends CI_Controller { function index() { $this->load->model('tenant'); $data['tenants']= $this->tenant->getTenants(); //Get rid of Echo $this->load->view('tenants', $data); } } 

View file:

<html> <head> <title>Tenants Listing</title> </head> <body> <h1>Tenants Listing</h1> <?php if(!isset($tenants)) { echo '<p>There are no Tenants to List</p>'; } else { foreach($tenants as $row): ?> <h2><a href="<?= base_url() . 'tenants/tenant/' . $row['T_ID']?>"><?= $row['T_Name'] ?></a></h2> <p>Mobile : <?= $row['T_Mobile'] ?></p> <?php endforeach; } ?> </body> </html> 

Yea doing echo is cool. Not a problem. But, try to minimize the use of closing php tag... Try to concat strings rather opening php after every break...

Questions?

Sign up to request clarification or add additional context in comments.

Comments

0

i think the only solution is now to write echo with every $row

i did it now like this

 <h2><a href="<?php echo base_url()?>tenants/tenant/<?php echo $row['T_ID']?>"><?php echo $row['T_Name']?></a></h2> <p>Mobile : <?php echo $row['T_Mobile']?></p> 

is this ok to write echo everywhere or is there any better solution?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.