I have a problem in CodeIgniter.
My code:
<?php
$kullanici_yetki_id=$this->session->user_sess['id'];
$result= array(
$this->db->select('*'),
$this->db->from('menuler'),
$this->db->join('menu_yetki', 'menuler.menu_id=menu_yetki.menu_id'),
$this->db->where('menuler.is_active', '1'),
$this->db->where('user_id', $kullanici_yetki_id),
$this->db->where('durum', "1"),
$this->db->limit('1')
);
$sorgu=$this->db->query->$result();
if($sorgu->num_rows()>0){
foreach ($sorgu as $row) {
echo $row->menu_adi;
} } else{
echo "nope";
}
?>
When I run it, it returns this error:
Message: Undefined property: CI_DB_mysqli_driver::$query
Filename: admin/_leftmenu.php Line Number: 34
Backtrace:
File: C:\wamp64\www\bireberberci\application\views\admin_leftmenu.php
Line: 34 Function: _error_handler
File:
C:\wamp64\www\bireberberci\application\controllers\admin\Home.php
Line: 16 Function: view
File: C:\wamp64\www\bireberberci\index.php Line: 315 Function:
require_once
And this:
A PHP Error was encountered Severity: Error
Message: Method name must be a string
Filename: admin/_leftmenu.php
Line Number: 34
You are missing the get() and your session part user_sess['id'] is wrong
https://www.codeigniter.com/user_guide/general/models.html#loading-a-model
<?php
class Example_model extends CI_Model {
public function example() {
$data = array();
$this->db->select('*');
$this->db->from('menuler');
$this->db->join('menu_yetki', 'menuler.menu_id=menu_yetki.menu_id');
$this->db->where('menuler.is_active', '1');
$this->db->where('user_id', $this->session->userdata('id'));
$this->db->where('durum', "1");
$this->db->limit('1');
$sorgu = $this->db->get();
if($sorgu->num_rows() > 0) {
foreach ($sorgu->result() as $row) {
$data[] = $row->menu_adi;
}
}
return $data;
}
}
You are close, but your syntax is a tad off. You can refer to the manual about query builder for more information.
Your biggest mistake was putting all the db calls into an array. This is much closer to what you want.
$kullanici_yetki_id = $this->session->userdata['id'];
$this->db->from('menuler');
$this->db->join('menu_yetki', 'menuler.menu_id=menu_yetki.menu_id');
$this->db->where('menuler.is_active', '1');
$this->db->where('user_id', $kullanici_yetki_id);
$this->db->where('durum', "1");
$this->db->limit('1');
$sorgu = $this->db->get();
if ($sorgu->num_rows() > 0) {
foreach ($sorgu->result() as $row) {
echo $row->menu_adi;
}
} else {
echo "nope";
}
Related
I am trying to get the data in my view in codeigniter but it gives me a Message: Undefined variable: topics_array
here is my model :
public function studentTopics($courseId, $periodId)
{
$this->dbi->select('topicitem.TopicItemID, topicItem.Content');
$this->dbi->from('topicitem, topics, periods');
$this->dbi->where('topics.PeriodItemID', $periodId)
->where('topics.TopicItemID = topicitem.TopicItemID')
->where('topics.PeriodItemID = periods.PeriodItemID')
->where('periods.CourseID', $courseId);
$res = $this->dbi->result_array();
return $res;
}
here is my controller
public function student_topic($courseId, $periodId)
{
$this->load->library('table');
$this->load->model('Student_model');
$data['topics_array'] = $this->Student_model->studentTopics($courseId, $periodId);
$this->load->view('Student/StudentView', $data);
}
and my view
<?php
$courseID = $this->uri->segment(4);
$periodID = 1;
foreach ($topics_array as $key =>$value) {
echo "<tr>
<td>".$value['Content']. "</td>
</tr>"; }?>
You seem to be missing a get().
$res = $this->dbi->get()->result_array();
return $res;
Hope this will help you :
change this line of code : $res = $this->dbi->result_array();
$this->dbi->select('topicitem.TopicItemID, topicItem.Content');
$this->dbi->from('topicitem, topics, periods');
$this->dbi->where('topics.PeriodItemID', $periodId)
->where('topics.TopicItemID = topicitem.TopicItemID')
->where('topics.PeriodItemID = periods.PeriodItemID')
->where('periods.CourseID', $courseId);
$query = $this->dbi->get(); // missing this line of code
$res = $query->result_array();
for more : https://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-specific-data
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/index.php
Line Number: 214
Backtrace:
File: C:\Program Files
(x86)\EasyPHP-DevServer-14.1VC9\data\localweb\masterlinkci2\application\site\views\index.php
Line: 214 Function: _error_handler
File: C:\Program Files
(x86)\EasyPHP-DevServer-14.1VC9\data\localweb\masterlinkci2\application\site\controllers\Cspages.php
Line: 32 Function: view
File: C:\Program Files
(x86)\EasyPHP-DevServer-14.1VC9\data\localweb\masterlinkci2\index.php
Line: 315 Function: require_once
views/index.php
Line 214:
<?php foreach($pic as $pic_item): ?>
<img src="<?php echo base_url('assets1/images/slider/'.$pic->pic_item
);?>">
controllers/Cspages.php
public function index()
{
$this->load->helper('url');
$this->load->model('gallery_model');
// slider
$pic_unique_id = 18;
$data['pic'] = $this->gallery_model->get_picture($pic_unique_id);
$this->load->view('index', $data);
}
models/Gallery_model.php
class Gallery_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_picture($pic_unique_id)
{
$query = $this->db->get_where('galleries_pictures', array('picture_unique_id' => $pic_unique_id));
return $query->result();
}
}
How to fix this error message, any clue? (Line 214)
Return $query->row() instead of $query->result() in model.
public function get_picture($pic_unique_id)
{
$query = $this->db->get_where('galleries_pictures', array('picture_unique_id' => $pic_unique_id));
return $query->row();
}
row() method returns the first matched row.If you use result() you need to use foreach loop in view.
For more see here Codeigniter Results Formats
Instead of this $this->load->database();
$autoload['libraries'] = array('database');
Your missing table name
public function get_picture($pic_unique_id) {
$query = $this->db->get_where('tablename', array('picture_unique_id' => $pic_unique_id));
return $query->result();
}
Or Try
public function get_picture($pic_unique_id) {
$this->db->where('picture_unique_id', $pic_unique_id);
$query = $this->db->get('table_name');
return $query->result();
}
Wrong use
<?php foreach($pic as $pic_item): ?>
<img src="<?php echo base_url('assets1/images/slider/'.$pic->pic_item
);?>">
$pic is array
try
$pic_item->column_from_gallery_model;
This my controller function
public function update_monthly() {
$data = $this->student_model->get_due_info();
foreach ($data as $value) {
$due = $this->student_model->get_due_by_id($value);
$grade = $this->student_model->get_grade_by_id($value);
$grade_due = $this->student_model->get_due_by_grade($grade);
$new_due = $due + $grade_due;
$this->db->set('md_due',$new_due, FALSE);
}
}
I am getting following error
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: database/DB_active_rec.php
Line Number: 427
and
A Database Error Occurred
Error Number: 1054
Unknown column 'Array' in 'where clause'
SELECT `md_due` FROM (`monthly_due`) WHERE `md_student_id` = Array
Filename: F:\xampp\htdocs\student\system\database\DB_driver.php
Line Number: 330
Model Code
public function get_due_by_id($id) {
$this->db->select('md_due');
$this->db->from('monthly_due');
$this->db->where('md_student_id', $id);
$query = $this->db->get();
return $query->result_array();
}
public function get_due_by_grade($id) {
$this->db->select('dt_fee');
$this->db->from('due_table');
$this->db->where('dt_grade', $id);
$query = $this->db->get();
return $query->result_array();
}
public function get_grade_by_id($id) {
$this->db->select('grade');
$this->db->from('student_info');
$this->db->where('student_gen_id', $id);
$query = $this->db->get();
return $query->result_array();
}
public function get_due_info() {
$this->db->select('md_student_id');
$this->db->from('monthly_due');
$query = $this->db->get();
return $query->result_array();
}
Pass $value['md_student_id'] to the model instead of $value in controller.
$data = $this->student_model->get_due_info();
foreach ($data as $value) {
$due = $this->student_model->get_due_by_id($value['md_student_id'] );
$grade = $this->student_model->get_grade_by_id($value['md_student_id']);
$grade_due = $this->student_model->get_due_by_grade($grade[0]['grade']);
//Add remaining logic
}
you are passing whole the array to your model not just the value you want
$due = $this->student_model->get_due_by_id($value);
the right thing is to pass
$value['md_student_id']
this is the value you actually want to pass
replace your code with this
foreach ($data as $value) {
$due = $this->student_model->get_due_by_id($value['md_student_id'] );
$grade = $this->student_model->get_grade_by_id($value['md_student_id']);
$grade_due = $this->student_model->get_due_by_grade($grade[0]['grade']);
$new_due = $due + $grade_due;
$this->db->set('md_due',$new_due, FALSE);
}
I have a question about using form_dropdown().
table:category
cat_id
cat_name
Controller:
function index()
{
$this->load->model('category_model');
$data['category'] = $this->category_model->cat_getallcat();
$this->load->view('category_input',$data);
}
Model:category_model
function cat_getallcat()
{
$this->load->database();
return $this->db->get('category')->result();
}
View:
<?php
$this->load->helper('form');
echo form_open('send');
$list[''] = 'Please select a Category';
foreach($query as $row)
{
$list[$row->cat_id] = ucfirst(htmlspecialchars($row->cat_name));
}
echo form_dropdown('category', $list);
echo form_close();
?>
error obtained:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/category_input.php
Line Number: 28
the ->result() will return the first row only, so this function is what you want to loop over.
Model
function cat_getallcat()
{
$this->load->database();
return $this->db->get('category');
}
View
foreach($query->result() as $row)
{
$list[$row->cat_id] = ucfirst(htmlspecialchars($row->cat_name));
}
EDIT:
Also, you are sending the result as $data['category'] then trying to access it as $query. So really the foreach would be foreach($category->result() as $row) in this example
You are asking foreach to loop over $query, but I you haven't set $query as a variable anywhere that I can see.
Since you set $data['category'] to hold your query result() in your controller, you need to loop over $category in the view:
foreach($category as $row)
{
$list[$row->cat_id] = ucfirst(htmlspecialchars($row->cat_name));
}
I am getting this error "Fatal error: Call to undefined method CI_DB_mysql_driver::findVenueInfo()" when I try to use one of my models.
I have a view with this anchor:
echo anchor('welcome/searchVenue/' . $row->venue_id, $row->venue);
which generates a link like: http://localhost/ci-llmg/index.php/welcome/searchVenue/1
the method called is
function searchVenue()
{
$this->load->model('venues');
//get venue info
$data['info'] = $this->venues->findVenueInfo($this->uri->segment(3)); //this line generates the error
}
and the findVenueInfo function in the model (venues.php) is:
function findVenueInfo($id)
{
$data = array();
$this->db->where('id', $id);
$Q = $this->db->get('venues');
if ($Q->num_rows() > 0)
{
foreach ($Q->result() as $row)
{
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
..but the result of this is Fatal error: Call to undefined method CI_DB_mysql_driver::findVenueInfo()
I'm probably missing something stupid, but can't get it to work! What do you think?
Are you sure your index.php file at the root of ur project have included bootstrap in proper place
Go to the end of the index.php file and include the bootstrap file just before including codeigniter.php.
Your index.php file should have its end lines look like this.
/*
* --------------------------------------------------------------------
* LOAD THE BOOTSTRAP FILE
* --------------------------------------------------------------------
*
* And away we go...
*
*/
require_once APPPATH.'third_party/datamapper/bootstrap.php';
require_once BASEPATH.'core/CodeIgniter.php';
function findVenueInfo($id)
{
$data = array();
$this->db->select()->from('venues')->where('id', $id);<----change it to
$Q = $this->db->get();<----change it to
if ($Q->num_rows() > 0)
{
foreach ($Q->result() as $row)
{
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
I was getting a similar error and found that in 3.0 I needed to enable query_builder in application/config/database.php
$query_builder = TRUE;