Codeigniter Vote Candidate Function - php

i try to make a candidate vote system on Codeigniter. Each user only can vote 1 candidate, and after vote the candidate user status will change from '1' to '0' so user cant vote again. Can someone help me to create this function ?
Here is my code.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin_calon extends CI_Controller
{
function __construct() {
parent::__construct();
$this->load->model('Admin_dcalon');
$this->load->model('Admin_dvoter');
$this->load->model('Voter_model');
$this->load->helper('url');
$this->load->database();
}
public function index() {
$data['calon'] = $this->Admin_dcalon->tampil_data()->result();
$this->load->view('Admin_calon',$data);
}
function tambah_calon(){
$nama = $this->input->post('nama');
$config['upload_path'] = base_url(). './uploads/';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = '0';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload();
$img = $this->input->post('img');
$data = array(
'nama' => $nama,
'img' => $img,
'voted' => 0
);
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Data Tersimpan</div>');
$this->Admin_dcalon->tambah_data($data,'calon');
redirect('Admin_calon');
}
public function delete_calon($id_calon){
$where = array('id_calon' => $id_calon);
$this->Admin_dcalon->hapus_data($where,'calon');
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Data Terhapus</div>');
redirect('Admin_calon');
}
function edit_calon($id_calon){
$where = array('id_calon' => $id_calon);
$data['calon'] = $this->Admin_dcalon->edit_data($where,'calon')->result();
$this->load->view('Admin_ecalon',$data);
}
function update_calon(){
$id_calon = $this->input->post('id_calon');
$nama = $this->input->post('nama');
$img = $this->input->post('img');
$data = array(
'nama' => $nama,
'img' => $img,
);
$where = array(
'id_calon' => $id_calon
);
$this->session->set_flashdata('msg','<div class="alert alert-info text-center">Perubahan Tersimpan</div>');
$this->Admin_dcalon->update_data($where,$data,'calon');
redirect('Admin_calon');
}
function vote_calon(){
$id_calon = $this->input->post('id_calon');
$voted = $this->input->post('voted');
$data = array(
'id_calon' => $id_calon,
'voted' => +1
);
$where = array(
'id_calon' => $id_calon,
);
$this->Admin_dcalon->vote_calon($where,$data,'calon');
redirect('voter/voter_sukses');
}
public function logout() {
$data = ['id_admin', 'username'];
$this->session->unset_userdata($data);
redirect ('Admin_login');
}
}
?>
and this Model code.
<?php
class Admin_dcalon extends CI_Model{
function tampil_data(){
$this->db->select('*');
$this->db->from('calon');
return $this->db->get();
}
function tambah_data($data,$table){
$this->db->insert($table,$data);
}
function hapus_data($where,$table){
$this->db->where($where);
$this->db->delete($table);
}
function edit_data($where,$table){
return $this->db->get_where($table,$where);
}
function update_data($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}
function vote_calon($where,$data,$table)
{
$this->db->where($where);
$this->db->update($table,$data);
}
}
?>
Please help me. Thanks for every one.
*

Related

Can't update my database record in codeigniter, no error is appearing

