How do I make a view showing selected id? - php

I know how to show all data from db using foreach but do not know how to show only 1 record that I select.
My code:
public function go($id)//controller
{
$data["log"] = $this->product_model->getAll();
$data['id']=$id;
$this->load->view("admin/kampus/list1",$data);
}
public function getById($id)//modal
{
return $this->db->get_where($this->_table, ["ID" => $id])->row();
}
//view
<?php foreach ($log as $product): ?>
<tr>
<td>
<?php echo $product->ID ?>
</td>
<td>
<?php echo $product->TGL_MULAI ?>
</td>
<td>
<?php echo $product->KETERANGAN ?>
</td>
</tr>
<?php endforeach; ?>
Please help me to show one record using foreach.

All you have to do is remove the foreach.
Or, change row() to result().

You should remove foreach and do this
return $this->db->get_where($this->_table, ["ID" => $id])->row_Array();
than you can use this
<tr>
<td>
<?php echo $product['id'] ?>
</td>
<td>
<?php echo $product->['tg_mulai'] ?>
</td>
<td>
<?php echo $product->['keterangan'] ?>
</td>
</tr>
you can use foreach() for only multiple rows

foreach operate on arrays and to show single record you should send it to view as array of objects. To do this just replace $data["log"] with $data["log"][] when you get single record from db. Check below code
public function go($id)//controller
{
$data["log"][] = $this->product_model->getById($id);
$data['id']=$id;
$this->load->view("admin/kampus/list1",$data);
}
public function getById($id)//modal
{
return $this->db->get_where($this->_table, ["ID" => $id])->row();
}
//view
<?php foreach ($log as $product): ?>
<tr>
<td>
<?php echo $product->ID ?>
</td>
<td>
<?php echo $product->TGL_MULAI ?>
</td>
<td>
<?php echo $product->KETERANGAN ?>
</td>
</tr>

