display images from mysql codeigniter - php

my controller is
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class pickoftheday_display extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
//$this->load->library('upload');
//$this->load->library('form_validation');
$this->load->helper('url');
$this->load->model('pickoftheday_display_model','',TRUE);
$this->pickoftheday_display_model->get_pickoftheday_image();
$this->load->database();
}
public function index()
{
$this->load->view('pickoftheday_display');
}
public function get_pickoftheday_image()
{
$data['get_pick_picture']=$this->pickoftheday_display_model->get_pickoftheday_image();
}
}
model is
class pickoftheday_display_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library('encrypt');
$this->load->helper('security');
$this->load->helper('string');
$this->load->library('email');
}
public function get_pickoftheday_image()
{
$query=$this->db->query("SELECT * FROM pick_of_the_day order by id desc ");
if($query->num_rows>0)
{
return $query->result();
}
else
{
return false;
}
}
}
view code is
$val=array();
$link=array();
var_dump($get_pick_picture);
foreach($get_pick_picture as $key)
{
$val[]= $key->image;
$link[]= $key->link;
}
echo $link[0]
how to display image in view..

You did not pass data to view file.
You didn't set data to pass to view file.
In index function, you have to call
public function index()
{
$data['pick_picture']=$this->pickoftheday_model->get_pickoftheday_image();
$this->load->view('pickoftheday_display',$data);
}

Related

Sum up the options selected by a user with codeigniter

I am working on a personality quiz and I want to sum up the options selected by the user. What I have currently is that the programs count the number of values selected in this line and displays it $this->db->where('options', 2);, here it is 2. Rather than it to count I want it to add instead. Below are my model and controller.
Model
<?php
class testing_model extends CI_Model
{
public function get_q()
{
return $this->db->get('questions')->result();
}
public function get_o($q_id)
{
$this->db->where('q_id', $q_id);
return $this->db->get('options')->result();
}
public function result($q_id)
{
$this->db->where('q_id', $q_id);
$this->db->where('options', 2);
return $this->db->get('options')->row()->o_id;
}
}
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class testing_con extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->model("testing_model");
}
public function index()
{
$data['question'] = $this->testing_model->get_q();
$this->load->view('testing_view', $data);
}
public function submit()
{
$score = 0;
foreach($_POST['quetionId'] as $q_id) {
if($this->testing_model->result($q_id) == $_POST['questions_' .$q_id]){
$score++;
}
}
$data['score'] = $score;
$this->load->view('result_view', $data);
}
}

How to call model in controller on constructor because I need write only one time

