Get data from helper in CodeIgniter - 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; ?>

Related

Code Igniter:Navigation(View) to Model:Severity: Notice Message: Undefined property:

UPDATE:
$ci =&get_instance();
$ci->load->model(your model);
$ci->(your model)->(your function);
Note: You have to call your model in your controller.Its working fine
Using above answer from (access model from view in codeigniter?)
My above code should work. But why it does not.
I am loading my menu in navigation using database. Im connecting directly from my model. In my navigation I have
$CI =& get_instance();
$CI->load->model('masterdata/MasterDataRole_','masterdatarole');
$menu = $CI->masterdatarole->loadMenu();
In my masterdata/MasterDataRole_ I have
<?php
class MasterDataRole_ extends CI_Model{
//menu
//submenu
//screen
//check access rights
public function hasAccess($page_id,$level){
$query = $this->db->select('RoleAccess')
->from('masterdatarolemapping')
->where('ItemLevel',$level)
->where('ItemSysID',$page_id)
->where('MasterDataRoleID',$this->session->userdata('UserID'))
->get();
if($query){
if($query->num_rows() > 0){
$data = $query->row();
return $data->access;
}
}
}
}
But I get error saying
A PHP Error was encountered
Severity: Notice
Message: Undefined property: MasterDataRole::$menu
Filename: templates/navigation.php
Line Number: 117
In my Navigation I have:
<?php foreach ($menu as $m): echo $m;?>
<?php if($CI->menu->hasAccess($m->SysID,'menu') == 'yes'): ?>
<?php endif; ?>
<?php endforeach; ?>
Any Idea is appreciated.
UPDATE
error is on this <?php if($CI->menu->hasAccess($m->SysID,'menu') == 'yes'): ?>
this line <?php foreach ($menu as $m): echo $m;?> give the list of menu
$CI->load->model('masterdata/MasterDataRole_','masterdatarole');
$menu = $CI->masterdatarole->loadMenu();
Should be masterdatarole not menu

Fetching and updating data from a database

I want to fetch and update data from database using codeigniter. I am facing some problem. This is my code:
This is my Model by the name of Update_site_model.
<?php
class update_site_model extends CI_Model{
function show_invoice_id($data) {
$this->db->select('*');
$this->db->from('invoices');
$this->where('invoiceId', $data);
$query = $this->db->get();
$result = $query->result();
return $result;
//Update Query For Selected Invoice.
function update($id,$data) {
$this->db->where('invoiceId',$id);
$this->db->update('invoices',$data);
}
}
?>
This is my Controller by the name of update site.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Update_site extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->view('includes/header');
$this->load->view('site/update');
$this->load->view('includes/footer');
//load the model
$this->load->model('update_site_model');
}
function show_invoices() {
$id = $this->uri->segment(3);
$data['single_invoice'] = $this->update_site_model->show_invoice_id($id);
$this->load->view('site/update', $data);
}
function update() {
$id = $this->input->post('invoiceId');
$data = array(
'Date' => $this->input->post('invoiceDate'),
'Client' => $this->input->post('invoiceClient'),
'Amount' => $this->input->post('invoiceAmount'),
'Status' => $this->input->post('invoivceStatus')
);
$this->update_site_model->update($id, $data);
}
}
This is my view by the name of update. it is inside a folder by the name of site.
<div id="content">
<ol>
<?php foreach ($invoices as $invoice): ?>
<li><?php echo $invoice->invoiceId; ?></li>
</ol>
<?php endforeach; ?>
</div>
and this is the problem.
Severity: Notice
Message: Undefined variable: invoices
Filename: site/update.php
Line Number: 11
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: site/update.php
Line Number: 11
Please help me to solve the problem.
Look at this in your controller:
$data['single_invoice'] = $this->update_site_model->show_invoice_id($id);
and now look at this in your view:
<?php foreach ($invoices as $invoice): ?>
did you got your mistake..?
no..?
let me tell you , since you are storing the database result in $data['single_invoice'] you should access this by $single_invoice in the view.
so correct usage in your view is :
<?php foreach ($single_invoice as $invoice): ?>
and next time when you ask any question give appropriate title to it.

codeigniter: modify value in view from controller

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 ?>

Best way for creating dynamic sidebar section in Symfony 1.4

I have few frontend modules which has own sidebar menu link. I want to create those links in actions class of module:
public function preExecute()
{
$items['plan/new'] = 'Create Plan';
$items['plan/index'] = 'Plans Listing';
$this->getResponse()->setSlot('sidebar', $items);
}
Slot file sidebar.php
#apps/frontend/templates/sidebar.php
<?php slot('sidebar') ?>
<ul>
<?php foreach($items as $url => $title) : ?>
<li><?php echo link_to($url, $title) ?></li>
<?php endforeach ?>
</ul>
<?php end_slot() ?>
layout.php:
<?php if (has_slot('sidebar')): ?>
<div id="sidebar"><?php include_slot('sidebar') ?></div>
<?php endif ?>
but my output is Array, how can I render my slot?
You seem to be mixing slots and partials. In your action, you set your slot to an array, later you call include_slot, and the string representation is Array, that is correct.
You should pass items via $this->items = $items, then in your action see if isset($items) is true, and call include_partial("sidebar", array("items" => $items)) if neccesary. This will look for a file called _sidebar.php.
For more detailed information of how this stuff works, read the Inside the View Layer: Code fragments part of the sf1.4 book.

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