First view, where you click to see selected data
<?php foreach ($log as $product): ?>
<a href="<?= base_url('controller/function/').$product->id; ?>"
class="btn btn-info">Edit</a>
<?php endforeach; ?>
Then Controller will receive the id like
public function edit_product($id){
$data['datas'] = $this->your_model->model_function($id);
$this->load->view('your_view', $data);
Model
public function model_function($id){
$this->db->select('*');
$this->db->where('id', $id);
$this->db->from('table_name');
$query = $this->db->get();
return $query->result();
}
Single Data View
<?php foreach($datas as $data): ?>
<?php echo $data->id; ?>
<?php echo $data->name; ?>
<?php endforeach; ?>
Let me know if it's ok

Related

Unable to insert checked value into database

Currently I have a permissions table where the user can use a checkbox to give access or revert access for a URL link to a user with a particular role. So for this I've made it so that when a checkbox is pressed, the id of that checkbox and the role of the user are inserted in my database, but I'm unable to insert it in the database for some reason. I've tried the following code:
Controller Class:
public function permission()
{
if ($this->form_validation->run() == FALSE)
{
$main['permissions']=$this->users_model->get_permission_array();
$main['roles']=$this->users_model->get_roles_array();
foreach($main['roles'] as $key => $val):
$main['access'][$val['roles_id']]=$this->users_model->get_access_array(array('roles_id'=>$val['roles_id']));
endforeach;
$main['page'] = 'crm/users/permission';
$this->load->view('crm/index', $main);
}
if($this->input->post())
{
$loginid=false;
foreach($main['roles'] as $key => $val):
if(isset($_POST['roleid'.$val['roles_id']])){
$this->users_model->clear_access(array('roles_id'=>$val['roles_id']));
foreach($_POST['roleid'.$val['roles_id']] as $id => $access):
$data=array('roles_id'=>$val['roles_id'],'permissions_id'=>$access);
$loginid=$this->users_model->permission_access($data);
endforeach;
}
endforeach;
if($loginid){
$this->session->set_flashdata('message', '<p>Permission updated Successfully.</p>');
redirect('users/permission');
} else {
$this->session->set_flashdata('message', '<p>Error!! - Permission not updated.</p>');
redirect('users/permission');
}
}
}
Model Class:
function get_permission_array()
{
$query = $this->db->get("crm_client_permissions");
return $query->result_array();
}
function get_access_array($cond)
{
$this->db->select("permissions_id");
$this->db->where($cond);
$query = $this->db->get("crm_client_role_access");
return $query->result_array();
}
function clear_access($cond)
{
return $this->db->delete("crm_clients_access",$cond);
}
function permission_access($data)
{
return $this->db->insert("crm_clients_access",$data);
}
function get_roles_array($cond='')
{
if($cond !='') $this->db->where($cond);
$query = $this->db->get("crm_client_roles");
return $query->result_array();
}
View Class:
<div <?php echo form_open_multipart('users/permission'); ?>>
<table>
<?php if($permissions) $i=0;foreach($permissions as $key => $permission): ?>
<tr>
<td class="align-center"><?php echo ++$i; ?></td>
<td><?php echo $permission['page']; ?></td>
<td><?php echo $permission['url']; ?></td>
<?php foreach($roles as $rolekey => $role):
if($role['roles_id'] == 1)$checked = 'checked';
if(in_array($permission['permissions_id'],array_map('current',$access[$role['roles_id']])))
$checked = 'checked';
else
$checked = ''; ?>
<td align="center"><div class="checkbox checkbox-success m-t-0"><input type="checkbox" class="accessbox"
id="role<?php echo $rolekey ?>-<?php echo $key ?>" name="roleid<?php echo $role['roles_id']; ?>[]"
<?php echo $checked?> <?php echo ($role['roles_id'] == 1) ? 'disabled="disabled"' : '' ?> value="<?php echo $permission['permissions_id']; ?>" />
<label for="role<?php echo $rolekey ?>-<?php echo $key ?>"></label></div></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
<div class="text-center">
<button type="submit" class="btn btn-info">Save Permission</button> Cancel
</div>
<?php echo form_close(); ?> </div>
But here I always get Error!! - Permission not updated. in my view class which means it jumps to the else part of my controller. I'm not sure where I'm going wrong here
It seems problem is here $loginid=$this->users_model->permission_access($data); and here return $this->db->insert("crm_clients_access",$data);.
My advise is to:
use XDebug to debug your code and see what insert function returns and why.
Also you may look for php log to see if there is any database errors. To use error logs you need to find php.ini config file and enable line 'error_log = /path/to/fige.log'.
Also you may to lookup the database data to determine if insert function makes new row in table crm_clients_access, if it doesn't? you need to check your connection config to database host:port, so database process must be available at theese host:port.

Trying to make a select list for product category upload in CodeIgniter

I'm making a website so users can add products to the website. On the form where users can add a certain product I'm trying to make a select dropdown bar so users can select a certain category that belongs to a product but I'm not sure how I can do that..
Database info:
category table: categories
Rows in the category table:
1. id
2. name
In my products table I also have a row called: category_id
This is my db helper file (db_helper.php) :
<?php if (!function_exists('get_categories_h')) {
function get_categories_h(){
$CI = get_instance();
$categories = $CI->Product_model->get_categories();
return $categories;
} } ?>
This is the my Product_model file where I made the get_categories function:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product_model extends CI_model {
public function saveProduct($data) {
$this->db->insert('products', $data);
$product_id = $this->db->insert_id();
return $product_id;
}
public function get_product_details($product_id) {
$arrReturn = array();
$this->db->select('*');
$this->db->from('products');
$this->db->where('product_id', $product_id);
$query = $this->db->get();
$result = $query->result_array();
if (!empty($result)) {
$arrReturn = $result[0];
}
return $arrReturn;
}
/*
Get categories
*/
public function get_categories(){
$this->db->select('*');
$this->db->from('categories');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
}
?>
This is my view form where I'm trying to make the option select bar of the categories:
<?php echo form_open_multipart('Product/upload'); ?>
<table class="aanbieding-cadeau">
<tr>
<td>
<?php echo form_input(array('id'=>'product_naam', 'name'=>'product_naam', 'placeholder' => '1. Naam van het cadeau', 'size'=>25));?>
</td>
</tr>
<tr>
<?php foreach (get_categories_h() as $category) : ?>
<select name="category">
<?php echo $category['name']; ?>
</select>
<?php endforeach; ?>
</tr>
<tr>
<td>
<?php echo form_input(array('id'=>'ophaal_plaats', 'name'=>'ophaal_plaats', 'placeholder' => '3.Kies een stad', 'size'=>25));?>
</td>
</tr>
<div class="checkbox">
<label><input type="checkbox" value="">Gebruik adres van mijn account</label>
</div>
<tr>
<td>
<h4>Upload foto</h4>
<input type="file" name="userfile" />
</td>
</tr>
<tr>
<td>
<?php echo form_textarea(array('type'=>'textarea','id'=>'product_beschrijving', 'name'=>'product_beschrijving', 'placeholder' => '5. Vertel iets over dit cadeau..', 'size'=>25));?>
</td>
</tr>
<tr>
<td>
<input type="submit" class="btn btn-primary" name="submit" value="Cadeau aanbieden!" />
</td>
</tr>
</table>
</form>
When I load the view form I don't see one dropdown category option menu but I see 10 small selectbars that are empty when you click on them. ( PS: I have inserted 10 categories to my database)
I hope someone can help me
The issue is here:
<?php foreach (get_categories_h() as $category) : ?>
<select name="category">
<a href="#"><?php echo $category['name']; ?>
</a>
</select>
<?php endforeach; ?>
you have put the select tag inside the loop, and a tag is not the part of select. Only put the option inside the loop and exclude the select from loop.
Change the code to:
<select name="category">
<?php foreach (get_categories_h() as $category) : ?>
<option><?php echo $category['name']; ?></option>
<?php endforeach; ?>
</select>
Change your select tag code to :
<tr>
<select name="category">
<?php foreach (get_categories_h() as $category) : ?>
<option ><?php echo $category['name']; ?></option>
<?php endforeach; ?>
</select>
</tr>
Note:
1)Select tag must be out side of loop.
2) use option with select tag not anchor**

