How to get value column where column in codeigniter 3 - mysql

I have a table like:
sysopt|sysval
......................
site_url|http://domain.com/
site_title|My Website
......................
in mysql i use:
$query = $db->query("SELECT * FROM sysconfig");
while ($result = $db->fetch_array($query)) {
$settings[$result['sysopt']] = $result['sysval'];
}
But in CI:
class Sysinfo
{
var $info = array();
public function __construct()
{
$CI =& get_instance();
$settings = $CI->db->select("*")
->get("sysconfig");
foreach($settings as $setting) {
$this->info[$setting['sysopt']] = $setting['sysval'];
}
}
In view i call:
<?php echo $this->Sysinfo->info->site_url; ?>
Show error.
Message: Undefined property: CI_Loader::$Sysinfo
Thankyou any solution fix.

Why just not extend the CI_Model? For me, in framework..just extend the class so you would be easier
In your model:
class Sysinfo extends CI_Model
{
var $info = array();
public function __construct()
{
//$CI =& get_instance();
$settings = $this->db->select("*")
->get("sysconfig")->result_array(); //Return result as array
foreach($settings as $setting) {
$this->info[$setting['sysopt']] = $setting['sysval'];
}
}
function get_sysinfo_data()
{
return $this->info;
}
}
In your Controller
public function __construct()
{
$this->load->model('Sysinfo');
}
function index()
{
$data['arr_result'] = $this->Sysinfo->get_sysinfo_data();
$this->load->view('your_view',$data);
}
In view:
foreach($arr_result as $row_array):
var_dump($row_array); // To view your result
endforeach;
If you want Sysinfo as library..then do like this:
Your library should be name as Sysinfo.php:
class Sysinfo
{
var $info = array();
public function __construct()
{
$CI =& get_instance();
$settings = $CI->db->select("*")
->get("sysconfig")->result_array(); //Return result as array
foreach($settings as $setting) {
$this->info[$setting['sysopt']] = $setting['sysval'];
}
}
function get_sysinfo_data()
{
return $this->info;
}
}
In your Controller:
public function __construct()
{
$this->load->library('Sysinfo');
}
function index()
{
$data['arr_result'] = $this->Sysinfo->get_sysinfo_data();
$this->load->view('your_view',$data);
}
Hope it helps.

The error is appearing because you haven't extended your class with CI_Model base class.
Try defining your class as:
class Sysinfo extends CI_Model{ //your code }
will get rid of this error message.

Related

Controller not defined laravel

the error :
Action App\Http\Controllers\formController#form not defined. (View: C:\xampp\htdocs\ucar3\resources\views\layouts\Form.blade.php) (View: C:\xampp\htdocs\ucar3\resources\views\layouts\Form.blade.php)
I tried changing the route in web.php
web.php
Route::resource('Inscription','inscriController');
Controller
class FormController extends Controller
{
public function show()
{
return view('pages.Inscription');
}
public function insert(Request $request)
{
$Cin = $request->input('Cin');
$nom = $request->input('nom');
$prenom = $request->input('prenom');
$email = $request->input('email');
$telephone = $request->input('telephone');
$specialite = $request->input('specialite');
$typedediplome = $request->input('typedediplome');
$mentiondiplome = $request->input('mentiondiplome');
$redoublement = $request->input('redoublement');
$communication = $request->input('communication');
$publication = $request->input('publication');
$experiencePedagogiqueSecondaire = $request
->input('experiencePedagogiqueSecondaire');
$experiencePedagogiqueSupérieur = $request
->input('experiencePedagogiqueSupérieur');
$data = array(['Cin'=>$Cin,
'nom'=>$nom,
'prenom'=>$prenom,
'email'=>$email,
'telephone'=>$telephone,
'specialite'=>$specialite,
'typedediplome'=>$typedediplome,
'mentiondiplome'=>$mentiondiplome,
'redoublement'=>$redoublement,
'communication'=>$communication,
'publication'=>$publication,
'experiencePedagogiqueSecondaire'=>$experiencePedagogiqueSecondaire,
'experiencePedagogiqueSupérieur'=>$experiencePedagogiqueSupérieur
]);
DB::table('users')->insert($data);
return view('pages.success');
}
}
Model
class form extends Model
{
public $table = "form";
protected $fillable = [
'Cin',
'nom',
'prenom',
'telephone',
'email',
'specialite',
'typedediplome',
'mentiondiplome',
'redoublement',
'communication',
'publication',
'experiencePedagogiqueSecondaire',
'experiencePedagogiqueSupérieur'
];
public $timestamps = true;
}
As the Error says
formController#form not defined.
but in your class you've
FormController extends Controller
Please check if you are calling FormController with lower case 'F'.
I think you have problems with your inscriController and your routes, use the following code:
web.php
use App\Http\Controllers\inscriController;
Route::resource('Inscription', inscriController::class);
app/Http/Controllers.php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class inscriController extends Controller {
public function __construct() {
$this->middleware('auth');
}
}
Check if you set the correct namespace in the FormController.php
You are also missing a function form inside your FormController.

Can I store a repeating sql query separately and call them inside a function in model - Codeigniter

My current model is somewhat like
class select extends CI_Model{
function __construct(){
parent::__construct();}
public function get_a(){
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2','table1.id =
table2.id');
$this->db->where('x',1);
}
public function get_b(){
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2','table1.id =
table2.id');
$this->db->where('x',2);
}
public function get_c(){
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2','table1.id =
table2.id');
$this->db->where('x',3);
}
As you can see the same Join operation is repeating in all the three functions. Is there a way which I can just do this join once and use it with the where clause
And there are other questions. If that's possible is it efficient than this current method?
Repeating code is wasteful and avoidable. Here is one function that takes an argument to be used with where.
public function get_x($where){
$this->db
->select('*')
->from('table1')
->join('table2','table1.id = table2.id')
->where('x',$where);
}
if i understand you correct - you can do the following
class select extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function get_a()
{
$this->initializeBaseQuery();
$this->db->where('x',1);
}
public function get_b()
{
$this->initializeBaseQuery();
$this->db->where('x',2);
}
public function get_c()
{
$this->initializeBaseQuery();
$this->db->where('x',3);
}
private function initializeBaseQuery()
{
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2','table1.id = table2.id');
}
}
You can try this solution for your problem :
Select modal file :
<?php
class Select extends CI_Model{
function __construct(){
parent::__construct();
}
public function get_selection($where = array()){
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2','table1.id = table2.id', 'left');
if(!empty($where)) {
foreach ($where as $key => $value) {
$this->db->where($key,$value);
}
}
$query_result = $this->db->get();
$result = $query_result->result();
return $result;
}
}
?>
Text Controller file :
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Test extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function get_select(){
$where = array("x"=>"1"); // you can pass more than one key and value for condition.
$result =$this->select->get_selection($where);
echo "<pre>";
print_r($result);
exit;
}
}
}
?>
I Hope it will help you.

