my query in codeigniter cannot produce result - php

i've been debbuging it many times based on my knowledge, i am new to codeigniter, any solution idea would be much appreciated... ty
here's my code
MODEL:
function userdisplay_info($id){
$result = $this->db->query("SELECT uid FROM user WHERE userIdentificationNumber LIKE '$id'");
//if($result->num_rows() > 0){
$info_data = $result->result();
$row = $info_data[0];
$result->free_result();
}
CONTROLLER:
function r_book(){
$data = array();
$bid = $this->input->post('bid');
$u_id = $this->input->post('userinputid');
$data = array(
'book_reserved' => 1
);
if($this->module2->reserved_book($bid,$data))
{
$data['info'] = $this->module2->userdisplay_info($u_id);
$this->load->view('User/template/header');
$this->load->view('User/template/navigator');
$this->load->view('User/landingpage',$data);
$this->load->view('User/template/footer');
return true;
}else
{
$this->load->view('User/template/header');
$this->load->view('User/template/navigator');
$this->load->view('User/landingpagefail');
$this->load->view('User/template/footer');
return false;
}
}
VIEW:
<div class="col-md-3">
<div class="panel panel-default">
<div class="panel-body">
<?php if($info) : foreach($info as $u_info) : ?>
<ul class="list-unstyled">
<li>Last Name : <?php echo $u_info->uid; ?></li>
<li>First Name : <?php echo $u_info->userFirstname; ?></li></li>
<li>Middle Name : <?php echo $u_info->userMiddlename; ?></li></li>
<li>Course : <?php echo $u_info->userCourse; ?></li></li>
</ul>
<?php endforeach; ?>
<?php else :?>
<h4> No Record Found</h4>
<?php endif; ?>
</div>
</div>
</div>

Rewrite your model function as
function userdisplay_info($id){
$this->db->select('uid');
$this->db->like('userIdentificationNumber', $id);
$query = $this->db->get('user');
return $query->result_array();
}

Related

Codeigniter foreach whithout $id

