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)?>
Related
I can't pass the data from the controller to the view in CodeIgniter 3. All the thing seem okay. Do I need to put the Controller's part in the index() method?
Donor_Model.php
function viewDonors() {
$query = $this->db->get('donors');
return $query;
}
Staff.php controller
public function viewDonors()
{
$this->load->Model('Donor_Model');
$data['donors'] = $this->Donor_Model->viewDonors();
$this->load->view('viewdonors', $data);
}
When I try to call the $data in the viewdonors.php view, it shows the $donors as undefined.
screenshot of viewdonors.php
#TimBrownlaw is right. Even though the IDE shows an error, the code runs without a problem.
You need to edit your model page to:-
function viewDonors() {
$query = $this->db->get('donors');
return $query->result();
}
As you want to loop through donors data in the view page so you need to return result function in the model.
According to Yii 2 Rest APi documentation, I have a CountriesCountry that extends \yii\rest\ActiveController and a corresponding Countries model. This is the code for my Controller class.
<?php
namespace app\controllers;
class CountriesController extends \yii\rest\ActiveController{
public $modelClass = 'app\models\Countries';
public function actionIndex(){
}
public function actionView(){
}
public function actionCreate(){
}
public function actionUpdate(){
}
public function actionDelete(){
}
public function actionOptions(){
}
}
When I send a get request, it returns all the countries in my database.
My Question
is it possible to return my own result from action methods. Like in the actionIndex(), I will like to limit the result to 20 records. I did something like this but it is not working.
public function actionIndex(){
$model = Countries::find()->limit(20);
print_r($model);
}
I know that I can get all the countries from by database and loop through it and obtain only 20 results but I want to just query for 20 records from database.
Your class CountriesController that extends from \yii\rest\ActiveController automatically supports GET, PUT, POST calls etc. No need for actionIndex(), actionCreate(), etc if you just want regular REST functionality. Read about it in the Yii2 guide.
To limit the results you could just set another page size in your controllers afterAction-method. Add this to your controller. (I believe that 20 records is default of Pagination class, so if that is what you want you don't need this code at all. Just use the default functionality of yii/rest/ActiveController.)
public function afterAction($action, $result) {
if (isset($result->pagination) && ($result->pagination !== false)) {
$result->pagination->setPageSize(100);
}
return parent::afterAction($action, $result);
}
Suppose your api link is:
http://localhost/yii2-rest/api/country/?limit=15&order=id
Controller:
public function actionIndex(){
$model = Countries::find()
->orderBy($_GET['order'])
->limit($_GET['limit'])
->all();
return $model;
}
Take care about security!
You can get query string this way:
$limit = Yii::app()->getRequest()->getQuery('limit');
Straightway
//SELECT * FROM countries LIMIT 20
$countries= Countries::find()->limit(20)->all();
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.
I encountered a strange behaviour which I do not understand when using type-hinting in a Laravel Controller. I have a Profile controller, which should call the same function of a service twice based on different parameters. However, when I call the function the second time, the result of the first call is changed.
Here is my code; Two users' profiles should be displayed on one page:
//use statements are omitted for brevity
class ProfileController extends Controller {
protected $compose_profile_service;
public function __construct(ComposeProfileService $compose_profile_service) {
$this->compose_profile_service = $compose_profile_service;
}
public function compare($user1, $user2) {
$user1_profile = $this->compose_profile_service->composeProfile($user1);
$user2_profile = $this->compose_profile_service->composeProfile($user2);
dd($user1_profile); //Problem: This dd() gives me the profile of user2 instead of user1
}
}
class ComposeProfileService {
public function composeProfile($user) {
//Get some stuff from the DB
$profile_data = $user->profile()->get();
return $profile_data;
}
}
Of course, if I dd() $user1 or $user2 above the respective call of the compose function, the right user is provided.
I have been developing using non framework for about 3 years and I have heard about PHP framework codeigniter.
I think I missed something using this framework which is used for API for my mobile application.
I've facing some problem to get data from database using Phil's CI framework, RESTful plugin. My browser shows error code:
{"status":false,"error":"Unknown method."}
Using my logic below:
controller: user.php
<?php
require(APPPATH.'libraries/REST_Controller.php');
class User extends REST_Controller{
public function user_get()
{
//IF USER NOT EXIST
if(!$this->get('id'))
{
$this->response(NULL, 400);
}
//CHECK TO MODEL FUNCTION
$user = $this->user_model->get_user($this->get('id'));
//IF USER EXIST
if($user)
{
$this->response($user, 200); // 200 being the HTTP response code
}
}
public function user_put()
{
// create a new user and respond with a status/errors
}
public function user_post()
{
// update an existing user and respond with a status/errors
}
public function user_delete()
{
// delete a user and respond with a status/errors
}
}
?>
model: user_model.php
<?php
class User_model extends CI_Model{
function __construct() {
parent::__construct();
}
function get_user($id){
$query = $this->db->get('mt_user', array('idmt_user'=>$id));
return $query->row_array();
}
}
?>
i'm accessing the database which is has this rows:
idmt_user,username,password, emails, createdate
accessed using mozilla:
#http://localhost/<project>/index.php/user/<userid>
Where's the error(s) ?
thanks.
update,
i am already defined the autoloads for database. But this problem still persist. Is there any helps? thanks
as stated below in answer, i tried to access the url, /user/get_user/ but still showing the same result. Is there any problem using idmt_user in database?
Insert in controllers:
function __construct() {
parent::__construct();
$this->load->model('user_model');
}
And try this route:
http://localhost//index.php/user/user/id/
I believe that the url you are accessing with is wrong.. Try accessing with the url #http://localhost/<project>/index.php/user/<method_name>/<user_id>
Another way of doing it would be using the URI Class.
Instead of writing the following:
//IF USER NOT EXIST
if(!$this->get('id'))
{
$this->response(NULL, 400);
}
Changed to:
$userid = $this->uri->segment(3);
Would be 3 based off the url being http://localhost/<project>/user/<method_name>/<user_id>
Reference for URI Class: http://www.codeigniter.com/user_guide/libraries/uri.html
In order to use $this->get(); there would have to be a query parameter to get. Example: id=
If you are passing query parameters than you could store these values in a array as shown below.
if(!empty($this->get()))
{
$params[] = $this->get();
}
I hope this helps.
You should to try include your model file into your controller file by using a __construct() function, like this:
function __construct(){
parent::__construct();
$this->load->model(array('user_model'));
}