I passed parameter from view to controller via URL. Now I want to send it from controller to model so that I can use it to pick data from tables. Here is my code:
controller:
function view(){
if(isset($_GET['r'])) {
$rank = $_GET['r'];
}
$rank=$this->uri->segment($rank);
$this->load->model('names_rank');
$data=$this->names_rank->get_names($rank);
print_r($rank);
}
model:
function get_names($rank){
$this->db->select('u.*,v.*');
$this->db->from('unit_member u, Vyeo v');
$this->db->where('v.fno = u.fno');
$this->db->where('u.present = ""');
$this->db->where('v.rank', $rank);
$this->db->where('v.date_of_end="0000-00-00"');
$query = $this->db->get();
return $query->result_array();
}
this is the result:
A PHP Error was encountered Severity: Warning Message: Missing
argument 1 for Names_rank::get_names(), called in
C:\xampp\htdocs\unit\application\controllers\names.php on line 32 and
defined
This will work to send to model but your code isn't understandable for me, you re-declare the variable after setting it in the IF? are you trying to print_r() the output from the model?
I think you are trying to achieve this maybe?
function view() {
if(isset($_GET['r'])) {
$rank = $_GET['r'];
}else{
$rank = $this->uri->segment($rank);
}
$this->load->model('names_rank');
$data = $this->names_rank->get_names($rank);
print_r($data);
}
You can pass a Parameter to your model. First you have to call your model within your controller if you not enable it on autoload.
Your Model:
<?php
class AwesomeModel extends CI_Model
{
publif function do_work($param, $anotherParam)
{
//code here
}
}
Then your controller:
<?php
class AwesomeController extends CI_Controller
{
public function __construct()
{
/*
* load in constructor so not need to recall every time you want use it
* second parameter is model renaming (optional)
*/
$this->load->model('AwesomeModel', 'awe');
}
public function pass_data()
{
$this->awe->do_work($param1, $param2);
}
?>
Thats all.
Related
I'm still very new to this codeigniter. So i have a tables in my database called users table and t_lowongan table. The t_lowongan table have user_id field as a foreign key for the id_user in the users table. So when the user login and create a job posting, the user_id will be filled by the id_user from that user. I want to get the user data so that this user know which job posting that he has created by that user, but the data isn't showing, how can i fix it??
The Controller Lowongan :
public function lowongan()
{
$this_id = $this->session->userdata('id_user');
$data['t_lowongan'] = $this->db->get_where('t_lowongan', ['user_id', $this_id])->result();
$this->load-view('lowongan', $data);
}
Your approach to debugging isn't quite right here. Firstly, you must understand that the database object's result() method returns an array of row objects in the form of $row->field (or in your case, $row->user_id). On the other hand, the database object's result_array() method returns an array of arrays (in your case, $row['user_id']). Since you've used the former, I hope you've dealt with those row objects accordingly in the lowongan view which you then show. Failure to do so might result in the problem you've described in the question.
Assuming you've taken care of the view, the second thing to do then is to place an error_log() statement and see what value it's returning. You can place something like this just after the get_where() statement and then observe your HTTP server logs what value it's getting:
error_log("THE RESULT IS: " . print_r($data, true));
Depending on whether or not this is printing the correct value, you can then further troubleshoot where exactly the problem lies.
You must create controller and model and call model from construct
constroller :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class LowonganController extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model(array('m_lowongan'));
}
public function lowongan() {
$id = $this->session->userdata('id_user');
$data['t_lowongan'] = $this->m_lowongan->get_user_by_id($id);
$this->load-view('lowongan', $data);
}
}
model:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class M_Lowongan extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_user_by_id($id)
{
$this->db->where('user_id', $id);
$query = $this->db->get('your_table_name');
return $query->row_array();
}
}
So what i did is that. I create a function called get_data on my model
The model :
public function get_data($where, $table){
return $this->db->get_where($table, $where);
}
And in the controller i just do this :
The Controller :
public function lowongan()
{
$this_id = $this->session->userdata('id_user');
$data['t_lowongan'] = $this->m_lowongan->get_data($where,'t_lowongan')->result();
$this->load-view('lowongan', $data);
}
And it works :D. But please let me know if this is like a wrong thing to do, Thank you :D
Try this
public function lowongan()
{
$this_id = $this->session->userdata('id_user');
$data['t_lowongan'] = $this->db->get_where('t_lowongan', ['user_id', $this_id])->row();
$this->load-view('lowongan', $data);
}
in your views
<?php print_r($t_lowongan)?>
Looks simple (and maybe) but I need pass a received variable in a function to another function. Here's my code:
PD: I using Laravel Eloquent's Scopes
class myParentModel extends Model {
public function scopeMyScope($query, $VAR_I_WANT_TO_PASS=[]) {
return $query->with('model'])->whereHas('model', function($q, $VAR_I_WANT_TO_PASS=[]) {
$q->where('colum1',$VAR_I_WANT_TO_PASS[0])->where('colum2',$VAR_I_WANT_TO_PASS[1])->where('colum3',$VAR_I_WANT_TO_PASS[2]);
})->take(10);
}
}
and I want to do this:
$result = myParentModel::myScope([3,1,6])->get();
I resolve this using use:
class myParentModel extends Model {
public function scopeMyScope($query, $VAR_I_WANT_TO_PASS) {
return $query->with('model'])->whereHas('model', function($q) use ($VAR_I_WANT_TO_PASS) {
$q->where('colum1',$VAR_I_WANT_TO_PASS[0])->where('colum2',$VAR_I_WANT_TO_PASS[1])->where('colum3',$VAR_I_WANT_TO_PASS[2]);
})->take(10);
}
}
Simple example
I would like to know if there is any possible way that $data could already exist without being set in that method, and if so how to set it.
public function index(){
if(isset($data)){
//how is this possible?
} else if(isset($this->data){
// set in parent::__construct
// ok i'm going to have to set $data in every method in every controller
$data = $this->data;
}
}
additional info
This is my specific problem,
I am using a framework with a controller class which is extended for every controller.
class ControllerBlog extends Controller {}
Every method in every controller perform a few almost identical tasks. Some of these tasks return data within the scope of the method called.
//e.g
public function index(){
$this->loadthis('blog');
$this->loadthat('blog');
$data = $this->get_this('blog');
...
...
$data['title'] = 'blog title';
use_data($data);
}
I would like to move these tasks to Controller class function __construct to limit the amount of code repeated.
<?php
class Controller {
public function __construct() {
//load this and that and return data;
$data = $this->load_and_return_all(get_class($this));
//class level
$this->data = $data;
}
}
is there a way to get the $data variable for use within the scope of the method without the need of adding any additional code to every method of every controller?
class ControllerBlog extends Controller {
public function index(){
//adding this to every method seems silly
$data = $this->data;
// i would like $data to be set in the construct;
}
}
I am developing a joomla 2.5 component where I need to pass data from controller to model. The controller is receiving data from url. I find that controller is getting the value properly. Now I need to move that value to model from controller. From different post I have found a snippet of code for controller like below.
$datevalue = JRequest::getVar('day',$day); //receiving value from view
$item = JRequest::setVar('day',$datevalue); //setting variable
$model =& $this->getModel('WeeklyProgram'); //assign model
$model->setState('dayVar', $item); // assign value for model
The problem is that I don't know how to receive this value 'dayVar' from model. Can anybody help me on this issue? Thanks.
Use following things
In Modal
class CommunityModelCevent extends JCCModel
{
var $membersCount = null;
function getMembersCount($value) {
$this->membersCount = $value // set your value here 15
// Now you can access this variable into model
}
}
In controller
$ceventModel = CFactory::getModel( 'cevent' );
$membersCount = $ceventModel->getMembersCount(15);
You can do like this . First you make get and set function in the model.Second load the model in the controller and simply pass the values to setter function.Example as follows:
updateratings.php---this is my model
class RatingManagerModelUpdateRatings extends JModelLegacy
{
public $data;
public function get_data(){
$data=$this->data;
return $data;
}
public function set_data($data){
$this->data=$data;
}
}
Controller.php
class RatingManagerController extends JControllerLegacy
{
public function save_ratings(){
$tips = JRequest::getVar('tips'); //get data from front end form
$model = $this->getModel('UpdateRatings'); //load UpdateRatings model
$model->set_data($tips); //update setter function of model
$res=$model->get_data(); // retrieve getter function
//print_r($res);
}
}
My model class:
<?php class Permissions extends CI_Model {
private $userID = '';
private $permissions = '';
function __construct($userID)
{
// Call the Model constructor
parent::__construct();
$this->userID = $userID;
....
}
function __construct()
{.....}
?>
and I want to load this model with a parameter, apparently I could not do it.
Without a parameter I can load parameterless constructor by this way:
$this->load->model('Permissions');
My first question: is loading a model with a parameter nonsense?
Second one: if it is doable, how can I do that?
Thanks in advance.
you can instantiate the class it self, instead of loading it.
$permission = new Permission($param);
You could take a look at this forum thread: http://codeigniter.com/forums/viewthread/115681/
But I can't see why would you want to give a userid as a parameter in a way for permission checking? Guessing you use sessions to save userdata, write the userid in the session and call this in the Model with $this->session->userdata('user_id').
Happy coding!
You can do your customization in you model's function level. If you provide why do you extend the model, I can say whether it makes sense or not. If you want to create a user specific rule, you had better to do it on a controller.
class Shop_m extends CI_Model {
function getProductPriceInfo($cat,$id) {
$this->db->where('shop_price.catid', $cat);
$this->db->where('shop_price.relid', $id);
$this->db->select('optional.title,optional.desc,shop_price.*');
$this->db->join('optional', 'optional.id = shop_price.relid');
$q = $this->db->get('shop_price');
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}