public function get_active_posted_job_by_id($id){
$Q = $this->db->query("CALL get_active_posted_job_by_id($id)");
if ($Q->num_rows() > 0){
$return = $Q->row();
} else {
$return = 0;
}
$Q->next_result();
$Q->free_result();
return $return;
}
Do like this
return $this->db->get_where('table_name', ['id' => $id])->row();
This will give only one record according to $id
Related
This is somewhat related to [CodeIgniter active records' problems calling multiple stored procedures
but I am not experiencing a blank page; instead when I am passing my data array to view it seems that the the preceding array also being drag to the view.
model
public function data1($student) {
$year = 1;
$sem = 1;
$course = $this->getStudentCourseByStudentId($student);
$sql = "CALL EVALUATION_BY_YEAR_SEM(?,?,?,?)";
$query = $this->db->query($sql, array($course, $student, $year, $sem));
if (!$query) {
return $this->db->error();
} else {
mysqli_next_result( $this->db->conn_id );
return $query->result();
}
}
public function data2($student) {
$year = 1;
$sem = 2;
$course = $this->getStudentCourseByStudentId($student);
$sql = "CALL EVALUATION_BY_YEAR_SEM(?,?,?,?)";
$query = $this->db->query($sql,array($course,$student,$year,$sem));
if (!$query) {
return $this->db->error();
} else {
mysqli_next_result( $this->db->conn_id );
return $query->result();
}
}
Controller:
$data['data1']=data1 from my model(SP);
$data['data2']=data2 from my model(SP);
View:
foreach($data2 as key => $value ) {
echo ....;
}
Here's the PROBLEM... in the view, I only wanted to output the $data2 but to my surprise $data1 is also being output.
Does anybody else have this issue?
I JUST SOLVE IT.
Model
public function data1($student){
**$this->db->initialize();**
$year = 1;$sem = 1;
$course = $this->getStudentCourseByStudentId($student);
$sql = "CALL EVALUATION_BY_YEAR_SEM(?,?,?,?)";
$query = $this->db->query($sql,array($course,$student,$year,$sem));
if (!$query) {
return $this->db->error();
}else {
mysqli_next_result( $this->db->conn_id );
return $query->result();**$this->db->close();**
}
}
public function data2($student){
**$this->db->initialize();**
$year = 1;$sem = 2;
$course = $this->getStudentCourseByStudentId($student);
$sql = "CALL EVALUATION_BY_YEAR_SEM(?,?,?,?)";
$query = $this->db->query($sql,array($course,$student,$year,$sem));
if (!$query) {
return $this->db->error();
}else {
mysqli_next_result( $this->db->conn_id );
return $query->result();**$this->db->close();**
}
}
controller
$data['data1']=data1 from my model(SP);
**$this->db->close();**
$data['data2']=data2 from my model(SP);
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;
}
}
my function with advanced wheres is not giving any syntax error but its not even working. Its displaying. i have written this function following this example http://laravel.com/docs/queries#advanced-wheres
public function getData($wheres1 = array(),$wheres2 = array(),$wheres3 = array(),$wheres4 = array(),$wheres5 = array(),$wheres6 = array(),$wheres7 = array())
{
$query = Listing::query();
$result = array();
$query = $query->where(function($q){
if(!empty($wheres1)){
$count=0;
foreach($wheres1 as $where){
if($count==0)
{ $q = $q->where($where['field'], $where['operator'], $where['value']);
$count++;
}
else
$q = $q->orWhere($where['field'], $where['operator'], $where['value']);
}
}
})->where(function($q){
if(!empty($wheres2)){
$count=0;
foreach($wheres2 as $where){
if($count==0)
{ $q = $q->where($where['field'], $where['operator'], $where['value']);
$count++;
}
else
$q = $q->orWhere($where['field'], $where['operator'], $where['value']);
}
}
})->where(function($q){
if(!empty($wheres3)){
$count=0;
foreach($wheres3 as $where){
if($count==0)
{ $q = $q->where($where['field'], $where['operator'], $where['value']);
$count++;
}
else
$q = $q->orWhere($where['field'], $where['operator'], $where['value']);
}
}
});
$result = $query->orderBy('id', 'desc')->paginate(3);
return $result;
}
You need to add use for each where
$query = $query->where(function($q) use ($wheres1) {
....
}
You may try this approach:
$query = Listing::query();
if(is_array($wheres1) && count($wheres1)) {
$first = array_shift($wheres1);
$query->where($first['field'], $first['operator'], $first['value']);
if(count($wheres1)) {
foreach($wheres1 as $where) {
$query->orWhere($wheres['field'], $wheres['operator'], $wheres['value']);
}
}
}
Now repeat the same process for rest of the wheres, for example ($wheres2):
if(is_array($wheres2) && count($wheres2)) {
$first = array_shift($wheres2);
$query->where($first['field'], $first['operator'], $first['value']);
if(count($wheres2)) {
foreach($wheres2 as $where) {
$query->orWhere($wheres['field'], $wheres['operator'], $wheres['value']);
}
}
}
At last:
return $query->orderBy('id', 'desc')->paginate(3);
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.
I'm trying to match up suppliers from a postcode search:
Model code:
function get_suppliers(){
$this->db->from('suppliers');
$this->db->where('postcode', $data);
$this->db->select('name,type,site,contact,number');
$q = $this->db->get();
if($q->num_rows() > 0) {
foreach($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
Controller code:
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('postcode','Postcode', 'required|numeric|exact_length[4]');
$data = array(
'postcode' => $this->input->post('postcode')
);
if($this->form_validation->run() == FALSE)
{
## reload page ##
$this->load->view('welcome_message');
}
else
{
$this->load->model('site_model');
$this->site_model->add_record($data);
echo("postcode entered");
$data['rows'] = $this->site_model->get_suppliers($data);
print_r($data);
}
}
Obviously ignore the printers and echo thats just me bring to see whats going on I'm pretty sure i need to just change the $data in model to something just not sure what(tried heaps of things)
Model:
function get_suppliers($postcode = '*') // <-- Capture the postcode
{
$this->db->from('suppliers');
$this->db->where('postcode', $postcode); // <-- pass it in here
$this->db->select('name,type,site,contact,number');
$q = $this->db->get();
if($q->num_rows() > 0)
{
foreach($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
Controller:
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('postcode','Postcode', 'required|numeric|exact_length[4]');
$data = array(
'postcode' => $this->input->post('postcode')
);
if($this->form_validation->run() == FALSE)
{
## reload page ##
$this->load->view('welcome_message');
}
else
{
$this->load->model('site_model');
$this->site_model->add_record($data);
$data['rows'] = $this->site_model->get_suppliers( $data['postcode'] ); // <-- pass the postcode
echo("postcode entered: " . $data['postcode'] . "<pre>");
print_r($data);
}
}
In your model:
function get_suppliers($data = ''){
i.e.: You haven't included $data as the argument