how i can sort some informations without $id? My problem is, that i ca read all teams and users. but how i can sort this?
So that i get a team and the players from this team in one box. WIth my code i have all users in all boxes.
Can i foreach in a foreach?
view what i try:
<?php foreach ($teams as $item): ?>
<div class="col-xs-12">
<?php echo $item->teamname ?>
</div>
<?php foreach ($users as $item): ?>
<div class="col-xs-12">
<?php echo $item->username ?>
</div>
<?php endforeach ?>
<?php endforeach ?>
This is my database
table users
user_id username team_id
1 paul 1
2 tom 2
3 brad 1
4 pim 2
table team
team_id teamname
1 team1
2 team1
Now i have a subside like domaincom/teams
And i want on this side
team 1 : paul, brad
team 2 : tom, pim
controller:
<?php
class Teams extends CI_Controller {
public $layout = 'full';
public $module = 'teams';
public $model = 'Teams_model';
public function __construct() {
parent::__construct();
$this->load->model($this->model);
$this->_primary_key = $this->{$this->model}->_primary_keys[0];
}
public function index()
{
$data['teams'] = $this->{$this->model}->get_teams();
$data['users'] = $this->{$this->model}->get_users();
$data['teamsWithUsers'] = $this->{$this->model}->get_users_by_team();
$data['items'] = $this->{$this->model}->get();
$this->load->view($this->module, $data);
}
}
this is my model
<?php
class Teams_model extends CI_model
{
public $_table = 'teams';
public $_primary_keys = array('teams_id');
function get_teams()
{
return $this->db->get('teams')->result();
}
function get_users()
{
return $this->db->get('users')->result();
}
function get_users_by_team($teamID = null)
{
$this->db->select('u.user_id, u.username, u.team_id, t.teamname');
$this->db->from('users AS u');
$this->db->join('teams AS t', 't.team_id = u.team_id');
if ($teamID != '') {
$this->db->where('t.team_id', $teamID);
}
$this->db->order_by('t.teamname', 'ASC');
$this->db->order_by('u.username', 'ASC');
$query = $this->db->get();
return $query->result();
}
}
What i looking for is this view sample
what about that?
your controller
public function index()
{
$data['teams'] = $this->{$this->model}->get_teams();
$data['users'] = $this->{$this->model}->get_users();
$arrUsersPerTeam = [];
foreach($data['users'] AS $objUser)
{
$arrUsersPerTeam[$objUser->team_id][] = $objUser;
}
$data['arrUsersPerTeam'] = $arrUsersPerTeam;
$data['items'] = $this->{$this->model}->get();
$this->load->view($this->module, $data);
}
and your view
<?php
foreach ($teams as $item):
?>
<div class="col-xs-12">
<?php echo $item->teamname ?>
</div>
<?php
if (isset($arrUsersPerTeam[$item->team_id])) :
foreach ($arrUsersPerTeam[$item->team_id] as $objUser):
?>
<div class="col-xs-12">
<?php echo $objUser->username ?>
</div>
<?php
endforeach;
endif;
endforeach;
You should use a single join query to get your results make sure results are ordered by team
/**model*/
function some func(){
$this->db->select( 'u.username, t.team_id, t.teamname' );
$this->db->from( 'team AS t' );
$this->db->join( 'users AS u', 't.team_id = u.team_id' );
$this->db->order_by( 't.team_id', 'ASC' );
$query = $this->db->get();
return $query->result();
}
In your controller collect results returned by above method defined in your model and pass it to view.
In view there is no need for nested loop only single loop will do the job here like $results contains all the records then in view you can display your records as
/* view */
$parent = false;
$index = 1;
<div class="col-xs-12">
<?php foreach ($results as $result){ ?>
<?php if($parent !=$item->teamname ){ ?>
<?php if($index !=1 ){ ?>
</div>
<?php } ?>
<div class="col-xs-12">
<h2><?php echo $result->teamname ?></h2>
<?php } ?>
<p><?php echo $result->username ?></p>
<?php
$parent =$result->teamname;
$index++;
} ?>
</div> // To close the opened div
2 things: you can pick out key from the foreach, that you can use later, second the "$item" is used 2 places where you might overwrite it by accident.
<?php foreach ($teams as $key => $team_item ): ?>
<div class="col-xs-12">
<?php echo $team_item->teamname ?>
</div>
<?php foreach ($users as $user_item): ?>
<?php if($item->team_id == $key): ?>
<div class="col-xs-12">
<?php echo $user_item->username ?>
</div>
<?php endif ?>
<?php endforeach ?>
<?php endforeach ?>
I'm not really sure if thats what you are trying to achieve

Subcategory inside category in the Codeigniter framework

I want to show subcategory inside the main category. My table looks like this. but when i print_r($cat_id) it returns only the last id of the category. How can I fix this? Here is my controller:
$data['categories'] = $this->courses_model->get_all_categories_for_courses();
foreach($data['categories'] as $ct){
$cat_id = $ct['id'];
}
$data['subcategories'] = $this->courses_model->get_all_subcategories_for_courses($cat_id);
$this->load->view('templates/header');
$this->load->view('courses/courses-grid-fullwidth', $data);
$this->load->view('templates/footer');
}
and here is my model:
public function get_all_categories_for_courses() {
$query = $this->db->get_where('categories', array('parent_id' => NULL));
return $query->result_array();
}
function get_all_subcategories_for_courses($cat_id) {
$query = $this->db->get_where('categories', array('parent_id' => $cat_id));
return $query->result_array();
}
and here is my view page:
<?php foreach($categories as $cat): ?>
<div class="col-md-3 col-sm-6 incat">
<h4>
<a href="#" class="catname"><i class="fa <?= $cat['icon']; ?>" aria-hidden="true"></i>
<?= $cat['name_rus']; ?></a>
</h4>
<ul>
<?php foreach($subcategories as $subcat): ?>
<li><?= $subcat['name_rus']; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
Thanks in advance!
Or you can do this
<?php foreach($categories as $cat): ?>
<div class="col-md-3 col-sm-6 incat">
<h4>
<a href="<?= site_url('/courses/category/'.$cat['category_slug']); ?>" class="catname"><i class="fa <?= $cat['icon']; ?>" aria-hidden="true"></i>
<?= $cat['name_rus']; ?></a>
</h4>
<ul>
<?php
$query = $this->db->get_where('categories', array('parent_id' => $cat['id']));
?>
<?php foreach($query->result() as $subcat): ?>
<li><?= $subcat->name_rus; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
As Ukasyah said in a comment on your question, you're overwriting the $cat_id value each time you loop through your categories.
What you will need to do is get an array populated with the IDs and use $this->db->where_in() to match an array rather than a single value.
Your controller will need to look more like this:
$data['categories'] = $this->courses_model->get_all_categories_for_courses();
$cat_ids = [];
foreach($data['categories'] as $ct){
$cat_ids[] = $ct['id'];
}
$data['subcategories'] = $this->courses_model->get_all_subcategories_for_courses($cat_ids);
$this->load->view('templates/header');
$this->load->view('courses/courses-grid-fullwidth', $data);
$this->load->view('templates/footer');
While you will need to change your model function to look like this:
function get_all_subcategories_for_courses($cat_ids) {
$query = $this->db->where_in('parent_id', $cat_ids)->get('categories');
return $query->result_array();
}
Best of luck.

