please explain the dataflow in MVC specially in codeigniter - php

Can anyone please explain me the object flow in codeigniter MVC ? I can see for example when I put the followong code in controller it works, but i am not being able to figure out which part of this goes in model in vews. I tried several ways but coudn't. When i use the example codes from other it works but myself i am getting confused. Please help
$query = $this->db->query("YOUR QUERY");
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->body;
}

That would translate into something like:
Model:
class SomeModel extends Model {
function SomeModel() {
parent::Model();
}
function get_some_data() {
return $this->db->query('some_query')->result_array();
}
}
Controller:
class SomeController extends Controller {
function SomeController() {
parent::Controller();
}
function index() {
$this->load->model('SomeModel');
$some_data = $this->SomeModel->get_some_data();
$this->load->view('some_view');
}
}
View:
foreach($some_data as $data) {
echo $data->title;
echo $data->name;
echo $data->body;
}
However, for your communication between controller and view I would recommend using a template parser such as Dwoo or Twig (I don't like the one that comes with CI).

On the controller part I was doing:
class SomeController extends Controller {
function SomeController() {
parent::Controller();
}
function index() {
}
function show_data(){
$this->load->model('SomeModel');
$some_data = $this->SomeModel->get_some_data();
$this->load->view('some_view.php');
$this->index()
}
}
is that not the way to do it when we have many function ? When i look at other's code I see something like that or I am wrong ?

Related

Laravel, is there a way to include codes from resource/view path into controller?

My goal is to include codes from another sources which is located in resources/views. I have tried using resource_path('views/myfiles.php') but it does nothing.
Controller
class MyController extends Controller
{
public function test(Request $request)
{
if($request->input('name') == "chair")
{
$theFilesLocation = "resources.views" . $request->input('name');
#include($theFilesLocation) //something like this
}
}
}
myfiles.php
<?php
dump("if this shows up, then the code works")
?>
Try bellow code but I think it is not a good way.
class MyController extends Controller
{
require_one(resource_path('views/myfile');
}
Or with Laravel File facade
class MyController extends Controller
{
\File::requireOnce(resource_path('views/myfile');
}
You should create a class and put your code there then call it from the controller is a better solution.
What you are looking for is a trait. This allows the easy sharing of code and functionality without having to inherit from a specific base class causing an inheritance hell.
namespace MyCode\Traits;
trait SharedCodeForThing {
public function blaTheBla() {
dump("if this shows up, then the code works");
}
}
and then in your controller
use MyCode\Traits\SharedCodeForThing ;
class MyController extends Controller
{
use SharedCodeForThing;
}
Now if you wish to just render the contents of the view which it seems you're after:
public function test(Request $request)
{
if($request->input('name') == "chair")
{
$view = view('resources.views' . $request->input('name'));
return $view->render();//or echo $view->render(); whatever you like
}
}

Where should I define a custom function in CodeIgniter and how can I use it in every page?

I'm a newbie in CodeIgniter. I want to use the following function and needs to be run in every page. I'm calling the function in the top header of the view. Even though different subviews are loaded in the content below, the navigation bar should load this function in order to count notifications and messages on every run.
function count_records_in_table($table, $where)
{
$this->db->select('count(id) as rows');
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
foreach($query->result() as $r)
{
return $r->rows;
}
}
first create the common model
then put this function on it and load this model in
$autoload['model'] = array('mdl_common');
autoload file in config
finally you can use your function by calling model common like this
$this->mdl_common->count_records_in_table("table_name","where");
you can use in every page you want try it it will helps .
Here is suitable solution
First create one file say common.php in application/libraries/common.php
Now write below code in common.php file
class Common extends CI_Controller
{
public function customFailMessage($msg)
{
$xml = new SimpleXMLElement('<Response/>');
$xml->addChild('status','success');
$xml->addChild('msg',"my message");
print ($xml->asXML());
die;
}
}
Now call you function from any of your controller like this
class Ws_plan extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('common');
}
public function test()
{
$this->common->customFailMessage();
}
}
Hope you got idea how to use common function in all controller

MVC - Model not working correctly

This is the code for my controller:
class products extends CI_Controller {
public function index() {
$this->load->model('getproducts');
$this->load->view('productlist');
}
}
?>
When I load this, it returns a blank page, however when I comment out the line that loads the model, it returns the view as expected (pointing to an error in the model). Here is the code for the model:
<?php
class getproducts extends Model {
function listProducts() {
$query = $this->db->get('ProjectProducts');
if ($query->num_rows() > 0) {
foreach ($query->result() as $row {
$productdata[] = $row;
}
return $productdata;
}
}
}
?>
Am I missing something obvious here? I'm not even using any of the data from the model yet.
What version you use of codeigniter
please note that you should extends CI_Model not model
class getproducts extends CI_Model {
Please make sure your controller start letter be capital ie. upper
case and extend it as CI_Model
you are missing
function __construct()
{
parent::__construct();
}
and this code before your function listProducts and after class starting hopefully this will resolve your issue.
and also use same function in your controller as well

display result from a query

I'm learning kohana 3.3. I'm working on the model part. Basic query. But i don't know how to display the results i got from a query.
Here's the model.
APPPATH/classes/model/people.php
class Model_People extends Model {
public function show_data() {
$query = DB::query(Database::SELECT, 'SELECT * FROM people');
return $query;
}
}
APPPATH/classes/controller/people.php
class Controller_People extends Controller {
public function action_index() {
$model = Model::factory('people');
$view = View::factory('base_template');
$model->user = $model->show_data();
$this->response->body($view);
}
}
APPPATH/views/base_template.php
<?php
foreach($user as $row) {
echo "<h2>".$row['Name']."</h2>";
}
?>
I don't want to use ORM I'm using QUERY BUILDER. When I run the code it says variable user not defined. How do I display the results correctly? thanks.
Since you are learning Kohana, I would suggest using ORM, Kohana offers quite a powerful module for this.
After enabling the module you can use it like this
Model_People
class Model_People extends ORM {
}
Controller_People
public function action_index() {
$people = ORM::factory('People')->find_all();
$view = View::factory('base_template');
$view->people = $people;
$this->response->body($view);
}
base_template
<?php foreach ($people as $person) : ?>
<h2><?php echo $person->Name; ?></h2>
<?php endforeach; ?>
ORM offers many advantages, such as relationships, validation and filters. Without you having to write complex additional code (such as DB expressions).
Additionally when working on views, you might want to use a Controller that extends Controller_Template instead of Controller
Try this
class Controller_People extends Controller {
public function action_index() {
$model = Model::factory('people');
$view = View::factory('base_template');
$view->user = $model->show_data();
$this->response->body($view);
}
}
then loop in view
<?php
foreach($user as $row) :
echo "<h2>".$row['Name']."</h2>";
endforeach;
?>

Simple Code Igniter Controller and Model causing a 500 error - no CI error shown

I've just started learning Code Igniter and I've run in to a problem almost straight away.
Using the code below and a database setup with the correct privileges I get a 500 error (server error). CI is not throwing an error and I can't see anything wrong with this code.
I'm using the latest version of CI and testing this code:
Controller: products.php
class Products extends CI_Controller {
public function index() {
$this->load->model('products');
$this->load->view('products');
}
}
Model: products.php
class Products extends CI_Model {
function __construct() {
parent::__construct();
}
function get_products() {
$q = $this->db->get('product');
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
I've put these classes in the directories they should be in with a filename of the same name as the class....if I comment out the load-model code in the controller my view is shown. My database connections are correct because if I change them to something incorrect CI throws an error. There's some sort of problem with the model or the loading of it.
You have conflicting class names, the modell and the controller class both called Products.
Try this:
class Products extends CI_Controller {
public function index() {
$this->load->model('products_model','products');
$this->load->view('products');
}
}
Model: products_model.php
class Products extends CI_Model {
function __construct() {
parent::__construct();
}
function get_products() {
$q = $this->db->get('product');
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
A good convention to use is to name your controller ProductsController and then leave the model Products. So all controllers are SomethingController.
Example:
class ProductsController extends CI_Controller
where the filename become products_controller.php
and the model remains:
class Products extends CI_model
and remains products.php
Now your code should work.

Categories