calculate the same amount of data on mysql using codeigniter - php

I want to calculate the same amount of data in one field as in this database
Code Model "M_profile"
class M_profile extends CI_Model
{
public function get_count()
{
$sql = "SELECT count(kd_order) as total FROM tbl_order WHERE kd_pelanggan group by kd_order";
$result = $this->db->query($sql);
return $result->row()->total;
}
}

Using the query builder instead of writing the queries yourself is always a good idea.
From what I see of your table your function should look something like this:
class M_profile extends CI_Model
{
public function get_count()
{
$this->db->select('count(kd_order) as total');
$this->db->group_by('kd_order')
$q = $this->db->get('tbl_order');
return $q->row()->total;
}
}
This way you'll get all the items grouped by order.

Your question isn't clear to me. I am little bit confused. If you want to count based on a condition, you should be put where with a value like this:
class M_profile extends CI_Model
{
public function get_count()
{
$query = $this->db->select('COUNT(kd_order) as total')
->where('kd_pelanggan', $your_value)
->group_by('kd_order')
->get('tbl_order');
return $query->row()->total;
}
}
Or if you want to count without any condition then try this:
class M_profile extends CI_Model
{
public function get_count()
{
$query = $this->db->select('COUNT(kd_order) as total')
->group_by('kd_order')
->get('tbl_order');
return $query->row()->total;
}
}

Related

Get laravel data with relation between tables

I have the following data models:
class Cliente extends Model
{
public function sector()
{
return $this->belongsTo(Sector::class,'sectoresId');
}
}
class Sector extends Model
{
public function sectorLanguage()
{
return $this->hasMany(SectorLanguage::class,'sectoresId');
}
public function cliente()
{
return $this->hasMany(ClienteLanguage::class,'sectoresId');
}
}
class SectorLanguage extends Model
{
public function sector()
{
return $this->belongsTo(Sector::class,'sectoresId');
}
public function idioma()
{
return $this->belongsTo(Idioma::class,'idiomasId');
}
}
I want to recover all the active clients and the name of the sector to which it belongs, if I do something like this
$cliente = Cliente::where('active','1');
When I run $client I can not enter the attribute
foreach($cliente as $cli) {
$cli->sector->sectorLanguage->nombre;
}
Why? It only works for me when I find it by id
$cliente = Cliente::find(1);
echo $cliente->sector->sectorLanguage->nombre;
How can I get what I need without resorting to doing SQL with Query Builder.
Thank you very much, greetings.
According to your defined relations, the Sector has many sectorLanguage, it means you're receiving a collection, so you should work on an object like this:
$cliente->where('active', 1)
->first()
->sector
->sectorLanguage
->first()
->nombre;
Cliente::where('active', 1) and sectorLanguage gives you the collection, so you
should first get the first item from $cliente & sectorLanguage and then apply the
desired relationship
Hope it should work!

Return a single row in model, pass it to controller and view - Codeigniter

Sorry for posting such a noob question, but I've had hard times when working with returning a single
row from database and pass it to model. Here's my method from my model:
public function test($user_id)
{
$query = $this->db->query("SELECT COUNT(*) AS test FROM test WHERE user_id = '.$user_id.'");
return $query->first_row('array');
}
Here's an example of my controller with some other returned value from my model:
class MY_Controller extends CI_Controller
{
public $layout;
public $id;
public $data = array();
public function __construct()
{
parent::__construct();
$this->output->nocache();
$this->load->model('subject_model');
$this->load->model('user_model');
$this->load->model('survey_model');
$this->id = $this->session->userdata('user_id');
$this->data['check_if_already_posted_it_survey'] = $this->survey_model->checkIfAlreadyPostedSurvey('it_survey', $this->id);
$this->data['check_if_already_posted_lvis_survey'] = $this->survey_model->checkIfAlreadyPostedSurvey('lvis_survey', $this->id);
$this->data['test']= $this->survey_model->test($this->id);
$this->layout = 'layout/dashboard';
}
I can pass all the values from that data array to my view except "test". I've basically tried everything.
CheckIfAlreadyPostedSurvey method will return
number of rows with num_rows and I can easily print the value from them in my view by writing:
<?=$check_if_already_posted_it_survey?>
What should I do to print out that "test" in my view?
Thanks in advance and apologizes...
I probably got the question wrong, but have you tried
echo $this->survey_model->test($this->id);
Try this:
public function test($user_id)
{
$query = $this->db->query("SELECT COUNT(*) AS test FROM test WHERE user_id = '".$user_id."'");
return $query->row()->test;
}
This will return a single row as an object and then the referenced row name, which in this case is test.
The following will also work.
public function test($user_id)
{
$query = $this->db->query("SELECT * FROM test WHERE user_id = '".$user_id."'");
return count($query->result());
}
You also messed up the quoting in your SQL statement.

Codeigniter: Simple echo of data from DB

What is the best way to create my model so I can call it from my controller like this?
$this->model_model->function->dbname
I have the following model but its rubbish:
Model:
function systemName()
{
$query = $this->db->query("SELECT cms_name FROM options");
return $query->result();
}
Update:
function systemOptions($options)
{
$this->db->select($options);
$query = $this->db->get('options');
return $query->num_rows();
}
Why would you want to? Why not call it like this?:
$this->model_model->functionname('dbname');
A complete skeleton of a model is like this:
<?php
class Mymodel extends Model
{
function get_some_entries($number_rows)
{
return $result = $this -> db -> query("SELECT id, name
FROM tablename
LIMIT $number_rows");
}
}
?>
Best way? You can probably read on CodeIgniter general guide and alter to your situation. But the basic idea is that, you create the model class then load from your controller. Then just call the model function accordingly. For example,
class Custom_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function systemName()
{
$query = $this->db->query("SELECT cms_name FROM options");
return $query->result();
}
...
...
function systemOptions($options)
{
$this->db->select($options);
$query = $this->db->get('options');
return $query->num_rows();
}
}
<?php
class CustomController extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('custom_model', 'fubar');
}
public function index()
{
$result = $this->fubar->systemName('dbname');
print_r ($result);
}
}
?>

