How to codeigniter Sending Parameters Between Controller and View different function - php

I'm new codeigniter developer.
I trying sending controller between views but only index function sending.
When I try different function, I get this error.
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: sample
Filename: views/merhaba_sayfasi.php
Line Number: 8
My controller file = merhaba.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Merhaba extends CI_Controller {
public function index()
{
$data["title"]="İlk codeigniter sayfam";
$data["giris_baslik"] = "Merhaba Dünya";
$this->load->view('merhaba_sayfasi',$data);
}
public function example(){
$data["sample"] = "Sample php";
$this->load->view("merhaba_sayfasi",$data);
}
}
?>
My view file = merhaba_sayfasi.php
<html>
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
</head>
<body>
<h3><?php echo $giris_baslik; ?></h3>
<?php echo $sample; ?>
</body>
</html>
My explorer image
http://www.medyasef.com/questions/codeigniter_function_error.png

It depends what URL you are calling?
If you call "/app/Merhaba" or "/app/Merhaba/index" you will not see the sample variable.
If you call "/app/Merhaba/example" then you should see it. It all depends what function of the controller you are calling - by default its only the index function

You're trying to display a variable, whose value hasn't been set, that's why the error is being thrown.
You could check in your view to ensure that a variable has a value before attempting to echo it.
So, instead of:
<?php echo $sample; ?>
You could use:
<?php if(isset($sample)) {echo $sample;} ?>
Although, I'd recommending keeping the logic in your controller and setting all of the values in your controller before loading the view

Related

post input data to codeigniter controller from outside codeigniter folder

I am facing a problem. I have a normal html files (contact.html) in cpanel's public_html folder. And admin panel which is developed in CodeIgniter. Now what i am trying to do is print or receive input data from contact.html file to codeigniter controller. Below mention the code
contact.html
<html>
<form name ="userinput" action="admin/index.php/contact/contact" method="post">
<div class="row contact-form">
<div class="col-lg-6">
<input type="text" name="yourname" id="yourname" class="form-control" placeholder="Your Name">
</div><!-- end col -->
</div><!-- end row -->
</form>
controller : contact.php
<?php
class Contact extends CI_Controller
{
function contact()
{
echo 'test';
echo $yourname = $this->input->post('yourname');
}
}?>
now when i am submitting data 'test' is printing but i can not get the post data of textbox.
The error is mention below.
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Contact::$input
Filename: controllers/contact.php
Line Number: 12
please help me how will i get it.
<?php
class Contact extends CI_Controller
{
public function contact(){
echo 'test';
echo $yourname = $this->input->post('yourname');
$this->load->view('index'); // put this file in application/views/index.php ,also rename to .php
}
}
?>
Try creating the function in your controller. Hope it helps !
You need to have a index function or another function
File name: Contact.php file name and class should have only first letter upper case with controllers and models.
<?php
class Contact extends CI_Controller {
public function index() {
$yourname = $this->input->post('yourname');
echo $yourname;
// Since you had named the view file with a .html you need to add .html
$this->load->view('contact.html');
}
}
If you name your view files with .php instead of a .html
$this->load->view('contact');
I would autoload the url helper on autoload.php and would look into form helper
It's possible that since your function name is same as class name that it's treating it as constructor. Rename your function to something else, update your action link in form and try again.
Better try native PHP way of getting value
echo $_POST['yourname'];
Hope this helps

code igniter - loading a controller and its data within another controller / view

