Image processing with codeigniter - codeigniter

I am trying to show picture in codeigniter. Either one or few pictures that I store in the gallery.
gallery_model.php
class Gallery_model extends CI_Model {
public function get_picture($pic_unique_id)
{
$query = $this->db->get('galleries_pictures', $pic_unique_id);
return $query->result();
}
}
controllers/cspages.php
public function index()
{
$this->load->gallery_model();
$this->load->get_picture($pic_unique_id);
$this->load->view('index');
}
views/index.php
<?php foreach($pic as $pic_item) { ?>
<?php endforeach; } ?>
<!-- MAIN IMAGE -->
<img src="<?php echo base_url(); ?>assets1/images/slider/1.jpg">
How to get the $pic_unique_id and $pic_item ?

Try This IN model:
class Gallery_model extends CI_Model {
public function get_picture($pic_unique_id)
{
$query = $this->db->get_where('galleries_pictures',array('pic_id'=> $pic_unique_id));
return $query->result();
}
}
IN Controller:
public function index()
{
$this->load->helper('url');
$this->load->model('gallery_model');//loads model
$data['pic'] = $this->gallery_model->get_picture($pic_unique_id);//calls to model function
$this->load->view('index',$data);//loads view with data
}
IN view
<?php foreach($pic as $pic_item) { ?>
<img src="<?php echo base_url(assets1/images/slider/).$pic_item->pic_item; ?>">
<?php } ?>

Try this in gallery_model.php
class Gallery_model extends CI_Model {
public function get_picture($pic_unique_id)
{
$query = $this->db->get_where('galleries_pictures',array('pic_id'=> $pic_unique_id));
return $query->result();
}
}
Try this in controllers/cspages.php
public function index()
{
$this->load->gallery_model();
$data['pic'] = $this->load->get_picture($pic_unique_id);
$this->load->view('index',$data);
}
in views/index.php
<?php foreach($pic as $pic_item) { ?>
<img src="<?php echo base_url(); ?>assets1/images/slider/<?= $pic_item->pic_item ?> ">
<?php endforeach; } ?>

Related

How to autocomplete select option in CodeIgniter

