Codeigniter: Is a view supposed to be reusable - codeigniter

in MVC pattern, does the view need to be reusable. Are there any principles or points to be noted while outputting data via views.
So if i have a view with certain amount of text and a table with 3 columns, and if i would need another view with minute changes like the text would be a little different and a table with 4 columns, so should i use variables to store the table structure in the model itself and then pass it to the view via the controller,
or
use variables to change the flow of execution in the view,
or best define a new view?
Moreover what if i have an object with the table data, is it good to use the object in the view, use a for loop and display the data or are there better ways? Thanks
P.S I'm new to codeigniter

If you want to display data from 2 tables and your desired output is different I recommend you to use 2 views. In your model you should make function to get all the neccessary data and then pass it to controller. In controller you should assign them to variable and pass them to view. See the example below.
tbl_articles
article_id | article_title | article_text
1 Title of article Sample text of article
2 Title of article no. 2 sample text of article no. 2
tbl_reviews
review_id | review_title | review_text | review_rate
1 Title of review Sample text 5
2 Title of review no. 2 Sample text 2 4
Controller:
<?php
class Site extends CI_Controller {
public function articles()
{
$this->load->model('main_model');
$data['articles'] = $this->main_model->get_articles();
$this->load->view('articles', $data);
}
public function reviews()
{
$this->load->model('main_model');
$data['articles'] = $this->main_model->get_reviews();
$this->load->view('reviews', $data);
}
}
Model:
<?php
class Main_model extends CI_Model {
public function get_articles()
{
return $this->db->select('article_id, article_title, article_text')
->get('tbl_articles')
->result();
}
public function get_reviews ()
{
return $this->db->select('review_id, review_title, review_text, review_rate')
->get('tbl_reviews')
->result();
}
}
Views:
- Article view:
<?php
if (count($articles) == 0):
echo 'Nothing to show';
else:
foreach($articles as $article):
?>
<h1><?php echo $article->article_title; ?></h1>
<p><?php echo $article->article_text; ?></p>
<?php
endforeach;
endif;
?>
- Reviews view:
<?php
if (count($reviews) == 0):
echo 'Nothing to show';
else:
foreach($reviews as $ review):
?>
<h1><?php echo $ review -> review _title; ?></h1>
<p><?php echo $ review -> review text; ?></p>
<p>Rate of user: <?php echo review->review_rate; ?></p>
<?php
endforeach;
endif;
?>

Related

codeigniter: modify value in view from controller

I am trying to modified value from controller to view. I would like to achieve that when a user is signing up, and submited, I can return feedback from model to controller to view.
my view: main_view
<?php include('header.php'); ?>
<?php echo $sign_up_results ?>
<?php include('forms/forms.php'); ?>
<?php include('footer.php'); ?>
my controller
function __construct(){
parent::__construct();
$this->load->helper('url');
$template = $this->load->View('main_view');
}
function form_sign_up_controller(){
$this->load->model("form_sign_up");
$database_insert_results = $this->form_sign_up->insert_user_detail_into_db();
$data['sign_up_results']=$database_insert_results;
$template = $this->load->View('main', $data);
}
The problem is when the view is loaded, the value "$sign_up_results" is not yet defined. Is there anyway that I can define the value and then change the value according to the results return from model to controller.
just use is set to check if the parameter is defined or not
<?php if(isset($sign_up_results)) echo $sign_up_results ?>

Codeigniter pass form value as url segment

I have an issue with codeigniter. I want to pass a form value (from a dropdown) as URL segment in the next page. However I have searched high and low but could not find the solution.
Here is my view:
<?= form_open('admin_gallery_upload/upload_images/'); ?>
<?= form_label('Gallerij', 'gallery_id'); ?><br/>
<?= form_dropdown('gallery_id', $select_dropdown); ?>
<?= form_submit('submit', 'Volgende', 'class="submit"'); ?>
<?= form_close(); ?>
My controller:
function upload_images() {
$gallery_id = $this->input->post("gallery_id");
echo $gallery_id;
}
So instead of echoing the $gallery_id as done in the controller, it should become the third url segment
Try this and put select box id as gallery_id:
function redirectFunction(){
var val = document.getElementById("gallery_id").value;
alert(val); //see if alerts the correct value that is selected from the dropdown
window.location.href = '<?php echo base_url()?>admin_gallery_upload/upload_images/'+val;
}
Change the following lines, to add the id of the dropdown as I said,
$js = 'id="gallery_id" onChange="redirectFunction();"';
form_dropdown('gallery_id', $select_dropdown,'',$js);
You can pass in form open function extra parameter by default it is post see below code :-
<? $arr = array('method'=> 'GET');?>
<?= form_open('admin_gallery_upload/upload_images/',$arr); ?>
And get on controller using :-
$this->input->get();

How to make second root category navigation in magento?