Code Igniter Count Function

What I want to do is I want to count the total records from the table called "songs_tbl" from my database. So I wrote this function in controller.
private function getHeaderInfo()
{
$total_songs = $songs->count('distinct songs_tbl.song_id');
$this->mysmarty->assign('total_songs',$total_songs);
}
I got this error
Fatal error: Call to a member function count() on a non-object in
Any suggestion ? Thank you.
With Regards,
I think you are looking for:
$this->db->count_all('songs_tbl');
or if you want the distinct in there you will need to do something like this:
$this->db->select('song_id');
$this->db->distinct();
$this->db->from('songs_tbl');
$query = $this->db->get();
return $query->num_rows();
As there is/was? an issue with using count_all_results() function and DISTINCT
EDIT
I have never used smarty but based on the code in the question I imagine something like this might work, please correct me if I am wrong:
private function getHeaderInfo()
{
$total_songs = get_all_songs();// This function should be called through a model
$this->mysmarty->assign('total_songs',$total_songs);
}
function get_all_songs(){ //THIS SHOULD BE IN A MODEL
$this->db->select('song_id');
$this->db->distinct();
$this->db->from('songs_tbl');
$query = $this->db->get();
return $query->num_rows();
}
Edit 2
My suggested layout would be something along these lines (UNTESTED) using CodeIgniter WITHOUT smarty:
Model Song.php
class Song extends CI_Model {
//Constructor and other functions
function count_all_songs(){
$this->db->select('song_id');
$this->db->distinct();
$this->db->from('songs_tbl');
$query = $this->db->get();
return $query->num_rows();
}
}
Controller Songs.php
class Song extends CI_Controller {
//Constructor and other functions
function index(){ //This could be any page
$this->load->model('Song'); //Could be in constructor
$total_songs = $this->Song->count_all_songs();
$this->load->view('songs_index.html', array('total_songs' => $total_songs));
}
}
View songs_index.html
<html><head></head><body>
Total Songs: <?php echo $total_songs ?>
</body></html>
You could query the table and request a count from the table itself, like this:
$result = mysql_query(SELECT count(*) FROM songs_tbl);
Try this
echo $this->db->count_all('songs_tbl');
It permits you to determine the number of rows in a particular table.
you can use this
$query = $this->db->get('distinct');
if($query->num_rows())
{
return $query->num_rows();
}else{
return 0;
}

filter some data(row) in zend view

I have
1.Table:user(userId,userName,userGroup)
2.Model:userModel
3.usercontroller
there i a simple code:
Controller:
class UserController extends Zend_Controller_Action
{
public function getuser()
{
$userModel = new userModel();
$this->view->usergroup = $userModel;
}
}
Model:
class Model_UserGroupModel extends Zend_Db_Table_Abstract {
public function getuser(
{
$select = $this->select();
return $this->fetchAll($select);
}
}
view:
please tell me what code I must insert in view to only have user with specific row like user with group teacher also i use partialoop???
foreach ($this->usergroup->toArray() as $userGroup) {
echo $userGroup['row'];
}
should work
To filter rows based on a particular field like userGroup you need to do a query with where condition.
$where = $this->getAdapter->quoteInto("userGroup = ?", 'teacher');
$select = $this->select()->where($where);
So now you can get users for a particular user group.

Categories