I'm attempting to submit a form with codeigniter and when i submit the form a get a blank page. Where did i go wrong?
View
echo "<form class='order_ctn_parent' method='POST' action='add/insert_orders'>";
//inputs
echo "</form>";
Controller
class Add extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index() {
}
public function insert_orders()
{
$this->load->model('order_model', 'order');
$this->order->insert_orders();
redirect('view_orders', 'location');
}
}
Model
class Order_model extends CI_Model {
public function insert_orders()
{
$DB2 = $this->load->database('orders', TRUE);
$timber_array = $this->input->post("timber_choose");
$products_array = $this->input->post("product_choose");
$qty_array = $this->input->post("quantity");
$price_array = $this->input->post("price");
$qty_array = $this->input->post("quantity");
$loop = 0;
foreach($products_array as $product) {
$price = str_replace("£","",$price_array[$loop]);
$data = array(
'product_code' => "",
'timber_type' => $timber_array[$loop],
'product' => $product,
'quantity' => $qty_array[$loop],
'price' => $price
);
$DB2->insert('timber_order_products', $data);
$loop++;
}
}
}
Try something like this
?>//end of php
<form class='order_ctn_parent' method='POST' action='<?php echo base_url()?>add/insert_orders'>
</form>
and load model in __construct(), like this
public function __construct()
{
parent::__construct();
$this->load->model('order_model', 'order');
}
and no need of check $DB2 = $this->load->database('orders', TRUE);
and Codeigniter insert should be Like This
Please check your url after submitting . i am sure your form action is not working . give proper controller and its function url into Form action . example is here .
<?php echo site_url('add/insert_orders);?>
where add will be your controller and insert_orders will be function of add controller.
Related
I have a model called register_model.php, which I have loaded in a function in my controller (register_controller.php). Model file is placed into model folder itself. Still, I get this error.
An Error Was Encountered
Unable to locate the model you have specified: register_model
register_controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
// function index()
// {
//
// }
public function register() {
if ($this->session->userdata('logged_in'))
{
//user is already logged in
redirect('index.php');
}
else {
//init
//$data['country_list']=$this->config->item('um_country_list');
$data['username'] = '';
$data['firstname'] = '';
$data['lastname'] = '';
$data['email'] = '';
// $data['password'] = '';
//$data['userlevel'] = '';
//load rules
$rules = $this->config->item('um_register_rules');
//default msg
$data['msg'] = $this->lang->line('um_form_msg');
$this->load->model('register_model');
if (isset($_POST['submit'])) {
//the user has submitted the form
//get the user input
$data['username'] = $this->input->post('username');
$data['firstname'] = $this->input->post('firstname');
$data['lastname'] = $this->input->post('lastname');
$data['email'] = $this->input->post('email');
$data['password'] = $this->input->post('password');
//$data['userlevel'] = $this->input->post('userlevel');
$this->form_validation->set_rules($rules); //check with the rules
if ($this->form_validation->run() === FALSE) {
//validation failed
$data['msg'] = $this->lang->line('um_form_error');
$this->load->view('user_register_form', $data);
} else {
//validation passed
$dbdata = array(
'username' => $this->input->post('username'),
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
//'userlevel' => $this->input->post('userlevel')
);
$this->register_model->register_user($dbdata);
$data['msg']=$this->lang->line('um_form_activate');
//render the view
$this->load->view('um_msg', $data);
}
} else {
//render the view
$this->load->view('user_register_form', $data);
}
}
}
}
?>
register_model.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Register_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function register_user($dbdata) {
$this->db->insert('users', $dbdata);
}
}
?>
Probably you should include your model inside your controller, try to include after parent::__construct
This should look like so:
class Registration extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('register_model');
}
or if you using this model in most controllers you can include it using autoload, go to /config/autoload.php search for $autoload['model'] and add your model inside the suitable array
First of all load your model in your controller ..
function __construct() {
parent::__construct();
$this->load->model('register_model');
}
This will work for you...
I'm a newbe in codeigniter.
something is going wrong with I think the model.
this is the controler:
<?php
class Fuel extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('html');
$this->load->library('table');
}
public function image() {
$data['title'] = 'test';
$data['main_content'] = 'imagetest';
$this->load->view("template", $data);
}
public function overview() {
$this->load->model('Get_DB');
$this->Get_DB->overview() ;
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$this->load->view("template", $data);
}
when I load the image function, it works just fine, but the function overview is the problem.
this is my model:
<?php
class Get_DB extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function overzicht() {
$query = $this->db->query("SELECT * FROM invoer "
. "ORDER BY datum DESC");
$gen_query = $this->table->generate($query);
return $gen_query;
}
}
and this is my view:
<?php
echo $gen_query;
and if you want to know: my template is this:
<?php
$this->load->view('templates/header');
$this->load->view($main_content);
$this->load->view('templates/footer');
now when I open my view I get this message:
A PHP Error was encountered
Severity: Notice Message: Undefined variable: gen_query Filename:
views/overzicht.php Line Number: 3
in the model you see that I have made a var $gen_query
so why is that undifined?
regards,
Ralph
Try:
public function overview() {
$this->load->model('Get_DB');
$data = array();
$data['gen_query'] = $this->Get_DB->overzicht() ; #corrected model function and save the result in `gen_query`
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$this->load->view("template", $data);
}
In Controler:
public function overview() {
$this->load->model('Get_DB');
$result = $this->Get_DB->overview() ;
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$data['re'] = $result;
$this->load->view("template", $data);
}
And In view page you can retrive the result
like
foreach($re->result() as $row)
{
//You can get each row data here $row->your_field_names
}
I've followed Codeigniter's configuration straight out of the manual - and just wondered if there was a simpler or more efficient way of coding my controllers...
eg.
class Home extends CI_Controller {
public function index()
{
$this->load->helper('segment1');
$this->load->model('segment1/Leftsidebar_model');
$data['articles'] = $this->Leftsidebar_model->articles();
$this->load->model('segment1/Default_model');
$data['head'] = $this->Default_model->segment1();
$data['segment1'] = $this->Default_model->segment1();
$data['segment2'] = $this->Default_model->segment2();
$this->load->model('Rightsidebar_model');
$data['coming_up'] = $this->Rightsidebar_model->coming_up();
$data['featured_pages'] = $this->Rightsidebar_model->featured_pages();
$data['recommended_link'] = $this->Rightsidebar_model->recommended_link();
$data['testimonials'] = $this->Rightsidebar_model->testimonials();
$this->load->view('head_view', $data);
$this->load->view('header_view', $data);
$this->load->view('segment1/__leftSidebar_view', $data);
$this->load->view('segment1/__mainContent/default_view', $data);
$this->load->view('segment1/__mainContent/segment2_view', $data);
$this->load->view('__rightSidebar_view', $data);
$this->load->view('footer_view', $data);
}
}
helpers and models can be auto loaded, this can be specified in the confiq file. This will save you from having to manually load them.
As for the rest:
You can subclass CI_Controller. Eg:
My Controller extends CI controller and this would then contain a 2 methods
1) to load page head and header
2) to load page bottom
you could then subclass your controller from My_Controller and call those methods
load_page_top();
//insert whatever you have to load
load_page_bottom();
other than that the rest is up to you
Eg:
class Home extends MY_Controller
{
index()
{
$data = get_data();
load_page_top();
//insert your views here specific to the controller
load_page_bottom();
}
get_data()
{
//gather all your needed data here and return it as an array
return data;
}
}
I love this structure, clean and neat.
class Home extends CI_Controller {
public function index()
{
// Load libraries, helpers, models
$this->load->helper('segment1');
$this->load->model('segment1/Leftsidebar_model');
$this->load->model('segment1/Default_model');
$this->load->model('Rightsidebar_model');
// Data for views
$data = array(
'articles' => $this->Leftsidebar_model->articles(),
'head' => $this->Default_model->segment1(),
'segment1' => $this->Default_model->segment1(),
'segment2' => $this->Default_model->segment2(),
'coming_up' => $this->Rightsidebar_model->coming_up(),
'featured_pages' => $this->Rightsidebar_model->featured_pages(),
'recommended_link' => $this->Rightsidebar_model->recommended_link(),
'testimonials' => $this->Rightsidebar_model->testimonials()
);
// Load views
$this->load->view('head_view', $data);
$this->load->view('header_view', $data);
$this->load->view('segment1/__leftSidebar_view', $data);
$this->load->view('segment1/__mainContent/default_view', $data);
$this->load->view('segment1/__mainContent/segment2_view', $data);
$this->load->view('__rightSidebar_view', $data);
$this->load->view('footer_view', $data);
}
}
Of course, the view loading could be managed in other way, having a view loading all common sections.
I just write little additional library for rendering similar page blocks.
Something like this:
class Display_Lib{
private $_CI;
private $_template_data;
public function __construct()
{
$this->_CI =& get_instance();
}
public function set($key, $value)
{
$this->_template_data[$key] = $value;
}
public function get($key)
{
return $this->_template_data[$key];
}
public function get_template_data()
{
return $this->_template_data;
}
public function display_page($view, $data = array())
{
$this->set('content', $this->_CI->load->view($view, $data, TRUE));
$this->_CI->load->view('templates/main_template', $this->get_template_data());
}
}
Set this library in auto load:
$autoload['libraries'] = array('session', 'database', 'display_lib');
And call it in controller:
class Main extends CI_Controller{
public function index()
{
$some_data = array();
$this->display_lib->display_page('views/main_view', $some_data);
}
}
I have a base controller with a method to return a twitter feed to my view.
I want to move this in the view from the page view to the default blade to reduce redundancy as it will be appearing site wide. How do I pass data from the base controller to blade?
I can send it to my view from the page controller like so:
public function get_index()
{
..................
$this->layout->nest('content', 'home.index', array(
'tweets' => $this->get_tweet()
));
}
and in the view, output it like this:
if ($tweets)
{
foreach ($tweets as $tweet)
{
..............
I want to do all this from within default.blade.php and my Base_Contoller:
<?php
class Base_Controller extends Controller {
/**
* Catch-all method for requests that can't be matched.
*
* #param string $method
* #param array $parameters
* #return Response
*/
public function __call($method, $parameters)
{
return Response::error('404');
}
public function get_tweet()
{
...........
return $tweets;
}
}
How is this possible?
//////////////////////UPDATE/////////////////////////////
application/models/tweets.php
<?php
class Tweets {
public static function get($count = 3)
{
Autoloader::map(array(
'tmhOAuth' => path('app').
'libraries/tmhOAuth-master/tmhOAuth.php',
'tmhUtilities' => path('app').
'libraries/tmhOAuth-master/tmhUtilities.php'
));
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'xxx',
'consumer_secret' => 'xxx',
'user_token' => 'xxxxx',
'user_secret' => 'xxxxx',
'curl_ssl_verifypeer' => false
));
$code = $tmhOAuth->request('GET',
$tmhOAuth->url('1.1/statuses/user_timeline'), array(
'screen_name' => 'xxx',
'count' => $count
));
$response = $tmhOAuth->response['response'];
$tweets = json_decode($response, true);
return $tweets;
}
}
application/views/widgets/tweets.blade.php
#foreach ($tweets)
test
#endforeach
application/views/layouts/default.blade.php
....
{{ $tweets }}
....
application/composers.php
<?php
View::composer('widgets.tweets', function($view)
{
$view->tweets = Tweets::get();
});
View::composer('layouts.default', function($view)
{
$view->nest('tweets', 'widgets.tweets');
});
application/controllers/base.php
<?php
class Base_Controller extends Controller {
/**
* Catch-all method for requests that can't be matched.
*
* #param string $method
* #param array $parameters
* #return Response
*/
public $layout = 'layouts.default';
public function __call($method, $parameters)
{
return Response::error('404');
}
}
application/controllers/home.php
<?php
class Home_Controller extends Base_Controller {
public $layout = 'layouts.default';
public $restful = true;
public function get_index()
{
Asset::add('modernizr', 'js/thirdparty/modernizr.js');
Asset::add('jquery',
'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');
Asset::add('scripts', 'js/scripts.js');
$this->layout->title = 'title';
$this->layout->nest('content', 'home.index', array(
//'data' => $some_data
));
}
}
Is giving me an
Undefined variable: tweets
error
Step 1 - Make a view just for your tweets, let's call it widgets/tweets.blade.php, that will accept your $tweets data. This makes it very easy to cache the tweets view in the future if you want a little more performance. We also want a model that will generate the tweet data for you.
Step 2 - Pass the tweet data into your tweets view, let's use a View Composer for this so the logic is kept with (but outside) the view.
Step 3 - Create your default layout, let's call this layout/default.blade.php. This will accept $content and $tweets. We'll nest the tweets view with another View Composer. You can nest the $content in your controller actions.
Step 4 - Set the $layout on your Base_Controller.
Step 5 - Profit!
Note - If these are your first view composers then you'll need to include them in application/start.php
// application/models/tweets.php
class Tweets {
public static function get($count = 5)
{
// get your tweets and return them
}
}
// application/views/widgets/tweets.blade.php
#foreach ($tweets)
{{-- do something with your tweets --}}
#endforeach
// application/views/layouts/default.blade.php
<section class="main">{{ isset($content) ? $content : '' }}</section>
<aside class="widget widget-tweets">{{ $tweets }}</aside>
// application/composers.php
View::composer('widgets.tweets', function($view)
{
$view->tweets = Tweets::get();
});
View::composer('layouts.default', function($view)
{
$view->nest('tweets', 'widgets.tweets');
});
// application/start.php (at the bottom)
include path('app').'composers.php';
// application/controllers/base.php
class Base_Controller extends Controller {
public $layout = 'layouts.default';
}
// application/controllers/home.php
class Home_Controller extends Base_Controller {
public $restful = true;
public function get_index()
{
$this->layout->nest('content', 'home.welcome');
}
}
View::share('key', 'value');
in you view use (Blade syntax)
{{$key}}
or (PHP syntax)
echo $key;
I have changed my application core/MY_Controller.php extendable. Before I could get data from mysql but Now I get Message: Undefined variable: records. How can i get data again ?
MY_Controller.php :
class MY_Controller extends CI_Controller {
protected $data = array();
function __construct() {
parent::__construct();
}
function render_page($view) {
//do this to don't repeat in all controllers...
$this->load->view('templates/header', $this->data);
//menu_data must contain the structure of the menu...
//you can populate it from database or helper
$this->load->view($view, $this->data);
$this->load->view('templates/footer', $this->data);
}
My home controller :
class Home extends MY_Controller {
function __construct() {
parent::__construct();
}
public function view($page = 'home')
{
$this->load->helper('text');
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$this->data);
}
Services_model :
class Services_model extends CI_Model {
function getAll() {
$q = $this->db->get('services');
if($q->num_rows() > 0){
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
This is the where i get error in home.php view :
<ul class="blog-medium">
<?php foreach($records->result() as $row): ?>
<li><h1><?php echo $row->title; ?></h1></li>
<?php endforeach ?>
The error message shows : Message: Undefined variable: records
In your controller it looks like you are setting $data['records'] when you should be setting $this->data['records']
I found a solution. In my Home controller, I was using the view as :
$this->render_page('pages/'.$page,$this->data);
But I have to use it as here :
class Home extends MY_Controller {
function __construct() {
parent::__construct();
}
public function view($page = 'home')
{
$this->load->helper('text');
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$data);
}
I dont know why but I tried this before. It did not make but it is working now.