Get categorised result taking arguments present inside loop codeigniter - php

I have got two tables 'category' and 'organization' . Under my view i want to list all those organizations which work under respective categories of category table . What i did is shown below :
*Here's my controller : *
function organization()
{
$data['category'] = $this->category_model->get_all_category();
$data['organization'] = $this->organization_model->get_categorised_organization();
$data['title'] = "Welcome to the organization page";
$this->load->view('organization_index',$data);
}
Here's the model :
function get_categorised_organization() function get_categorised_organization() {
$category = $this->category_model->get_all_category();
$i = 0;
foreach ($category as $c):
$sql = "SELECT * FROM ss_organization where org_working_area='$c->category_name'";
$query = $this->db->query($sql);
$result[] = $query->result();
endforeach;
return $result;
} {
$category = $this->category_model->get_all_category();
$i = 0;
foreach ($category as $c):
$sql = "SELECT * FROM ss_organization where org_working_area='$c->category_name'";
$query = $this->db->query($sql);
$result[] = $query->result();
endforeach;
return $result;
}
and here's the view
<?php foreach($category as $c): ?>
<div class="categorybox">
<h2><?php echo $c->category_name;?></h2><hr>
<ul>
<?php //print_r($organization); die();?>
<?php foreach($organization as $o):?>
<?php foreach($o as $p): ?>
<li><?php echo $p->org_name;?></li>
<?php endforeach; ?>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach;?>
Now what i'm getting is same organizations over different categories .. How can i get respective organizations working under the respective category

I make my version of this, and please just adapt to your code
Controller:
function organization()
{
$this->load->model('myModel');
$data['category'] = $this->myModel->getAllCategory();
$data['title'] = "Welcome to the organization page";
$this->load->view('organization_index',$data);
}
Model:
public function getAllCategory()
{
$query = $this->db->get('category');
if($this->db->_error_number() > 0)
{
return false;
}
else
{
return $query->result_array();
}
}
function getOrganizationByCategory($category_id) {
$this->db->where('category_id = ' . (int)$category_id);
$query = $this->db->get('ss_organization');
if($this->db->_error_number() > 0)
{
return false;
}
else
{
return $query->result_array();
}
}
View:
<?php foreach($category as $c): ?>
<div class="categorybox">
<h2><?php echo $c['name'];?></h2><hr>
<ul>
// here you call your function to display organizations by category
<?php $organization = $this->myModel->getOrganizationByCategory($c['id']);?>
<?php foreach($organization as $o):?>
<li><?php echo $o['name'];?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>

Related

How to create pagination with Codeigniter?

I want to create pagination in a search bar in CodeIgniter, but I don't get actual pagination.
Controller code:
public function compatabilty(){
$this->load->model('user_model');
$this->load->model('admin_model');
$data['result'] = $this->user_model->get_compatibility();
$this->load->view('compatabilty',$data);
}
public function compatabilty_search()
{
$submit = $this->input->post('searchcompatible');
if(isset($submit)){
$new = $this->input->post('search_text');
$datas['result'] =$this->admin_model->fetch_data($new);
$this->load->view('compatabilty_search',$datas);
}
}
Model:
function fetch_data($new)
{
$this->db->select("*");
$this->db->from("au_compatible_products");
if($new != '')
{
$this->db->like('Device', $new);
}
$this->db->order_by('id', 'DESC');
$query = $this->db->get();
return $query->result();
}
View:
<div class="search-result" >
<ul><font>Serch Results for</font>
<?php foreach ($result as $key ) { ?>
<li><?php echo $key->Model_Number;?> </li>
<?php } ?>
</ul>
</div>

Codeigniter - controller can't send data to view