I'm a bit confused on including a controller / view within another controller/view. I'm doing the following and getting funny rendering issues:
//CONTROLLER About
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends CI_Controller {
public function about(){ //the standalone about page
$data['about_inner'] = $this->about_inner();
$this->load->view('pages/about',$data);
}
public function about_inner(){ //this is separate so it can be loaded by the landing page without the html shell around it
$this->load->model('about_model');
$data['about'] = $this->about_model->get_content();
$this->load->view('pages/about-inner',$data);
}
}
//view about.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div class="container about" data-page="about">
<div class="scroll-container about">
<?=$about_inner; ?>
</div>
</div>
</body>
</html>
The issue I'm getting is that $about_inner does not end up loading inside of the "scroll-container" - it loads and renders before everything else, as shown in the screenshot.
What's the best way to get the about_inner view and all of its associated data to load within the about view? I need all the content ($this->about_model->get_content()) to come from about_inner since it can also be loaded by other pages via ajax.
SOLUTION
public function about(){ //the standalone about page
$data['nav'] = $this->load->view('templates/nav', NULL, TRUE);
$data['about_inner'] = $this->about_inner(true);
$this->load->view('pages/about',$data);
}
public function about_inner($print =false){ //this is separate so it can be loaded by the landing page without the html shell around it
$this->load->model('about_model');
$data['about'] = $this->about_model->get_content();
return $this->load->view('pages/about-inner',$data, $print);
}
//HTML
//view about:
<div class="container about" data-page="about">
<?=$nav; ?>
<div class="scroll-container about">
<?=$about_inner; ?>
</div>
</div>
You just need to tell the about_inner function to return the data and not print the data. That's the 3rd argument to function view($template, $data, $return);
$this->load->view('pages/about-inner',$data, true);
If you need to do either or, just set a boolean flag as the argument to the function
function about_inner($print = false){
...
$this->load->view('pages/about-inner', $data, $print);
}
Then when you call the function you can simply pass true to the function in order to get it to return the HTML instead of printing the HTML.
$this->about_inner(true)
if your JS folder is in root you have to define it with base_url() function. So correct all the path in get_content method
Ex
<script src ="<?php echo base_url() ?>path/to/your/folder/aaa.js" .....
And i think you can keep one controller method and remove one.(You can keep about and remove about_inner)

How can I set facebook meta tags dynamically?

I have an opencart site, and I'm trying to configure facebook share options of my products.
Since everything is loaded as a separate module, I can't set the facebook meta tags like this (header.tpl):
<meta property="og:description" content="<?php echo $description; ?>" />
Because the $description doesn't exist while the header module is loading.It is created in the controller of product module. So I tried to change the content value dynamically (product.tpl):
$("meta[property='og:description']").attr('content','<?php echo $description; ?>');
And it worked, I can see that the value is changed (in the page source), then I debugged my page but I couldn't get the value.. I think I know the reason, I have to set the value before the page load but I'm not sure how can I do that.. do you have any idea ?
You could use the Document class to add those facebook tags (as many as you want). Just add two extra methods setFacebookDescription and getFacebookDescription, so you have to add the followings:
<?php
class Document {
private $facebook_description;
public function getFacebookDescription() {
return $this->facebook_description;
}
public function setFacebookDescription($facebook_description) {
$this->facebook_description = $facebook_description;
}
}
On each controller, you will find at the end of each method, a call which loads the header of Opencart, something like this $data['header'] = $this->load->controller('common/header'); (example). Note that it might differ from yours, it depends on Opencart version.
Now, in header.php controller you add:
<?php
class ControllerCommonHeader extends Controller {
public function index() {
$data['facebook_description'] = $this->document->getFacebookDescription();
}
}
this will get the facebook_description variable and pass it to the view header.tpl. Next, add the facebook tags between your <head> tags in header.tpl file:
<!DOCTYPE html>
<head>
<?php if ($facebook_description != '') { ?><meta property="og:description" content="<?php echo $facebook_description; ?>" /><?php } ?>
</head>
Finally, you can set facebook_description in each controller, by calling $this->document->setFacebookDescription('my description');.
Example: in product.php controller you add
<?php
class ControllerProductProduct extends Controller {
public function index() {
// code...
$this->document->setTitle($product_info['meta_title']);
$this->document->setDescription($product_info['meta_description']);
$this->document->setFacebookDescription($product_info['meta_description']);
// the rest of the code...
}
}
here you set the $product_info['meta_description'] as facebook description tag, however you could also use $product_info['name'] or other variable.
Final note: you can change all the Opencart system classes with the vqmod.

Passing POST value from VIEW to CONTROLLER to MODEL in Codeigniter

Ok I stripped out validation and other code to make this example as transparent as possible. I am unable to POST a VAR to my MODEL.
VIEW
<?php echo form_open('invoice'); ?>
<?php echo validation_errors(); ?>
<?php echo form_input('order_num', $this->input->post('order_num')); ?>
<?php echo form_submit('submit','submit'); ?>
<?php echo form_close(); ?>
CONTROLLER
public function invoice()
{
$order = $this->input->post('order_num');
$this->General_Model->get_customer_order($order);
}
MODEL
function get_customer_order($order)
{
. . .
$this->db->where('client_orders.id', $order);
$result = $this->db->get('client_orders');
. . .
}
Ok Basically you enter an order number on the form. Then it goes to the controller which does the validation (i removed it here to keep example simple) and finally it passes the data to the model which runs the query on the db and returns the $result.
However instead of my desired output im getting: Fatal error: Call to a member function get_customer_order() on a non-object and A PHP Error was encountered Severity: Notice Message: Undefined property: Account_dashboard::$General_Model
What am I doing wrong here? Thanks for the help.
Make sure you're loading the model in your controller:
$this->load->model('General_Model');
The controller line should
$data['order'] = $this->General_Model->get_customer_order($order);
Then you pass $data to the view.

