how to solve "Undefined variable:id" - codeigniter

function empAll()
{
$this->db->where('id',$id);
$q = $this->db->get('employee');
if($q->num_rows()>0)
{
foreach($q->result() as $rows)
{
$data[]=$rows;
}
return $data;
}

Generally we pass id in the Url:
Base_url()/index.php/empAll/25. Now codeigniter automatecaly pass the $id = 25 in the method. In case id not received it will assign the id by 0 and then you will not get this error.
function empAll()
{
$q = $this->db->where('id',$this->input->post('id'))
->get('employee');
if($q->num_rows()>0)
{
foreach($q->result() as $rows)
{
$data[]=$rows;
}
}
return $data;
}

Related

How to get id from url parameter to url function codeigniter4

How to get id from url parameter to url function codeigniter4
public function show($id = null)
{
$data = $this->model->where('id', $id)->findAll();
if ($data) {
return $this->respond($data, 200);
} else {
return $this->failNotFound("Data Not Found to $id");
}
}
You can pass the value using post request.
and in controller function you can read like this
public function show( ) {
$id = $this->request->getVar('id');
$data = $this->model->where('id', $id)->findAll();
if ($data) {
return $this->respond($data, 200);
} else {
return $this->failNotFound("Data Not Found to $id");
}
}

Controller method's result is not accessible on view in CodeIgniter

I am trying to print a controller method's result on view but it is giving me an error:
Undefined variable: $states. Can someone help me to point out what is wrong in my code?
Model code:
public function state_names() {
$query = $this->db->select('name')
->get('place')
->where('parent','India');
$query->db->get();
return $query->result();
}
Controller Code:
public function state_names() {
$st['states'] = $this->place_model->state_names();
if ($this->form_validation->run('resource_signup') == TRUE) {
if (isset($st['states']) && $st['states']->num_rows() > 0) {
$this->load->view('/web/resource_signup',$st);
}
}
return array();
}
View code:
<?php foreach ($states as $state) {
echo $state->name;
}
try this one
public function state_names() {
$this->db->select('name')
$this->db->get('place')
$this->db->where('parent','India');
$query=$this->db->get();
return $query->result_array();
}
In your model you are running query twice - for each method get(). You should run it once:
public function state_names() {
$query = $this->db->select('name')
->where('parent','India')
->get('place');
return $query->result();
}
In your controller you can't check num_rows() because there are results - not full response from database.
public function state_names() {
$st['states'] = $this->place_model->state_names();
if ($this->form_validation->run('resource_signup') == TRUE) {
if (isset($st['states'])) {
$this->load->view('/web/resource_signup',$st);
}
}
return array();
}
Your Problem is 2 place First in Model and second in controller
if (isset($st['states']) && $st['states']->num_rows() > 0)
and Model
Model Solution
public function state_names() {
$query = $this->db->select('name')
->where('parent','India')
->get('place');
// $query->db->get();
if($query->num_rows() > 0){
return $query->result();
}
}
N.B [ Here Where will be first and get will be last its good practice ];
Controller Solution :
public function state_names() {
$st['states'] = $this->place_model->state_names();
if ($this->form_validation->run('resource_signup') == TRUE) {
if (isset($st['states']) ) {
$this->load->view('/web/resource_signup',$st);
}
}
return array();
}`

Return false limits multiple error message to one?

On my multiple upload library, I have a set error function.
On my upload function I use a in_array to check file extensions. If the in_array detects error it displays multiple error messages correct.
The problem I am having is for some reason when I use return FALSE; under the $this->set_error('file_extension_not_allowed') then will on display one message. Not sure why return FALSE limits error messages.
Question: How is it possible to use my return false but be able to display multiple message correct.
<?php
class Multiple_upload {
public $set_errors = array();
public function __construct($config = array()) {
$this->CI =& get_instance();
$this->files = $this->clean($_FILES);
empty($config) OR $this->set_config($config);
}
public function set_config($config) {
foreach ($config as $key => $value) {
$this->$key = $value;
}
return $this;
}
public function upload($field = 'userfile') {
$allowed_extension = explode('|', $this->allowed_types);
if (empty($this->upload_path)) {
$this->set_error('upload_path_not_set', 'upload_path_check');
return FALSE;
}
if (!realpath(FCPATH . $this->upload_path)) {
$this->set_error('upload_path_in_correct', 'location_check');
return FALSE;
}
if (!empty($this->files[$field]['name'][0])) {
foreach ($this->files[$field]['name'] as $key => $value) {
$this->file_name = $this->files[$field]['name'][$key];
$get_file_extension = explode('.', $this->files[$field]['name'][$key]);
$this->get_file_extension_end = strtolower(end($get_file_extension));
$array_1 = array(
$allowed_extension,
);
$array_2 = array(
$get_file_extension[1],
);
if (!in_array($array_2, $array_1)) {
$this->set_error('file_extension_not_allowed', 'extension_check');
return FALSE;
}
}
return $this;
}
}
public function set_error($message, $type) {
$this->CI->lang->load('upload', 'english');
$this->error_message[] = $this->CI->lang->line($message);
return $this;
}
public function display_error_messages($open_tag = '<p>', $close_tag = '</p>') {
foreach($this->error_message as $msg) {
var_dump($msg);
}
}
public function clean($data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
unset($data[$key]);
$data[$this->clean($key)] = $this->clean($value);
}
} else {
$data = htmlspecialchars($data, ENT_COMPAT, 'UTF-8');
}
return $data;
}
}
Maybe this can help...
public function upload($field = 'userfile')
{
$allowed_extension = explode('|', $this->allowed_types);
if (empty($this->upload_path))
{
$this->set_error('upload_path_not_set', 'upload_path_check');
return FALSE;
}
if (!realpath(FCPATH . $this->upload_path))
{
$this->set_error('upload_path_in_correct', 'location_check');
return FALSE;
}
if (!empty($this->files[$field]['name'][0]))
{
$check_error = 0;//added this
foreach ($this->files[$field]['name'] as $key => $value)
{
$this->file_name = $this->files[$field]['name'][$key];
$get_file_extension = explode('.', $this->files[$field]['name'][$key]);
$this->get_file_extension_end = strtolower(end($get_file_extension));
$array_1 = array(
$allowed_extension,
);
$array_2 = array(
$get_file_extension[1],
);
if (!in_array($array_2, $array_1))
{
$this->set_error('file_extension_not_allowed', 'extension_check');
$check_error++;
}
}
if($check_error > 0 )
{
return FALSE;
}
return $this;
}
}

Laravel 5 controller returns controller

i have a controller function that needs to be redirected to a route with a different function to avoid redundancy of codes. is it possible to put a redirect to a different function?
Here is the code:
public function index()
{
$x = Auth::user()->id;
$id = DB::table('requests')->where('id', $x)->lists('userid');
if (!is_null($id)) {
$frnd = DB::table('users')->whereIn('id', $id)->get();
if (!is_null($frnd)) {
return view('friendlist', compact('frnd'));
} else {
$frnd = null;
return view('friendlist', compact('frnd'));
}
} else {
$frnd = null;
return view('friendlist', compact('frnd'));
}
}
public function respond()
{
$frnds = new Friend;
$id = Auth::user()->id;
$friendid = Request::input('friendid');
$frnds->id = $id;
$frnds->friendid = $friendid;
if (Input::get('accept')) {
$frnds->save();
}
DB::table('requests')->where('id', $id)->where('userid', $friendid)
return // this is where i should redirect to page with function index()
}
Name the index route in routes definition like this
Route::get('home', ['uses' => 'YourController#index', 'as' => 'home']);
Then use redirect method to redirect to this route:
return redirect()->route('home');
For more info on redirects use official docs
http://laravel.com/docs/5.1/responses#redirects
I don't think is a perfect, but someone prefer this way:
private function _index()
{
$x = Auth::user()->id;
$id = DB::table('requests')->where('id', $x)->lists('userid');
if (!is_null($id)) {
$frnd = DB::table('users')->whereIn('id', $id)->get();
if (!is_null($frnd)) {
return view('friendlist', compact('frnd'));
} else {
$frnd = null;
return view('friendlist', compact('frnd'));
}
} else {
$frnd = null;
return view('friendlist', compact('frnd'));
}
}
public function index()
{
$this->_index();
}
public function respond()
{
$frnds = new Friend;
$id = Auth::user()->id;
$friendid = Request::input('friendid');
$frnds->id = $id;
$frnds->friendid = $friendid;
if (Input::get('accept')) {
$frnds->save();
}
DB::table('requests')->where('id', $id)->where('userid', $friendid)
$this->_index();
}
private function for repeated code.

How to fix "Call to a member function getUser()" in facebook via CodeIgniter?

I am sitting in my chair for 2 hours and I cannot find what is wrong with this. I'm new to codeigniter things and I only got this error for the first time. Can you guys help me? Any help would be appreciated. Thanks in advance so much ...
****A PHP Error was encountered
Severity: Notice
Message: Undefined property: Facebook_model::$facebook
Filename: models/facebook_model.php
Line Number: 15
Fatal error: *Call to a member function getUser() on a non-object in C:\xampp\htdocs\hpbge\hpbge_files\system\hpbgestrong text_web_app\models\facebook_model.php on line 15*****
<?php
class Facebook_model extends Model {
public function get_user() {
$query = $this->facebook->getUser();
if ($query) {
$data['is_true'] = TRUE;
$data['facebook_uid'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function get_access_token() {
$query = $this->facebook->getAccessToken();
if ($query) {
$data['is_true'] = TRUE;
$data['access_token'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function get_api_secret() {
$query = $this->facebook->getApiSecret();
if ($query) {
$data['is_true'] = TRUE;
$data['api_secret'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function get_app_id() {
$query = $this->facebook->getApiSecret();
if ($query) {
$data['is_true'] = TRUE;
$data['app_id'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function get_logout_url() {
$query = $this->facebook->getLogoutUrl(array('next' => base_url()));
if ($query) {
$data['is_true'] = TRUE;
$data['logout_url'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function get_signed_request() {
$query = $this->facebook->getSignedRequest();
if ($query) {
$data['is_true'] = TRUE;
$data['signed_request'] = $query;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function set_access_token($access_token) {
$query = $this->facebook->setAccessToken($access_token);
if ($query) {
$data['is_true'] = TRUE;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function set_api_secret($app_secret) {
$query = $this->facebook->setApiSecret($app_secret);
if ($query) {
$data['is_true'] = TRUE;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
function set_app_id($app_id) {
$query = $this->facebook->setAppId($app_id);
if ($query) {
$data['is_true'] = TRUE;
return $data;
} else {
$data['is_true'] = FALSE;
return $data;
}
}
//function is formatted for the following
//https://graph.facebook.com/ID/CONNECTION_TYPE?access_token=123456
function get_facebook_object($object, $facebook_uid, $access_token) {
$fb_connect = curl_init();
curl_setopt($fb_connect, CURLOPT_URL, 'https://graph.facebook.com/'.$facebook_uid.'/'.$object.'?access_token='.$access_token);
curl_setopt($fb_connect, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($fb_connect);
curl_close($fb_connect);
$result = json_decode($output);
if (isset($result->error)) {
$data['is_true'] = FALSE;
$data['message'] = $result->error->message;
$data['type'] = $result->error->type;
$data['code'] = $result->error->code;
return $data;
} else {
$data['is_true'] = TRUE;
$data['data'] = $result->data;
return $data;
}
}
}
It seems that you have not loaded the facebook library put this line in your construct of model file
$this->load->library('facebook', array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET'
));
and make sure that you have put the facebook.php and base_facebook.php in libraries folder under application.

Resources