codeigniter inside multiple checkbox

I have small problem with my PHP code. I must insert to database multiple values but I dont have any idea how I can do this.
My View:
<?php foreach($myKidsGroupID as $row): ?>
<label><input type="checkbox" name="user_my_group_msg" value="<?php echo $row->id; ?>" class="my_group_msg pull-right"><?php echo $row->firstname; ?> <?php echo $row->lastname; ?></label><br>
<?php endforeach; ?>
And my Model looks like this:
...
elseif($checked_my_group == 1) {
foreach ( ?? ) {
$new_mail = array(
'to_user' => $this->input->post('user_my_group_msg'),
);
$this->db->insert('mailbox', $new_mail);
}
}
...rest code....
Inside my View I display all users as checkbox but If I select two persons I must INSERT two query to database. Anyone can help me?
simple thing is
use serialize function don't use foreach
serialize $this->input->post('user_my_group_msg')
try it once it will help you your View code will look as like
<?php foreach($myKidsGroupID as $row): ?>
<label><input type="checkbox" name="user_my_group_msg" value="<?php echo $row->id; ?>" class="my_group_msg pull-right"><?php echo $row->firstname; ?> <?php echo $row->lastname; ?></label><br>
<?php endforeach; ?>
Model will be corrected like this
<?php
elseif(sizeof($this->input->post('user_my_group_msg'))>=1) {
foreach ( $this->input->post('user_my_group_msg') as $value ) {
$new_mail = array(
'to_user' => $value,
);
$this->db->insert('mailbox', $new_mail);
}
}
.....rest code
?>

Message: Undefined variable: username Filename: views/receiver_view.php Line Number: 58

Brothers your help needed.
i am trying to develop a chat application in which i am displaying friends at the right top but the following error occurs. i have tried again and again but cant solve the problem.
my code is:
function is:
function get_sessionId() {
$session_id = $this->session->userdata('logged_in');
$data['usernames'] = $this->chat_model->show_user($session_id['id']);
$this->load->view('chat_view',$data);
}
foreach loop is:
<table>
<?php foreach($users as $row): ?>
<tr>
<td>
<a href="javascript:void(0);" onclick="set_user(<?php echo $row->id; ?>)">
<?php echo $row->username;?>
</a>
</td>
</tr>
<?php endforeach; ?>
</table>
and the query that i have write in model is:
function show_user($sessionId) {
$query = $this->db->query("select id, username from users where id <>'$sessionId'");
return $query->result();
}
In your Controller you have assigned the users to:
$data['usernames'] = $this->chat_model->show_user($session_id['id']);
$this->load->view('chat_view', $data);
So in View Section it should be like this:
<?php foreach($usernames as $row): ?>
not
<?php foreach($users as $row): ?>

Kohana CRUD: Undefined Variable

PHP Function to select and display all the records stored under the column "clubName", from the table 'clubs'. The foreach statement ($club) returns an undefined variable error. What have I missed?
Controller:
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Club_Controller extends Template_Controller {
public $template = 'kohana/template';
public function index()
{
$this->template->title = 'All clubs';
$this->template->content = new View('allclubs');
$clubs = ORM::factory('club')->find_all();
$this->template->content->club = $clubs;
}
}
?>
View:
<?php defined('SYSPATH') OR die('No direct access allowed.'); ?>
<div class="box">
<b><?php echo html::anchor('entry/form', 'add entry') ?></b><br>
<table cellpadding="10">
<?php foreach($clubs as $club): ?>
<tr>
<td align="left">
<?php echo html::anchor('entry/form/'.$club->id, 'edit') ?>
<?php echo html::anchor('entry/delete/'.$club->id, 'delete',
array('onclick'=>'return confirm("Are you realy realy want to do it?")')) ?>
</td>
<td align="left">
<?php echo $club->clubName ?>
</td>
</tr>
<?php endforeach ?>
</table>
</div>
Change this
$this->template->content->club = $clubs;
to this
$this->template->content->clubs = $clubs;

Categories