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><?php $row['T_Name']?></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></h2>
<p>Mobile : </p>
<h2></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?
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><?= $row['T_Name'] ?></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?
i think the only solution is now to write echo with every $row
i did it now like this
<h2><?php echo $row['T_Name']?></h2>
<p>Mobile : <?php echo $row['T_Mobile']?></p>
is this ok to write echo everywhere or is there any better solution?
Related
I'm working on a custom template part that includes an h2 with the classes "sectionmarker" and "bodycontent", like this:
<h2 class="sectionmarker bodycontent">Title</h2>
When I call get_template_part() on a page, the output I'm getting is:
<h2 class="bodycontent">Title</h2>
For some reason, it seems not to like "sectionmarker". I even switched the order of the two classes, like this:
<h2 class="bodycontent sectionmarker">Title</h2>
And I get the same output with only the bodycontent class applied. What's going on here?
Full template content:
<?php
$abstract=get_post_field('post_content',224);
$news=get_posts(array(
'post_type'=> 'news'
));
$newsdata=array();
foreach ($news as $n) {
$item=new stdClass();
$item->title=get_the_title($n->ID);
$item->date=get_the_date("",$n->ID);
$item->content=get_field('more_info',$n->ID);
array_push($newsdata,$item);
}
$newsdata_json=json_encode($newsdata);
?>
<script type="text/javascript">
console.log(<?php echo $newsdata_json ?>);
</script>
<?php
echo $abstract;
if(count($newsdata)>0){
?>
<h2 class="sectionmarker bodycontent" >News</h2>
<?php foreach ($newsdata as $newsitem) { ?>
<h4 class='datemarker bodycontent'><?php echo $newsitem->date ?></h4>
<p class='newstext bodycontent'><?php echo $newsitem->title ?></p>
<?php } } ?>
and here's the call I'm making (in home.php):
get_template_part('custom-home-template',null,array());
Nevermind! I just remembered I have some JavaScript that's changing the h2 classes on load.
I trying to query in database, and the result is an Object. I want to show that result but $response always Null in my show.php. I using CodeIgniter
My Controller
home.php
class Home extends CI_Controller{
public function _contruct(){
parent::_contruct();
}
public function index(){
$this->load->view("from.html");
}
public function findID(){
$this->load->database();
$this->load->helper('url');
$id = $this->input->post("findId");
$sql="SELECT * FROM cli_articles WHERE ID='{$id}'";
$response=$this->db->query($sql);
//$row=$response->row();
// echo $row->name;
$this->load->view("show",$response);
}
}
?>
My view
from.html
<!DOCTYPE html>
<html>
<head>
</head>
<header>From</header>
<body>
<form name="findID" action="findID" method="POST">
<input type="text" name="findId">
<input type="submit" value="FindID">
</form>
</body>
and show.php
<?php
while($show_db=mysql_fetch_array($response)){
?>
<p> id : <?php echo $show_db['id'];?><br></p>
<p> category_id : <?php echo $show_db['category_id'];?><br></p>
<p> content : <?php echo $show_db['content'];?><br></p>
<p> created : <?php echo $show_db['created'];?><br></p>
<p> description : <?php echo $show_db['description'];?><br></p>
<p> image : <?php echo $show_db['image'];?><br></p>
<p> mt_description : <?php echo $show_db['mt_description'];?><br></p>
<p> mt_keyword : <?php echo $show_db['mt_keyword'];?><br></p>
<p> name : <?php echo $show_db['name'];?><br></p>
<p> ngay_dang : <?php echo $show_db['ngay_dang'];?><br></p>
<p> order : <?php echo $show_db['order'];?><br></p>
<p> slug : <?php echo $show_db['slug'];?><br></p>
<p> status : <?php echo $show_db['status'];?><br></p>
<p> view : <?php echo $show_db['view'];?><br></p>
<?php
}
?>
anh i don't know why $show_db=mysql_fetch_array($response) not correct
Thank for your help!
Using codeigniter, you really should consider using it's 'active record':
$sql="SELECT * FROM cli_articles WHERE ID='{$id}'";
$response=$this->db->query($sql);
I would instead suggest:
$this->db->select('*');
$this->db->where('id', $id);
$response = $this->db->get('cli_articles')->result_array(); // for array, result() for object, row() for a single result object, row_array() for a single array
But if you want to keep your style:
$sql="SELECT * FROM cli_articles WHERE ID='{$id}'";
$response=$this->db->query($sql);
$view_data['results'] = $response->result_array();
$this->load->view("show",$view_data);
And in your view:
<?= $results['id']; // or you made need to loop, depending on your array structure ?>
You need to use the result_array method.
In your controller you can use CI's ActiveRecord syntax:
$response = $this->db->where('ID', $id)->get('cli_articles');
Then replace your while statement in your view with:
foreach ($response->result_array() as $show_db) { ...
I m learing codeigniter and i want to add an image in my view page.
I am using tag but the image is not added in the page.
So help me how i can add the image in view page of codigniter.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Blog</title>
</head>
<body>
<h2><img src="../images/logo.png"></h2>
<?php $this->load->view('blog/menu');
if ($query):foreach ($query as $post): ?>
<h4><?php echo $post->entry_name; ?> (<?php echo $post->entry_date; ?>)</h4>
<?php echo $post->entry_body; ?>
<?php endforeach;
else: ?>
<h4>No entry yet!</h4>
<?php endif; ?>
</body>
</html>
store images in an images folder at the same level as the application folder . then just link to it like this using code.
<img src="<?php echo base_url('images/logo.png'); ?>" />
Store images in an images folder at the same level as the application folder, Then just link to it like this:
<img src="../../images/image1.jpg" />
See, first you create a file with the name user_profile.php in the controller folder. no uppercase letters.
To call a View page you created in the index:
function index()
{
$this->load_controller('myView');
}
this is an example from the link i sent you that is perfect, that shows peaces of a views and how to combine them to one code from the controller.
Please go to the codeigniter website and start learning the simple stuff. or search for codeigniter via youtube. you will find a lot!
Class User_Profile extends Controller
{
function index()
{
$this->load_controller('Left_Nav');
$this->load_controller('Content_Nav');
$this->load_controller('Login_Name');
$this->load_controller('Leaderboard', 'Board');
$this->Left_Nav->index(array('highlight_selected_page' => 'blah'));
$this->load('User');
$content_data = $this->User->get_profile_details();
$this->view->load('content', $content_data);
$this->Login_Name->index();
$this->Board->index();
}
}
image in assets
<img src="<?php echo base_url(); ?>assets/images/image.png">
I have a layout file as follows:
<?php echo $this->doctype(); ?>
<html>
<head>
<?php echo $this->headTitle(); ?>
<?php echo $this->headLink(); ?>
</head>
<body>
<?php echo $this->layout()->content; ?>
</body>
</html>
I have a menu system which is written in another template
<p>
<div>
menu code goes here
</div>
<p>
<?php echo $this->actionContent; ?>
</p>
</p>
I wanted the action method's output should be placed in $this->actionContent and all of that should go to the layout.
Then I wrote a Controller plugin as follows:
class ZFExt_Controller_Plugin_Addmenu extends Zend_Controller_Plugin_Abstract
{
public function postDispatch(Zend_Controller_Request_Abstract $request)
{
$view = Zend_Controller_Front::getInstance()
->getParam('bootstrap')
->getResource('view');
if (false !== $request->getParam('menu'))
{
$response = $this->getResponse();
$content = $response->getBody(true);
$view->menuContent = $content['default'];
$updatedContent = $view->render('menu.phtml');
$response->setBody($updatedContent);
}
}
}
In the controller class
class IndexController extends Zend_Controller_Action {
public function indexAction() {
}
public function viewAction()
{
$this->getRequest()->setParam('menu', false);
}
}
So whichever action does not want the menu there we can pass a parameter 'menu' with value 'false'.
My question is: Is this the right way to do ?
First, I probably wouldn't render the menu from an action. I tend to think of actions as corresponding to HTTP requests, building full pages/responses, rather than just page fragments, for the waiting client. I would either have a separate class/component handle menu creation or just use Zend_Navigation.
Beyond that, if I understand correctly, you simply want each action to be able to enable/disable the menu portion of the layout, right?
So, how about simply setting a switch in the view that enables/disables the menu in the layout.
The layout looks like:
<?php echo $this->doctype(); ?>
<html>
<head>
<?php echo $this->headTitle(); ?>
<?php echo $this->headLink(); ?>
</head>
<body>
<?php if ($this->renderMenu): ?>
// render menu here
<?php endif; ?>
<?php echo $this->layout()->content; ?>
</body>
</html>
Then in your action, if you want to disable the menu rendering, you can set:
$this->view->renderMenu = false;
Probably also worthwhile to set a default value for the $view->renderMenu flag at some point in the request dispatch cycle - perhaps at bootstrap, or in a controller plugin, or in controller init().
I have a main view with a menu which helps me display another view. It's similar to this:
<div id="page">
<div id="menu">
Page1
Page2
</div>
<div id="content">
<!-- Page1 or Page2 are displayed here -->
</div>
</div>
I'm using php's Yii framework. Which makes me not to use <?php include("menuview.php"); ?>. So I'm looking for a different solution. I can do this with Ajax, but I would also like the link to change to mypage/controller/Page2. With Ajax I can only get it to this: mypage/controller/index#Page2
in main view, instead of include do
<?php echo $this->renderPartial('_page1', array('model'=>$model)); ?>
UPDATE:
protected/views/controller/page1.php and protected/views/controller/page2.php content at your liking
protected/views/layouts/custom.php:
<?php $this->beginContent('//layouts/main'); ?>
<div id="page">
<div id="menu">
<?php echo CHtml::ajaxLink('Page1', array('controller/page1'), array('update' => '#content')); ?>
<?php echo CHtml::ajaxLink('Page2', array('controller/page2'), array('update' => '#content')); ?>
</div>
<div id="content">
<?php echo $content; ?>
</div>
</div>
<?php $this->endContent(); ?>
protected/controllers/ControllerController.php:
class ControllerController extends Controller {
/**
* #var string the default layout for the views.
*/
public $layout = '//layouts/custom';
public function actionPage1() {
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('page1');
else
$this->render('page1');
}
public function actionPage2() {
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('page2');
else
$this->render('page2');
}
}
UPDATE2:
If you need the link in address bar to change too then your only option is to use regular link and not ajax <?php echo CHtml::link('Page1', array('controller/page1')); ?>
using ajax the preferred way is using hash like you mentioned.