This is my database php page
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => '192.168.1.200',
'username' => 'name',
'password' => 'password',
'database' => 'codeignter_tutorial',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
I am unable to load model page from the controller page.
This is the error am getting any help would be appreciated.
Fatal error: Call to undefined method CI_Loader::models() in /var/www/html/bcit-ci-CodeIgniter-3.1.11-0-gb73eb19/bcit-ci-CodeIgniter-b73eb19/application/controllers/Welcome.php on line 16
A PHP Error was encountered
Severity: Error
Message: Call to undefined method CI_Loader::models()
Filename: controllers/Welcome.php
Line Number: 16
Backtrace:
This is my controller page Welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('dashboard');
}
public function __construct()
{
parent::__construct();
$this->load->helper(array("form","url"));
$this->load->models('models');
}
public function login()
{
$this->load->view('loginPage');
}
public function dashboard()
{
$data['name']=$this->input->post('name');
$data['age']=$this->input->post('age');
$this->models->dbdata($data);
}
}
?>
This is my model page called models.php
<?php
class models extends CI_model
{
public function dbdata($inserttodb)
{
$this->db->insert("registration",$inserttodb);
return $var->result_array();
}
}
?>
In model page model's 'm' should be capital
class **M**odels extends CI_model
in controll page
this->load->model('models');
This is how you load model from your controller:
public function __construct()
{
parent::__construct();
$this->load->model('models', 'your_model'); //use 'your_model' to reference your model
}
This is how you called your model:
$this->your_model->dbdata($data);
//'your_model' is from constructor, 'dbdata' is the name of your function in model
Then make sure the method accept the 'data':
public function dbdata($data)
{
//your code goes here
}
Related
Good Day!
I have this problem using infinityfree.net as my hosting site.
I used codeigniter framework for my code.
The problem is:
An uncaught Exception was encountered
Type: RuntimeException
Message: Unable to locate the model you have specified: Login_model
My code working properly on xampp.
database.php
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'HOSTNAME',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'database' => 'DATABASE_NAME',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Here's my login_model.php
class Login_model extends CI_Model{
public function __construct()
{
parent::__construct();
}
function login($par1,$par2){
//CODE HERE
}
Here's my controller
class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('url', 'form');
}
public function login()
{
$this->load->model('login_model');
}
PS. i didnt include much more code in controller because thats the only thing that has error and also Im unable to post too much code because of the rule of stackoverflow. Hoping you guys to help me. Thanks in advance
Fixed
I just renamed my login_model to Login_model
When I run my localhost, I get these errors
A PHP Error was encountered Severity: Warning
Message: mysqli::real_connect(): (HY000/2002): A connection attempt
failed because the connected party did not properly respond after a
period of time, or established connection failed because connected
host has failed to respond.
Filename: mysqli/mysqli_driver.php
Line Number: 203
Backtrace:
File: C:\xampp\htdocs\ciblog\application\models\Post_model.php Line: 4
Function: database
File: C:\xampp\htdocs\ciblog\index.php Line: 315 Function:
require_once
A Database Error Occurred Unable to connect to your database server
using the provided settings.
Filename: C:/xampp/htdocs/ciblog/system/database/DB_driver.php
Line Number: 436
I already tried to change the hostname to "mysql.hostingprovider.com:3306" but still get the error:
database.php file
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'mysql.hostingprovider.com:3306',
'username' => 'root',
'password' => '123456',
'database' => 'posts',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Post_model.php file
<?php
class Post_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_posts($slug = FALSE){
if($slug === FALSE){
$query = $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts', array('slug' => $slug));
return $query->row_array();
}
}
post.php file
<?php
class Posts extends CI_Controller{
public function index (){
$data['title'] = 'Latest Posts';
$data['posts'] = $this->post_model->get_posts();
print_r($data['post']);
$this->load->view('templates/header');
$this->load->view('posts/index', $data);
$this->load->view('templates/footer');
}
}
Change hostname:
*$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
............*
I am trying to retrieve user information through api in CS-cart. But it returns only limited information. How can we modify the code to get all the user info for ex- user profiles, address, gst and all.
you can create your own API.
/var/www/html/app/addons/addon_name/Tygh/Api/Entities/Egg.php
<?php
namespace Tygh\Api\Entities;
use Tygh\Api\AEntity;
use Tygh\Api\Response;
class Egg extends AEntity
{
public function index($id = '', $params = array())
{
if(empty($id))
{
$dd=db_get_array("SELECT * FROM ?:table_name");
//result all rows
}
else
{
// for filtering purpose
$where=array("id"=>$id);
$dd=db_get_array("SELECT * FROM ?:table_name where ?w",$where);
//result-> specific one row
}
return array(
'status' => Response::STATUS_OK,
'data' => $dd
);
}
public function create($params)
{
return array(
'status' => Response::STATUS_OK,
'data' => array()
);
}
public function update($id, $params)
{
return array(
'status' => Response::STATUS_OK,
'data' => array()
);
}
public function delete($id)
{
return array(
'status' => Response::STATUS_NO_CONTENT,
);
}
public function privileges()
{
return array(
'index' => true,
'create' => 'create_things',
'update' => 'edit_things',
'delete' => 'delete_things',
'index' => 'view_things'
);
}
public function privilegesCustomer()
{
return array(
'index' => true
);
}
}
?>
Remarks:
file name,
class name,
file path
Or you can edit the user API entity from this location.
app/Tygh/Api/Entities/Users.php
Any doubts , then kick me in...
I have started to create a controller called User in codeigniter
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library(array( 'encrypt', 'form_validation' ));
}
public function index()
{
}
/*
* Create loginform
* Parameters: void
* Return: html of loginform;
*/
public static function loginform() {
// Login form setup
$loginform_data = array();
$loginform_data['attributes'] = array('id' => 'loginform');
$loginform_data['username'] = array(
'name' => 'username',
'id' => 'username',
'value' => '',
'maxlength' => '100'
);
$loginform_data['pass'] = array(
'name' => 'pass',
'id' => 'pass',
'value' => '',
'maxlength' => '100'
);
$contentdata = array();
$contentdata['loginform'] = $this->load->view('partials/forms/login', $loginform_data, true);
return $contentdata;
}
/*
* Check login username, password from form
* Parameters: void
* Return: void;
*/
public function login() {
$name = $this->input->post('username');
$pass = $this->input->post('pass');
$this->form_validation->set_rules('username', 'Användarnamn', 'required');
$this->form_validation->set_rules('pass', 'Lösenord', 'required');
if ($this->form_validation->run() == false)
{
$this->load->view('home');
}
else
{
$this->load->view('formsuccess');
}
}
}
I can call the user/login - function through the url. But I can't call User::loginform() from another controller. Shouldn't I be able to do that?
Here's what I'm trying: (from my Home-class)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
//Create login and register-forms
$this->load->helper('form');
//Registration form setup
$registerform_data = array();
$registerform_data['attributes'] = array('id' => 'registerform');
$registerform_data['company'] = array(
'name' => 'name-company',
'id' => 'name-company',
'value' => '',
'maxlength' => '100'
);
$registerform_data['orgnr'] = array(
'name' => 'orgnr-company',
'id' => 'orgnr-company',
'value' => '',
'maxlength' => '100'
);
$registerform_data['contact'] = array(
'name' => 'contact-company',
'id' => 'contact-company',
'value' => '',
'maxlength' => '100'
);
$registerform_data['phonecompany'] = array(
'name' => 'phone-company',
'id' => 'phone-company',
'value' => '',
'maxlength' => '100'
);
$registerform_data['emailcompany'] = array(
'name' => 'email-company',
'id' => 'email-company',
'value' => '',
'maxlength' => '100'
);
//What content to pass to view
$contentdata = array();
$contentdata['loginform'] = User::loginform();
$contentdata['registerform'] = $this->load->view('partials/forms/registration', $registerform_data, true);
$this->load->view('home', $contentdata);
}
}
$contentdata['loginform'] = User::loginform(); gives me error:Fatal error: Class 'User' not found in C:\Program...
What am I missing?
While you are extending Home class ,extend to User controller like
require_once(APPPATH.'controllers/user.php');
class Home extends User {
Because you need to extract the function from User class and then it will Inherit the parent class User functions And since login_form is your public function in User controller,you can call this in Home controller now.And no need to use Static here I think so.
There is also an way to do this.Just write the login_form function in an helper and call it on both the controllers then your problem may solve.
Edit: As #IJas said,we need to include the controller file that you are extending.
Hi I'm creating my first admin section for my extension. I've created a menu which links to a page displaying a grid. The problem is when you click through to edit the record, its displaying this error
Fatal error: Call to a member function setData() on a non-object in /Applications/MAMP/htdocs/theBookClub/app/code/core/Mage/Adminhtml/Block/Widget/Form/Container.php on line 129
For the life of me I can't see any reference in any of the relevant files
class Namespace_Bookshelf_Block_Adminhtml_Bookshelf_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct()
{
parent::__construct();
$this->_objectId = 'id';
$this->_blockGroup = 'bookshelf';
$this->_controller = 'bookshelf_admin';
$this->_mode = 'edit';
$this->_addButton('save_and_continue', array(
'label' => Mage::helper('adminhtml')->__('Save And Continue Edit'),
'onclick' => 'saveAndContinueEdit()',
'class' => 'save',
), -100);
$this->_updateButton('save', 'label', Mage::helper('bookshelf')->__('Save Example'));
$this->_formScripts[] = "
function toggleEditor() {
if (tinyMCE.getInstanceById('form_content') == null) {
tinyMCE.execCommand('mceAddControl', false, 'edit_form');
} else {
tinyMCE.execCommand('mceRemoveControl', false, 'edit_form');
}
}
function saveAndContinueEdit(){
editForm.submit($('edit_form').action+'back/edit/');
}
";
}
public function getHeaderText()
{
if (Mage::registry('example_data') && Mage::registry('example_data')->getId())
{
return Mage::helper('bookshelf')->__('Edit Example "%s"', $this->htmlEscape(Mage::registry('example_data')->getName()));
} else {
return Mage::helper('bookshelf')->__('New Example');
}
}
and this
class Namespace_Bookshelf_Block_Adminhtml_Bookshelf_Grid extends Mage_Adminhtml_Block_Widget_Grid {
public function __construct() {
parent::__construct();
$this->setId('bookshelf_grid');
$this->setDefaultSort('bookshelf_id');
$this->setDefaultDir('desc');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection() {
$collection = Mage::getModel('bookshelf/bookshelf')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns() {
$this->addColumn('bookshelf_id', array(
'header' => Mage::helper('bookshelf')->__('ID'),
'align' => 'right',
'width' => '50px',
'index' => 'bookshelf_id',
));
$this->addColumn('customer_id', array(
'header' => Mage::helper('bookshelf')->__('Name'),
'align' => 'left',
'index' => 'customer_id',
));
$this->addColumn('bookshelf_name', array(
'header' => Mage::helper('bookshelf')->__('Name'),
'align' => 'left',
'index' => 'bookshelf_name',
));
return parent::_prepareColumns();
}
public function getRowUrl($row) {
return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}
}
and this
class Newdaymedia_Bookshelf_Block_Adminhtml_Bookshelf extends
Mage_Adminhtml_Block_Widget_Grid_Container
{
public function __construct()
{
$this->_controller = 'bookshelf_admin';
$this->_blockGroup = 'bookshelf';
$this->_headerText = Mage::helper('bookshelf')->__('Item Manager');
$this->_addButtonLabel = Mage::helper('bookshelf')->__('Add Item');
parent::__construct();
}
protected function _prepareLayout()
{
$this->setChild( 'grid',
$this->getLayout()->createBlock( $this->_blockGroup.'/' . $this->_controller . '_grid',
$this->_controller . '.grid')->setSaveParametersInSession(true) );
return parent::_prepareLayout();
}
}
Any help would be gratefully received!
NEW
class Namespace_Bookshelf_Block_Adminhtml_Bookshelf_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
if (Mage::getSingleton('adminhtml/session')->getExampleData())
{
$data = Mage::getSingleton('adminhtml/session')->getExamplelData();
Mage::getSingleton('adminhtml/session')->getExampleData(null);
}
elseif (Mage::registry('example_data'))
{
$data = Mage::registry('example_data')->getData();
}
else
{
$data = array();
}
Mage::log("this is the form class");
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
'enctype' => 'multipart/form-data',
));
$form->setUseContainer(true);
$this->setForm($form);
$fieldset = $form->addFieldset('example_form', array(
'legend' =>Mage::helper('bookshelf')->__('Example Information')
));
$fieldset->addField('name', 'text', array(
'label' => Mage::helper('bookshelf')->__('Name'),
'class' => 'required-entry',
'required' => true,
'name' => 'name',
'note' => Mage::helper('awesome')->__('The name of the example.'),
));
$fieldset->addField('description', 'text', array(
'label' => Mage::helper('bookshelf')->__('Description'),
'class' => 'required-entry',
'required' => true,
'name' => 'description',
));
$fieldset->addField('other', 'text', array(
'label' => Mage::helper('bookshelf')->__('Other'),
'class' => 'required-entry',
'required' => true,
'name' => 'other',
));
$form->setValues($data);
return parent::_prepareForm();
}
}
This might solve your issue:
In your form container class:
Namespace_Bookshelf_Block_Adminhtml_Bookshelf_Edit
extends Mage_Adminhtml_Block_Widget_Form_Container
If you add these test lines, you'll see which Form class Magento is trying to load
$form_block = $this->_blockGroup . '/' .
$this->_controller . '_' .
$this->_mode .
'_form';
echo $form_block;
For this to work with your code (above), your class:
Namespace_Bookshelf_Block_Adminhtml_Bookshelf_Edit_Form
should echo a test value of:
bookshelf/adminhtml_bookshelf_edit_form
and should be in the following filepath:
app/code/local/Namespace/Bookshelf/Block/Adminhtml/Bookshelf/Edit/Form.php
You might have to adjust the class name(s), or the filepath, or both, in order to get it working.
Good luck!
You need to have a helper created for your module (even if it does nothing). In your module's helper directory create a file called Data.php and create a class in there called Namespace_Module_Helper_Data that extends Mage_Core_Helper_Abstract.
I think that should help clear the problem up.
Assuming your Adminhtml controller is under
Namespace/ModuleName/controllers/Adminhtml/BookshelfController.php
try $this->_controller = 'adminhtml_bookshelf'; on Your Form Container.
It is important to clarify that $this->_controller is not the actual controller name but the block class name and $this->_blockGroup is actually the module name.
so try:
$this->_blockGroup = 'namespace_bookshelf';
$this->_controller = 'adminhtml_bookshelf';