I want to get max id value from table in codeigniter - php

in model file i write this code:
public function maxcode()
{
$this->db->select (max(ClassID)+1 as ClassID)-> from ('class');
$query=$this->db->get();
if($query->num_rows>0)
{
$row = $query->row_array();
return $row;
}
echo $row['ClassID'];
}
in controller function i write.....this code
public function maxcode()
{
$this->insert->maxcode();
}

Now I understand what your problem is.
You need to transfer this value to the template. This is done in the page controller
Example
$data['ClassID'] = $this->insert->maxcode();
$this->view('tenplate Name', $data);
And used in template
<?php echo $ClassID; ?>

Related

how to get specific value from db and show result in view codeigniter

i have problem to show my query result in my view, i user codeigniter with mvc structure, in model my code its look like this,
<?php
class Model_Kabalitbang extends CI_Model{
public function getPaguAnggaran(){
$query = "SELECT pagu_anggaran_program_modalutama FROM program_modal_utama WHERE id_program_modalutama = '3'
";
return $this->db->query($query);
}
}
and in my controller i call my model like this
<?php
class Kabalitbang extends CI_Controller{
function __construct(){
parent::__construct();
if($this->session->userdata('logged_in') !== TRUE){
redirect('login');
}
$this->load->model('Model_Kabalitbang');
}
function index(){
//Allowing akses to kabalitbang only
if($this->session->userdata('level')==='2'){
// Jumlah PAGU
$pagu = $this->Model_Kabalitbang->getPaguAnggaran();
$paguanggaran = $pagu->num_rows();
$data = array(
'jml_pagu' => $paguanggaran,
);
$this->load->view('kabalitbang/dashboard_view', $data);
}else{
echo "Access Denied";
}
}
}
but when i call in view <?=$jml_pagu ?> this show just 1, but the value form field in my query is 24392
how to make my code run?
Your query is already return a row containing a number, so you just need to display the row, not counting the row again :
$pagu = $this->Model_Kabalitbang->getPaguAnggaran();
$paguanggaran = $pagu->row_array();
$data = array(
'jml_pagu' => $paguanggaran['pagu_anggaran_program_modalutama'],
);
use this...
$this->db->where('id_program_modalutama ',3);
$result['data']=$this->db->get('Table Name')->result();
$this->load->view('kabalitbang/dashboard_view', $result);
now use loop on $data in your view...
Example :-
foreach($data as $allData)
{
print_r($allData);
}

Select and display all rows in Codeigniter

