if i have to display some data from the database, how do i pass this data in the admin controller? Say i am defining a block in my controller
$block = $this->getLayout()->createBlock('core/text')->setText('<h1>Main Block</h1>');
Can i pass any sort of data to the setText() function to display the data i require in the admin page or are there other functions? I want to display a few order no from my custom table in the back end.
I tried something like this : $block = $this->getLayout()->createBlock('core/text')->setText('<h1>Main Block</h1><div>'.$this->showAllRecordsAction().'</div>');
The showAllRecordsAction() retrieves data from the custom table and displays it. When i try this out, i see the data at the top of the page and not in the content block.
How can i display it in a content block? Thanks.
I was able to get the content displayed through a block.
Used this code in my indexController :
$this->loadLayout();
$this->_addContent($this->getLayout()->createBlock('adminhelloworld/view'))
->renderLayout();
This is my View.php in the Block/View.php
<?php
class Foostor_Adminhelloworld_Block_View extends Mage_Core_Block_Template
{
protected function _toHtml()
{
$html="";
$records = Mage::getModel('direcpay/database')->getCollection();
foreach($records as $db_record){
$html = $html . 'Order no : '.$db_record->getOrder_no().' Referecnce id : ';
$html = $html . $db_record->getDirecpay_refid()."<br/>";
}
return $html;
Hope this helps someone.
Related
I am novice in Codeigniter and I am working on pagination. But there is something wrong in my code. What mistake I have been done in the following code?
I have attached my code snippet below.
Route
$route['default_controller'] = "pages";
$route['default_controller/(:num)'] = "pages/index/$1";
Controller
public function index($page='home')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$config=[
'base_url' => base_url().'/pages/index',
'per_page' => 5,
'total_rows' =>$this->products_model->record_count()
];
$this->pagination->initialize($config);
$data['title'] = ucfirst($page); // Capitalize the first letter
$data['products'] = $this->products_model->get_products($config['per_page'], $this->uri->segment(3));
$data['request'] = 'pages/home';
$this->load->view('templates/content',$data);
}
Model
public function get_products($limit, $offset)
{
$this->db->order_by('id', 'DESC');
$this->db->limit($limit, $offset);
$query=$this->db->get('product');
return $query->result_array();
}
public function record_count($value='')
{
return $this->db->count_all("product");
}
View
<?php echo $this->pagination->create_links() ?>
In the View, I am displaying the records from a table. The first page of records displays, but when I click the second page it shows Error
"**404 Page Not Found**"
Any kind of help regarding this issue will be highly appreciated. Thanks in advance.
From your logic, the line "index($page='home') the index method gets the script to display from the parameter passed to it. On the first load, the default page, 'home.php' is matched, but when you select the second page, the CI router and index function look for maybe '1.php' or '2.php' and so on, depending on your paging, do those files exist for every page you request? Otherwise, you should instead check if the page variable is a valid page (e.g non-negative) then continue execution and load the results to your view
My master layout here's below and working fine. I just want little bit more passing a default variable with this master layout that I can get in every pages.
class MY_Controller extends CI_Controller {
public $layout;
function __construct() {
parent::__construct();
$this->layout='layout/master';
}
}
I need to pass variable like below:
function __construct() {
parent::__construct();
$data['msg'] = $this->session->flashdata('usermsg');
$this->layout=('layout/master',$data);
}
How do I get this.
If you are loading up the data dynamically from the controller with the help of $this->layout you can send the data like this.
Method 1:
If you are using the general method to load the data to the view you can use this method.
$this->load->view('profile_view', $data);
This will load the profile_view page along with the $data as you passs into it with the help of array()
Method 2:
If you have created a master Layout and you are passing the data from the controller to the Master Layout you need to do like this.
<?php
public function master_layout () {
$this->template['header'] = $this->load->view('include/header', $this->Front_End_data, true);
$this->template['navigation'] = $this->load->view('include/navigation', $this->Front_End_data, true);
$this->template['center'] = $this->load->view($this->middle, $this->Front_End_data, true);
$this->template['footer'] = $this->load->view('include/footer', $this->Front_End_data, true);
$this->load->view('include/index', $this->template);
?>
In this code the below line alone will be loaded dynamically based on the page which you call in the master Layout.
$this->template['center'] = $this->load->view($this->middle, $this->Front_End_data, true);
In order to pass the data to this center layout you can use the funciton like this.
$data['msg'] = 'Success';
$this->template['center'] = $this->load->view ($this->middle = 'pages/view_oage',$data, true);
$this->master_layout();
And in the page you can get the data to be printed using the foreach loop as follows.
foreach($msg as $value)
{
echo $value;
}
Im getting the id from a form using
$id=$this->uri->segment(2)
I need your help to replace that by something else generic without getting the id from the url
im using pagination in several pages too & im using
$this->uri->segment()
too ..
PS:when i want to display a list o programs that belong to a category & when i want to display the list of programs of channel that belong to a category ,the segment change so i wont use the segment i wanna get the id dynamically ..
Best regards
Don't know if this is what you are after, but you can send parameters in a standard codeigniter install with URI and catch them in the method params.
class Welcome extends MY_Controller {
public function index($a = NULL, $b = NULL, $c= NULL)
{
echo 'Testing params: <br>
Param1: ' . $a . '<br>Param2: ' . $b . '<br>Param3: ' . $c;
}
}
Calling http://example.com/index.php/welcome/index/test1/test2/test3 will render
Testing params:
Param1: test1
Param2: test2
Param3: test3
Example
Going by this comment code:
foreach $rows as $row .. .. id; ?>"
href="program/details_program/id; ?>"> program_title; ?> page detail.php $id
= $this->uri->segment(2); $query = $this->db->get_where('programs', array('id' => $id));
it looks like you have a program controller with a details_program method that you are linking to and this is where you want a different solution to uri->segment. If this is the case, something like this would work with a URL like http://example.com/program/program_details/123:
class Program extends CI_Controller{
function program_details($id = NULL){
$query = $this->db->get_where('programs', array('id' => $id));
}
}
If this isn't what you want, please update the question with all of the relevant (properly formatted) code
Can I set a variable in view file?
for exemple:
I have a controller: welcome.php
Its load:
$this->load->view('header');
$this->load->view('main');
$this->load->view('footer');
I need set a variable on file main.php and get on footer.php
Its possible?
You can pass a variable from one view to another just like you would from a controller to a view. You just have to load the view file that sets that variable before the view that uses that variable:
$this->load->view('main'); //load before
$this->load->view('footer'); //load after
Inside of main.php do $this->load->vars(array('your_variable'=>'it's value')); and you will be able to call it in the footer like you would any other variable. The only requirement is that main.php be loaded before footer.php.
It is not possible to set a variable in a view file and access it from another. There is no reason you should be setting variables in your view files anyway. The controller should be handling all of your application logic so you should be setting the variable there. I would recommend reading through the user guide or looking at some articles to get a better grasp of MVC principles.
To access the same variable in multiple views, pass it to each view you load.
// Set your variable
$data['variable'] = 'value';
// Pass variable to multiple views
$this->load->view('main', $data);
$this->load->view('footer', $data);
You can pass both array or object to your view, for example:
$data = new StdClass;
$data->title = "The Title";
$data->content = "The Content";
$this->load->view('main', $data);
or
$data = new SomeClass;
$this->load->view('main', $data);
Also, in each view you are able to pass a different data:
$data1 = array("key" => "val");
$data2 = $this->some_class->some_method($params);
$data3 = $this->another_class->another_method($params);
$this->load->view('navigation', $data1);
$this->load->view('main', $data2);
$this->load->view('footer', $data3);
Yes you can, to pass variable from controller you can type the variable data after the name of view, this is the example :
$data["vehicle"] = $vehicle;
$this->load->view('header',$data);
then in your view just call the name of variable :
<td>
<?php echo $vehicle ?>
</td>
Hope it can help
I have a controller with an index function as follows:
function index()
{
$this->load->model('products_model');
$data['product'] = $this->products_model->get(3); // 3 = product id
$data['product_no'] = 3;
$data['main_content'] = 'product_view';
//print_r($data['products']);
$this->load->view('includes/template', $data);
}
This is the get function in the products_model file
function get($id)
{
$results = $this->db->get_where('products', array('id' => $id))->result();
//get the first item
$result = $results[0];
return $result;
}
The products table contains fields such as name, price etc. Please can you tell me how to output variables from $data['product'] after it is passed into the view? I have tried so many things but nothing is working, even though the print_r (commented out) shows the data - it is not being passed into the view. I thought it may have been because the view calls a template file which references the main_content variable:
Template file contents:
<?php $this->load->view('includes/header'); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view('includes/footer'); ?>
but i tried creating a flat view file and still couldn't access the variables.
Many thanks,
your $data array is "ripped" into single variables within the view.
print $product;
print $product_no;
print $main_content;
Handling headers & footers this way sucks and get's unmanageable pretty quickly.
Try using my Template library, your data will be passed through to the product_view no problem.