I am trying to the array variable $popular_data in Codeigniter’s view, it is installed with Modesty script.
I tried to append this variable to the one already passed to view by array_merge, $data + $variable
in Model:
public function get_popular_products(){
$popular_data = $this->db->query(‘SELECT... ’)->result_array();
return $popular_data;
}
in Controller:
$this->load->model("Popularproduct_model");
$popularproducts = $this->Popularproduct_model->get_popular_products();
var_dump($popularproducts) here(in controller) shows the queried popular products on top of view as
array(3) {
[0]=>
array(34) {
["id"]=>
string(1) "3"
["title"]=>
string(13) "Patchouli Oil"
["slug"]=>
string(15) "patchouli-oil-3"
...
other variable passed to view as:
$this->load->view('index', $data);
So how to send the $popularproducts to the view and use the values?
Thanks.
store it in $data
$data['popularproducts'] = $this->Popularproduct_model->get_popular_products();
then make foreach inside your view.
foreach($popularproducts as $data){
echo $data['title'].'<br>'.
$data['slug'];
}
at first your can add the data in $data['popularproducts'] and then pass it into the view.
$data['popularproducts'] = $popularproducts;
$this->load->view('index', $data);
Make an array with each of variables:
$data['variable1'] = $variable1;
$data['variable2'] = $variable2;
$data['variable3'] = $variable3;
$this->load->view('template_name',$data) // add data array here in 2nd parameter
In CodeIgniter, we can send data from Controller to view as an associative array.
The function call $this->load->view() has two parameters:
1) View (template) file (path if its not in the default /application/views folder.
2) The associative data array to be passed.
$data['popularproducts'] = $popularproducts;
$this->load->view('index', $data);
If you pass $data in above scenario, your template index.php has a variable $popularproducts.
$popularproducts in the template/view is same as you passed from Controller.
To Pass multiple values from controller to view you should use an array as below given example
Each variable are as examples, you can convert it as per your requirement
Controller:
$data['variable'] = $variable;//from db or hardcoded
$data['arrayofObject'] = $arrayofObject;//from db or hardcoded
$data['arrayofArray'] = $arrayofArray;//from db or hardcoded
$this->load->view('index',$data) // add data array here in 2nd parameter
View:
i.e:
<div>
<span><?php echo $variable; ?></span>
</div>
<div>
<ul>
<?php if(!empty($arrayofObject)) { ?>
<?php for($arrayofObject as $obj) { ?>
<li><?php echo $obj->key; ?></li>
<?php } ?>
<?php } ?>
</ul>
</div>
<div>
<ul>
<?php if(!empty($arrayofArray)) { ?>
<?php for($arrayofArray as $ary) { ?>
<li><?php echo $ary['key']; ?></li>
<?php } ?>
<?php } ?>
</ul>
</div>
Related
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")
));
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;
I have the foloowing controller (supernavigationloggedin):
<?php
class Supernavigationloggedin extends CI_Controller {
function index(){
#get current session id
$currentSessionID = $this->session->userdata('session_id');
#get all the account row for the given sessionID
$data['info'] = $this->db->get_where('Client', array('session_id'=>$currentSessionID))->row();
#views
$this->load->view('supernavigationloggedin',$data);
}
}
?>
and the following view named(supernavigationloggedin):
<div id="superNavigation">
<h5><strong>Welcome</strong>, <?php $info->fname; ?> Account Settings</h5>
<div class="clearL"> </div>
</div
>
It keeps throwing an error on line:<h5><strong>Welcome</strong>, <?php echo $info['fname']; ?> <a href="#">Account that Message: >>> Trying to get property of non-object
I've tried : <?php echo $info['fname']; ?> <?php echo $info->fname; ?> but neither seem to work.
It's because $info is empty and no database request are made. You have to do this this way if you want your query to return an object:
$data['info'] = $this->db->get_where('Client', array('session_id'=>$currentSessionID))->row();
Or this way, if you prefer to get it into an array:
$data['info'] = $this->db->get_where('Client', array('session_id'=>$currentSessionID))->row_array();
This way it should work. row() or row_array() is necessary to execute your query.
Where should we process mysql queries in CodeIgniter application?
For example in a simple project we do like this :
for controller:
class Blog extends CI_Controller {
function posts(){
$data['query'] = $this->blog_model->index_posts();
$this->load->view('blog_view', $data);
}
}
and in view :
<?php
while ($post = mysql_fetch_object($query)):
?>
<div>
<p><?= $post->body; ?></p>
</div>
<?php endwhile; ?>
But, if we want to do something with body of post before print where should it be done?
For example, I want to write a function that formats the body of post and pass the body to it before doing echo.
Where should it be placed according to CoeIgniter's structure and recommended practices? (best option)
in the controller? (if so , how to use it)
in the view?
write a helper?
other approaches ?
Here's what is recommended:
Controller:
function posts() {
$this->load->model("blog_model");
$data['rows'] = $this->blog_model->index_posts();
$this->load->view("blog_view", $data);
}
Model: (blog_model.php)
function index_posts() {
$this->load->database();
$query = $this->db->get('your_table');
$return = array();
foreach ($query->result_array() as $line) {
$line['body'] = ... do something with the body....
$return[] = $line;
}
return $return;
}
View: (blog_view.php)
<?php foreach ($rows as $line): ?>
<div>
<p><?php echo $line['column']; ?></p>
</div>
<?php endforeach; ?>
Basically what happens is your model returns a multidimensional array that is passed the view and processed using a foreach() loop.
Good luck!
If you want to reuse that function create a helper. If you want this function only once put it in your controller and call from that controller.
Models are just for accessing database or maybe in few other cases, but mostly just for accessing things in database or editing, deleting etc. and sending the result to controller for further processing.
In your case I would stick with helper.
E.g. you will create a file top_mega_best_functions.php and put it inside helpers folder.
Than you write ther e.g. something like
function red_text($input) {
echo '<span style="color: red;">';
echo $input;
echo '</span>';
}
Then load the helper in your autoloader file or load before using.
And use in your view or controller like
$blablabla = "This text will be red";
red_text($blablabla);
I have a model that returns a list of artist's names from a database along with their ID. I want to loop through these artists in my view and create links to their pages with the following format:
http://www.example.com/the-artist-name/artist-portfolio/ID.html
What is the best way to do this?
Controller
$data['artists'] = $this->artists_model->get_all(); // should return array
$this->load->view('yourview', $data);
View
<?php foreach($artists as $artist): ?>
<a href="http://example.com/<?php echo $artist['name']; ?>/artist-portfolio/<?php echo $artist['id']; ?>.html">
<?php echo $artist['name']; ?>
</a>
<?php endforeach; ?>
Pass the data from the model into the view and loop through it like you normally would.
In your controller:
$view_data['artists'] = $this->artist_model->get_artists();
$this->load->view('view.php', $view_data);
In your view:
foreach ($artists as $artist) {
echo "{$artist['name']}";
}