codeigniter: modify value in view from controller - codeigniter

I am trying to modified value from controller to view. I would like to achieve that when a user is signing up, and submited, I can return feedback from model to controller to view.
my view: main_view
<?php include('header.php'); ?>
<?php echo $sign_up_results ?>
<?php include('forms/forms.php'); ?>
<?php include('footer.php'); ?>
my controller
function __construct(){
parent::__construct();
$this->load->helper('url');
$template = $this->load->View('main_view');
}
function form_sign_up_controller(){
$this->load->model("form_sign_up");
$database_insert_results = $this->form_sign_up->insert_user_detail_into_db();
$data['sign_up_results']=$database_insert_results;
$template = $this->load->View('main', $data);
}
The problem is when the view is loaded, the value "$sign_up_results" is not yet defined. Is there anyway that I can define the value and then change the value according to the results return from model to controller.

just use is set to check if the parameter is defined or not
<?php if(isset($sign_up_results)) echo $sign_up_results ?>

Related

dynamically page load using codeigniter

helow, I am a newbie in codeigniter. I want to load my pages into my view dynamically. That means, Using a function.
For example i have 4 page home, about, contact, info. In my my view folder I have created a base layout where i include header and footer which is same for all those pages. now i want when i click those page link it just load into content body without loading full page. Using CI normal procedure i can do it. which is
function home(){
$data['main_content'] = 'home';
$this->load->view('template/layout', $data);
}
function about(){
$data['main_content'] = 'about';
$this->load->view('template/layout', $data);
}
but i want to create a function which call this pages into a single function for example,
function pageload($page){
}
this function check that requested file either exits or not exist. if exist it load the page otherwise load "page not found" 404 error page. How can I make it...plz help.
My view file is:
<nav>
<ul class="sf-menu">
<li class="current"><?php echo anchor('home/index', 'Home');?></li>
<li><?php echo anchor('home/amenities', 'Room & Amenitis');?></li>
<li><?php echo anchor('home/reservations', 'Reservations');?></li>
<li><?php echo anchor('home/photos', 'Photographs');?></li>
<li><?php echo anchor('home/pakages', 'Pakages');?></li>
<li><?php echo anchor('home/explore', 'Explore');?></li>
<li><?php echo anchor('home/contact', 'Contact');?></li>
</ul>
</nav>
And Controller:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller{
function __construct(){
parent::__construct();
}
function pageload($page){
$data['main_content'] = $page;
$this->load->view('template/layout', $data);
}
}
?>
You can do something lilke this
function pageload($page){
$data['main_content'] = $page;
$this->load->view('template/layout', $data);
}
if u dont want to show controller_name/pageload in url then in application/config/routes.php
add an array
$route['(home|about_us|other_page|another_page)'] = 'controller_name/pageload/$1';
and to load your 404 page just go to the application/config/routes.php
find this line $route['404_override'] and set its value to snything u want like
$route['404_override'] = 'page_not_found';
then in your controller just add a controller named page_not_found.php and load ur desired 404 custom view from that controller.
Try this function i think this will help you
function build_view($flake) {
$this->data['content'] = $this->load->view($flake, $this->data, TRUE);
$this->load->view('header', $this->data);
}
after you add this you need to add to other function like in index function
function index(){
$this->$data['main_content'] = $page;
$this->build_view('template/layout.php')
}

Codeigniter Message: Undefined variable: infos