How to pass a variable from another function to the view in index function

I want to pass the $data variable from the findCity() function with the json return to the view in the index function.
How can I do that please ?
class HomeController extends Controller
{
public function index(Request $request)
{
$countries = Location::select('country_name')
->groupBy('country_name', 'country_name')
->get();
$cities = Location::select('city_name','city_name')
->where('region_name', $request->region_name)
->groupBy('city_name', 'city_name')
->get();
$areas = Area::get()->toTree();
return view('home', compact('areas','countries', 'cities'));
}
public function findCity(Request $request)
{
$data = Location::select('city_name','city_name')
->where('region_name', $request->region_name)
->groupBy('city_name', 'city_name')
->get();
return response()->json($data);
}
}
The Ajax code :
$(document).on('change','.regionName',function(){
//console.log("hmm its change");
var cat_id=$(this).val();
//console.log(cat_id);
var div=$(this).parent();
var op=" ";
$.ajax({
type:'get',
url:'{!!URL::to('findCityName')!!}',
data:{'region_name':cat_id},
success:function(data){
//console.log('success');
//console.log(data);
//console.log(data.length);
op+='<option value="0" selected disabled>Choose City</option>';
for(var i=0;i<data.length;i++){
op+='<option value="{{ route('user.location.store', $cities) }}">'+data[i].city_name+'</option>';
}
div.find('.cityName').html(" ");
div.find('.cityName').append(op);
},
error:function(){
}
});
});
});
You can make findCity() to be an static method and call it from inside the index() method:
public function index(Request $request)
{
// The rest of your code here...
// Calling the static method
$city = self::findCity($request);
return view('home', compact('areas', 'countries', 'cities', 'city'));
}
public static function findCity(Request $request)
{
$data = Location::select('city_name', 'city_name')
->where('region_name', $request->region_name)
->groupBy('city_name', 'city_name')->get();
return $data;
}
Read more about static methods in the PHP documentation.
You need to call,pass and return using a function. like this one. I think it can help you.
class HomeController extends Controller
{
public function index(Request $request)
{
$countries = Location::select('country_name')
->groupBy('country_name', 'country_name')
->get();
$cities = $this->findCity($request)
$areas = Area::get()->toTree();
return view('home', compact('areas','countries', 'cities'));
}
public function findCity($request)
{
$data = Location::select('city_name','city_name')
->where('region_name', $request->region_name)
->groupBy('city_name', 'city_name')
->get();
return response()->json($data);
}
}