Warning: Invalid argument supplied for foreach()

I have an error in the product detail, I do not know what else to change where again. when I change $detail into $categories succeed but is the same as function submenu. It was not according to my wishes
My View
<!-- navbar -->
<div class="row">
<?php foreach ($categories as $row) : ?>
<div class="col-sm-3">
<h5><?php echo $row->categoriesName; ?></h5>
<ul><li><a href="<?php echo base_url();?>member/detail">
<?php echo $row->productName; ?></a> </li></ul>
</div>
<?php endforeach; ?>
</div>
<!-- product detail -->
<?php foreach ($detail as $details_new) : ?>
<div class="box">
<h1 class="text-center"><?php echo $details_new->productName;?></h1>
<p class="price"><?php echo $details_new->price;?></p>
</div>
<?php endforeach; ?>
My Controller
public function home() {
$data=array('title'=>'Pasar Online | Ayo Belanja Sekarang',
'username'=> $this->session->userdata('username'),
'product' => $this->product_model->all(),
'categories' => $this->categories_model->get_main(),
'isi' =>'member/index');
$this->load->view('template_home/wrapper',$data);
}
public function detail() {
$data['username'] = $this->session->userdata('username');
$data['categories'] = $this->categories_model->get_main();
$data['detail'] = $this->categories_model->get_details();
$this->load->view('member/coba',$data);
}
My Model
public function get_main(){
$this->db->select('product.*,categories.*');
$this->db->from('product');
$this->db->join('categories','product.categoriesID=categories.categoriesID');
$this->db->limit(5);
$query = $this->db->get();
if($query->num_rows() > 0) {
$results = $query->result();
} return $results;
}
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
}
still can not display a single product
I do not know where my mistake... please help me, thanks
you are iterating details array in wrong format.
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
return $row;
}
This will return only single row without any indexing like - 0,1,2...
and on view page you are using foreach loop and foreach loop use indexing to iterate value you can iterate like that --
<div class="box">
<h1 class="text-center"><?php echo $detail->productName; ?></h1>
<p class="price"><?php echo $detail->price; ?></p>
</div>
please use this way hope it will work..
In codeigniter row() selects only first row of your result in object format.So no need to use foreach loop.Just try like this...
<div class="box">
<h1 class="text-center"><?php echo $detail->productName;?></h1>
<p class="price"><?php echo $detail->price;?></p>
</div>
And
In model
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
return $row;
}
it is because of $detail variable is empty, no data available in this variable, always check for data in variable before looping
<?php if(!empty($detail)){
foreach ($detail as $details_new) : ?>
<div class="box">
<h1 class="text-center"><?php echo $details_new->productName;?></h1>
<p class="price"><?php echo $details_new->price;?></p>
</div>
<?php endforeach; } ?>

How to print each row value one by one in php codeigniter

