FuelPHP basics, using Model result in View - php

I am a little confused using fuelPHP 1.7.
The controller
class Controller_Website extends Controller
{
public function action_index()
{
// http://fuelphp.com/docs/general/views.html
$data = Website::get_results();
//var_dump($data) // (data is found here);
$views = array();
$views['head'] = View::forge('common/head', $data);
$views['header'] = View::forge('common/header', $data);
$views['sidebar'] = View::forge('common/sidebar', $data);
$views['content'] = View::forge('common/content', $data);
$views['footer'] = View::forge('common/footer', $data);
// return the rendered HTML to the Request
return View::forge('website', $views)->render();
}
}
The model
class Website extends \Model
{
public static function get_results()
{
// Database interactions
$result = DB::select('menu', 'url', 'title', 'text')
->from('aaa_website')
->where('id', '=', 1035)
->and_where('visible', '1')
->execute();
return $result;
}
}
All well sofar. Data is queried and found in the controller. What I am trying to accomplish is to use the data in my:
(nested) view
<html>
<head>
<?php echo $head; ?>
</head>
<body>
<header>
<div class="container">
<?php echo $header; ?>
</div>
</header>
<div class="row">
<div class="container">
<div class="col-md-4">
<?php echo $sidebar; ?>
</div>
<div class="col-md-8">
<?php echo $content; ?>
</div>
</div>
</div>
<footer>
<div class="container">
<?php echo $footer; ?>
</div>
</footer>
</body>
</html>
Head view (nested):
<title><?php echo $title; ?></title>
Content view (nested):
<h1><?php echo $title; ?></h1>
<div class="welcome_user"><?php echo $text; ?></div>
And so on.
The variables in the view in this example are not available because they are not explicitly set in the controller. Do they have to be set explicitly or is passing the data object also possible? If so, how do I access this objects data in the right way? FuelPHP is lacking good examples here and I am stuck now.
How do I do it?

The view data is converted from array indexed to view variable named. So:
View::forge('something', array('param' => 'value'));
Will correspond to the following view:
<h1><?=$param?></h1>
Where things are going wrong is is that you pass the plain DB result to the view. You'd need to get the first result from the database result, like this:
class Website extends \Model
{
public static function get_results()
{
// Database interactions
$result = DB::select('menu', 'url', 'title', 'text')
->from('aaa_website')
->where('id', '=', 1035)
->and_where('visible', '1')
->as_assoc()
->execute()
->to_array();
return reset($result);
}
}
Note that I've first used ->to_array() to convert the result object to an array, then reset() to get the first result. I've also added ->as_assoc() to make sure you get an array result, ->as_object() would give you a stdClass instance.

Related

Load View in View CodeIgniter

I want to load a view which come from logical statement (in controller) IN a view. Simply like this
Controller
public function index(){
$this->lib();
$this->view('main');
}
public function lib(){
if(TRUE){
// $this->view('something1') in $this->view('main')
}
else{
// $this->view('something2') in $this->view('main')
}
}
** View **
<html>
<body>
<!-- view from $this->view('main') -->
<!-- want to show view from lib() -->
</body>
</html>
How to show $this->view('something') get from lib() in $this->view('main') ? Any idea ?
The only way that I've found works is this:
Controller:
public function index() {
$data['internal_view'] = $this->load->view('stmt', [], true);
$this->load->view('main', $data);
}
"main" View:
<html>
<body>
<?php echo $internal_view; ?>
</body>
</html>
Note: the 3rd param in view allows for the view to be returned as a string rather than automatically outputted to the browser. Because of this functionality, you can assign a view as a return string from another function and use it to generate internal_view or whatever you decide to call it.
You could preserve the main view and also keep the lib conditional view and pass it to the main controller.
Controller :
public function index(){
$data['lib_view'] = $this->lib();
$this->view('main', $data);
}
public function lib(){
if(TRUE){
return $this->load->view('something1', true);
}
else{
return $this->load->view('something2', true);
}
}
'main' View :
<html>
<body>
<!-- view from $this->view('main') -->
<?php echo $lib_view ?>
</body>
</html>

Codeigniter admin users dashboard issue

Actually, i'm working on Codeigniter admin dashboard users module,i'm trying to show (dashboard) how many users is there from user table from database enter image description here
model file code:
function usercount_total($userId)
{
$this->db->select('count(1)');
$this->db->from('tbl_users');
$query = $this->db->get();
return $query->result();
}
controller file:
$res['total_users'] = $this->user_model->usercount_total($userId);
$this->loadViews("dashboard", $this->global, $res, NULL);
view file:dashboard.php
<div class="inner">
<!-- <h3>44</h3> -->
<h3><?php echo $total_users; ?></h3>
<p>New User</p>
</div>
You can do it with the help of helpers (an alternative)
Add a file name custom_helper.php in helpers folder and load it with the help of autoload.php like this;
$autoload['helper'] = array('custom');
In custom_helper.php add a method called users_count() like this :
function users_count()
{
$ci = & get_instance();
return $ci->db->count_all('tbl_users');
}
In view do like this :
<div class="inner">
<h3><?php echo users_count(); ?></h3>
<p>New User</p>
</div>
For more :https://www.codeigniter.com/user_guide/general/helpers.html
Use $this
function usercount_total($userId)
{
$this->db->select('count(*)');
$this->db->from('tbl_users');
$query = $this->db->get();
return $query->num_rows();
}

