Retrieve data from two tables in PHP Codeigniter - php

I want to retrive count of datas from one table and some datas from another table , also i want to know how to display these in a single foreach loop.
Model
public function counter(){
$this->db->where('package_id', $id);
$this->db->from('gt_package');
$cnt = $this->db->count_all_results();
}
Controller
public function package_category_listing()
{
$data['records'] = $this->gt_package_model->counter();
$query = $this->db->get("gt_package_category");
$data['records'] = $query->result();
$this->load->view('admin/package/package_category_listing',$data );
}
View part :
<?php foreach ($records as $r) { ?>
<div class="col-sm-6 col-md-3 col-xs-12">
<div class="bg-purple">
<h4 class="text-white">
<?php echo $r->category_name; ?>
</h4>
<span>200</span> Packages
<br>
<a style="margin-top: 10px;" href="<?php echo site_url() . "/gt_package/listing/" . $r->category_id ?>" class="btn btn-default">View Packages</a>
</div>
</div>
<?php } ?>

As per your question to pass the data model to controller code bellow
Model:
public function counter()
{
$this->db->where('package_id', $id);
$this->db->from('gt_package');
$query = $this->db->get('gt_package');
$cnt = $query->num_rows();
return $cnt;
}
Controller:
public function package_category_listing()
{
$data['records'] = $this->gt_package_model->counter();
$query = $this->db->get("gt_package_category");
$data['records'] = $query->result();
$this->load->view('admin/package/package_category_listing',$data );
if($res){
$data['result'] = $res;
$this->load->view('manage_accounts_view', $data);
} else {
echo "Fail";
}
print_r($data['result']);
}

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

Displaying the blog pagetitle as a url in codeigniter php

I am having a blog page where the blogs are inserted from admin panel and blog title will be inserted into new column as separated by " - " if there are spaces between the page titles.For example if the page title is " welcome to something " then in the database it is will be inserted into two columns. In once column it will be inserted as same and in other column it will be inserted as welcome-to-something.
when clicking on readmore button i need to display in url as (www.example.com/blob/article/welcome-to-something) in this format i need to display the url.
Here is the code:
Controller:
public function index()
{
$this->load->model('blogs_model');
$data["records2"] = $this->blogs_model->get_all_blogs($config["per_page"], $page);
$data['mainpage'] = "blog";
$this->load->view('templates/template',$data);
}
public function article()
{
$this->load->model('blogs_model');
$data['records2']= $this->blogs_model->getblogsdata($this->uri->segment(3));
$data['mainpage']='blogs';
$this->load->view('templates/templatess',$data);
}
Model:
function get_all_blogs()
{
$this->db->select('B.*');
$this->db->from('blogs AS B');
$this->db->where(array('B.status'=>1));
$this->db->order_by("position", "asc");
$q = $this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
function getblogsdata($id)
{
$this->db->select('blogs.*');
$this->db->from('blogs');
$this->db->where(array('blogs.blog_id'=>$id));
$q=$this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
View:
<div class="col-md-9 blogs">
<?php if(isset($records2) && is_array($records2)):?>
<?php foreach ($records2 as $r):?>
<div class="blog1">
<img src="<?php echo base_url();?>admin/images/blogimages/thumbs/<?php echo $r->image_path;?>" class="testimonials1"/>
<h3 class="heading1"><?php echo $r->blog_title;?></h3>
<div class="blogtext1 read">
<?php echo $r->description;?>
</div>
<a href="<?php echo base_url()?>blog/article/<?php echo $r ->blog_id ;?>" class="read_more7" target="_blank" >Read More</a>
</div>
<?php endforeach ;endif;?>
<div class="pagination"><?php echo $links; ?></div>
</div>
blogs table
blog_id | blog_title | blogtitle
1 welcome to something welcome-to-something
Model:
function getblogsdata($id,$slug)
{
$this->db->select('blogs.*');
$this->db->from('blogs');
$this->db->where(array('blogs.blogtitle'=>$id));
$this->db->where(array('blogs.blogtitle' => $slug));
$this->db->order_by("ne_views", "asc");
$q=$this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
View:
<a href="<?php echo base_url()?>blog/article/<?php echo $r->blogtitle;?>" class="read_more7" target="_blank" >Read More</a>

I need to fetch data from database and display in my view using php codeigniter

I am fetching the data from the database.But I am getting error.Here I need to display product name and image in the page.But I am getting the error undefined variable 'name' in the page. Please check my code give your valuable suggestions,Thank you.
My controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class productdisplay_ctrl extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->model("productdisplay_model");
}
public function productfetch(){
$this->load->model("productdisplay_model");
$result = $this->productdisplay_model->fetchData();
$data['name'] = $result->sub3_category_name;
$this->load->view('home', $data);
}
}
?>
my model
<?php
class productdisplay_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function fetchData() {
$this->db->where($where);
$this->db->order_by('rand()');
$this->db->select('*');
$this->db->from('sub3_category');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
?>
my view
<div class="container-main">
<div class="col-xs-12 col-sm-4 col-md-3">
<div class="prod-container">
<a href="<?php echo base_url(); ?>index.php/welcome/productlist">
<div class="prod_img">
<img src="<?php echo base_url();?>images/img-electronics.jpg" width="100%"/>
</div>
<div class="prod_desc">
<div class="prod-round-icon"></div>
<h4 class="prod_title"><?php echo $name; ?></h4>
<p class="prod_text">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<div class="view-more"> view more</div>
</a>
</div>
</div>
Controller
defined('BASEPATH') OR exit('No direct script access allowed');
class productdisplay_ctrl extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->model("productdisplay_model");
}
public function productfetch(){
$this->load->model("productdisplay_model");
$result = $this->productdisplay_model->fetchData();
$data['name'] = $result->sub3_category_name;
$this->load->view('home', $data);
}
}
?>
Model:
<?php
class productdisplay_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function fetchData() {
$this->db->select('*');
$this->db->where($where);
$this->db->order_by('rand()');
$this->db->from('sub3_category');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row();
}
return false;
}
}
?>
view:
<div class="container-main">
<div class="col-xs-12 col-sm-4 col-md-3">
<div class="prod-container">
<a href="<?php echo base_url(); ?>index.php/welcome/productlist">
<div class="prod_img">
<img src="<?php echo base_url();?>images/img-electronics.jpg" width="100%"/>
</div>
<div class="prod_desc">
<div class="prod-round-icon"></div>
<h4 class="prod_title"><?php echo $name; ?></h4>
<p class="prod_text">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<div class="view-more"> view more</div>
</a>
</div>
</div>
Change in model.like this.use row() which returns result in object format having matched row.so that you can access columns using arrow operator.
And what about your $where variable.set it.
public function fetchData() {
$this->db->select('*');
$this->db->where($where);
$this->db->order_by('rand()');
$this->db->from('sub3_category');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row();
}
return false;
}
inside the foreach loop, remember $row var is a object
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row->field;
}