I'm trying to make a select option in CodeIgniter, and want to make it autocomplete fill. How do I do that?
This is my code:
Model:
public function get_nama_customer(){
$data = array();
$query = $this->db->get('tabel_customer');
if($query->num_rows() > 0){
foreach($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
Controller:
public function add_data()
{
$this->load->model('model_tabel_material');
$data['tabel_customer'] = $this->model_tabel_material->get_nama_customer();
$this->load->view('datamaster/tabel_material/v_add_material', $data);
}
public function do_add()
{
$nama_customer = $_POST['nama_customer'];
$data_add = array(
"nama_customer" => $nama_customer,
);
$temp = $this->model_tabel_material->insert_data('tabel_material', $data_add);
if($temp >=1){
redirect('tabel_material/controller_tabel_material/index');
}
}
View:
<select class="form-control" name="nama_customer" id="nama_customer" required>
<?php if(count($tabel_customer)){ ?>
<option value=''>--Pilih Customer--</option>
<?php foreach ($tabel_customer as $list){ ?>
<?php
echo "<option value='".$list['nama_customer']."'>".$list['nama_customer']."</option>";
?>
<?php } ?>
<?php } ?>
</select>

Why this error appears stdClass::$pic_item?

site/views/index.php
<?php foreach($pic as $pic_item): ?> {
<img src="<?php echo base_url('assets1/images/slider/'.$pic_item->pic_item );?>">
}
<?php endforeach;
?>
controllers/Cspages.php
public function index()
{
$this->load->model('gallery_model');
$pic_unique_id = 18; // slider
$data['pic'] = $this->gallery_model->get_picture($pic_unique_id);
$this->load->view('index', $data);
}
models/Gallery_model.php
public function get_picture($pic_unique_id)
{
$query = $this->db->get_where('galleries_pictures', array('picture_unique_id' => $pic_unique_id));
return $query->result();
}
How to fix the following error?
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$pic_item
Filename: views/index.php
Line Number: 214
Backtrace:
File: C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\masterlinkci2\application\site\views\index.php
Line: 214
Function: _error_handler
File: C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\masterlinkci2\application\site\controllers\Cspages.php
Line: 31
Function: view
File: C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\masterlinkci2\index.php
Line: 315
Function: require_once
http://localhost/masterlinkci2/assets1/images/slider/"> }
Change your foreach loop in view as below:
<?php foreach($pic as $pic_item): ?>
<img src="<?php echo base_url('assets1/images/slider/').$pic_item->pic_item;?>">
<?php endforeach;?>
OR
<?php foreach($pic as $pic_item): ?>
<img src="<?php echo base_url('assets1/images/slider/'.$pic_item->pic_item );?>">
<?php endforeach;?>
UPDATE
Model
<?php
public function get_picture($pic_unique_id)
{
$this->db->where('picture_unique_id',$pic_unique_id);
$query = $this->db->get_where('galleries_pictures');
return $query->row;
}
Controller
public function index()
{
$this->load->helper('url');//load url helper
$this->load->model('gallery_model');
$pic_unique_id = 18; // slider
$data['pic'] = $this->gallery_model->get_picture($pic_unique_id);
$this->load->view('index', $data);
}
view
<img src="<?php echo base_url('assets1/images/slider/'.$pic->pic_item );?>">

How I display title in categories only one title in one blogpost?

Hi all i'm a new of codigniter I create post and join two table I want to show categories title only one in my blog post .but it not folow me.it show many title categories in every post.
My Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Site extends CI_Controller {
function index(){
$this->home();
}
function home(){
$this->load->view('header_view');
$this->load->view('slide_view');
$this->load->view('crumb_view');
$this->load->view('footer_view');
}
function menu(){
$this->load->model('tinjeat_md');
$data["menu_pro"] = $this->tinjeat_md->menu_get();
$this->load->view('header_view');
$this->load->view('crumb_view');
$this->load->view('menu_view',$data);
$this->load->view('footer_view');
}
}
<?php
class Tinjeat_md extends CI_Model{
function __construct(){
parent::__construct();
}
function menu_get(){
$this->db->select('*');
$this->db->from('menu','categories_menu');
$this->db->join('categories_menu','menu.cate_id=
categories_menu.id','left');
$query = $this->db->get();
return $query->result();
}
}
?>
View:
<div class="menu">
<?php foreach ($menu_pro as $value): ?>
<p class="title_cate">
<?php echo $value->cate_name_menu; ?>
</p>
<div class="menu_image">
<?php
echo img(array(
'src'=>'./images/'.$value->image,
'width'=>'220px',
'height'=>'150px'
));
?>
</div>
<div class="menu_description">
<h3><?php echo $value->title; ?></h3>
<p><?php echo $value->description; ?></p>
<p>Prices:<?php echo $value->prices; ?></p>
</div>
<?php endforeach; ?>
</div>
My database:
categories_menu
-id
-cate_menu_name
menu
-id_menu
-title
-image
-description
-prices
-cate_id
In your model.Try like this.
function menu_get(){
$this->db->select('*');
$this->db->from('menu');
$this->db-join('categories_menu','menu.cate_id=categories_menu.id','left');
$query = $this->db->get();
return $query->result();
}
for user guide
you can check your sql query by using $this->db->last_query().

dynamic dependant drop using codeigniter hmvc is not working

i'm trying populate all the course present in the database which has higher course_id than the selected one in first dropdown.
course.php (controller)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Course extends MX_Controller {
public function __construct() {
parent::__construct();
$this -> load -> model('course_model');
}
public function index() {
$data['courses'] = $this -> course_model -> get_course();
$this -> load -> view('dropdisplay', $data);
}
public function get_next_course($course){
$this->load->model('nextcourse_model');
header('Content-Type: application/x-json; charset=utf-8');
echo(json_encode($this->nextcourse_model->get_course($course)));
}
}
course_model.php (model)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Course_model extends CI_Model {
public function __construct() {
$this -> load -> database();
echo "course_model <br/><br/>";
}
function get_course()
{
$this -> db -> select('course_id, course_name');
$query = $this -> db -> get('course');
$courses = array();
if ($query -> result())
{
foreach ($query->result() as $course)
{
$courses[$course -> course_id] = $course -> course_name;
}
return $courses;
}
else
{
return FALSE;
}
}
}
?>
nextcourse_model.php (model)
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Nextcourse_model extends CI_Model {
public function __construct() {
$this->load->database();
echo "nextcourse_model <br/><br/>";
}
function get_course($cour, $tree = null) {
$this->db->select('course_id, course_name');
$this-db->from('course');
$this->db->where('course_id >', $cour);
if ($tree != NULL) {
$this->db->where('course_id', $cour);
}
$query = $this->db->get('course');
$nextcourses = array();
if ($query->result()) {
foreach ($query->result() as $value) {
$nextcourses[$value->course_id] = $value->course_name;
}
return $nextcourses;
} else {
return FALSE;
}
}
}
?>
dropdisplay.php (view)
<html>
<head>
<title>Course</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">// <![CDATA[
// $(document).ready(function(){
var course_id;
$(document).ready(function() {
$('#course').change(function() {
$("#whatnext > option").remove();
// course_id = $('#course').val();
course_id = $('this').val();
});
});
//$('#check').change(function(){
// $("#whatnext > option").remove();
//var course_id = $('#course').val();
function getcondition(id)
{
if (id == 1)
{ //alert(id);
// alert(course_id);
$.ajax({
type: "POST",
url: "localhost/coursepath/course/get_next_course/" + course_id, // passing this course_id to course controller
//data: '&course_id='+course_id,
success: function(nextcourses)
{
alert(course_id);
$.each(nextcourses, function(id, course)
{
var opt = $('<option />');
opt.val(id);
opt.text(course);
$('#whatnext').append(opt);
});
}
});
}
else if (id == 2)
{
// alert("course_id=" + course_id);
}
}
// ]]>
</script>
</head>
<body>
<?php $courses['#'] = 'Please Select'; ?>
<label for="course">What am I: </label>
<?php echo form_dropdown('course_id', $courses, '#', 'id="course"'); ?><br />
<label for="next_course">Category: </label>
<select id="check" class="bot" onchange="getcondition(this.value);">
<!-- <select id="check" class="bot"> -->
<option value="" selected=selected> Select Category</option>
<option value="1"> Course</option>
<option value="2"> Job</option>
</select><br />
<?php $whatnext['#'] = 'Please Select'; ?> <br />
<label for="next_course">What I want to be: </label>
<?php echo form_dropdown('stream_id', $whatnext, '#', 'id="whatnext"'); ?><br />
</body>
</html>

Codeigniter - MPDF not exporting data

I am new to Codeigniter and want to export data present in my MYSQL database into PDF file using MPDF. The code is as follows:
View:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Export PDF</title>
</head>
<body>
<div id="container">
<h4>Member Data</h4>
<table border="1">
<tr>
<th>group_id</th>
<th>group_name</th>
<th>Archieved</th>
</tr>
<?php
foreach ($member as $rows) {
echo $rows['group_id'];
?>
<tr>
<td><?php echo $rows['group_id'] ?></td>
<td><?php echo $rows['group_name']?></td>
<td><?php echo $rows['archieved'] ?></td>
</tr>
<?php
$i++;
}
?>
</table>
<br> <br>
<a href='<?php echo base_url(); ?>index.php/member_con/topdf'><span style='color:green;'>Export to Pdf</span></a>
</div>
<?php
?>
</body>
</html>
Controller:
<?php
class Member_con extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('member_model');
$this->load->helper('url');
$this->load->library('mpdf');
}
public function index() {
$data['member'] = $this->member_model->alldata();
$this->load->view('member_view', $data);
}
function topdf() {
$this->mpdf->useOnlyCoreFonts = true;
$filename = "VISH";
$data['member'] = $this->member_model->alldata();
$html = $this->load->view('member_view', $data['member'], true);
$this->mpdf->setTitle('Posts');
$this->mpdf->writeHTML($html);
$this->mpdf->output($filename, 'D');
}
}
?>
Model:
<?php
class Member_model extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
function Member_Model() {
parent::Model();
}
function alldata()
{
$this->db->select('*');
$this->db->from('groups');
$this->db->order_by('group_id','ASC');
$getData = $this->db->get();
if($getData->num_rows() > 0)
return $getData->result_array();
else return null;
}
}
?>
with this code, it is giving me a blank PDF file, with only text as 'Member Data' and 'Export as pdf'. I have checked whether it is passing data to view, and yes it is doing so.
But don't know what is the matter with 'foreach' loop. I is printing everything outside 'foreach' loop, but bot the data members. Can anyone please let me know what should I do?
Thanks in advance....
Got the answer. In controller, instead of
$html = $this->load->view('member_view', $data['member'], true);
I used following:
$html = $this->load->view('member_view', $data, true);

Resources