PhpStorm type recognition/suggestions

Is there anything I can do to get PhpStorm (2016.1) to recognize types outside the "new X()" scope?
SomeClass.php:
class SomeClass
{
public function DoMagic()
{
echo "doing magic";
}
}
DummyClass.php:
class DummyClass
{
protected $mParamsList;
function __construct()
{
$this->mParamsList = array();
}
public function InitParamsList()
{
$this->mParamsList[] = new SomeClass();
}
public function GetParamsList()
{
return $this->mParamsList;
}
}
UserClass.php - no suggestions:
class UserClass
{
public function DoMagic()
{
$dummy2 = new DummyClass();
$params = $dummy2->GetParamsList();
foreach ($params as $param)
{
$param-> * nothing happens *
}
}
}
?>
I found adding this hack works, but it's getting frustrating to employ it:
if (false)
{
$param = new SomeClass();
}
So the full working example would be:
class UserClass
{
public function DoMagic()
{
$dummy = new DummyClass();
$params = $dummy->GetParamsList();
foreach ($params as $param)
{
if (false)
{
$param = new SomeClass();
}
$param-> * suggestions pop up *
}
}
}
You should use doc-type comments before your function:
/**
* #return \MyObject
*/
public function GetMyObject()
{
return new MyObject();
}

Zend Framework: Select query

I'm newbie on Zend Framework, I created DbTable , my primary key is id , and my table name is user:
<?php
class Application_Model_DbTable_User extends Zend_Db_Table_Abstract
{
protected $_name = 'user';
protected $_primary = 'id';
}
after that ,I reviewed Abstract.php(Db/Table/Abstract.php) and I found out that I had to use insert(array $data), so I created a model: (Register.php)
<?php
class Application_Model_Register
{
public function CreateUser($array)
{
$dbTableUser = new Application_Model_DbTable_User();
$dbTableUser -> insert($array);
}
}
and finally , in my Controllers , I created IndexController.php
<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$register = new Application_Model_Register();
$register-> CreateUser(array(
'username'=>'test'));
}
}
It works correctly , but I have no idea about Select, How I select query from user table?
our controller should have to be like below
<?php
class IndexController extends Zend_Controller_Action
{
public function getdataAction()
{
$register = new Application_Model_Dbtable_Register();
$register->getListOfUser();
}
}
Now your model should have to be like this,
<?php
class Application_Model_DbTable_Register extends Zend_Db_Table_Abstract
{
protected $_name = 'user';
protected $_primary = 'id';
public function getListOfUser()
{
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$select = $db->select()
->from($_name,array('*'))
->where(1);
$data = $db->query($select)->fetchAll();
return $data;
}
}