How can I show an associative array in codeigniter view from controller page?
Here is my code below. I want to show the values of array in view.
Please help me!
function colorr(){
$color['color'] = array("RED"=>"#FFCC00","GREEN"=>"#99FF00","YELLOW"=>"#FF0000");
$this->load->view('color_all',$color);
}
In your view file, you can access it by:
print $color["RED"]; // will output "#FFCC00"
print $color["GREEN"]; // will output "#99FF00"
Or you can loop:
foreach($color as $k => $v)
{
print $k . " => " . $v . " <br />";
}
In your case, this will output something like this:
RED => #FFCC00
GREEN => #99FF00
YELLOW => #FF0000
Related
this code in controller gives me five rows but when i use it in view file it just returns first row like:
a
b
c
d
e
controller:
$data = [
'lesson' => $x->name,
];
echo $data['lesson'].'</br>';
$this->view('manage/add-cat', $data);
view:
echo $data['lesson'].'</br>';
it returns just:
a
!!!
I've tried:
foreach($data['lesson'] as $y):
echo $y;
endforeach;
I'd be thankful if you help :)
Array ( [0] => 18 [1] => 1 )
how i can get only 0 index value?. I am using this code in codeigniter. Can any one help?. Is this possible with for each loop so it access all indexes but show only zero index?
foreach($m as $m)
{
echo $m->['0'];
}
If you want only first index then you don't need to foreach loop
Just write:
echo $m[0];
And if you want all index of array then:
foreach ($m as $key => $value) {
echo $key;
}
Try this:
print_r($m[0]);
or
foreach($m as $m)
{
echo $m['0'];
}
Following is the code to get value of index 0
//$array variable declared containing multiple values
$array=array('0' =>"first value",'1' =>"second value",'2' =>"third value" );
echo "<pre>";
print_r($array); // helps in printing the value key and value
//iterating through each values in the array
foreach ($array as $key => $value) {
if($key==0) //checks if key is 0
{
echo $value; //prints the value in key 0
}
}
I've add module "categories" to homepage, and now I see categories list with numer of products on homepage but can't see category thumbs.
in file: /public_html/catalog/view/theme/MY-THEME/template/module/category.tpl I found around line 10:
<?php echo $category['name']; ?>
and I think sommewhere there I also should add something like: category['thumb'] to display image before category name but do I have to add something in controller files, I soppouse yes but need help here.
I thing i should define $category['thumb'] in controller but how to do this?
You're correct in saying that you need to edit the controller file for this module, you need to add the model that gets the image for that category as well as get the image for any child categories. So there are two edits you need to make, first to the controller file
Open the catalog/controller/module/category.php & find this line:
$this->load->model('catalog/product');
Now you need to add the model so that you can get the image data from the DB, add this line below it:
$this->load->model('tool/image');
Now find this line, it will be in the foreach loop:
$children = $this->model_catalog_category>getCategories($category['category_id']);
Add the following above the line:
$category_info = $this->model_catalog_category>getCategory($category['category_id']);
if ($category_info['image']) {
$image = $category_info['image'];
} else {
$image = '';
}
You also need to do this for the child categories, find this line in the next foreach loop:
$product_total = $this->model_catalog_product->getTotalProducts($data);
Add this directly below it:
$child_info = $this->model_catalog_category>getCategory($child['category_id']);
if ($child_info['image']) {
$child_image = $child_info['image'];
} else {
$child_image = '';
}
At the moment we now have two variables that contain the image paths: $image and $child_image, these need to be passed to the template file, you can do so by editing the two arrays that are created. We'll do the child array first as that is almost directly below the last edit you made. Find this line in the children_data array:
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
Add a comma to the end of the line and paste this line directly below it:
'image' => $child_image
Now we add the image to the $category array, find this line and add a comma to the end as with the last step:
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
The next step is obvious now, add the following line directly below it:
'image' => $image
Now you should note that the template file can access a new variable $image when in the foreach loops. I'll leave the rest to you as you'd know how and where you'd like to display the image. The code below will generate an image if it's used in the foreach loop of either the Category:
<img src="<?php echo $category['image']; ?>
Or this can be used for a child category:
<img src="<?php echo $child['image']; ?>
I have some $data that i am transfering to view, ok that is easy
But i want this
$data['promenjive']=array
(
'1' => prva,
'2' => 'druga',
'3' => 'treca',
);
And
$this->load->view("view_home", $data);
My question is how to echo a single value from $data['promenjive']
To make something like this
1
I dont want to foreach entire array just one element?
In your example prva would echoed from within your view template with:
<?php echo $promenjive[1];?>
To echo 1 from your example you would have to do:
<?php echo array_search('druga',$promenjive);?>
I have following method:
public function actionIndex() {
$companyModel = Company::model()->findAll();
$supplierProductModel = SupplierProduct::model()->findAll();
$this->render('index', array(
'companyData' => $companyModel,
'supplierProductData' => $supplierProductModel,
));
}
Here I have passed model objects to render function and want to access these objects in view (Active Relational Type) but when I am accessing its in view its showing error:
Trying to get property of non-object
view file (index.php)
echo $companyData->name . "\n";
echo $this->companyModel->phone . "\n";
echo $this->companyModel->fax . "\n";
echo $this->companyModel->cell . "\n";
Any help would be appreciated.
you need to declare $this->companyModel in your controller/action
$this->companyModel = Company::model()->findByPk($companyId);
with Company::model()->findAll() you get an array of Company-Models you can iterate over in your view-file.
foreach ($companyData as $companyModel) {
var_dump($companyModel->attributes);
}
It is happening becoz of findAll()
findAll() reruns all rows of company table in multidimensional array, so here
$companyData is multidimensional Array, now change your code in index like bellow,
<?php
foreach ($companyData as $compSub)
{
echo $compSub->name . "\n";
echo $compSub->phone . "\n";
echo $compSub->fax . "\n";
echo $compSub->cell . "\n";
}
?>
If you want a company data(single row), change your query like this
$companyModel = Company::model()->findByPk($id_Of_company);
//$companyModel is single dimensional array, it has all the info of a company.
Send this to view
$this->render('index', array(
'companyData' => $companyModel,
....................
));
Now you can show the data using bellow code
echo $companyData->name . "\n";
echo $companyData->phone . "\n";
echo $companyData->fax . "\n";
echo $companyData->cell . "\n";
You are trying to get all the entries from the database as findAll() returns all data in multidimensional array of objects.If you need all the entries you could iterate over it in the view file and get the results as shown
In the View File do as shown
<?php foreach($companyData as $value){
echo $vlaue->name . "\n";
echo $value->phone . "\n";
echo $value->fax . "\n";
echo $value->cell . "\n";
?>
With this you get all the entries from the table
If you want to get a particular record use condition using Cdbcriteria and pass the object and get the single result