I want to send two data arrays from my controller to view how can I do it ?
Following is my controller code
class Home extends CI_Controller {
public function box() {
$url = $this->pageURL();
$id_from_url = explode('/', $url);
$id = $id_from_url[6];
$query = $this->db->get_where('mc_boxes', array('idmc_boxes' => $id));
$row = $query->row();
$rowcount = $query->num_rows();
if ($rowcount <= 0) {
echo 'ID not found';
} else {
$box_id = $row->idmc_boxes;
$customer_id = $row->customers_idcustomers;
$language_id = $row->languages_idlanguages;
$template_id = $this->getTemplateID($box_id);
$template_data = $this->getTemplateData($template_id);
$variables_data = $this->getVariables($customer_id, $language_id);
$title = $variables_data[0]['value'];
$this->load->view('template', $template_data);
}
}
}
In my template view when I echo $title it says it is undefined
how can I send the whole $variables_data array with $template_data array
Thanks :)
Instead of using each array ,all do set to one
Like that,giving important sections only
...................
$data['template_data'] = $this->getTemplateData($template_id);
$data['variables_data'] = $this->getVariables($customer_id, $language_id);
$data['title'] = $variables_data[0]['value'];
$this->load->view('template', $data);
you can take $template_data and $variables_data in view files
Generally you pass in data as:
$data['template_data'] = $template_data;
$data['title'] = $$title;
....
$this->load->view('template', $data);
Related
I wanted to pass some values to the view.
The $result may be 0 or 1 or '' and I tried to use this below code:
public function whatwedo($result='')
{
$result = array();
$result['status'] = $result;
$this->load->view('admin/whatwedo',$result);
}
public function add_whatwedo()
{
//$this->load->library('form_validation');
$this->form_validation->set_rules('text-input','Title','required');
if($this->form_validation->run() != true)
{
$result = 0;
$this->whatwedo($result);
}
else
{
$this->load->model('admin_model');
$result = $this->admin_model->ins_whatwedo($this->input->post());
//print_r($result);exit();
$this->whatwedo($result);
}
}
And in the view:
<?php
print_r($status);
?>
But, the $status is Array ( )
The problem is this line:
$result = array();
Because now the $result variable is an empty array so when you create the index status on result you assign it an empty array.
To fix you can do something like:
public function whatwedo($input = '')
{
$result['status'] = $input;
$this->load->view('admin/whatwedo', $result);
}
or even...
public function whatwedo($input = '')
{
$this->load->view('admin/whatwedo', ["status" => $input]);
}
why my public variable cannot read by other function?
my public variable :
public $dataku = array();
first i set data to my public variable from model:
function cari() {
$penerima = $this->input->post('penerima');
$tanggal = $this->input->post('tanggal');
$tanggal2 = $this->input->post('tanggal2');
$id_komoditas = $this->input->post('id_komoditas');
$this->dataku['judul'] = $this->judul;
$this->dataku['pencarian'] = $this->mdl->get_data_antara_tanggal($penerima, $tanggal, $tanggal2, $id_komoditas);
$this->dataku['url'] = $this->url;
$title = "Data ".$this->judul;
$subtitle = $this->url;
$content = $this->load->view('cari.php', $this->dataku, true);
$this->load_content($title, $subtitle, $content);
}
but, when i want to read the public variable data again, it hasnt a data :
function check_data(){
$data = isset($this->dataku['pencarian']) ? $this->dataku['pencarian'] : 'no data';
print_r($data);
}
please, i need your help.
you need to access the class properties with $this. Change the following lines:
$this->dataku['judul'] = $this->judul;
$this->dataku['pencarian'] = $this->mdl->get_data_antara_tanggal($penerima, $tanggal, $tanggal2, $id_komoditas);
$this->dataku['url'] = $this->url;
Also edit the following (you have an extra $):
function check_data(){
$data = isset($this->dataku['pencarian']) ? $this->dataku['pencarian'] : 'no data';
print_r($data);
}
I am displaying some data in popup but i don't want the header and footer there how can i achieve this in code-igniter
Note : i do not want to hide them by jQuery or CSS
This is my controller function to display data
function singleBookmarksView()
{
$web_url = $this->uri->segment(2);
$url_split_Array = explode('-', $web_url);
$web_id = $url_split_Array['0'];
//print_r($url_split_Array);
$data['singleBookmark'] = $this->bookmark_model->getSingleBookmark($web_id);
$data['bookmarkLabels'] = $this->getBookmarkLabel();
$this->load->view("header_view.php");
$this->load->view("bookmark_view.php", $data);
$this->load->view("footer_view.php");
}
Thanks
To do this you could either
function singleBookmarksView()
{
$web_url = $this->uri->segment(2);
$url_split_Array = explode('-', $web_url);
$web_id = $url_split_Array['0'];
$data['singleBookmark'] = $this->bookmark_model->getSingleBookmark($web_id);
$data['bookmarkLabels'] = $this->getBookmarkLabel();
$this->load->view("header_view.php");
$this->load->view("bookmark_view.php", $data);
$this->load->view("footer_view.php");
}
//create a new route for this
function singleBookmarksViewPopup()
{
$web_url = $this->uri->segment(2);
$url_split_Array = explode('-', $web_url);
$web_id = $url_split_Array['0'];
$data['singleBookmark'] = $this->bookmark_model->getSingleBookmark($web_id);
$data['bookmarkLabels'] = $this->getBookmarkLabel();
$this->load->view("bookmark_view.php", $data);
}
Or
//with this method add an extra value to the URI and it
//will show without the header and footer
function singleBookmarksView($popup = null)
{
$web_url = $this->uri->segment(2);
$url_split_Array = explode('-', $web_url);
$web_id = $url_split_Array['0'];
$data['singleBookmark'] = $this->bookmark_model->getSingleBookmark($web_id);
$data['bookmarkLabels'] = $this->getBookmarkLabel();
if (!$popup) {
$this->load->view("header_view.php");
}
$this->load->view("bookmark_view.php", $data);
if (!$popup) {
$this->load->view("footer_view.php");
}
}
I have a link that placed in my view page using codeigniter
<a href='".base_url()."user/pass_confirmation/$encrypted_string/$email'>
But the email and encrypted_string from controller..
that is not get to my view
controller
function email_check()
{
$email=$this->input->post('email');
echo $email;
$data = array(
'user_email' =>$email,
);
$result = $this->UM->email_verify($data);
if($result)
{
echo $result;
$date = date(Y-m-d);
$string = $email."-".$date;
$encrypted_string = md5($string);
echo $encrypted_string;
$res=$this->UM->insert_key($encrypted_string,$result);
redirect(base_url()."user/forgot_pass");
}
}
What can i do?
you need to get it from the uri segments like:
$string = $this->uri->segment(3);
$email = $this->uri->segment(4);
like this in your route.php file:
$route['controllerclass/function/(:any)/(:any)'] = "pass_confirmation/$1/$2";
and your link will be
domain.com/pass_confirmation/someval/someval
and then you can get string and email using segment like
$val1 = $this->uri->segment(3);
$val2 = $this->uri->segment(4);
I am working on a codeigniter site, in which a page is built using multiple views, I have come to a point where I need to work with a variable that is being set in another view is it possible to access it from another view?
This is code I have
Controller:
public function index() {
// $this->output->enable_profiler(TRUE);
$data = array();
if($query = $this->category_model->get_all_online()) {
$data['main_menu'] = $query;
}
$this->load->model('image_model');
/*
* Sort out the users backgrounds, basically do a check to see if there is a 'special' background
* if there is not a 'special' background then IF the user is logged in and has a background of there
* own show that one, if not show a generic one, if they are not logged in show a bang one
*/
$image = array();
if ($query = $this->image_model->get_special_backgrounds()) {
$image['special'] = $query;
} elseif(!isset($image['special']) && !isset($this->session->userdata['user_id'])) {
if($query = $this->image_model->get_mysite_background()) {
$image['generic'] = $query;
}
}
if(isset($this->session->userdata['user_id'])) {
if($query = $this->image_model->get_user_backgrounds($this->session->userdata['user_id'])) {
$image['user_background'] = $query;
}
}
$data = array_merge($data, $image);
$this->load->view('home/main_page.php', array_merge($data, $image));
}
Model:
public function get_mysite_background() {
$this->db->select('*');
$this->db->from('background');
$this->db->where('users_user_group_group_id', 1);
$this->db->where('is_special', 0);
$query = $this->db->get();
return $query->result_array();
}
public function get_special_backgrounds() {
$this->db->select('*');
$this->db->from('background');
$this->db->where('is_special', 1);
$query = $this->db->get();
return $query->result_array();
}
public function get_user_backgrounds($user_id) {
$this->db->select('*');
$this->db->from('background');
$this->db->where('users_user_id', $user_id);
$this->db->where('is_special', 0);
$query = $this->db->get();
return $query->result_array();
}
The Views in Question: Firstly where the variable is getting set,
</div>
<div id="background-select">
<?php
$count = 0;
if(isset($special)) {
foreach ($special as $row) {
$count ++;
print '<div class="select">';
print "<a href='index.php/home/category/".$row['background_id']."'>$count</a>";
print '</div>';
if($count = 1) {
$background = $row['background_name'];
}
}
}
if(isset($generic)) {
foreach ($generic as $row) {
$count ++;
print '<div class="select">';
print "<a href='index.php/home/category/".$row['background_id']."'>$count</a>";
print '</div>';
if($count = 1) {
$background = $row['background_name'];
}
}
}
if(isset($user_background)) {
foreach ($user_background as $row) {
$count ++;
print '<div class="select">';
print "<a href='index.php/home/category/".$row['background_id']."'>$count</a>";
print '</div>';
if($count = 1) {
$background = $row['background_name'];
}
}
}
?>
And where I want to use the variable:
<body>
<div id="wrapper" style=<?php echo"background:url(/media/uploads/backgrounds/".$background.");";?>>
The variable I looking to use $background
You can use the following helper class to transfer the values from one view to another:
class VarStore {
private static $values = array();
public static function get($key, $default = NULL) {
return (isset(self::$values[$key])? self::$values[$key] : $default);
}
public static function set($key, $value) {
self::$values[$key] = $value;
}
}
Call VarStore::set('background', $background); in the first view and $background = VarStore::get('background'); in the second.
Most of the problems in programming are conceptual and of understanding... The point is a View is to VIEW the data and not calculate it. calculations are carried out in models whose functions can be called by multiple modules when required. So better is before a view is called work-out all the data it has to display and use the view for VIEWING only not callucations... I hope this change of paradigm will help