I am creating a sample image test where in I want to display all of the images in a database table in CodeIgniter. The question is, how will I able to display the values of the database table using CodeIgniter?
Here is the code of my model:
<?php
class Image_model extends CI_Model{
/*Sample test function for the image dropdown list*/
public function getImages()
{
$query = $this->db->select('main_image_url'));
$this->db->get('product_master');
return $query->result();
}
/*End sample test function*/
}
?>
Here is the code of my controller:
<?php
if(! defined('BASEPATH')) exit('No direct script access allowed');
class Sample_image_dropdown extends MX_Controller{
public function __construct()
{
parent::__construct();
}
public function index()
{
$data['main_view'] = 'sample_view/image_dropdown_view';
$this->load->view('sample_view/image_dropdown_view', $data);
}
public function display_all_images()
{
$this->load->model('sample_model/image_model');
$data['images'] = $this->image_model->getImages();
$data['main_view'] = "sample_view/image_dropdown_view";
$this->load->view('sample_view/image_dropdown_view', $data);
}
}
?>
Here is the code of my view:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div>
<select name="" id="">
<option value="all">All Image</option>
<option value="with-image">With Image</option>
<option value="no-image">Without Image</option>
</select>
<input type="submit" value="Search">
</div>
<?php if (isset($images)):?>
<?php foreach ($images as $image):?>
<table>
<tr>
<td>
<th>
Images
</th>
</td>
</tr>
<tr>
<td>
<img src="<?php echo "$image->main_image_url";?>">
</td>
</tr>
</table>
<?php endforeach; ?>
<?php endif; ?>
</body>
</html>
You made a mistake in your model and in the controller, I mentioned the changes please note down and try,
Model File
class Image_model extends CI_Model{
/*Sample test function for the image dropdown list*/
public function getImages()
{
$query = $this->db->select('main_image_url');
$this->db->from('product_master');
$query = $this->db->get();
return $query->result();
}
/*End sample test function*/
}
Controller File,
public function __construct()
{
parent::__construct();
}
public function index()
{
$data['main_view'] = 'sample_view/image_dropdown_view';
$this->display_all_images();
}
public function display_all_images()
{
$this->load->model('sample_model/image_model');
$data['images'] = $this->image_model->getImages();
$data['main_view'] = "sample_view/image_dropdown_view";
$this->load->view('sample_view/image_dropdown_view', $data);
}
I assume you are storing images in directory at the time of uploading at
uploads/abc.png
then you can get data from db (which you are already doing)
and in view :
<img src="echo dir_path./$image->main_image_url;" />
or if you are storing whole path in DB then :
<img src="echo $image->main_image_url;" />
Related
I want to do a simple search with pagination in codeigniter...
I dont really know how to do it..
My Model
public function testSearch()
{
$this->db->like('Profile_for', 'Myself');
$query = $this->db->get('users');
return $query->result();
}
My Controller
public function searcher()
{
$config['base_url']='http://localhost/index.php/controller/searcher';
$config['total_rows'] = $this->Model->testSearch()->num_rows();
$config['per_page'] = 5;
$config['num_links'] = 2;
$this->pagination->initialize($config);
$data['records'] = $this->Model->testSearch($config['per_page'], $this->uri->segment(3));
$this->load->view('pagination', $data);
}
I THINK MY CONTROLLER CODES ARE WRONG....
Please help me guyz....
Thanks in advance.....
1.Open routes.php under Config folder
2.Now create a file called pagination.php under controllers folder and paste the following code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pagination extends CI_Controller
{
public function __construct() {
parent:: __construct();
$this->load->helper("url");
$this->load->model("Countries_Model");
$this->load->library("pagination");
}
public function index() {
$config["base_url"] = base_url() . "pagination";
$config["total_rows"] = $this->Countries_Model->record_count();
$config["per_page"] = 10;
$config["uri_segment"] = 2;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2))? $this->uri->segment(2) : 0;
$data["results"] = $this->Countries_Model
->get_countries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view("pagination", $data);
}
}
3.Create a file called countries_model.php under models folder and paste the following code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Countries_Model extends CI_Model
{
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("tblcountries");
}
public function get_countries($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("tblcountries");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
4.Lastly, create a file called pagination.php under views folder and paste the following code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Pagination with CodeIgniter</title>
</head>
<body>
<div id="container">
<h1 style="text-align:center">Page Navigation in PHP</h1>
<table width="400" border="1" align="center">
<tr>
<td width="100" bgcolor="#CCCCCC"><p>Country ID</p></td>
<td width="300" bgcolor="#CCCCCC">Country</td>
</tr>
<h1 style="text-align:center">Countries</h1>
<div id="body">
<?php
foreach($results as $data) {
?>
<tr>
<td><?php echo $data->CountryID ?></td>
<td><?php echo $data->Country ?></td>
</tr>
<?php
}
?>
</table>
<div style="text-align:center"><?php echo $links; ?></div>
</div>
</div>
</body>
</html>
This is code concept is same as your code.I hope this will help you
lot Thank you
So I'm in the middle of creating an E-Commerce site for a class. On my Profile page I'm planning to display various info stored in the db. Right now I'm only messing with the user photo. I have a function in the model that gets the Users photo from the column 'Image_Path' in DB 'users'. My problem is that for each profile view it shows the same photo. I think its return the whole array of pictures and maybe just sets it as the first one?
here is the model
<?php
class Profile_Model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
// public function get_profile_picture()
// {
//
// $query = $this->db->get('users');
// return $query -> result();
// }
public function get_single_profile($id){
//get whole table
$this->db->select('Username');
$this->db->select('Image_Path');
$this->db->from('users');
$this->db->where('User_ID', $id);
$query = $this->db->get();
return $query->result();
}//end get_single_profile
//for uploading profile pics
public function set_profile_pic(){
}
}//CLASS
?>
The Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Profile extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('session'); //needed to use global session variables
$this->load->helper('language');
$this->load->model('Profile/Profile_Model');
}
public function index()
{
if($this->session->userdata('logged_in'))
{
$data['profilepicture'] = $this->Profile_Model->get_single_profile();
$this->load->view('Profile/Profile', $data);
}// echo session variable logged_in
else{
redirect( base_url().'Login'); //redirect to user profile view after verification)
}
}//END Index
public function logout(){
$this->session->sess_destroy();
redirect(base_url(). 'Login');
}//END LOGOUT
public function display_profile_pic(){
$data['profilepicture'] = $this->Profile_Model->get_profile_picture();
$this->load->view('Product/Product', $data);
}
}//END CLASS
The View
<!DOCTYPE html>
<html>
<head>
<title><?php echo $_SESSION['logged_in']?></title>
<style type="text/css">
.container {
width: 15%;
text-align: center;
clear:both;
}
.container input {
width: 100%;
clear: both;
}
.logout {
position: fixed;
bottom: 10%;
right: 10%;
}
.aboutme{
position:fixed;
right:50%;
bottom:50%;
}
.shopbutton{
position:fixed;
right:17%;
top:10%;
}
.checkout{
position:fixed;
right:10%;
top:10%;
}
</style>
</head>
<body>
<h3>Logged In As: <?php echo $_SESSION['logged_in']?> </h3>
**where the picture is displayed
<?php foreach ($profilepicture as $row)
{
echo '<img src='.base_url().'../'.$row->Image_Path.' style="Height: 300px; Width:200px;"><br>';
} ?>
<form action=" <?php echo base_url().'Profile';?>" method='post'>
<input type='submit' value='Upload Picture'>
</form>
<div class = 'aboutme'>
<p>About Me</p>
</div>
<div class = 'logout'>
<form action= '<?php echo base_url().'Profile/logout';?>' method='post'>
<input type="submit" value="Log Out">
</form>
</div>
<div class = 'checkout'>
<form action = '<?php echo base_url().'Cart/display_cart';?>' method='post'>
<input type='submit' value='Check Out'>
</form>
</div>
<div class = 'shopbutton'>
<form action = '<?php echo base_url().'Product/display_products';?>' method='post'>
<input type='submit' value='Shop'>
</form>
</div>
</body>
</html>
First, try changing the select portion of your query. It should be like:
$this->db->select('Image_Path, Username');
if it still not working, try to echo your last query in get_single_profile()
echo $this->db->last_query();
exit;
Changed my Model function to:
public function get_single_profile($id){
//get whole table
$this->db->select('Image_Path');
$this->db->from('users');
$this->db->where('Username', $id);
$query = $this->db->get();
echo $this->db->last_query();
return $query->result();
}//end get_single_profile
And My Controller Index To:
public function index()
{
if($this->session->userdata('logged_in'))
{
$data['profilepicture'] = $this->Profile_Model->get_single_profile($this->session->userdata('logged_in') );
$this->load->view('Profile/Profile', $data);
}// echo session variable logged_in
else{
redirect( base_url().'Login'); //redirect to user profile view after verification)
}
}//END Index
in the model I changed the Username from the table and set it to $id, In the controller I used a previously defined session variable to set $id to the session...I think
I hope someone can help me. How can I best dynamically change page title, desc, or key vs..vs..by extending the original class? Here is what I have so far:
<?php
class siteGlobal
{
public $arr = array('title'=>'Page Default title','desc'=>'Page default description');
public function pageHeader()
{ ?>
<!doctype html><html>
<?php }
public function metaTags()
{ ?>
<head>
<meta charset="utf-8">
<title><?php echo $this->arr['title'];?></title>
<meta name="description" content="<?php echo $this->arr['desc'];?>">
<?php }
public function pageBody()
{ ?>
</head>
<body>
<?php }
public function pageFooter()
{ ?>
</body>
</html>
<?php }
} ?>
OTHER PAGE
require_once('siteGlobal.php');
class pageDetail extends siteGlobal
{
public function __construct()
{
$this->pageHeader();
$this->newMetas();
$this->startBody();
$this->sayfaBilgileri();
$this->pageFooter();
}
public function newMetas()
{
parent::metaTags();
}
public function sayfaBilgileri()
{
//HOW I can write a new title and description in this function to siteGlobal.php (public $metaInfo) ?>
<div style="width:640px; margin:0 auto;">
<h1>Page New Title</h1>
<h2>Page New Subtitle</h2>
<p>Lorem Ipsum, bla bla....</p>
</div>
<?php }
}
$writePage = new pageDetail(); ?>
You can try to create a method in the parent class say:
protected function setTitle($title){
$this->arr['title'] = $title;
}
and use it in the extended class this way:
parent::setTitle("My title");
I am a newbee to CodeIgniter and PHP. I am trying to insert data using a form and display it on the same view page using MVC pattern given by CodeIgniter. My View is:
<html>
<head>
<title>My Form</title>
</head>
<body>
<? echo form_open('books/input'); ?>
<? echo $id; ?>:
<? echo form_input('id'); ?>
</br>
<? echo $title; ?>:
<? echo form_input('title'); ?>
</br>
<? echo $body; ?>:
<? echo form_input('body'); ?>
</br>
<? echo form_submit('mysubmit','Submit!'); ?>
<? echo form_close(); ?>
</body>
</html>
and My Controller is:
<? class Books extends Controller{
function Books(){
parent::Controller();
}
function main(){
$this->load->model('books_model');
$data = $this->books_model->general();
//$this->load->view('books_main',$data);
}
function input(){
$this->load->helper('form');
$this->load->model('books_model');
$data = $this->books_model->general();
$this->load->view('books_input',$data);
}
} ?>
and My model is just returning the data entered by the user as:
<? class books_model extends Model{
function books_model(){
$this->load->helper('url');
}
function general(){
$data['id'] = 'id';
$data['title'] = 'title';
$data['body'] = 'body';
return $data;
}
} ?>
But when I try to access the form, it shows me an error quoting:
load->model('books_model'); $data = $this->books_model->general(); }
function input(){ $this->load->helper('form'); $this->load->model('books_model'); $data = $this->books_model->general();
$this->load->view('books_input',$data); } } ?>
404 Page Not Found
though I am accessing it with the same URL: localhost/CodeIgniter/index.php/books/input
How can I access this form and insert data successfully?
Every Class name should start with capital letter
like
class Books_model extends Model
and do type like this
$this->load->model('Books_model');
this could be the issue that you are getting "404 page not found".
Read some basic things that you have to follow from guide
http://ellislab.com/codeigniter/user-guide/general/models.html
I have write a some codeigniter code, when I click button add a data, and the table will ajax reload, not reload the page. How to modify my code? Thanks
This is my controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Big_category extends CI_Controller {
function __construct() {
...
}
function index() {
if($this->session->userdata('logged_in')) {
$this->page(0);
} else {
show_404('page');
}
}
function page($page) {
....
$data['pagelist']=$this->pagination->create_links();
$data["main_content"] = "big_category_view";
$this->load->view('template', $data);
}
This is my model
class Big_category_model extends CI_Model
{
...
function get_big_category($limit, $start)
{
$this->db->limit($limit, $start);
$query = $this->db->get_where('category1', array('c1_status' => 1)); //SELECT * FROM category1
return json_encode($query->result());
}
This is my view
...
<? $data = json_decode($all_big_category); ?>
<? foreach($data as $key => $value): ?>
<tr class="modify_tr">
<td><?=($key+1)?></td>
<td id="td_id_<?=$value->c1_id?>" class="modify_td">
<span id="c1_id_<?=$value->c1_id?>"><?=$value->c1_name?></span>
<form class="form-search td_id_<?=$value->c1_id?>">
<input type="text" name="input_c1_id" id="input_c1_id_<?=$value->c1_id?>">
<button class="btn modify" id="button_c1_id_<?=$value->c1_id?>" c1-id="<?=$value->c1_id?>" type="button"><i class="icon-edit"></i></button></td>
</form>
<td><button class="btn btn-danger"><i class="icon-remove-sign"></i></button></td>
</tr>
<? endforeach ?>
</table>
<?=$pagelist?>
...
Here is a simple technique of how you can achieve this.
Layout(Template)
<html>
<body>
<div><!--other stuff here--></div>
<div id = 'form_id'>
<?php echo $content?>
</div>
</body>
</html>
Controller
function index()
{
if($this->input->is_ajax_request()){
//load form here
$this->load->view('form');
}else{
//this will load the form for the first time
//pass third parameter as TRUE so it will returned as string
//assigned to index content which will be displayed in layout
$data['content'] = $this->load->view('form',$data , TRUE);
//load template here
$this->load->view('template', $data);
}
}
JQuery
$.ajax({
type : 'POST',
url : '<?php echo site_url('controllername/index')?>',
data : '' // query string
success : function(formagain){
$('#form_id').html(formagain);
//this will replace the content of div with new form
}
});