I create a model for fetching data then write code
model..
function get_courses(){
$this->db->from(TABLE_COURSE);
$this->db->where('name !=', '');
$query = $this->db->get();
$result = '';
if($query){
if($query->num_rows() > 0)
$result = $query->result();
}
return $result;
}
Controller
defined('BASEPATH') OR exit('No direct script access allowed');
class Quiz extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('Mastermodel','',TRUE);
}
public function index()
{
$data['courses'] = $this->Mastermodel->get_courses();
$data['view_file'] = "content/quiz/quiz_list";
$this->load->view('layout/dashboard/layout', $data);
}
public function quiz_of_day()
{
$data['courses'] = $this->Mastermodel->get_courses();
$data['view_file'] = "content/quiz/quiz_of_day";
$this->load->view('layout/dashboard/layout', $data);
}
public function quiz_edit()
{
$data['courses'] = $this->Mastermodel->get_courses();
$data['view_file'] = "content/quiz/quiz_edit";
$this->load->view('layout/dashboard/layout', $data);
}
}
How to call model in controller on constructor because I need write only one time?
To achieve what you want you can do the following...
You need to make the $data array a property of the class
Reference the new $this->data throughout the class
You need to move your call to the model in the constructor.
So what you get is this
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Quiz extends CI_Controller {
protected $data = array(); // Old school definition of an array (instead of [])for safety
public function __construct() {
parent::__construct();
$this->load->model('Mastermodel', '', TRUE);
$this->data['courses'] = $this->Mastermodel->get_courses();
}
public function index() {
$this->data['view_file'] = "content/quiz/quiz_list";
$this->load->view('layout/dashboard/layout', $this->data);
}
public function quiz_of_day() {
$this->data['view_file'] = "content/quiz/quiz_of_day";
$this->load->view('layout/dashboard/layout', $this->data);
}
public function quiz_edit() {
$this->data['view_file'] = "content/quiz/quiz_edit";
$this->load->view('layout/dashboard/layout', $this->data);
}
}
Tim's method would work just find, but I tend to do it more this way.
class Quiz extends CI_Controller {
public $courses; // can also be set to private
public function __construct(){
parent::__construct();
$this->load->model('Mastermodel','',TRUE);
$this->courses = $this->Mastermodel->get_courses();
}
public function index()
{
$data['courses'] = $this->courses;
$data['view_file'] = "content/quiz/quiz_list";
$this->load->view('layout/dashboard/layout', $data);
}

How to pass an array to the model from the controller in codeIgniter

I am trying to pass an array to the model from the controller in codeIgnitor I know this question have some answers but I tried all of them and no solution works with me. So this my code:
Controller:
<?php
if(! defined('BASEPATH')) exit('No direct script access allowed');
class SearchController extends CI_Controller{
var $controller = "user";
public function index(){
$this->home();
}
public function home(){
$this->load->view("search_view");
}
public function search(){
if(isset($_POST['submit'])){
$data['type']=$this->input->post('type');
$data['value']=$this->input->post('value');
$this->load->model("SearchModel");
$this->load->SearchModel->getCadre($data);
}
}
}
?>
Model:
<?php
class SearchModel extends CI_Model{
function construct(){
parent::__construct();
$this->getCadre();
}
function getCadre(){
$query = $this->db->get('cadres');
$query = $this->db->where($type,$value);//the argument witch i want to take from array
if($query->num_rows()>0){
$values = result();
return $query->result();
}
else{
return NULL;
}
}
}
?>
Try below code in your model::
class SearchModel extends CI_Model{
function construct(){
parent::__construct();
}
function getCadre($data = array()){
$this->db->where($data);//the argument witch i want to take from array
$query = $this->db->get('cadres');
if($query->num_rows()>0){
// $values = result();
return $query->result();
}
else{
return NULL;
}
}
}
OR you can pass individual data as below and return result...Also try as below
Controller:
if(! defined('BASEPATH')) exit('No direct script access allowed');
class SearchController extends CI_Controller{
var $controller = "user";
public function index(){
$this->home();
}
public function home(){
$this->load->view("search_view");
}
public function search(){
if(isset($_POST['submit'])){
$type=$this->input->post('type');
$value=$this->input->post('value');
$this->load->model("searchmodel");
$data = $this->searchmodel->getCadre($type,$value);
if($data==NULL){
echo 'No records found!!';
}
else
{
//send data to view
$this->load->view('view_name',$data);
}
}
}
}
Model
class SearchModel extends CI_Model{
function construct(){
parent::__construct();
$this->load->database();
}
function getCadre($type,$value){
$this->db->where('type',$type);//the argument witch i want to take from array
$this->db->where('value',$value);
$query = $this->db->get('cadres');
if($query->num_rows()>0){
// $values = result();
return $query->result();
}
else{
return NULL;
}
}
}
?>

Getting a drop down list from a database in CodeIgniter

I am new to CodeIgniter and I have been trying to populate the drop down list on the view page with data from the database with no success. I tried using the recomendations from this question but the drop down is still empty (display data from database to dropdown CodeIgniter)
Here is my view:
<label>City</label>
<select class="form-control>
<option value="">All</option>
<?php
foreach($groups as $city)
{
echo '<option value="'.$city['cityidd'].'">'.$city['city'].'</option>';
}
?>
</select> <br/>
Here is my controller:
<?php
class Main_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
public function index()
{
$this->load->helper('form');
$this->load->view('supplier_add');
}
}
Here is my model:
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT city FROM citys');
return $query->result();
}
}
The table name is "citys" then the corresponding column heads are "cityidd" and "city"
There are several issue found there. Make changes as below
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load_model('Site_model');
$this->load->database();
}
public function index(){
$this->load->helper('form');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
Finally model
function getAllGroups(){
$query = $this->db->query('SELECT cityidd,city FROM citys');
return $query->result_array();
}
and now test
First change your SELECT query as
SELECT cityidd, city FROM citys
In the index() of controller change the code as
public function index()
{
$this->load->helper('form');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
you have to call your model method and pass it to view in the controller, code:
public function index()
{
$this->load->helper('form');
$this->load->model('site_model');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
if you are working on linux dont forget upper and lowercase names!
class Main_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
$this->load->model('Site_model');
}
public function index()
{
$this->load->helper('form');
$data['groups']=$this->Site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
}
Here is my model:
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT * FROM citys');
return $query->result();
}
}
Try like this
Make these changes
IN Model convert data to array as you are using it as array in view so change to this
function getAllGroups()
{
$query = $this->db->query('SELECT * FROM citys');
return $query->result_array();
}
In Controller
public function index()
{
$this->load->helper('form');
$this->load->model('site_model');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
You are not loading model load the Model
for example:-
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Site_model')
$this->load->database();
}
public function index()
{
$this->load->helper('form');
$data['record']=$this->Site_model->getAllGroups()
$this->load->view('supplier_add', $data);
}
}
Model:-
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT city FROM citys');
return $query->result();
}
}

Codeigniter model empty query

this is the model
<?php
Class Clase_model extends CI_Model
{
function __construct(){
parent::__construct();
}
function getAll(){
$query = $this-> db ->get("clase");
if($query -> num_rows() == 1)
{
return $query->result();
}
else
{
return false;
}
}
}
?>
and this is the controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Clase extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('clase_model','',TRUE);
}
function index()
{
if($this->session->userdata('logged_in')){
$data['clases'] = $this->clase_model->getAll();
//$this->load->view('header', $data);
//$this->load->view('clase_view', $data);
print_r ($data['clases']);
}
else{
redirect('login', 'refresh');
}
}
}
?>
The connexion with the database is correct because another model is working correct ,but in this when I try to print the result of the query it's empty but the table it's not empty,it's something wrong??
Can you try this:
<?php
Class Clase_model extends CI_Model
{
function __construct(){
parent::__construct();
}
function getAll(){
$query = $this-> db ->get("clase");
if($query -> num_rows() == 1)
{
return $query->result_array();
}
else
{
return false;
}
}
}
?>
your model is working for me... i think it's not load properly at your end.
can you try this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Clase extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('clase_model');
}
function index()
{
if($this->session->userdata('logged_in')){
$data['clases'] = $this->clase_model->getAll();
echo $this->db->last_query();
print_r ($data['clases']);
}
else{
redirect('login', 'refresh');
}
}
}
?>

Categories