Reputation: 6080
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?
Upvotes: 0
Views: 2351
Reputation: 4763
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?
Upvotes: 1
Reputation: 6080
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?
Upvotes: 0