How to add array to $data array CodeIgniter - codeigniter

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

Related

why pass data in view file just returns first row?

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 :)

Opencart 1.5.5.1 categories images on homepage

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']; ?>

Active records on codeigniter page

I want print a table from database on view page with where condition, means I want print table of student where class is seven and fee status is paid.
How we do this?
i tried it but not work:
<?php
$students = $this->db->get_where('student' , array('status' => paid,'class_id'=>7))->result_array();
foreach($students as $row):?>
I have used this code to find the answer for you Please see it and try it out
$query = $this->db->get_where('tasks', array('description' => 7,'status' => 1));
echo "<pre>";
print_r($query->result_array());
change your array element as this since paid is a string it must be in quots
array('description' => "paid",'class_id'=>7)

cakephp211 date insertion

this is my variable:
<?php
//...
$creat='2000-03-15' ,from an input field.
?>
this is my insertion code:
<?php
//...
$this->Comment->updateAll(
array('Comment.c' => $creat ),
array('Comment.id' => '3' ));
?>
when i want to save it in a table which have a date field type, instead of '2000-03-15', i have '0000-00-00'?
why?
thanks for comments!
the updateAll function does not escape your input. It will also not wrap your data in quotes.
In your example, to properly escape your $creat variable, i would suggest using the following code:
<?php
App::uses('Sanitize', 'Utility');
$Comment->updateAll(array(
'Comment.c' => "'" . Sanitize::escape($creat) . "'"
), array(
'Comment.id' => 3
));
We wrap the date with quotes and also pass it through Sanitize::escape to make sure we protect ourselves against SQL injection.

CodeIgniter - Returning multiple file upload details

Im using the codeigniter upload library to upload multiple files, which works fine ... What im having problems with is returning the information about the files.
Im using the following code to print the results for testing
echo '<pre>'; print_r($this->upload->data()); echo '</pre>';
A cut down version of the results are as follows
Array
(
[file_name] => Array
(
[0] => filename1.gif
[1] => filename2.jpg
)
)
The way my view is setup, is that i use jquery to insert multiple dynamic file input fields so the amount of files can be 1, it can be 50 and so on.
Im wondering how i would loop through that array to send each filename to the database
This would loop through the results and insert each into a table:
$data = $this->upload->data();
$table_name = 'files'; // change to whatever you need.
$column_name = 'file'; // change to whatever you need.
foreach ($data['file_name'] as $file_name){
$this->db->insert($table_name, array($column_name => $file_name));
}

Resources