Display the result of a function in codeigniter view - php

In the index function of my controller I am calling getDirContents and I need to display the result of $data['arrDirContents'] in view
public function index()
{
$this->accesscontrol->can_or_redirect('view', 'translation');
$dir = './application/language/english';
$data['arrDirContents']=$this->getDirContents($dir, "~^.+_lang\.php$~i");
$this->output->view('translation/language',$data);
}
Any help on how to get the result of that function in view?

<?php
$dir = './application/language/english';
$data['arrDirContents']=$this->getDirContents($dir, "~^.+_lang\.php$~i");
?>
If this values contents single value means
In View Page just do echo
<?php echo $arrDirContents; ?>
If this values contents multiple value having array value
Do foreach print each variable value
<?php
foreach( $arrDirContents as $directs):
echo $directs;
endforech;
?>

$this->load->vars( array(
'arrDirContents'=>$this->getDirContents($dir, "~^.+_lang\.php$~i")
));

Related

How to display data from MYSQL in codeignitor?

I want to create a dynaic page, I have created model and controller and also data subitted in database successfully. Now, i'm having problem while displaying that data on front end.
Here is my Modal:
function getcorporate(){
$q="SELECT * from corporate";
$query=$this->db->query($q);
return $query->result_array();
}
Here is my Controller:
function corporate()
{
$popular['popular'] = $this->auth_model->getPopularcourses();
$data1['corporate'] = $this->auth_model->getcorporate();
$data["institute_details"] = $this->auth_model->getInstitutedetails();
$data1['course'] = $this->auth_model->getcoursesdetailes();
$this->load->view('nulearnFront/header', $data);
$this->load->view('nulearnFront/corporate', $data1);
$this->load->view('nulearnFront/footer', $popular);
}
Try this
First you can print_r() the data you receive.
print_r($corporate);
After that you can use foreach to display all the data
foreach($corporate as $value)
{
////do code according to your requirement
}
I hope this may be help out to solve your problem
Add View file this code
<?php
if (isset($corporate) && !empty($corporate)) {
foreach ($corporate as $cdata) {
echo $cdata->YourValue(db column name);
}
}
?>
You are so close to the answer. You are passing the data from your Controller class. So what you have to do is just get that data as the follows,
I get the corporate values as it is returning an array data. So here you go,
In your view.php file,
<?php
if (isset($corporate)) { // Check if the data is set or not
foreach ($corporate as $corporateData) {
?>
// Your HTML goes here, table or etc.
<?php echo $corporateData->databaseColumnName // Value that need to print from the database ?>
<?php
}
}
?>
Hope this helps you.
print the query and run it to check
function getcorporate(){
$q="SELECT * from corporate";
$query=$this->db->query($q);
print_r($this->db->last_query());die();
return $query->result_array();
}
if query works fine then you can foreah the query
foreach($corporate as $corporate)
{
echo corporate;
}
if it does not return result then change result_array() to result() in model

How to send multidimensional array as data using array key in codeigniter?

I just want to send some data as array from view to controller using a array key. But can not solve this following problem whenever it comes to send multidimensional array.
Here is my controller
public function all_products(){
$data['ls'] = $this->product_details('product1');
$data['ws'] = $this->product_details('product2');
$data['wr'] = $this->product_details('product3');
$this->load->view('view_page', $data);
}
public function product_details($key){
$data['g'] = // Some rows from database
$data['p'] = // Some rows from database
$data['o'] = // Some rows from database
return $data;
}
After that, if I print the data key, the output showing in view
<?=print_r("ls")?>
// ls1 [Output]
Thanks in advance.
You only need this in your function:
public function product_details($key)
{
$this->db->select()->from('mydb')->where('id', $key);
return $this->db->get()->row();
}
And retrived it in your Controller:
public function all_products()
{
$data['product1'] = $this->Product_model->product_details($key1);
$data['product2'] = $this->Product_model->product_details($key2);
$data['product3'] = $this->Product_model->product_details($key1);
$this->load->view->('product', $data);
}
In your view:
<p>Name of Product 1: <?php echo $product1->product_name; ?></p>
<p>Name of Product 2: <?php echo $product2->product_name; ?></p>
<p>Name of Product 3: <?php echo $product3->product_name; ?></p>

How to pass a value of a variable from view to controller in codeigniter

using form post method i need to pass a value of numIDto a controller
for example assigning a value to numID inside view,
$numID= 25;
i need to use value of numID in a controller and assign that to temp variable. so i use following line of code,
$temp= $_POST[numID];
but its unable to get the exact value. Please help me on this. If have any other alternate way please let me know.
Here is an example of sending info from view to the controller:
View:
<?php echo form_open('invoice'); ?>
<?php echo validation_errors(); ?>
<?php echo form_input('order_num', $this->input->post('order_num')); ?>
<?php echo form_submit('submit','submit'); ?>
<?php echo form_close(); ?>
Controller:
public function invoice()
{
$order = $this->input->post('order_num');
$this->General_Model->get_customer_order($order);
}
Model:
function get_customer_order($order)
{
. . .
$this->db->where('client_orders.id', $order);
$result = $this->db->get('client_orders');
. . .
}
Note we are using the input name and id as = "order_num"
Then we ask the controller to find it as $this->input->post('order_num');

How to display 'tree' data in the view in Cake PHP

I am doing the tree tutorial and would like to display the data in a view and wrap it in html etc. I am new to Cake PHP and this has been a bit of a pain. Below is an example of what I've tried. In short, I figure I could assign the output to a variable using set. I am doing everything wrong.
Controller
<?php
class CategoriesController extends AppController {
public function index() {
$this->set('output', $this->Category->generateTreeList(null, null, null, ' '));
}
}
?>
View
<?php foreach ($output as $data): ?>
<div><?php echo $data['Category']['name']; ?></div>
<?php endforeach; ?>
<?php unset($data); ?>
unlike other find methonds, generateTreeList doesen't return a named array but a plain array with numeric indexes
try print_r($output) and you'll see how the array is formatted and how you can use it in your view
to show your data, in your foreach cicle you just have to do this
echo $data;

PHP data array variable not defined in view

I am using codeIgniter and I am trying to pass an array of data. I have written like this:
$data['username']="Dumbo";
I also wrote this:
$data['shouts']=$this->Musers->getShout(); // retrieve data from table
Then I write:
$this->load->view("welcome_message", $data);
In view page, I wrote:
<?php echo $username;
foreach ($shouts as $shout)
{
echo $shout->shout;
echo '<br>';
echo $shout->timeStamp;
}
?>
Problem is that while the view did retrieve data from table and display results in view page, an error came up for $data['username'] saying:
"Undefined variable: username"
Why is that? The $data['username'] is already defined! Or what did I do wrong?
<?php echo $data['username']; ?>
If you wrote this, the error will occur.
Correct way is to write like
<?php echo $username; ?>
'username' is the index in the data array, which is passed to the view using the load method
$this->load->view("welcome_message", $data);
If you need to pass an array...
$data['usernames'] = $username_array;
$this->load->view("welcome_message", $data);
Then in the view,
<?php print_r($usernames); ?>
In your view, do this..
$data = array();
$data['username'] = "something";
$data['shouts']=$this->Musers->getShout();

Categories