I got an error like this :
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: tasks
controller
public function show() {
$data['tasks'] = array();
$data['tasks'] = $this->Tasks_model->show_task()->result();
$this->load->view('pages/all', $data);
}
view
<?php
if( isset($tasks) && ( is_array($tasks) && count($tasks)>0 ) ) {
//echo"<pre>"; print_r($tasks); die();
for($i=0;$i<count($tasks);$i++)
{ ?>
<span><?php echo $task[0]['job']; ?></span><br />
<?php } ?>
<?php }
else { ?>
<span>No tasks records found.</span>
<?php } ?>
Model
public function show_task() {
return $this->db->get('task');
}
what's wrong with my code?
Controller
public function show() {
$this->$data['tasks'] = array();
$this->$data['tasks'] = $this->Tasks_model->show_task();
$this->load->view('pages/all', $data);
}
View
//Here, you can also use for loop to display data.
<?php
if( isset($tasks) && ( is_array($tasks) && count($tasks)>0 ) )
{
//echo"<pre>"; print_r($tasks); die();
for($i=0;$i<count($tasks);$i++)
{ ?>
<span><?php echo $task[0]['job']; ?></span><br />
<?php } ?>
<?php }
else { ?>
<span>No tasks records found.</span>
<?php } ?>
EDIT =>
Model
public function show_task() {
$query = $this->db->get('task');
if( $query->num_rows() > 0 ) //Always check you are getting result or not.
{
return $query->result_array(); //result_array() returns the query result as a pure array
}
else
{
return array();
}
}
Here you are returning return $this->db->get('task'); directly. So you are not getting anythingin View.
First you have to assign $this->db->get('task') to variable e.g. $query and pass result array to controller return $query->result_array();.
Reference => https://www.codeigniter.com/userguide3/database/results.html#result-arrays
Controller:
public function show() {
$data['tasks'] = array();
$data['tasks'] = $this->Tasks_model->show_task();
$this->load->view('pages/all', $data);
}
Model:
public function show_task() {
return $this->db->get('task')->result();
}
View Page:
<?php
if( isset($tasks) && ( is_array($tasks) && count($tasks)>0 ) )
{
//echo"<pre>"; print_r($tasks); die();
for($i=0;$i<count($tasks);$i++)
{ ?>
<span><?php echo $task[0]['job']; ?></span><br />
<?php } ?>
<?php }
else { ?>
<span>No tasks records found.</span>
<?php } ?>

how can I do for my json return properly?

this function is for check modules what the user has permission,but when I used what this function return me like json dont show up all modules. it just show me up the last one.
controller
public function getModules($module_id){
if ($this->session->userdata('log')){
$data = $this->session->userdata('log');
$menu = array();
$seccions = $this->module->get_rows();
foreach ($seccions as $index => $seccion) {
$modules = $this->module->query("SELECT CONCAT('".$seccion['id']."',storelte_modulo.id) AS id,CONCAT('".base_url('assets/img/sidebar')."','/',storelte_modulo.icon) as icon, storelte_modulo.modulo AS value,storelte_modulo.seccion_id,CONCAT('".base_url()."',storelte_modulo.url) AS url FROM storelte_modulo INNER JOIN storelte_modulo_perfil ON storelte_modulo_perfil.modulo_id = storelte_modulo.id WHERE seccion_id = $seccion[id] AND storelte_modulo_perfil.perfiles_id = $data[id] AND storelte_modulo_perfil.STATUS = 1");
//assuming you get 1 row only from the above query
if (!empty($modules)) {
$modules['index'] = $index;
$menu['seccions'] = $modules;
}
}
foreach ($seccions as $item) {
array_push($menu,$item);
}
$this->json($menu);
$this->load->view('modules_view',$menu);
}
}
model
public function get_rows(){
$this->db->select('id,seccion');
$this->db->from('storelte_seccion');
return $this->db->get()->result_array();
}
public function query($query){
return $this->db->query($query)->result_array();
}
view html
<div class="row">
<h3 class="text-center">Welcome to storeLTE, click a module below to get started!</h3>
<div class="home_module_list">
<div class="module_item">
<?php foreach ($seccions as $session) { ?>
<div class="module_item" title="<?= $session['value'];?>">
<img src="<?= $session['icon']; ?>"/>
<?= $session['value']?>
</div>
<?php } ?>
</div>
</div>
</div>
my controller returns me this json
{"seccions":{"0":{"id":"22","icon":"http:\/\/localhost\/storeLTE\/assets\/img\/sidebar\/suppliers.png","value":"Compras","seccion_id":"2","url":"http:\/\/localhost\/storeLTE\/compras\/compras"},"index":1}}
I would like to return it like this one:
[{"id":"1","seccion":"Inventario","data":[{"id":"11","icon":"http:\/\/localhost\/storeLTE\/assets\/img\/sidebar\/items.png","value":"Productos","seccion_id":"1","url":"http:\/\/localhost\/storeLTE\/inventario\/product"},{"id":"14","icon":"http:\/\/localhost\/storeLTE\/assets\/img\/sidebar\/employees.png","value":"Provedores","seccion_id":"1","url":"http:\/\/localhost\/storeLTE\/provedor\/provedor"}]},{"id":"2","seccion":"Compras","data":[{"id":"22","icon":"http:\/\/localhost\/storeLTE\/assets\/img\/sidebar\/suppliers.png","value":"Compras","seccion_id":"2","url":"http:\/\/localhost\/storeLTE\/compras\/compras"}]}]

Can't send correct data with view in codeIgniter