my query in codeigniter cannot produce result

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();
}

Error message not displaying in CodeIgniter

I want to edit some data after a POST in CodeIgniter.
The problem is when I make an edit I want to show a message that tells me the edit is done. I tried to do the following, but no message is displayed. Why?
Controller
function edit($id = 0) {
$this->load->model('Event_Model');
if ($id > 0) {
$this->data['eventinfo'] = $this->Event_Model->getInfo($id);
}
$this->data['pagetitle'] = "Edit";
$this->data['formID'] = $this->uri->segment(2)."-".$this->uri->segment(3);
$this->template->build('admin/events/add',$this->data);
}
function do_edit() {
$this->load->model('Event_Model');
$id = $this->input->post('hID');
$data = array(
'ev_text' => $this->input->post('ev_text'),
'ev_pic' => $this->input->post('ev_pic'),
);
$this->Event_Model->update($id, $data);
$this->data['success'] = "Done";
$this->data['errors'] = $this->errors;
$this->template->build('admin/events/add',$this->data);
}
Model
function update($id, $data) {
$this->db->where('ev_id', $id);
$this->db->update('events', $data);
}
View (where I want the message)
<?php
if (isset($success)) { ?>
<div class="alert alert-success normal-alert" style="display: block;">
<p><span class="ico-text ico-alert-success" ></span><?= $success; ?></p>
</div>
<?php
}
if (isset($errors)) { ?>
<div class="alert alert-error normal-alert" style="display: block;">
<div>
<span class="ico-text ico-alert-error"></span>
<?php if (count($errors) > 0) { ?>
<ul>
<?php
foreach ($errors as $error) {
echo "<li>$error</li>";
}
?>
</ul>
<?php } ?>
<div class="clear"></div>
</div>
</div>
<?php } ?>
and the second error, update data with picture not work, its get me error in dubg that call me the $_FILES['choose-file']['name'] not define
Try This:
Controller
function do_edit() {
$this->load->model('Event_Model');
$id = $this->input->post('hID');
$data = array(
'ev_text' => $this->input->post('ev_text'),
'ev_pic' => $this->input->post('ev_pic'),
);
$result = $this->Event_Model->update($id, $data);
if($result) {
$this->data['success'] = "Done";
$this->template->build('admin/events/add',$this->data);
}
$this->data['errors'] = $this->errors;
$this->template->build('admin/events/add',$this->data);
}
Model:
function update($id, $data) {
$this->db->where('ev_id', $id);
$this->db->update('events', $data);
return $this->db->affected_rows();
}

Categories