Custom error pages with templates in CodeIgniter

I'm using the template library for CodeIgniter, http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html, and now I want to implement custom error pages too. I found one method involving a MY_Router extending the default router: http://maestric.com/doc/php/codeigniter_404 but that only treats 404 errors. I want all errors to show a simple user-friendly page, including database errors etc, and I want it to go through a controller, partly so I can use the template library, and partly so I can also implement an email function to send myself information about the error that occurred.
Someone asked about extending the functionality of the above MY_Router method for other errors, like error_db, but got no answer from the author, so I'm turning here to see if anyone knows how to do this, along the lines of the above method or any other simple way of achieving it. Please note that I'm a newbie, so do not assume too much about my knowledge of basic CodeIgniter functionality :-)
I've created an extension for the Exceptions class.
In this extension I've replaced the $this->Exceptions->show_error(); method, witch is used by the show_error() function of CI.
when I call show_error('User is not logged in', 401); this custom method is looking for an error_$status_code file first. In the case of the example above, it will look for an error_401.php file.
When this file does not exists, it wil just load the error_general.php file, like the default $this->Exceptions->show_error(); does.
In your case, you can use the following code to use in your library, controller or whatever should throw an error.
<?php
if(!(isset($UserIsLoggedin))){
$this->load->view('template/header');
show_error('User is not logged in', 401);
$this->load->view('template/footer');
}
?>
Your error_401.php file should than look like this:
<div id="container">
<h1><?php echo 'This is an 401 error'; ?></h1>
<?php echo $message; ?>
</div>
/application/core/MY_Exceptions.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Exceptions extends CI_Exceptions
{
function show_error($heading, $message, $template = 'error_general', $status_code = 500)
{
if((!isset($template)) || ($template == 'error_general')){
if(file_exists(APPPATH.'errors/error_'.$status_code.'.php')) {
$template = 'error_'.$status_code;
}
}
if (!isset($status_code)) $status_code = 500;
set_status_header($status_code);
$message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';
if (ob_get_level() > $this->ob_level + 1)
{
ob_end_flush();
}
ob_start();
include(APPPATH.'errors/'.$template.'.php');
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
}
?>
I do it like this:
I create my own error page, and whenever I should throw a 404 error, I actually load my 404 page.
So say my default controller is site.php, my site.php looks like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
public function view($page = "home" , $function = "index")
{
do_something();
if($status == "404")
{
$function = "404";
}
$this->load->view('templates/header', $data);
$this->load->view($page.'/'.$function, $data);
$this->load->view('templates/footer', $data);
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
So I serve the home/404.php whenever an error occurs. i.e., I don't allow CodeIgniter to call show_404(); therefore the 404 page looks like any other page.
p.s. I assume that you followed the nice tutorial in CodeIgniter's website.
The simplest way to create custom error pages is to edit the files at /application/views/errors/html/error_*.php such as error_404.php (for 404s), error_db.php (for database errors) and error_general.php (for most other errors).
As these pages are within your application directory, you are free to customise them to your needs.
If your normal view template looks something like this:
<?php $this->load->view('includes/header'); ?>
...
...
<?php $this->load->view('includes/footer'); ?>
You can adapt this in your /application/views/errors/html/error_*.php files like so:
<?php
$page_title = $heading;
include VIEWPATH.'includes'.DIRECTORY_SEPARATOR.'header.php';
?>
<div class="well">
<h1><?php echo $heading; ?></h1>
<?php echo $message; ?>
</div>
<?php include VIEWPATH.'includes'.DIRECTORY_SEPARATOR.'footer.php'; ?>
Notice that we're no longer using views, but instead including the view files for the header & footer.
Another thing to note:
In the header view, I'm passing a $data object which includes $data['page_title']. As the error pages don't use views, you have to add any variables that you'd normally pass into the view, hence the presence of $page_title.
config/routes.php
edit
$route['404_override'] = '';
type here your controller for example Error
create a function index and load your view

Categories