Iam try to run a query in codeigniter model.Its working but when I echo model function query is like below.
SELECT * FROM `table1` WHERE `id` = '17'
SELECT * FROM `table1` WHERE `id` = '20'
SELECT * FROM `table1` WHERE `id` = '21'
SELECT * FROM `table1` WHERE `id` = '22'
SELECT * FROM `table1` WHERE `id` = '23'
My model function is given below
function get_quick_navi_menu($q_code)
{
$this->db->select("*");
$this->db->where('q_id',$q_code);
$this->db->from("table0");
$q = $this->db->get();
//echo $this->db->last_query();
$final = array();
if ($q->num_rows() > 0)
{
foreach ($q->result() as $row) {
$this->db->select("*");
$this->db->from("table1");
$this->db->where("id",$row->id);
$q = $this->db->get();
echo $this->db->last_query();
if ($q->num_rows() > 0) {
$row->children = $q->result();
}
array_push($final, $row);
}
}
I want to run query like below
SELECT * FROM `table1` WHERE `id` = '17,18,19..'
Table Structure
Table0
id q_id value1
1 2 4
2 2 5
3 2 6
Table1
t1_id id value1 value2
1 1 2 2
2 2 5 6
3 3 8 12
View
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1" style="margin-left: 1px; opacity: .9;">
<?php foreach ($menus as $menu) { ?>
<li class="dropdown-submenu"><?php echo $menu->title;?>
<ul class="dropdown-menu"> <?php
if (isset($menu->children)) {
foreach ($menu->children as $child) {?>
<li><?php echo $child->menu_item;?></li> <?php
}
}
?></ul></li><?php } ?>
</ul>
Controller
$menus = $this->Home_model->get_quick_navi_menu($q_code);
$data = array('menus' => $menus);
Required Output
Selecting value1 and value2 from table1 according to id from table0.
How to solve this please help.
Use this function in controller
public function getTableData()
{
$this->db->select('GROUP_CONCAT(id) as id');
$tbl0 = $this->db->get('table0')->row_array();
if($tbl0) {
$ids = explode(',', $tbl0['id']);
$this->db->where_in('id', $ids);
$tbl1 = $this->db->get('table1')->result_array();
echo "<pre>"; print_r($tbl1);
}
}
Hope this will help you;
Get all ids in an array name here ids and use where_in outside the loop
public function get_quick_navi_menu($q_code)
{
$this->db->select("*");
$this->db->where('q_id',$q_code);
$this->db->from("table0");
$q = $this->db->get();
$final = array();
if ($q->num_rows() > 0)
{
foreach ($q->result() as $key => $row)
{
$ids[$key] = $row->id;
$data[$key] = $row;
}
$this->db->select("*");
$this->db->from("table1");
$this->db->where_in("id",$ids);
$q = $this->db->get();
//echo $this->db->last_query();
if ($q->num_rows() > 0)
{
if ( ! empty($data))
{
foreach ($data as $key => $item)
{
$item->children = $q->result();
$final[] = $item;
}
}
}
}
return $final;
/*print_r($final);*/
}
In your controller class:
make sure you have loaded your model and database in controller or in autoload.php
$q_code = 'q_code_value';
$data['menus'] =$this->Home_model->get_quick_navi_menu($q_code);
/* pass the $data in the view like this*/
$this->load->view('your_view_file_path',$data);
In your view :
<div><?php print_r($records);?></div>
For more : https://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-specific-data
Related
i'm try to foreach inside the foreach to get the count data with this code
Controller.php
$jadwals = DB::table('tb_jadwal')->get();
foreach ($jadwals as $key => $jadwal) {
$tgl = date("Y-m-d", strtotime($request->tgl));
$cek_tiket = array();
$cek_tiket = DB::table('tb_tiket')
->select(DB::raw('count( distinct id_tiket) as jumlah_kapasitas'))
->where('Tgl_Berangkat', '=', "'$tgl'")
->where('id_jadwal', '=', $jadwal->id_jadwal)
->get();
$jadwals[$key]->jumlah_kapasitas = $cek_tiket;
}
view.blade.php
#foreach($cek_tiket as $cek_tiket)
<?php
if ($cek_tiket->jumlah_kapasitas <= 70) {
echo "There data";
dd($cek_tiket);
}
?>
#endforeach
but the result of dd is 0
i have data like this
kdae | name
1 | MBC
2 | BBC
3 | CDC
i want to display inside my form_dropdown
<1 ---- MBC>
<2 ---- BBC>
<3 ---- CDC>
this my code in controller
$kdaes = $this->master_order->get_list_kdaes();
$opt = array('' => 'All AE');
foreach ($kdaes as $kdae) {
$opt[$kdae] = $kdae;
}
$data['form_kdae'] = form_dropdown('',$opt,'','id="kdae" name="kdae" class="form-control"');
this is code in my model
public function get_list_kdaes()
{
$this->db->select('kdae','Nama');
$this->db->from($this->table1);
$this->db->order_by('kdae','asc');
$query = $this->db->get();
$result = $query->result();
$kdaes = array();
foreach ($result as $row)
{
$kdaes[] = $row->kdae;
}
return $kdaes;
}
and code for my view
<input name="kodecat" type="text" hidden>
<div class="form-group">
<label for="kdae" class="col-sm-3 control-label">Kode AE</label>
<div class="col-sm-3">
<?php echo $form_kdae; ?>
</div>
</div>
but if i run my form_dropdown just display like this
1
2
3
theres any solution?
syntax is from_dropdown(name,options,selected_option,extra attribute )
so it should be
form_dropdown('kdae',$opt,'','id="kdae" class="form-control"');
You are having an unnecessary foreach loop in your model, just do:
return $query->result();
in your controller you retrieve the data returned from the model:
foreach ($kdaes as $row) {
$opt[$row->kodeae] = $row->kodeae. ' --- '.$row->Nama;
}
hint: pay attention in your select clause, as you have columns with lowercase (kodae) and others with uppercase (Nama), check if this is corect...
Try this code:
public function get_list_kdaes()
{
$this->db->select('kdae','Nama');
$this->db->from($this->table1);
$this->db->order_by('kdae','asc');
$query = $this->db->get();
$result = $query->result_array();
$kdaes = array();
foreach ($result as $row)
{
$kdaes[$row['kdae']] = $row['Nama'];
}
return $kdaes;
}
I have these two functions below
public function get_posts() {
$this->db->select('p.post_id, p.reply_id, p.user_id, u.username');
$this->db->from('post as p');
$this->db->join('user as u', 'u.user_id = p.user_id');
$this->db->where('p.post_id', $this->input->get('post_id'));
$this->db->or_where('p.reply_id', $this->input->get('post_id'));
// $this->db->group_by('p.user_id');
$query = $this->db->get();
return $query->result_array();
}
Results Output
SELECT * FROM `post` WHERE `post_id` = '1' AND `reply_id` = '0' AND `user_id` = '2'
SELECT * FROM `post` WHERE `post_id` = '3' AND `reply_id` = '1' AND `user_id` = '1'
SELECT * FROM `post` WHERE `post_id` = '5' AND `reply_id` = '1' AND `user_id` = '1'
Total Posts Function
public function total_posts_by_user($user_id, $reply_id, $post_id) {
$this->db->from('post');
$this->db->where('post_id', $post_id);
$this->db->where('reply_id', $reply_id);
$this->db->where('user_id', $user_id);
// $this->db->group_by('user_id');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->num_rows();
}
}
The total_posts_by user gets the number of post shown in image below
Question As you can see there are 2 admin row results showing. But I
would like them to be combined so it will just say row for admin and 2
posts.
I have tried using group_by() on get_post but dos not work
Controller
<?php
class Who_replied extends MX_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$data['posts'] = array();
$results = $this->get_posts();
foreach ($results as $result) {
$data['posts'][] = array(
'username' => $result['username'],
'total' => $this->total_posts_by_user($result['user_id'], $result['reply_id'], $result['post_id'])
);
$this->total_posts_by_user($result['user_id'], $result['reply_id'], $result['post_id']);
echo $this->db->last_query() . '</br>';
}
$data['total_posts'] = $this->total_posts();
return $this->load->view('default/template/forum/categories/who_replied_view', $data);
}
}
Use left join in first method because current it is using inner join. After that use group by
public function get_posts() {
$this->db->select('p.post_id, p.reply_id, p.user_id, u.username');
$this->db->from('post as p');
$this->db->join('user as u', 'u.user_id = p.user_id','left');
$this->db->where('p.post_id', $this->input->get('post_id'));
$this->db->or_where('p.reply_id', $this->input->get('post_id'));
$this->db->group_by('p.post_id');
$query = $this->db->get();
return $query->result_array();
}
and try. If still face same issue double check whether there are same TWO username admin
** Have you tried DISCTINCT?
hope it will help
I have found my solution from here using COUNT(p.user_id) AS total
public function get_posts() {
$this->db->select('p.post_id, p.reply_id, p.user_id, u.username COUNT(p.user_id) AS total');
$this->db->from('post as p');
$this->db->join('user as u', 'u.user_id = p.user_id');
$this->db->where('p.post_id', $this->input->get('post_id'));
$this->db->or_where('p.reply_id', $this->input->get('post_id'));
$this->db->group_by('p.user_id');
$query = $this->db->get();
return $query->result_array();
}
I use CI form with form_dropdown helper and tried to pull Mysql data into its options, from the below code, its only retrieve the last record from db into the option list?
please advise what is wrong with my code?
Model
public function getStates() {
$query = $this->db->get('states');
$return = array();
if($query->num_rows() > 0){
$return[''] = 'please select';
foreach($query->result_array() as $row){
$return[$row['state_id']] = $row['state_name'];
}
}
return $return;
}
Controller
$this->load->model('db_model');
$data['options'] = $this->db_model->getStates();
$this->load->view('create_new', $data);
View
$state = array(
'name' => 'state',
'id' => 'state',
//'value' => set_value('state', $state)
);
<?php echo form_label('State', $state['id']); ?>
<?php echo form_dropdown($state['name'], $options); ?>
<?php echo form_error($state['name']); ?>
<?php echo isset($errors[$state['name']])?$errors[$state['name']]:''; ?>
send full query result from the model to the view like this
Model
public function getStates() {
$query = $this->db->get('states');
return $query->result();
}
let the controller as it is and now you can populate states in dropdown like this:
View
<?php
foreach($options as $opt){
$options[$opt->state_id]=$opt->state_name;
}
echo form_dropdown($state['name'], $options);
?>
Try this:
function getStates() {
$return = array();
$query = $this->db->get('states')->result_array();
if( is_array( $query ) && count( $query ) > 0 ){
$return[''] = 'please select';
foreach($query as $row){
$return[$row['state_id']] = $row['state_name'];
}
}
return $return;
}
I want to make a dynamic drop down in CI.I had done it in CI by directly passing the values from model With its design by concatenation the html.. But now i want it without the html i.e. the function just only should return the array ...
my before function was something like this...
function category() {
$sql = $this->db->query("select * from ss_category where parent_id='0'");
$result = $sql->result();
//print_r($result);die();
$output = array();
foreach ($result as $ra) {
$output=$this->get_child($ra->cat_id);
}
//print_r($output);die();
return $output;
}
function get_child($parent_id) {
//echo "select * from ss_category where parent_id=" . $parent_id;
$sql = $this->db->query("select * from ss_category where parent_id=" . $parent_id);
$r = $sql->result();
if (!empty($r)) {
foreach ($r as $s) {
$this->get_child($s->cat_id);
}
//print_r($child);
}
//print_r($r);
}
I want to make drop down something like this..
-parent 1
-child 1
-subchild 1
-child 2
-child 3
-subchild 1
-parent 2
-child 1
My table structure is like this..
+---------------------------------------------------------------+
| cat_id category_name cat_slug parent_id level description |
+---------------------------------------------------------------+
| 1 Elecotronics Electronics 1 description |
+---------------------------------------------------------------+
function parent_sort($categories)
{
foreach($categories as $item)
$childs[$item->parent_id][] = $item;
foreach($categories as $item)
if (isset($childs[$item->category_id]))
$item->childs = $childs[$item->category_id];
return $childs[0];
}