I tried to get the all the rows of a database and it should loop itself for expected output. The image of the output is [enter image description here][1]
And the code is as follows:
In model:
function get_menugroup()
{
$data=array();
$sql = $this->db->query("SELECT MenuName FROM MenuGroup;");
$menu_res = $sql->result();
if($sql -> num_rows() > 0 )
{
return $menu_res;
}
else{
echo "Nothing to process";
}
}`
In controller
public function menugroupname()
{
$menugroups=$this->UserRight_model->get_menugroup();
if ($menugroups){
$data['menu'] = $menugroups;
//$data['count'] = count($data['menu']);
$this->load->view('UserRight_view',$data);
}
}
In view:
<ul class="collapsible" data-collapsible="accordion">
<li>
<?php
if(is_array($menu)){
foreach($menu as $menus){
$menulist = $menus ->MenuName;
//$menulist = $menus['MenuName'];
$fun = explode(",",$menus ->MenuName);
}
$no_menu = count($menu);
//echo $no_menu;
for($i=0; $i<$no_menu ; $i++){
?>
<div class="collapsible-header"><i class="material-icons">place</i><?php echo $menulist ?></div>
<div class="collapsible-body"><p>Submenu.</p></div>
</li>
<?php }} else { echo "Wrong way";} ?>
</ul>
And thanks in advance
Try this.
<ul class="collapsible" data-collapsible="accordion">
<li>
<?php
if(($menu)){
foreach($menu[0] as $menus){
$menulist = $menus->MenuName;
$fun = explode(",",$menus ->MenuName); }
$no_menu = count($menu);
for($i=0; $i<$no_menu ; $i++){
?>
<div class="collapsible-header"><i class="material-icons">place</i><?php echo $menulist ?></div>
<div class="collapsible-body"><p>Submenu.</p></div>
</li>
<?php }} else { echo "Wrong way";} ?>
</ul>
Hope this helps you!

How to get the options?

I'm trying to do a practice session. In the view page each question and the corresponding options has to be displayed.I am able to get all the questions but not the options. I think I need to pass each question id for displaying the corresponding options. But I'm not able to get that. Somebody plz help to get the options according to the question.
Controller code:
function index() {
$data['qn'] = $this->questions_model->questions_list();
foreach($data['qn'] as $row){
$qs_id = $row->qid;
$data['qn']['$qs_id']= $this->questions_model->option_list($qs_id);
$data['qq_id'] = $data['qn']['$qs_id'];
//print_r($data['qq_id']);
}
$this->load->view($this->session->userdata('web_view') . '/header', $data);
$this->load->view($this->session->userdata('web_view') . '/questions_list', $data);
$this->load->view($this->session->userdata('web_view') . '/footer', $data);
}
Model code:
function questions_list() {
$this->db->select('qbank.*,question_category.category_name');
$this->db->from('qbank');
$this->db->join('question_category','qbank.cid = question_category.cid');
$query = $this->db->get();
if ($query->num_rows() >= 1) {
return $query->result();
} else {
return false;
}
}
function option_list($qs_id){
$this->db->select('option_value');
$this->db->from('q_options');
$this->db->where('qid',$qs_id);
$query = $this->db->get();
if ($query->num_rows() >= 1) {
return $query->result();
} else {
return false;
}
}
view code:
<form id="profilefrm" class="sky-form" name="profilefrm" method="post">
<ol>
<?php foreach($qn as $row){ ?>
<li>
<?php echo $row->question; ?>
<ol type="A">
<?php foreach($qq_id as $rows){ ?>
<li><?php echo $rows->option_value; ?></li>
<?php } ?>
</ol>
</li>
<?php } ?>
</ol>
</form>
controller
'foreach($data['qn'] as $row){
$qs_id = $row->qid;
$data['qn']['$qs_id']= $this->questions_model->option_list($qs_id);'
model
'function option_list($qs_id){
$this->db->select('option_value');
$this->db->from('q_options');
$this->db->where('q_id','$qs_id');
$this->db->limit(4);
$query = $this->db->get();
if ($query->num_rows() >= 1) {
return $query->result();
} else {
return false;
} '
view
<li>
<?php echo $row->question; ?>
<ol type="A">
<?php
$qs_id = $row->qid;
foreach($row['$qs_id'] as $rows){ ?>
<li><?php echo $rows->option_value; ?></li>
<?php } ?>
</ol>
</li>'

Categories