I'm quite new to php and there's no error appearing but appereantly, can't update my data in the database.
The controller
public function update_user_view() {
$this->load->helper('form');
$user_id = $this->uri->segment('3');
$query = $this->db->get_where("users",array("user_id"=>$user_id));
$data['records'] = $query->result();
$data['old_user_id'] = $user_id;
$this->load->view('user_edit',$data);
}
public function update_user(){
$this->load->model('user_model');
$data = array(
'user_id' => $this->input->post('user_id'),
'name' => $this->input->post('name'),
'nickname' => $this->input->post('nickname'),
'email' => $this->input->post('email'),
'hadd' => $this->input->post('hadd'),
'gender' => $this->input->post('gender'),
'cpnum' => $this->input->post('cpnum'),
'comment' => $this->input->post('comment')
);
$old_user_id = $this->input->post('old_user_id');
$this->user_model->update($data,$old_user_id);
$query = $this->db->get("users");
$data['records'] = $query->result();
$this->load->view('user_view',$data);
}
the model
<?php
class User_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function insert($data) {
if ($this->db->insert("users", $data)) {
return true;
}
}
public function delete($user_id) {
if ($this->db->delete("users", "user_id = ".$user_id)) {
return true;
}
}
public function update($data,$old_user_id) {
$this->db->set($data);
$this->db->where("user_id", $old_user_id);
$this->db->update("users", $data);
}
}
?>
Just do
$this->db->where("user_id", $old_user_id);
$this->db->update("users",$data);
Remove $this->db->set($data); from update method of model. This is not required as update query data section is used to set table records.

Get Id User in codeigniter

