Sum up the options selected by a user with codeigniter - php

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

Related

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;
}
}
}
?>

How to call Base Controllers Method in Codeigniter?

I Have Defined MY_ Controller in my core folder.
Then i have two controllers:
Admin_Controller
Customer_Controller
Now i want to put a query into my Customer_Controller whose result i can access all the controller which extends to Customer_Controllers.
I have put this code in Customer_Controller
public function get_users()
{
$id = $this->session->userdata('id');
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
}
Now when i have a child class something like this
<?php
class User extends Customer_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
//Get Results from Customer_Controller
}
}
how do this?
you can follow this:
class Customer_Controller extends My_Controller
{
public function customer()
{
return 'costomers';
}
}
then
class User extends Customer_Controller
{
//call the function from Customer_Controller
$this->customer();
}
Let me give you a suggestion. Instead of creating a controller and extending the same, why don't you create a library class.
And then load your library inside the controller you want.
for example here is your library class file.
<?php
class users{
private $session;
public function __construct(){
$CI =& get_instance();
$this->session=$CI->session;
}
public function get_users()
{
// do the code and return the
}
}
AND in your controller
<?php
class User extends CI_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
$this->load->library('users');
$users = new users();
$userinfo = users->get_users();
//Results from library
}
}
You Try get_instance() like this in you User Controller
public function get_users()
{
$id = $this->session->userdata('id');
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
return $result;
}
class User extends CI_controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
$CI=&get_instance();
$result=$CI->get_users();
foreach($result as $row)
{
echo $row->id;//here add you table field name for id
}
}
}
Codeigniter is MVC framework.
So You will put get_users in model part.
example: you must making custom_model.php in models folder.
<?php
class custom_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function get_users($id)
{
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
return $result;
}
}
?>
next step: you are remake custom_controller.
public function get_users()
{
$id = $this->session->userdata('id');
$this->load->model('custom_model');
$result=$this->cutom_model->get_users($id);
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
}
and next step:
class User extends Customer_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
//
}
public function get_users(){
parent::get_users();
}

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');
}
}
}
?>

display images from mysql codeigniter

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

Categories