I am a newbie in CI. I used MY_Controller.php as main controller. I could open the ajax as the div#page loader. Now , My problem is although I load /about page, I get the database entries for the services model. How can i get the about table for the about controller ?
..
function render_page($view) {
if( ! $this->input->is_ajax_request() )
{
$this->load->view('templates/header', $this->data);
}
$this->load->view($view, $this->data);
if( ! $this->input->is_ajax_request() )
{
$this->load->view('templates/menu');
$this->load->view('templates/footer', $this->data);
}
}..
My services_model:
class Services_model extends CI_Model {
function getAll() {
$q = $this->db->get('services');
if($q->num_rows() > 0){
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
}
My home controller :
public function view($page = 'home')
{
$this->load->helper('text');
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$data);
}
When I use them in the home view there is no problem I can see the services_table :
<ul class="blog-medium">
<?php foreach($records as $row): ?>
<li>
<div class="blog-medium-text">
<h1><?php echo $row->title; ?></h1>
<p class="blog-medium-excerpt"><?php echo $row->content; ?><br />
Devamını Okumak için →</p>
</div>
<?php endforeach ?>
I want to use the same way in about page.
About_model:
class About_model extends CI_Model {
function getAll() {
$q = $this->db->get('abouts');
if($q->num_rows() > 0){
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
}
About controller :
public function view($page = 'about')
{
$this->load->helper('text');
$this->data['records']= $this->about_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$data);
}
And this is my view file of about :
<div id="content">
<?php foreach($infos as $row): ?>
<h3 style="text-align: center;"> <?php echo $row->title; ?></h3>
<div class="hr"> </div>
<?php echo $row->content; ?>
<?php endforeach; ?>
I get the error telling me :
Severity: Notice
Message: Undefined variable: infos
Filename: pages/about.php
Line Number: 3
Why cant i get the abouts table?
You are calling a variable $infos in your foreach, but it is never passed in as a variable to your view.
Read the docs about Adding Dynamic Data to the View
You will either need to set $data['infos'] to something or, and I'm guessing this what you intended, use $records in your foreach
The above answers your specific, but after you provided the repo for your source, there are issues you are struggling with. I highly suggest you read through the entire documentation, staring with the Introduction: Getting Started, continuing to the Tutorials and then General Topics.
The reason you are having problems here, you have your routes.php setup to that everything is routed into the Home controller executing the view method. This method, while it accepts the page you want to see is always returning a fetch of the services model. Your other controllers are not getting executed at all. Based on your controller setup, if you would just remove the custom route, http://theurl/about would route to the About Controller. The default method to be loaded is index so if you change view to index, then it would be displayed by default.

Get data from helper in CodeIgniter

I have a trouble with CodeIgniter.
I would like to get (recoup) data from a helper that I call in a view.
I will explain with the code
Here's my view :
// CALL THE FUNCTION FROM THE HELPER
<?php recup_email(); ?>
// DATA RECOUP FROM THE HELPER
<?php foreach($em as $e): ?>
<?php echo $e->email_membre; ?>
<?php endforeach; ?>
As you can see, I call the function and just after I would like to use data that I had recoup.
Here's my helper :
function recup_email()
{
$CI =& get_instance();
$CI->load->model('unite_model');
$data['em'] = $CI->unite_model->email_membre_model();
}
But I don't know how to recoup data without using
$layout_network['contenu'] = $this->load->view('list_vote_friend', $data, TRUE);
$this->load->view('layout_network',$layout_network);
because the view is already loaded.
I hope you understand, sorry for my bad english.
Many thanks.
In the example you have given, why not just return the data from the helper function?
function recup_email()
{
$CI =& get_instance();
$CI->load->model('unite_model');
$membres = $CI->unite_model->email_membre_model();
return $membres;
}
So that is you view;
// CALL THE FUNCTION FROM THE HELPER
<?php $em = recup_email(); ?>
// DATA RECOUP FROM THE HELPER
<?php foreach($em as $e): ?>
<?php echo $e->email_membre; ?>
<?php endforeach; ?>

How to pass multiple variable data from model to view

Could you please tell me how my model,controller and view should look like if I want to pass the following variable data($amount1, $amount2, $amount3) to my view file via controller from my model.
case 1: $amount1=100;
case 2: $amount2=500;
case 3: $amount3=1000;
I want to have the variables in a way that I don't have to echo them in any { } example:
foreach ($records as $row){ $i++; ?>
// I don't want to echo those inside in this.
// I want to echo it like this way- <? echo $amount1;?>
}
Thanks in Advance :)
If you pass an array of data from your controller to your view you can access each element as a variable in the view. Here is an example of what I mean:
model:
class Model extends CI_Model
{
public function get_data()
{
$data = array(
'amount1' => 100,
'amount2' => 500,
'amount3' => 1000,
);
return $data;
}
}
controller:
class Controller extends CI_Controller
{
public function index()
{
// get data from model
$data = $this->model->get_data();
// load view
$this->load->view('view', $data);
}
}
view:
<h1><?php echo $amount1; ?></h2>
<p><?php echo $amount2; ?></p>
<!-- etc... -->
I have found a solution myself. I just wanted to share so that it can help others. So here it is..
Your model should look like following :
function net_income(){
$data['amount1']=50;
$data['amount2']=100;
return json_encode($data);
}
Your controller:
function add(){
$this->load->model('mod_net_income');
$json = $this->mod_net_income->net_income();
$obj = json_decode($json);
$data['amount1']= $obj->{'amount1'};
$this->load->view('your_viewfile_name',$data);
}
And then in your view file: just
<? echo "$amount" ; ?>
Thanks :)

Unable to load database table contents with ActiveRecord

I'm new to CodeIgniter and am having trouble loading the contents of a simple database table (named 'entries') with the ActiveRecord syntax--I'm getting a blank page.
Here is my controller:
class Blog extends CI_Controller {
function Blog() {
parent::__construct();
}
function all() {
$this->load->model('Entries');
$data['rows'] = $this->Entries->load_all();
$this->load->view('view_all', $data);
}
}
Model:
class Entries extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
function load_all() {
$query => $this->db->get('entries');
return $query->result();
}
}
View:
<ol>
<? foreach($rows as $row): ?>
<li><?= $row->title ?></li>
<? endforeach; ?>
</ol>
NOTE: I can get it to work if I change the load_all() function in my model to:
function load_all() {
$sql = "SELECT * FROM entries";
$query = $this->db->query($sql);
return $query->result_array();
}
And my view to:
<ol>
<? foreach($rows as $row): ?>
<li><?= $row['title'] ?></li>
<? endforeach; ?>
</ol>
Any thoughts why the ActiveRecord syntax isn't working?
FYI: CodeIgniter 2.0, MySQL, PHP 5.3.2. Oh and the $active_record setting in config/database.php is TRUE.
Thanks.
In your load_all() function you have a misplaced '=>' after $query. It should be a '='
$query = $this->db->get('entries');
Then you can return your $query object.
return $query->result();
On another note, you don't need to use capital letters when calling your model. Even though the model name may be capitalized, the object function call is allowed to be lowercase. Your code won't break if you use capital, you just don't need to.
I think you need to tell your view that the query result needs to be extracted:
<ol>
<? foreach($rows->result() as $row): ?>
<li><?= $row->title ?></li>
<? endforeach; ?>
</ol>

Resources