I am starting my game with CodeIgniter and I have a problem - I would like to download the ID - of the product from the database so that it will be passed to the URL address:
Example address:
http://localhost/crm-SEO/api/admin/domains/{{ID in database}}/notes
When I have other methods like:
domains/GET/6
domains/update/6
Everything works fine for me, but I can not grasp it that I have domains/{{ID}}/
My code i models:
public function get( $id = false)
{
if ( $id == false ) {
$q = $this->db->get('domains');
$q = $q->result();
}
else{
$this->db->where('id', $id);
$q = $this->db->get('domains');
$q = $q->row();
}
return $q;
}
public function update($domain)
{
$this->db->where('id', $domain['id'] );
$this->db->update('domains', $domain);
}
public function create($domain)
{
$this->db->insert('domains', $domain);
}
public function delete($domain)
{
$this->db->where('id', $domain['id'] );
$this->db->delete('domains');
}
My code in controller:
public function __construct()
{
parent::__construct();
$post = file_get_contents('php://input');
$_POST = json_decode($post,true);
$this->load->model('admin/domains_model');
}
public function get($id = false)
{
$result = $this->domains_model->get($id);
echo '{"records":' . json_encode( $result ) . '}';
}
public function update()
{
$domain = $this->input->post('domain');
$this->domains_model->update($domain);
}
public function create()
{
$domain = $this->input->post('domain');
$this->domains_model->create($domain);
}
public function delete()
{
$domain = $this->input->post('domain');
$this->domains_model->delete($domain);
}
Related
If I try this is a class with static methods:
public static function chkLog($role)
{
$userrole = $this->getRequest()->getSession()->read('userrole');
return $userrole;
/// more code
Even tried:
public static function chkLog($role)
{
$userrole = Session::read('userrole');
return $userrole;
/// more code
In laravel I can:
public static function userRole($role = null)
{
$userrole = Auth::user()->role;
$checkrole = explode(',', $userrole);
if (in_array($role, $checkrole)) {
return $role;
}
return false;
}
usage
public function scopegetPets($query, $petsearch = '')
{
$petsearch = $petsearch . "%";
$query = Pet::where('petname', 'like', $petsearch);
if (ChkAuth::userRole('admin') === false) {
$userid = Auth::user()->id;
$query->where('ownerid', '=', $userid);
}
$results = $query->orderBy('petname', 'asc')->paginate(5);
return $results;
}
But in cakephp to call statically I had to write this:
public static function __callStatic($method, $params)
{
$instance = ChkAuth::class;
$c = new $instance;
return $c->$method(...array_values($params));
}
In order to call this:
public function userRole($role = null)
{
$userrole = $this->getRequest()->getSession()->read('userrole');
$checkrole = explode(',', $userrole);
if (in_array($role, $checkrole)) {
return $role;
}
return false;
// more
And usage:
public function getPets($offset = "", $rowsperpage = "", $petsearch = "")
{
$pagingQuery = "LIMIT {$offset}, {$rowsperpage}";
$petsearch = $petsearch . "%";
if (Auth::chkRole('admin') === true) {
return DB::select("SELECT * FROM " . PREFIX . "pets " . $pagingQuery);
}
$authid = Auth::authId();
return DB::select("SELECT * FROM " . PREFIX . "pets WHERE ownerid = :authid " . $pagingQuery, ["authid" => $authid]);
}
So basically I am trying to figure out how to use session in cake php inside a static method.
Note the __callStatic works, but as a work a round.
CakePHP's Router class includes a static function to return the request object. The request object includes a function to return the session. So:
Router::getRequest()->session()
or, as of version 3.5:
Router::getRequest()->getSession()
How do I retrieve (and process) the params I have being passed in from this...
Modules::load('MembersList', $this->input->get(NULL, TRUE);
... into the module its being passed into?
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MemberList extends MX_Controller {
function __construct()
{
parent::__construct();
$this->load->model('memberslist_model');
}
public function index()
{
// $params = how_do_i_retrieve it?
if ( ! isset($list) || $list == 'individuals')
{
$data['module'] = 'Individuals';
$data['results'] = $this->memberslist_model->list_individuals();
$data['count'] = count($data['results']);
$data['view'] = $this->load->view('list_individuals', $data, TRUE);
}
if ( $list == 'families')
{
$data['module'] = 'Families';
}
return $this->load->view('memberslist', $data, TRUE);
}
public function settings()
{
$data['settings'] = $this->memberslist_model->settings();
return $this->load->view('settings', $data, TRUE);
}
}
I understand that it is being passed as URI segments like codeigniter but I've tried everything and I can't get it to work.
I'm not sure which version you use, but you can try this:
$whatEver = Modules::run('MembersList/index', $this->input->get(NULL, TRUE));
class MemberList extends MX_Controller {
function __construct()
{
parent::__construct();
$this->load->model('memberslist_model');
}
public function index($params)
{
print_r($params);
// $params = how_do_i_retrieve it?
if ( ! isset($list) || $list == 'individuals')
{
$data['module'] = 'Individuals';
$data['results'] = $this->memberslist_model->list_individuals();
$data['count'] = count($data['results']);
$data['view'] = $this->load->view('list_individuals', $data, TRUE);
}
if ( $list == 'families')
{
$data['module'] = 'Families';
}
return $this->load->view('memberslist', $data, TRUE);
}
public function settings()
{
$data['settings'] = $this->memberslist_model->settings();
return $this->load->view('settings', $data, TRUE);
}
}
Hello any one can tell that how to load two function from model class in controller one method. I want to run multiple select quires in one page using codeigniter:
Controller
public function property_detail( $id )
{
$this->load->model('insertmodel');
$select1 = $this->insertmodel->find($id);
$select2 = $this->insertmodel->detail_list();
$data = array();
$this->load->view('home/property_detail', ['select1'=>$select1], ['select2'=>$select2]);
//$this->load->view('home/property_detail', ['select2'=>$select2]);
}
Model
public function find( $id )
{
$query = $this->db->from('article')->where(['id'=> $id])->get();
if( $query->num_rows() )
return $query->row();
return false;
}
public function detail_list(){
$query1 = $this->db->query("select * from article");
return $query1->result();
}
In Controller
public function property_detail( $id )
{
$this->load->model('insertmodel');
$data['select1'] = $this->insertmodel->find($id);
$data['select2'] = $this->insertmodel->detail_list();
$this->load->view('home/property_detail', $data);
}
In Model
public function find($id)
{
$query = $this->db->get_where('article', array('id' => $id), 0, 0)->get();
if( $query->num_rows() > 0 )
{
$result = $query->result_array();
return $result;
}
else
{
return false;
}
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
$result = $query1->result_array();
return $result;
}
In View
foreach ($select2 as $item) {
# your foreach lop goes here
}
As well check empty() before passing it to the foreach loop
As an alternative to #Rijin's useful answer newMethod() can make calls to the existing model methods. This might be useful if you don't want to break the interface already created for the model because you are using find($id) and detail_list() in other code.
Model:
public function find($id)
{
$query = $this->db->from('article')->where(['id' => $id])->get();
if($query->num_rows())
return $query->row();
return false;
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
return $query1->result();
}
public function newMethod($id)
{
$result['select1'] = $this->find($id);
if($result['select1'] !== FALSE)
{
$result['select2'] = $this->detail_list();
return $result;
}
return FALSE;
}
Controller:
public function property_detail($id)
{
$this->load->model('insertmodel');
$data = $this->insertmodel->newMethod($id);
$this->load->view('home/property_detail', $data);
}
Model :
public function find( $id )
{
$query = $this->db->from('article')->where(['id'=> $id])->get();
if( $query->num_rows() )
return $query->row();
return false;
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
return $query1->result();
}
Controller :
public function property_detail( $id )
{
$this->load->model('insertmodel');
$data['select1'] = $this->insertmodel->newMethod($id);
$data['select2'] = $this->insertmodel->detail_list();
$this->load->view('home/property_detail', $data);
}
I was doing a query in the controller but it came to my attention that I should do it in the model.
This is the code I have in the controller
$this->db->where('page', 'your-secret-skill');
$this->data['articles'] = $this->article_m->get();
and this is the get method I have in the core model:
public function get($id = NULL, $single = FALSE){
if ($id != NULL) {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->where($this->_primary_key, $id);
$method = 'row';
}
elseif($single == TRUE) {
$method = 'row';
}
else {
$method = 'result';
}
if (!count($this->db->ar_orderby)) {
$this->db->order_by($this->_order_by);
}
return $this->db->get($this->_table_name)->$method();
}
How would I go about this and do the query in the articles model and loading it with the controller?
You should inherit the CI_Model with the Articles model and in it you can write your query as:
class Articles_m extends CI_Model
{
function __construct()
{
parent::__construct();
$this->table_name = 'articles';
}
public function get()
{
$this->db->where('page', 'your-secret-skill');
$query = $this->db->get($this->table_name);
return $query->result();
}
}
and then in your controller:
$this->data['articles'] = $this->article_m->get();
Remove this line from the controller first:
$this->db->where('page', 'your-secret-skill');
Instead send your-secret-skill as a parameter to your model function:
$where = array( 'page', 'your-secret-skill' );
$this->data['articles'] = $this->article_m->get('','',$where);
In your model:
public function get($id = NULL, $single = FALSE, $where = array){
if(isset($where) && is_array($where)){
$this->db->where($where);
}
}
When I send a Request to this Page including POST-DATA ({"bot_hw_id":"2147483647"}), i get the following error:
<p>Severity: Notice</p>
<p>Message: Undefined offset: 0</p>
<p>Filename: models/prometheus_model.php</p>
<p>Line Number: 26</p>
My Code:
Controller(update_bot function):
[code]public function update_bot()
{
$bot_data = json_decode($this->input->post('bot_data'));
$to_update = array(
'bot_last_update' => time(),
'bot_ip' => $_SERVER['REMOTE_ADDR'],
'bot_port' => $_SERVER['REMOTE_PORT']
);
$bot_data = $this->prometheus_model->get_bot_data_by_hw_id(/*$bot_data->{'bot_hw_id'}*/$bot_data->{'bot_hw_id'});
//echo $bot_data['bot_id'];
print_r($bot_data);
$this->prometheus_model->update('bots', array('bot_id' => $bot_data['bot_id']), $to_update);
//var_dump($bot_data);
}
Model(prometheus_model):
<?php
class Prometheus_model extends CI_Model {
var $tables = array(
'bots' => 'bots'
);
function __construct() {
parent::__construct();
}
public function tablename($table = NULL) {
if(! isset($table)) return FALSE;
return $this->tables[$table];
}
public function get($table, $where = array(), $single = FALSE, $order = NULL) {
$this->db->where($where);
if(isset($order)) {
$this->db->order_by($order);
}
$q = $this->db->get_where($this->tablename($table),$where);
$result = $q->result_array();
if($single) {
return $result[0];
}
return $result;
}
public function update($table, $where = array(), $data) {
$this->db->update($this->tablename($table),$data,$where);
return $this->db->affected_rows();
}
public function insert($table, $data) {
$this->db->insert($this->tablename($table),$data);
return $this->db->insert_id();
}
public function delete($table, $where = array()) {
$this->db->delete($this->tablename($table),$where);
return $this->db->affected_rows();
}
public function explicit($query) {
$q = $this->db->query($query);
if(is_object($q)) {
return $q->result_array();
} else {
return $q;
}
}
public function num_rows($table, $where = NULL) {
if(isset($where)){
$this->db->where($where);
}
$q = $this->db->get($table);
return $q->num_rows();
}
public function get_bot_data_by_hw_id($bot_hw_id) {
$q = $this->get('bots', array('bot_hw_id' => $bot_hw_id), TRUE);
return $q;
}
}
?>;
How can i fix this error?
First, I should mention that it's not absolutely a good and safe idea to query user input exactly without any process (as we see in your code). It's better to have different models at least for each table.
Anyway...
This way your function will be corrected:
public function get($table, $where = array(), $single = FALSE, $order = NULL) {
$this->db->where($where);
if(isset($order)) {
$this->db->order_by($order);
}
$q = $this->db->get_where($this->tablename($table),$where);
$result = $q->result_array();
// You should use $q->num_rows() to detect the number of returned rows
if($q->num_rows() == 1) {
// Return the first row:
return $result[0];
}
return $result;
}
It returns the first row when there is only one, and brings an array when $q->num_rows() is not equal to 1.
Hope it helps