CodeIgniter pass variable from controller to view

Sorry if this question is already been answered, but i couldn't find an answer.
I want to pass a variable from my model to the controller and finally display it in the view.
I execute a query on the database and after that I count my results (via num_rows). Then I return the result.
public function countHighRisk(){
$this->db->select("ares_status");
$this->db->from("tbl_alert_result");
$this->db->join('tbl_status_standards', 'ares_status = sts_status_id');
$this->db->where('sts_status_risk', 3);
$query = $this->db->get();
return $query->num_rows();
}
After that i place the result in an array and pass the array to my view.
public function index()
{
$this->load->model('Alert_result_model');
$data['high'] = $this->Alert_result_model->countHighRisk();
$this->load->view('templates/head');
$this->load->view('templates/menu');
$this->load->view('pages/Dashboard', $data);
$this->load->view('templates/footer');
}
I get an error when I try to display the variable in the view.
<div class="col-xs-8 text-right">
<span> High Risk </span>
<h2 class="font-bold"><span class="count"><?= $high; ?></span></h2>
</div>
error: http://imgur.com/iHkYHiI
Thanks in advance.
Your whole idea of calling the views is wrong here. Please use it as follows:
Controller:
public function index()
{
$this->load->model('Alert_result_model');
$data['high'] = $this->Alert_result_model->countHighRisk();
$this->load->view('pages/Dashboard', $data);
}
View:
<? $this->load->view('templates/head'); ?>
<? $this->load->view('templates/menu'); ?>
<div class="col-xs-8 text-right">
<span> High Risk </span>
<h2 class="font-bold"><span class="count"><?= $high; ?></span></h2>
</div>
<? $this->load->view('templates/footer'); ?>
Also check what $this->Alert_result_model->countHighRisk(); is actually returning by doing print_r($this->Alert_result_model->countHighRisk();)
You can load a view into controller but do this way below
class Dashboard extends CI_Controller {
public function index() {
$this->load->model('Alert_result_model');
$data['high'] = $this->Alert_result_model->countHighRisk();
// Choose only one header examples:
$data['header'] = $this->load->view('header', Null, TRUE); // If No Data
$data['header'] = $this->load->view('header', $data, TRUE); // If Data
// Choose only one Footer examples:
$data['footer'] = $this->load->view('footer', Null, TRUE); // If No Data
$data['footer'] = $this->load->view('footer', $data, TRUE); // If Data
$this->load->view('dashboard', $data);
}
}
Dashboard View
<?php echo $header;?>
<?php foreach ($high as $low) { ?>
<?php echo $low['something'];?>
<?php }?>
<?php echo $footer;?>
If you have a header separate header controller and footer controller you will need HMVC if you are confused on user guide top right corner of default user guide in CI3 is a switch to more cleaner version.
please change
$this->load->view('pages/Dashboard', $data);
into
$this->load->view('pages/Dashboard', $data, true);
now it should work.

codeigniter join query, display data to view

im new to codeigniter & php, and i have some question about join() function.
i have this model:
public function get_categorie()
{
$this->db->select('*');
$this->db->from('foxCategory');
$this->db->join('foxAnnunci', 'foxCategory.id = foxAnnunci.id_foxCategory');
$query = $this->db->get();
}
this controller:
public function index()
{
$data['category']= $this->annunci_model->get_categorie();
$data['annunci'] = $this->annunci_model->get_annunci();
$data['titolo'] = 'Elenco annunci';
$this->load->view('templates/header', $data);
$this->load->view('annunci/index', $data);
$this->load->view('templates/footer');
}
and this view
<h2><?php echo $value['titolo'] ?></h2>
<div id="main">
<code>Data di creazione: <?php echo $value['creato_il'] ?></code><br />
<?php echo $value['descrizione'] ?>
</div>
<p>Guarda annuncio || <p><?php echo $category['category']['titolo'] ?></p></p>
i want to display the name of category as link (classic for blog category function), but this code show nothing, what is the problem?

ZF2 view rendering in another view

I don't want to add child to the viewModel in action controller:
// action controller
public function indexAction() {
$result = new ViewModel();
$result->setTemplate('application/view/another-action');
$comments = new ViewModel();
$comments->setTemplate('application/view/child-comments');
$result->addChild($comments, 'child_comments');
return $result;
}
...
// View
<div>
<?php echo $this->child_comments ?>
</div>
I want to include view in another view:
<div>
<?php
$view = new ViewModel();
$view->setVariables($this->var);
$view->setTemplate('page_nav.phtml');
// here I want to render view
?>
</div>
Is it possible?
This is what the Partial view helper does (docs seem to be outdated, but I'll link them anyway here):
<div>
<?php echo $this->partial('page_nav', array('var' => $this->var)) ?>
<div>
Obviously, your page_nav should be known by your view resolver.
I know this is old, but you can also simply call $this->render('path/to/view-script')

Categories