i have two database. tabel_jawaban and tabel_user.
in tabel_user there is field called "id_user"
i want to move/call "id_user from tabel_user" to "id_user in tabel_jawaban" when registration.
but i failed. anyone can help? there are my code....
model_user :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_user extends CI_Model {
function __construct() {
parent::__construct();
$this->tbl = "tabel_user";
}
function cek_user($username="",$password="") {
$query = $this->db->get_where($this->tbl,array('username' => $username, 'password' => $password));
$query = $query->result_array();
return $query;
}
function ambil_user($username)
{
$query = $this->db->get_where($this->tbl, array('username' => $username));
$query = $query->result_array();
if($query){
return $query[0];
}
}
function ambil_iduser($idUser)
{
$query = $this->db->get_where($this->tbl, array('id_user' => $idUser));
$query = $query->result_array();
if($query){
return $query[0];
}
}
function getAllUser() {
$this->db->from('tabel_user');
return $this->db->get();
}
function deleteUser($id)
{
$this->db->where('id_user', $id);
$this->db->delete('tabel_user');
}
}
?>
Jawaban_model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Jawaban_model extends Ci_Model {
public function Ambil() {
$data = $this->db->query('select * from tabel_jawaban, tabel_user where tabel_jawaban.id_user=tabel_user.id_user');
return $data;
}
public function AmbilNilai2($iduser) {
$data = $this->db->query("select * from tabel_jawaban, tabel_user where tabel_jawaban.id_user=tabel_user.id_user AND tabel_jawaban.id_user='$iduser'");
return $data;
}
public function Simpan($tabel, $data){
$res = $this->db->insert($tabel, $data);
return $res;
}
public function UpdateTotalNilai($id_jawaban,$data)
{
$this->db->where('id_jawaban',$id_jawaban);
$this->db->update('tabel_jawaban',$data);
}
}
Controller :
function daftar(){
$ambil_akun = $this->session->userdata('uname');
$idUser = $this->user_model->AmbilIdUser($ambil_akun);
$data = array(
'id_user' => $idUser,
'tgl_tes' => date("Y-m-d"),
);
$this->jawaban_model->Simpan('tabel_jawaban', $data);
$id=mysql_insert_id();
redirect('dashboard/soaluser/'.$id);
}
There is minor typo in your code. As far as I could notice, your user_model-> method is
function ambil_iduser($idUser)
{
$query = $this->db->get_where($this->tbl, array('id_user' => $idUser));
$query = $query->result_array();
if($query){
return $query[0];
}
Whereas, in your controller, you are calling for different method here,
$idUser = $this->user_model->AmbilIdUser($ambil_akun);
Both the method's name differs ambil_iduser and AmbilIdUser.
The correct controller calling state should be
function daftar(){
$ambil_akun = $this->session->userdata('uname');
$idUser = $this->user_model->ambil_iduser($ambil_akun);
$data = array(
'id_user' => $idUser,
'tgl_tes' => date("Y-m-d"),
...

Fail to update the fields in the database in codeigniter

I'm trying to create the function to update user details. but the database transaction in the model fails. It just pops out failure message without giving any db errors or anything.
Is there a way to view db logs for debugging in model like var_dump in controller
controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class edit_account extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->database();
$this->load->library('form_validation');
$this->load->model('medit_account');
}
function index() {
}
function edit_dept_officer() {
$user_id = $this->session->userdata('user_id');
// retrieve data from db to edit from
$table = 'department_officer';
$user_data = $this->medit_account->get_current_user($table, $user_id);
var_dump($user_data);
$userid = $user_data[0]->OfficerID;
$data = array(
'officer_no' => $user_data[0]->officer_no,
'first_name' => $user_data[0]->first_name,
'last_name' => $user_data[0]->last_name,
'contact_no' => $user_data[0]->contact_no,
);
var_dump($userid);
//validation rules
// $this->form_validation->set_rules('officer_no', 'Officer ID ', 'trim|required');
$this->form_validation->set_rules('first_name', 'First Name ', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name ', 'trim|required');
$this->form_validation->set_rules('contact_no', 'Contact Number', 'trim|required|numeric');
var_dump($data);
if ($this->form_validation->run() == FALSE) {
//fail validation
// $this->load->view('header');
$this->load->view('edit_account_dept_view', $data);
$this->load->view('footer');
// var_dump('point');
} else {
// pass validation
$officer_no = $user_data[0]->officer_no;
$first_name = $this->input->post('first_name', TRUE);
$last_name = $this->input->post('last_name' , TRUE);
$contact_no = $this->input->post('contact_no', TRUE);
$array1 = array(
'officer_no' => $officer_no,
'first_name' => $first_name,
'last_name' => $last_name,
'contact_no' => $contact_no,
);
var_dump($array1);
echo 'braekpoint';
$tableid = 'OfficerID';
var_dump($userid);
var_dump($tableid);
var_dump($table);
if($this->medit_account->update_user( $userid, $array1)) {
// user creation ok
echo 'success';
$this->session->set_flashdata('msg', '<div class="alert alert-success text-center">Department Officer is created !!!</div>');
// $this->load->view('header');
$this->load->view('edit_account_dept_view', $data);
$this->load->view('footer');
// redirect('edit_account');
} else {
// user creation failed
$this->session->set_flashdata('msg', '<div class="alert alert-warning text-center">There was a problem creating the Department Officer. Please try again.!!!</div>');
echo 'fail';
// send error to the view
$this->load->view('header');
$this->load->view('edit_account_dept_view', $data);
$this->load->view('footer');
}
}
}
model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class medit_account extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
public function get_current_user($table,$user_id) {
$this->db->select('*');
$this->db->from($table);
$this->db->where('LoginId', $user_id);
$query = $this->db->get();
$user_data = $query->result();
return $user_data;
}
public function update_user( $userid, $array1) {
$this->db->trans_start();
$this->db->where('OfficerID', $userid);
$this->db->update('department_officer', $array1);
return ;
}
}
?>

Codeiginter Session Issue

i have a problem in a session
ihave made a login page called"alogin"
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
class alogin extends CI_Controller{
public function __construct() {
parent::__construct();
$this->load->model('user_model');
$this->load->model('admin_model');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('articles_model');
$this->load->helper(array('form', 'url'));
}
function index(){
$this->form_validation->set_rules('username',' اسم المستخدم','trim|required|xxs_clean');
$this->form_validation->set_rules('password','كلمة المرور','trim|required|xss_clean');
$this->form_validation->run();
//post value
$data['username'] = $this->input->post('username');
$data['password'] = $this->input->post('password');
if($this->input->post('login')){
if($this->user_model->login($data)){
$this->setsession();
redirect('admin/index');
} else {
redirect('admin');
}
}
$this->load->view('admin/login');
}
public function setsession(){
$dat = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'loggedIn' => TRUE
);
$this->session->set_userdata($dat);
}
public function logout(){
if($this->session->sess_destroy()){
redirect('admin/alogin');
} else {
// redirect('admin/log/index');
}
}
}
and make the admin pages in controller file called"admin"
and this is the code
<?php
class admin extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->model('user_model');
$this->load->model('admin_model');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('articles_model');
$this->load->helper(array('form', 'url'));
if(!$this->session->userdata('loggedIn')){
redirect('alogin');
}
}
public function index(){
$data['count'] = $this->admin_model->count_message();
$data['messages'] = $this->admin_model->show_message();
$data['title'] = ' لوحة التحكم';
$data['subview'] = 'admin/main';
$this->load->view('admin/index',$data);
}
public function setting(){
$data['settings']= $this->admin_model->settings();
$data['title'] = ' تعديل اعدادات الموقع';
$data['subview'] = 'admin/setting';
$this->load->view('admin/index',$data);
}
public function set_update(){
$data = array(
'site_name' => $this->input->post('site_name'),
'site_desc'=> $this->input->post('site_desc')
);
$update = $this->admin_model->set_update($data);
if(isset($update)){
redirect('admin/setting');
}
}
function message(){
$data['messages'] = $this->admin_model->show_message();
$data['title'] = 'الرسائل';
$data['subview'] = 'admin/message';
$this->load->view('admin/index',$data);
}
/// articles
public function add_article(){
//
// do upload
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 3000;
$config['max_width'] = 1024;
$config['max_height'] = 1000;
$this->load->library('upload', $config);
if(!$this->upload->do_upload('file')){
$data['error'] = $this->upload->display_errors();
} else {
$data['img_data'] = $this->upload->data();
$img = $this->upload->data();
}
$articels = array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content'),
'date' => date("Y-m-d H:i:s") ,
'img' => #$img['full_path'],
);
if($this->input->post('add')){
//form validation
$this->form_validation->set_rules('title','title','required');
$this->form_validation->set_rules('author','title','required');
$this->form_validation->set_rules('content','title','required');
$this->form_validation->set_rules('img','title','required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('admin/add_c');
}
else
{
$this->load->view('admin');
}
//form validation
if($this->articles_model->add_article($articels)){
$data['message'] = 'تم اضافة الخبر بنجاح';
}
} else {
echo 'problem';
}
$data['title'] = 'اضافة خبر';
$data['subview'] = 'admin/add_c';
$this->load->view('admin/index',$data);
}
public function articles(){
$data['articels'] = $this->articles_model->get_articles();
$data['title'] = 'عرض المقالات';
$data['subview'] = 'admin/articles';
$this->load->view('admin/index',$data);
}
function delete_articels($id){
$id = $this->uri->segment(4);
if( $this->articles_model->delete_articles($id)){
redirect('admin/articles');
}
}
function edit_articels(){
$id = $this->uri->segment(3);
$data['articels'] = $this->articles_model->get_article_id($id);
$artc = array(
'title' => $this->input->post('title'),
'author' => $this->input->post('author'),
'content' => $this->input->post('content'),
'img' => $this->input->post('img')
);
if($this->input->post('update')){
if($this->articles_model->edit_c($id,$artc)){
echo 'تم تعديل المقال بنجاح';
} else {
echo 'مشكلة فى تعديل البيانات';
}
}
$data['title'] = 'تعديل المقال';
$data['subview'] = 'edit_c';
$this->load->view('admin/index',$data);
}
public function stat(){
$data['stats']= $this->admin_model->get_static();
$data['subview'] = 'admin/stat';
$data['title'] = 'تعديل احصائيات العيادة';
$this->load->view('admin/index',$data);
}
// pat
function add_pat(){
$pats = array(
'pat_name' => $this->input->post('pat_name'),
'pat_pat' => $this->input->post('pat_pat'),
'pat_content'=> $this->input->post('pat_content')
);
if($this->input->post('add')){
$this->admin_model->add_pat($pats);
}
$data['subview'] = 'admin/add_pat';
$data['title'] ='اضافة حالة جديدة';
$this->load->view('admin/index',$data);
}
function show_pats(){
$data['title'] = 'عرض الحالات ';
$data['pats'] = $this->admin_model->show_pat();
$data['subview'] = 'admin/show_pats';
$this->load->view('admin/index',$data);
}
function delete_pat(){
$id = $this->uri->segment(3);
if($this->admin_model->delete_pat($id)){
redirect('admin/show_pats');
}
}
function edit_pat(){
$id = $this->uri->segment(3);
$pat = array(
'pat_name' => $this->input->post('pat_name'),
'pat_pat' => $this->input->post('pat_pat'),
'pat_content' => $this->input->post('pat_content')
);
if($this->input->post('update')){
if($this->admin_model->edit_pat($id,$pat)){
redirect('admin/show_pats');
echo 'done';
} else {
redirect('home');
}
}
$data['title'] = 'تعديل';
$data['pats'] = $this->admin_model->show_pat_id($id);
$data['subview'] = 'admin/edit_pat';
$this->load->view('admin/index',$data);
}
/// videos
function show_videos(){
$data['videos']= $this->admin_model->get_videos();
$data['title'] = 'عرض الفديوهات';
$data['subview'] ='admin/show_videos';
$this->load->view('admin/index',$data);
}
function add_video(){
$add = array(
'video_title' => $this->input->post('video_title'),
'video_url' => $this->input->post('video_url')
);
if($this->input->post('add')){
$this->admin_model->add_video($add);
redirect('admin/show_videos');
}
$data['title'] = 'اضف فديو جديد ';
$data['subview'] = 'admin/add_video';
$this->load->view('admin/index',$data);
}
function edit_video(){
$id = $this->uri->segment(3);
$data['videos'] = $this->admin_model->get_video_id($id);
$data['id'] = $id;
$edit = array(
'video_title' => $this->input->post('video_title'),
'video_url'=> $this->input->post('video_url')
);
if($this->input->post('update')){
$this->admin_model->edit_video($id,$edit);
redirect('admin/show_videos');
} else {
echo 'problem';
}
$data['title'] = 'تعديل الفديو';
$data['subview'] = 'admin/edit_video';
$this->load->view('admin/index',$data);
}
function delete_video(){
$id = $this->uri->segment(3);
if($this->admin_model->delete_video($id)){
redirect('admin/show_videos');
}
}
}
the page is login successfuly but if i refresh or go to any inner admin page
redirect to login page ???
Array
(
[__ci_last_regenerate] => 1442853634
[username] => nader
[password] => 01147187698
[loggedIn] => 1
)
and the problem is when i refresh the index page or enter any page redirect to alogin(login page)
Try on your admin controller
public function __construct() {
parent::__construct();
if($this->session->userdata('loggedIn') == FALSE){
redirect('alogin');
}
$this->load->model('user_model');
$this->load->model('admin_model');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('articles_model');
$this->load->helper(array('form', 'url'));
}
And change on your session check ! to FALSE and move it to the top like shown
Also try and var dump sessions just to make sure working.
echo '<pre>';
echo print_r($this->session->all_userdata());
echo '</pre>';
just check the user session in constructor of each controller
in your controller where you are saving session ..?
first of all make a method in base/parent controller to check the user is login
then call it in each controller's constructor
you can check all stored session through below given method
$this->session->all_userdata();