I'm novice in Codeigniter framework and I want to ask if is good or it exist another method to display all rows from database in view page.
I have this controller:
class Dashboard extends CI_Controller{
public function index()
{
$data['user'] = $this->dashboard_model->get_user_details($this->session->userdata('logged_in'));
$this->load->view('includes/header', $data);
Model dashboard:
class Dashboard_model extends CI_Model{
public function get_user_details($user_id){
$this->db->select("*");
$this->db->where('id', $user_id);
$result = $this->db->get('users');
return $result->result_array ();
}
}
And I display in view page like:
<?php echo $user[0]['id']; ?>
<?php echo $user[0]['username']; ?>
Code is working, it show me what I want but I don't know if this is a good solution.
You can also use return $result->row_array (); if it returns only single row.
and then in your view file get the data by using : $user['id'];
for multiple rows :
your solution is fine but you need to add foreach loop and get the data
ex:
foreach($user as $usr) {
echo $usr['id']; echo "<br/>";
echo $usr['username'];
}
By applying $this->db->where('id', $user_id); you will only get results (1) for the user with $user_id (not all users) and (2) you will only get results if a user with that id exists in the database. The correct way to get all users, while slightly modifying your function to support returning only one user is as follows:
/**
* #param int $user_id When NULL will return all users
* #return array
*/
public function get_user_details($user_id = null) {
$this->db->from('users');
if (!is_null($user_id)) {
$this->db->where('id', $user_id);
$q = $this->db->get();
if ($q->num_rows() !== 1) {
return array();
}
return $q->row_array();
} else {
return $this->db->get()->result_array();
}
}
So to get all users: $this->dashboard_model->get_user_details()
To get logged in user: $this->dashboard_model->get_user_details($this->session->userdata('logged_in'))
To get a user with id 123: $this->dashboard_model->get_user_details('123')
When $user_id is blank you can go through results like:
if (count($users) !== 0) {
foreach ($users as $user) {
echo $user['id'];
echo $user['username'];
}
} else {
echo 'No users';
}
When $user_id is set you get a single result thus this will work:
if (count($users) !== 0) {
echo $users['id'];
echo $users['username'];
} else {
echo 'User with that id does not exist!';
}
For single user information I would use row_array() or row()
https://www.codeigniter.com/userguide3/database/queries.html
<?php
class Dashboard_model extends CI_Model{
public function get_user_details($user_id){
$this->db->where('id', $user_id);
$result = $this->db->get('users');
return $result->row_array();
}
}
Controller
<?php
class Dashboard extends CI_Controller {
public function index()
{
$this->load->model('dashboard_model');
// Use the users id stored in your session when logged in
$userinfo = $this->dashboard_model->get_user_details($this->session->userdata('id'));
$data['id'] = $userinfo['id'];
$data['username'] = $userinfo['username'];
$this->load->view('includes/header', $data);
}
}
Then echo on view
<?php echo $username;?> example <?php echo $id;?>
Updated answer : As per details provided in commnets
instead of return $result->result_array(); use return $result->row_array();
View :
<?php echo $user['id']; ?>
<?php echo $user['username']; ?>
For fetching multiple rows :
return $result->result_array(); will give you array of all the users present in users table.
What you need to do is, access those users in view page using foreach loop.
<?php
foreach ($user as $row) {
echo $row['id'];
echo $row['username'];
}
?>
In your MODEL
class Dashboard_model extends CI_Model{
public function get_user_details($user_id){
return this->db->get_where('users', array('id' => $user_id))->result_array(); //You can use result_array() for more than one row and row_array() only for one row
//If you want to show all rows form database use this return $this->db->get('users')->result(); OR result_array()
}
}
In your View
For more than one row use foreach loop
foreach($user as $usr){
echo $usr['']; //Add all your database data here like this
}

Search through name instead of id in code igniter..?

I am a new to php, have this code in controller:
public function selectquery($id){
$this->load->database();
$this->load->model('aboutmodel');
$data['h'] = $this->aboutmodel->selectquery($id);
$this->load->view('aboutpageview', $data);
}
and this in model:
public function selectquery($id){
$query =$this->db->get_where('phone_mobileinfos',array('id'=>$id));
// return $query->row_array();
return $query;
}
this in view:
{
<p>Mobile_name:<?php echo $h['mobile_name'];?></p>
<p>Price:<?php echo $h['price'];?></p>
}
Just want to search by name instead of id any suggestions..?
In model:
public function selectByName($name){
$query =$this->db->get_where('phone_mobileinfos',array('name'=>$name));
return $query->result_array();
}
use in controller:
$data['by_name'] = $this->aboutmodel->selectquery($id);
in view:
<p>Mobile_name:<?php echo $by_name['mobile_name'];?></p>
<p>Price:<?php echo $by_name['price'];?></p>
So, If I get you correctly and Adding to j.Litvak's code, you want to search the db for a name rather than an id? If so you would need to pass the controller method the name variable i.e. http://localhost/controller_name/method/search_parameter, in your case localhost/controller/selectquery/samsung
Your controller will be
public function selectquery($id){
$this->load->database();
$this->load->model('aboutmodel');
$data['h'] = $this->aboutmodel->selectByName($id);
$this->load->view('aboutpageview', $data);
}
And Model
public function selectByName($name){
$query =$this->db->get_where('phone_mobileinfos',array('name'=>$name));
return $query->result_array();
}
And View
foreach($h as $key => $value){ The code here is obviously in php tags but I cant show them
Mobile: echo $value['mobile_name'];
Price: echo $value['price'];
}
So you create a loop through the result, iterating each result found.
CAVEAT: you will need to test for a NULL result or you will get an error and be careful passing strings through the URL, codeigniter has sql injection protection but I always like to be safe

codeigniter how to send query->num_rows from model to controller to view

I'm new in codeigniter, I'm having a challenge with num_rows on view page
Here's the sample of the code - Updated
Model
public function __construct()
{
parent::__construct();
}
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Referrals_form_model', 'referral'); /* LOADING MODEL * Referral_form_model as referral */
}
public function index()
{
$this->load->helper('url');
$this->load->view('referrals_form_view');
}
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
}
View
<span><?php
echo $query->num_rows();
?></span>
when i run the code, it tell me undefined variable "query"
Please help.
Thank you
Please change your code as below.
MODEL:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
View :
<span>
<?php
echo $total_referred_in;
?>
</span>
As you can see in Controller line :
$this->load->view("referral_form_view", $data);
$data here is the data array you are passing to your view. In your view you can access data in $data variable by using $data variable's KEY as php variable(In our case its "total_referred_in").
You aren't returning anything from your function, you're just echoing. Try this:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller:
public function referral_in()
{
$data['num_rows'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
And then in your view:
<span><?=$num_rows?></span>

Getting Error : Trying to get property of non object

I am a beginner in the CodeIgniter. Working on one small basic project, while I am working on the List view I get an error " Trying to get property of non-object.
Please help me!
Here the screen shots.
Error
My code
Here is my view :
<ul id="actions">
<h4>List Actions</h4>
<li> Add Task</li>
<li> Edit List</li>
<li><a onclick="return confirm('Are you sure?')" href="<?php echo base_url();?>lists/delete/<?php echo $list_detail->id;?>"> Delete List</a></li></ul>
<h1><?php echo $list_detail->list_name; ?></h1>
Created on : <strong><?php echo date("n-j-Y",strtotime($list_detail->create_date)); ?></strong>
<br/><br/>
<div style="max-width:500px;"><?php echo $list_detail->list_body; ?></div>
<!-- <?php //echo print_r($query); ?>
-->
<br/><br/>
Here is Controller
public function show($id){
$data['list_detail'] = $this->List_model->get_lists($id);
$data['main_content']='lists/show';
$this->load->view('layout/main',$data);
}
Here is model
public function get_list($id){
$query=$this->db->get('lists');
$this->db->where('id', $id);
return $query->row();
}
This error is generating because you are trying to access an array in class object style.
Ex:
$data['list_details'] = 'some value';
and accessing it like:
$data->list_details; // it will generate the error "Trying to get property of non object"
After viewing your code. I think you have written wrong in your model function
Your model function must be like below
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row();
}
Also in your view before you print the value please check it with the condition of !empty($list_details). so if value is not there still it will not throw any error.
I hope this will help you.
From your controller you are calling the model function as get_lists().
But there is no function as get_lists in model. U need to change the function name in controller to get_list()
When your using $query->row() on your model function then on controller
Model Function
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row();
}
Controller
public function show($id){
// Gets data where it belongs with that id
$list_data = $this->model_name->get_list($id);
$data['id'] = $list_data->id;
$data['create_date'] = $list_data->create_date;
$data['list_body'] = $list_data->list_body;
$this->load->view('someview', $data);
}
When your using $query->row_array(); on your model function then on controller
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row_array();
}
Controller
public function show($id) {
// Gets data where it belongs with that id
$list_data = $this->model_name->get_list($id);
$data['id'] = $list_data['id'];
$data['create_date'] = $list_data['create_date'];
$data['list_body'] = $list_data['list_body'];
$this->load->view('someview', $data);
}
On view then you can access
<?php echo $id;?>
<?php echo $create_date;?>
<?php echo $list_body;?>
Update for multiple data
public function get_list() {
$query = $this->db->get('lists');
return $query->result();
}
Controller
$results = $this->model_name->get_list();
// Or
//$results[] = $this->model_name->get_list();
$data['list_detail'] = $results;
$this->load->view('someview', $data);
View
<?php foreach ($list_detail as $list) {?>
<?php echo $list->id;?>
<?php echo $list->create_date;?>
<?php echo $list->list_body;?>
<?php }?>

Categories