How to pass value to drop down list in CodeIgniter?
This is my view file HTML code:
<div class="form-group">
<select name="department" id ="department">
<?php
foreach($dept as $country)
{
echo '<option value="'.$country['dept_id'].'">'.$country['managers_name'].'</option>';
}
?>
</select>
</div>
This is my controller code:
public function department()
{
$this->load->model('insert_model');
$data['dept'] = $this->insert_model->category_name_get();
}
This is my model file code:
function category_name_get()
{
$query = $this->db->get('dept');
if ($query->num_rows >= 1)
{
foreach($query->result_array() as $row)
{
$data[$row['dept_id']]=$row['managers_name'];
}
return $data;
}
}
I think you are searching for Adding Dynamic Data to the View
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading method. Here is an example using an array:
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('blogview', $data);
In your case $data contains the department list in it as an array.
In Your COntroller file you will get all date on $data['dept'] that returned from model.
public function department()
{
$this->load->model('insert_model');
$data['dept'] = $this->insert_model->category_name_get();
$this->load->view('view_file_name',$data);
}
In your view file you will get this data
Just do print_r($dept); and check array..
you will find more info from this link
Probably your model is not returning "proper" data for your view. Perhaps something like this:
function category_name_get()
{
$data = array();
$query = $this->db->get('dept');
if ($query->num_rows >= 1)
{
foreach($query->result_array() as $row)
{
$data[] = array(
'dept_id' => $row['dept_id'],
'managers_name' => $row['managers_name']
);
}
}
return $data;
}
function category_name_get()
{
$query = $this->db->get('dept');
if ($query->num_rows() >= 1)
{
foreach($query->result_array() as $row)
{
$data[$row['dept_id']]=$row['managers_name'];
}
return $data;
}
}
Use num_rows() instead of num_rows. I hope it works now.
Related
Heres my controller :
public function create()
{
session();
$data = [
'main' => 'prosescutting/create',
'validation' => \Config\Services::validation(),
'title' => 'Form Tambah Proses Cutting',
'kodeunik' => $this->pcuttingModel->buat_kode()
];
return view('template/template', $data);
}
Model :
public function buat_kode() {
$query = $this->db->table('proses_cutting')
->orderBy('kode_packing_list', 'DESC')
->limit(1);
// $kodemax = $kode;
// $kodejadi = "PLIK059".$kodemax;
return $query;
It show me errors Object of class CodeIgniter\Database\MySQLi\Builder could not be converted to string the line that show was
<td><input type="text" name='kode_packing_list' class="form-control" value="<?= $kodeunik; ?>" readonly></td>
which is in my view/create.php
You have to set a result in CodeIgniter. Like so;
$result = $query->row_array(); // For single row as array
$result = $query->row(); // Single object
$result = $query->result_array(); // For multiple rows as array
$result = $query->result(); // For multiple rows as objects
Model:
public function buat_kode() {
$query = $this->db->table('proses_cutting')
->orderBy('kode_packing_list', 'DESC')
->limit(1);
$return = $query->row_array();
return $return;
}
Since you are working with codeigniter 4. Do this to help you solve this issue
In your controller
public function create()
{
session();
$data = [
'main' => 'prosescutting/create',
'validation' => \Config\Services::validation(),
'title' => 'Form Tambah Proses Cutting',
'kodeunik' => $this->pcuttingModel->buat_kode()
];
return view('template/template', $data);
}
Model :
public function buat_kode() {
$query = $this->db->table('proses_cutting')
->orderBy('kode_packing_list', 'DESC');
// $kodemax = $kode;
// $kodejadi = "PLIK059".$kodemax;
return $query->get()->getRow();
Then since you are assigning object data fetch from the model to the array index kodeunik, call this as object on the view file like this $kodeunik->what_column_name_you_are_calling_from_database;
That is
<td><input type="text" name='kode_packing_list' class="form-control" value="<?= $kodeunik->what_column_name_you_are_calling_from_database; ?>" readonly></td>
I hope this help you. If not call my attention. I am ready to help you
I am working on a basic blog application in Codeigniter 3.1.8 and Bootstrap 4.
I have been working for some time on this application and consequently, it has a lot of features. Most of these rely on the ids of posts, pages, etc.
For SEO purposes, I would like to use friendly URLs, instead of ids to display the individual posts, so I have added a slug column to the posts table.
The slugs are made from post titles, with this chunks of code in the Posts_model model and the Posts controller, respectively.
From the model:
public function slug_count($slug){
$this->db->select('count(*) as slugcount');
$this->db->from('posts');
$this->db->where('slug', $slug);
$query = $this->db->get();
return $query->row(0)->slugcount;
}
public function create_post($post_image, $slug) {
$data = [
'title' => $this->input->post('title'),
'slug' => $slug,
'description' => $this->input->post('desc'),
'content' => $this->input->post('body'),
'post_image' => $post_image,
'author_id' => $this->session->userdata('user_id'),
'cat_id' => $this->input->post('category'),
'created_at' => date('Y-m-d H:i:s')
];
return $this->db->insert('posts', $data);
}
From the controller:
// Create slug (from title)
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$slugcount = $this->Posts_model->slug_count($slug);
if ($slugcount > 0) {
$slug = $slug."-".$slugcount;
}
To achieve my SEO goal, I have considered and tried out the approach in this tutorial but since there are numerous functionalities needing ids (deleting posts via AJAX, for example) and other that do not require slugs (editing posts) I am unable (and unwilling) to substantially modify the post viewing method in the controller,
public function post($id) {
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['posts'] = $this->Posts_model->sidebar_posts($limit=5, $offset=5);
$data['post'] = $this->Posts_model->get_post($id);
if ($data['categories']) {
foreach ($data['categories'] as &$category) {
$category->posts_count = $this->Posts_model->count_posts_in_category($category->id);
}
}
if (!empty($data['post'])) {
// Overwrite the default tagline with the post title
$data['tagline'] = $data['post']->title;
// Get post comments
$post_id = $data['post']->id;
$data['comments'] = $this->Comments_model->get_comments($post_id);
$this->load->view('partials/header', $data);
$this->load->view('post');
} else {
$data['tagline'] = "Page not found";
$this->load->view('partials/header', $data);
$this->load->view('404');
}
$this->load->view('partials/footer');
}
and the corresponding code in the model:
public function get_post($id) {
$query = $this->db->get_where('posts', array('id' => $id));
if ($query->num_rows() > 0) {
$data = $query->row();
// run separate query for author name
$author_query = $this->db->get_where('authors', array('id' => $data->author_id));
if ($author_query->num_rows() == 1) {
$author = $author_query->row();
$data->first_name = $author->first_name;
$data->last_name = $author->last_name;
} else {
$data->first_name = 'Unknown';
$data->last_name = '';
}
return $data;
}
}
What would be a simple, less "intrusive" approach to replace post id with slug in the singe post view only?
Update in model
replace
public function get_post($id) {
$query = $this->db->get_where('posts', array('id' => $id));
with
public function get_post($slug) {
$query = $this->db->get_where('posts', array('slug' => $slug));
Update in Controller
replace
public function post($id) {
with
public function post($slug) {
and replace
$data['post'] = $this->Posts_model->get_post($id);
with
$data['post'] = $this->Posts_model->get_post($slug);
Also update your routes for better user friendly url.
//If you categorize your blog
$route['blog-category/(:any)'] = 'blogController/blogFunction/$1';
//this will accept any uri
$route['(:any)/(:any)'] = 'blogController/blogFunction'
Well, I think you can have both the id and the slug on the same function/url eg /id/slug. This way, you always have your id and seo is happy too because the url has the page title(slug). Then on your function, you can have public function post($id,$slug="") {.
I am just new to Codeigniter. I want to display the total sum of bill_rpt of this SQL query in the view page. here's the code
this is my Model
public function total() {
$sql="SELECT bill_rpt
FROM amilyar_bill
WHERE bill_status = 1";
return $this->db->query($sql);
}
this is my Controller
public function payments($token=''){
$this->isLoggedIn();
$data = array(
// get data using email
'token' => $token,
'admin_info' => $this->model->getAdminInfo($this->session->userdata('email'))->row(),
'notifications' => $this->model->notification_admin($this->session->userdata('email'))->result_array(),
'notification' => $this->model->all_notification_admin($this->session->userdata('email'))->result_array(),
'total' => $this->model->total($this->session->userdata('email'))->result_array(),
);
if ($this->session->userdata('position_id') == 2) {
$this->load->view('includes/admin_header',$data);
$this->load->view('admin/reports/payments',$data);
} else {
$this->logout();
}
}
this is my View
<h4 class="pull-right">Total:</h4>
Thanks in advance
Hope this will help you :
Use ci select_sum() to get the sum
Your total method should be like this :
public function total()
{
$this->db->select_sum('bill_rpt');
$this->db->where('bill_status' , '1');
$query = $this->db->get('amilyar_bill');
return $query->row()->bill_rpt;
}
Your $data part in controller payments method should be like this :
$data = array(
'token' => $token,
'admin_info' => $this->model->getAdminInfo($this->session->userdata('email'))->row(),
'notifications' => $this->model->notification_admin($this->session->userdata('email'))->result_array(),
'notification' => $this->model->all_notification_admin($this->session->userdata('email'))->result_array(),
'total' => $this->model->total()
);
In payments view part use $total like this :
<h4 class="pull-right">Total: <?=$total;?></h4>
You can use num_rows() method of Active Class.
$query->num_rows();
public function total(){
$sql="SELECT bill_rpt FROM amilyar_bill WHERE bill_status = 1";
$query = $this->db->query($sql);
return $query->num_rows();
}
And in View,
<h4 class="pull-right">Total: <?php echo $total;?></h4>
Try SUM function of MySql as:
In Model
public function total(){
$sql="SELECT SUM(bill_rpt ) as total_sum
FROM amilyar_bill
WHERE bill_status = 1";
return $this->db->query($sql);
}
I am new in Zend Framework. I am trying to display data from database using JSON. And I encoded the data and passed it to JQuery. But cannot retrieve value from database. Data displayed as "undefined". My controller function is as follows:
public function displayAction()
{
$data1 = array();
$request = $this->getRequest();
$response = $this->getResponse();
if ($request->isPost()) {
$response->setContent(\Zend\Json\Json::encode(array('data' => $this-> getStickyNotesTable() -> fetchAll())));
}
return $response;
}
My FetchAll() is:
public function fetchAll() {
$resultSet = $this->select(function (Select $select) {
$select->order('created ASC');
});
$entities = array();
foreach ($resultSet as $row) {
$entity = new Entity\StickyNote();
$entity->setId($row->id)
->setNote($row->note)
->setCreated($row->created);
$entities[] = $entity;
}
return $entities;
}
JQuery function :
function getUserList(element) {
$('#indicator').show();
$.post('stickynotes/display',
function(data, textStatus) {
renderUserList(data);
$('#indicator').hide();
},
"json"
);
}
function renderUserList(jsonData) {
var table = '<table width="600" cellpadding="5" class="table table-hover table-bordered"><thead><tr><th scope="col">Note</th></tr></thead><tbody>';
$.each(jsonData, function(index, data){
table += '<tr>';
table += '<td class="edit" field="note" user_id="'+data.id+'">'+data.note+'</td>';
table += '<td><i class="icon-remove icon-white"></i></td>';
table += '</tr>';
});
table += '</tbody></table>';
$('div#content').html(table);
}
I tested it using Firebug. It shows
{"data":[{},{},{},{},{},{},{},{},{},{},{},{},{}]}
as Response.
Anyone please help me. Thanks.
The issue is with your fetchAll method. Try with this updated version:
public function fetchAll() {
$resultSet = $this->select(function (Select $select) {
$select->order('created ASC');
});
$entities = array();
foreach ($resultSet as $row) {
$entity = array(
"id" => $row->id,
"note" => $row->note,
"created" => $row->created
);
$entities[] = $entity;
}
return $entities;
}
You'll need to configure your module.config.php and add a strategy within your template_map add.
'strategies' => array(
'ViewJsonStrategy',
),
to return a jsonModel.
If you want to work with a jsonModel within your controller you'll need to call it like so:
$json = new JsonModel(array(
'param' => 'foobar',
'success' => true,
));
return $json;
I have created a Facebook App that i need people to only enter their data to once.
It's all working and the database is being populated, but i need to make sure people don't keep coming back and re-entering their data endlessly.
What's the best way to check if the user has already submitted their data ?
The signed_request could still have been submitted and their data not entered so i need the check for both to work.
Ideally the PHP would just check for FB ID + other data, and only display a confirmation / thankyou page.
Currently my php to send to the database is:
class Users_Model extends CI_Model {
protected $_name = 'users';
function add($id,$key,$value) {
$data = array(
'id' => $id,
'name' => $key,
'value' => $value
);
return $this->db->insert($this->_name, $data);
}
function update($id,$key,$value) {
$data = array(
'value' => $value
);
$this->db->where(array(
'id' => $id,
'name' => $key
));
return $this->db->update($this->_name, $data);
}
function exists($id,$key=null) {
if($key == null) {
$this->db->where(array(
'id' => $id
));
} else {
$this->db->where(array(
'id' => $id,
'name' => $key
));
}
$query = $this->db->get($this->_name);
if($query->num_rows() > 0) {
return true;
}
return false;
}
function remove($id) {
$data = array(
'id' => $id,
);
return $this->db->delete($this->_name, $data);
}
function all() {
$query = $this->db->get($this->_name);
$results = array();
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
$results[]=$row;
}
}
return $results;
}
}
Any help much appreciated...
What's the best way to check if the user has already submitted their data ?
Check if you already have a record for the user’s Facebook id in your database.