display data from database to dropdown CodeIgniter

I'm having difficulty with display data from the db to dropdown.
This is what I have tried:
Model.php
public function __construct()
{
parent::__construct();
}
function getAllGroups()
{
/*
$query = $this->db->get('location');
foreach ($query->result() as $row)
{
echo $row->description;
}*/
$query = $this->db->query('SELECT description FROM location');
foreach ($query->result() as $row)
{
echo $row->description;
}
//echo 'Total Results: ' . $query->num_rows();
}
Controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Delivery_controller extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('delivery_model');
}
public function index()
{
$data['title']= 'Warehouse - Delivery';
$this->load->view('include/header',$data);
$this->load->view('include/navbar',$data);
$this->load->view('delivery_view', $data);
$this->load->view('include/sidebar',$data);
$this->load->view('include/footer',$data);
$data['groups'] = $this->delivery_model->getAllGroups();
}
}
View.php
<select class="form-control">
<?php
$data = $this->delivery_model->getAllGroups();
foreach($description as $each)
{ ?><option value="<?php echo $each['description']; ?>"><?php echo $each['description']; ?></option>';
<?php }
?>
</select>
But the results appear on top of my page. It's not appearing on the dropdown list. What am I doing wrong in here? Help is pretty much appreciated. Thanks.
You should not be calling your model from your view. Instead try calling you model and setting $data['groups'] before you load your views.
Also do not echo the row results in your model unless you want it displayed on your page.
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Delivery_controller extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('delivery_model');
}
public function index()
{
$data['title']= 'Warehouse - Delivery';
$data['groups'] = $this->delivery_model->getAllGroups();
$this->load->view('include/header',$data);
$this->load->view('include/navbar',$data);
$this->load->view('delivery_view', $data);
$this->load->view('include/sidebar',$data);
$this->load->view('include/footer',$data);
}
}
Model:
public function __construct()
{
parent::__construct();
}
function getAllGroups()
{
/*
$query = $this->db->get('location');
foreach ($query->result() as $row)
{
echo $row->description;
}*/
$query = $this->db->query('SELECT description FROM location');
return $query->result();
//echo 'Total Results: ' . $query->num_rows();
}
View:
<select class="form-control">
<?php
foreach($groups as $row)
{
echo '<option value="'.$row->description.'">'.$row->description.'</option>';
}
?>
</select>
This is what you should do:
Model:
public function __construct()
{
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT description FROM location');
return $this->db->query($query)->result();
}
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Delivery_controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('delivery_model');
}
public function index()
{
$data['title']= 'Warehouse - Delivery';
$data['groups'] = $this->delivery_model->getAllGroups();
//I take here a sample view, you can put more view pages here
$this->load->view('include/header',$data);
}
}
View:
<select class="form-control">
<?php foreach($groups as $each){ ?>
<option value="<?php echo $each->description; ?>"><?php echo $each->description; ?></option>';
<?php } ?>
</select>
Codeigniter already has specialized functions that minimize the amount of html that you have to dump in your code:
Model
public function description_pulldown(){
$this->db->from('location');
$query = $this->db->get();
foreach($query->result() as $row ){
//this sets the key to equal the value so that
//the pulldown array lists the same for each
$array[$row->description] = $row->description;
}
return $array;
}
Controller
public function index(){
$data['description_list'] = $this->delivery_model->description_pulldown();
//load all of your view data
$this->load->view('delivery_view', $data);
}
View
echo form_label("Description");
echo form_dropdown('description', $description_list, set_value('description'), $description_list);
If you need to have the view pull up the previous data in the dropdown list, you can do a foreach loop to obtain the previous value of the dropdown from the database ie... $description = $item->description; and in the view, change the 'set_value('description')' to simply '$description.'
Never call a model from a view. It is doable but the again you lose the point of using an MVC in the first place.
Call the model from your controller. Get the data and pass the data in to your view.
Use like below.
public function index(){
$data['title']= 'Warehouse - Delivery';
$data['groups'] = $this->delivery_model->getAllGroups();
$this->load->view('include/header',$data);
$this->load->view('include/navbar',$data);
$this->load->view('delivery_view', $data);
$this->load->view('include/sidebar',$data);
$this->load->view('include/footer',$data);
}
In your view, simply loop around the $groups variable and echo to your dropdown.
<select class="form-control">
<?php
$i = 0;
while($i < count($groups)){
$val= $groups[$i]['value'];
$des = $groups[$i]['description'];
echo "<option value='$i'>$des</option>";
}
</select>
And your model's function should be,
function getAllGroups(){
$query = $this->db->get('location');
return $query->result_array();
}
Better I think, in your view use:
On your model get all your data in an array with:
public function get_all_description()
{
$query = $this->db->get('description');
return $query->result_array();
}
In controller:
$data['description']=$this->model->get_all_description();
In view:
for($i=0;$i<sizeof($description);$i++)
{
$description2[$description[$i]['description']]=$marque[$i]['description'];
}
echo form_dropdown('description', $description22, set_value('description'));
This is Codeigniter 4 answer.
Controller
public function index()
{
$delModel = new delivery_model();
$groups=$delModel->getAllGroups();
$data = [
'title' => 'Warehouse - Delivery',
'groups' => $groups,
];
return view('include/header',$data);
return view('include/navbar',$data);
return view('delivery_view', $data);
return view('include/sidebar',$data);
return view('include/footer',$data);
}
Model
public function getAllGroups()
{
$db = \Config\Database::connect();
$query = $db->query("SELECT description FROM location;");
return $query->getResultArray();
}
View
<select>
<?php
foreach ($groups as $row) {
echo '<option value="' . $row["description"] . '">' .$row["description"] . '</option>';
}?>
</select>
public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->model('trip_model');
}
public function index(){
$data['trips']=$this->trip_model->get_all_trips();
$this->load->view("trip_view",$data);
}
public function trip_add(){
if(!empty($_FILES['trip_image']['name'])){
$config['upload_path'] = 'assests/images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 2048;
$config['file_name'] = $_FILES['trip_image']['name'];
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('trip_image')){
$uploadData = $this->upload->data();
$trip_image = $uploadData['file_name'];
}
else{
$trip_image = 'Hello..!!';
}
}
else{
$trip_image = 'Byeee..!!';
}
$data = array(
'trip_image' => $trip_image,
'name' => $this->input->post('name'),
'location' => $this->input->post('location'),
'trip_datetime' => $this->input->post('trip_datetime')
);
$insert = $this->trip_model->trip_add($data);
echo json_encode(array("status" => TRUE));
}
function do_upload(){
$url = "assests/images/";
$image = basename($_FILES['trip_image']['name']);
$image = str_replace(' ','|',$image);
$type = explode(".",$image);
$type = $type[count($type)-1];
if (in_array($type,array('jpg','jpeg','png','gif'))){
$tmppath="assests/images/".uniqid(rand()).".".$type;
if(is_uploaded_file($_FILES["trip_image"]["tmp_name"])){
move_uploaded_file($_FILES['trip_image']['tmp_name'],$tmppath);
return $tmppath;
}
}
}
public function ajax_edit($id){
$data = $this->trip_model->get_by_id($id);
echo json_encode($data);
}
public function trip_update(){
if(!empty($_FILES['trip_image']['name'])){
$config['upload_path'] = 'assests/images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['trip_image']['name'];
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->upload_img('trip_image')){
$uploadData = $this->upload->data();
$trip_image = $uploadData['file_name'];
}
else{
$trip_image = 'Hello..!!';
}
}
else{
$trip_image = 'Byeee..!!';
}
$data = array(
'trip_image' => $trip_image,
'name' => $this->input->post('name'),
'location' => $this->input->post('location'),
'trip_datetime' => $this->input->post('trip_datetime')
);
$this->trip_model->trip_update(array('trip_id' => $this->input->post('trip_id')), $data);
echo json_encode(array("status" => TRUE));
}
public function trip_delete($id){
$this->trip_model->delete_by_id($id);
echo json_encode(array("status" => TRUE));
}
}

Categories