Other than the normal navigation I get when I add subcategories to the main root category, I want to be able to make a new root category, assign subcategories to it and have it display as a separate menu.
Is this possible?
May this can help you :Link 1Link 2
To retrieve another root category
<?php
echo '<pre>';
$id=9;
$catagory_model = Mage::getModel('catalog/category');
$categories = $catagory_model->load($id); // where $id will be the known category id
if(!empty($categories))
{
echo 'category name->'.$categories->getName(); //get category name
echo '<br>';
echo 'category image url->'.$categories->getImageUrl(); //get category image url
echo '<br>';
echo 'category url path->'.$categories->getUrlPath(); //get category url path
echo '<br>';
}
?>
now $id=9; is my new root category id.
To retrieve sub categories of these new root category ($id=9;) below is the following reference code.Customize it according to your requirements.
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<?php foreach($categories as $category): ?>
<?php $subcategories = $category->getChildren() ?>
<?php foreach($subcategories as $subcategory): ?>
<?php $subsubcategories = $subcategory->getChildren() ?>
<?php foreach($subsubcategories as $subsubcategory): ?>
<?php endforeach; ?><!-- end foreach subsubcategories -->
<?php endforeach; ?><!-- end foreach subcategories -->
<?php endforeach; ?><!-- end foreach categories -->
In an ideal world you would have found the answer by now, but in case you didn't I modified Nikhil's answer to work for me to basically do what you describe, minus any convenience at all...
$id=9;
$catagory_model = Mage::getModel('catalog/category');
$categories = $catagory_model->load($id);
if(!empty($categories))
{
$cats = explode(",", $categories->getChildren());
foreach($cats AS $c)
{
$cat = $catagory_model->load(trim($c));
echo ''.$cat->getName().'';
}
}
I'm just pasting what I used. The reality is you will have to build the html to make this do whatever you want it to do. If you have subcategories within your loop, you will have to run another fetch in the foreach part.
I am not familiar with magento enough to know what methods you can run on the $cat object. I did a print_r($cat) to examine the object and made a lucky guess that getUrlKey would be available.
Magento... pfft! you'd think ebay would have higher standards than this.

Transform a horizontal table row into a vertical one

I am using CodeIgniter 2.1.0 and MySQL. I want to display a horizontal data row as a vertical one. When I fetch a single row from the database and echo it, it looks like
----------------------------------------
id | name | address | email |
----------------------------------------
1 | Foo | Bar | foo#bar.com |
----------------------------------------
I have used CodeIgniters table library to generate the above table. instead of this, I want it show like this:
------
id : 1
name: foo
address : bar
email: foo#bar.com
-------------------
How do I do this with CodeIgniter 2.1.0?
If you have any problem to implement my previous answer... Here is the details...
Let me know is it useful or not?
Thanks.
/* for controller */
$data['user_data'] = $this->modle_name->function_name;
modle_name= your modle name where the specific function exists.
function_name= function into the modle by which you get all the table value from mysql db.
then write the function currectly in modle and go to the view page:it may be like that
/* model */
function function_name(){
$sql = "select * from tablename where condition";
$query = $this->db->query($sql);
return $query->result_array();
}
/* for view */
<?php foreach ($user_data as $data) {?>
<tr>
<?php echo 'ID:' . ' ' ?>
<?php echo ($data['id']); ?>
<?php echo 'Name:' . ' ' ?>
<?php echo ($data['name']); ?>
<?php echo 'Address:' . ' ' ?>
<?php echo ($data['address']); ?>
<?php echo 'E-mail:' . ' ' ?>
<?php echo ($data['email']); ?>
</tr>
<?php } ?>
i have figured out one solution to this problem, but i think it is not the best way.
instead of using table library, i have changed my model,controller and view like this:
model:
function detail()
{
$this->db->where('id',$this -> session -> userdata('id'));
$query=$this->db->get('user');
$row=$query->row_array();
return $row;
}
controller:
$this->load->model('my_model');
$this->my_model->detail();
$data=array(
'id'=>$query['id'],
'name'=>$query['name'],
'address'=>$query['address'],
'email'=>$query['email']
);
$this->load->view('my_view',$data);
view:
<div>
id : <?php echo $id;?><br/>
name: <?php echo $name;?><br/>
address: <?php echo $address;?><br/>
email: <?php echo $email;?>
</div>
this works perfectly. but i wished to solve this in a more simpler manner.
This problem has a very easy solution by using a single foreach loop in your view page:
/* for controller */
$data['user_data'] = $this->modle_name->function_name;
modle_name= your modle name where the specific function exists.
function_name= function into the modle by which you get all the table value from mysql db.
then write the function currectly in modle and go to the view page:
/* for view */
<?php foreach ($user_data as $data) {?>
<tr>
<?php echo 'ID:' . ' ' ?>
<?php echo ($data['id']); ?>
<?php echo 'Name:' . ' ' ?>
<?php echo ($data['name']); ?>
<?php echo 'Address:' . ' ' ?>
<?php echo ($data['address']); ?>
<?php echo 'E-mail:' . ' ' ?>
<?php echo ($data['email']); ?>
</tr>
<?php } ?>

Best way for creating dynamic sidebar section in Symfony 1.4

I have few frontend modules which has own sidebar menu link. I want to create those links in actions class of module:
public function preExecute()
{
$items['plan/new'] = 'Create Plan';
$items['plan/index'] = 'Plans Listing';
$this->getResponse()->setSlot('sidebar', $items);
}
Slot file sidebar.php
#apps/frontend/templates/sidebar.php
<?php slot('sidebar') ?>
<ul>
<?php foreach($items as $url => $title) : ?>
<li><?php echo link_to($url, $title) ?></li>
<?php endforeach ?>
</ul>
<?php end_slot() ?>
layout.php:
<?php if (has_slot('sidebar')): ?>
<div id="sidebar"><?php include_slot('sidebar') ?></div>
<?php endif ?>
but my output is Array, how can I render my slot?
You seem to be mixing slots and partials. In your action, you set your slot to an array, later you call include_slot, and the string representation is Array, that is correct.
You should pass items via $this->items = $items, then in your action see if isset($items) is true, and call include_partial("sidebar", array("items" => $items)) if neccesary. This will look for a file called _sidebar.php.
For more detailed information of how this stuff works, read the Inside the View Layer: Code fragments part of the sf1.4 book.

Resources