I'm trying to build a easy booking system in codeIgniter and using a database with a table called conference_rooms. I'm calling for this in my Booking_Model.phplike this:
public function __construct()
{
$this->load->database();
}
public function get_room() {
$query = $this->db->get('conference_rooms');
return $query->result_array();
}
}
To display it I'm using my Booking.php class looking like this:
public function view()
{
$data['conference_rooms'] = $this->booking_model->get_room();
if (empty($data['conference_rooms'])) {
show_404();
}
$data['title'] = $data['conference_rooms']['title'];
$this->load->view('templates/header', $data);
$this->load->view('view', $data);
$this->load->view('templates/footer');
}
And my view.php:
<h3><?php echo $conference_rooms['title']; ?></h3>
<div class="main">
<?php echo $conference_rooms['seats']; ?>
</div>
It won't find $room. What am I doing wrong?
UPDATE:
Basically changed the whole code, my view class now looks like this (changed to index in my Booking controller)
public function index() {
$this->load->helper('form');
$this->load->view('bootstrap/header');
$this->load->model('Booking_Model');
$rooms = $this->Booking_Model->get();
$rooms_form_options = array();
foreach ($rooms as $id => $room) {
$rooms_form_options[$id] = $room->title;
}
$this->load->model('Package_Model');
$packages = $this->Package_Model->get();
$packages_form_options = array();
foreach ($packages as $id => $package) {
$packages_form_options[$id] = $package->package_name;
}
$this->load->view('booking', array(
'rooms_form_options' => $rooms_form_options,
'packages_form_options' => $packages_form_options,
));
$this->load->view('bootstrap/footer');
}
And my booking.php;
<div>
<?php echo form_label('Conference Room', 'id') ; ?>
<?php echo form_dropdown('id', $rooms_form_options, set_value('id')); ?>
</div>
<div>
<?php echo form_label('Package type', 'package_id') ; ?>
<?php echo form_dropdown('package_id', $packages_form_options, set_value('package_id')); ?>
</div>
<div>
<?php echo form_label('Antal deltagare', 'number_people') ; ?>
<?php echo form_input('number_people', set_value('number_people')) ; ?>
</div>
<div>
<?php echo form_submit('preview', 'Book'); ?>
</div>
Use this
In Model
<?
function __construct()
{
$this->load->database();
}
function get_room() {
$query = $this->db->get('conference_rooms');
$result = $query->result_array();
return $result;
}
?>
In controller
function view()
{
$data['conference_rooms'] = $this->booking_model->get_room();
if (empty($data['conference_rooms']))
{
show_404();
}
else
{
$this->load->view('templates/header', $data);
$this->load->view('view', $data);
$this->load->view('templates/footer');
}
}
?>
in view
<h3><?php echo $conference_rooms[0]['title']; ?></h3>
<div class="main">
<?php echo $conference_rooms[0]['seats']; ?>
</div>
cz of $conference_rooms[0] pointing 0 is we passing data with Objective array. so we need to point data. check with print_r
MODEL
You need to row_array(); to return an array
public function get_room() {
$query = $this->db->get('conference_rooms');
$rowcount = $query->num_rows();
if( $rowcount > 0 ){
return $row = $query->row_array();
} else {
return FALSE;
}
}
CONTROLLER
public function view()
{
$conference_rooms = $this->booking_model->get_room();// asing your array to variable
if (empty($conference_rooms)) {
show_404();
} else {
$data['conference_rooms']=$conference_rooms;// pass your variable to data array to pass into view
$this->load->view('templates/header', $data);
$this->load->view('view', $data);
$this->load->view('templates/footer');
}
}
VIEW
<h3><?php echo $conference_rooms['title']; ?></h3>
<div class="main">
<?php echo $conference_rooms['seats']; ?>
</div>

i want to use for-each on my code igniter but i am using conditione of (id) and my other conditione

on the view i want to use foreach but with the condition of the id i get from mypost
model
public function get_rows($id= FALSE)
{
if ($id === FALSE)
{
$query = $this->db->get('table1');
return $query->result_array();
$config['per_page'];
$this->uri->segment(3);
}
$query = $this->db->get_where('table1', array('id'=> $id,'type'=> 2));
return $query->row_array();
}
Controller
public function index($id){
$data['rows'] = $this->model->get_rows($id);
if (empty($data['rows']))
{
show_404();
}
$data['id'] = $data['rows']['id'];
$this->load->view('view', $data);
}
View
<?php foreach ($rows as $row): ?>
<li><?php echo $row->title; ?></li>
<?php endforeach; ?>
Here i want to use for each on my view but with the condition of(ID) i get from the post and the type i set my self
thanks in advance...
rafiq__
In view use $row['title']; not $row->title;
View
<?php foreach ($rows as $row): ?>
<li><?php echo $row['title']; ?></li>
<?php endforeach; ?>
and may be you need to change return $query->row_array(); to return $query->result_array(); in your model.
Model
public function get_rows($id = 0){
if ($id > 0){
$this->db->where(array('id'=> $id,'type'=> 2));
}
return $this->db->get('table1')->result_array();
}

Categories