Codeigniter view shows 0 record - php

i've insert 5 rows into ms_kategori_material table, and made some model here
function read()
{
$query = $this->db->get('ms_kategori_material');
if($query->num_rows()>0)
{
foreach ($query->result_array() as $value) {
echo $value['Kode_Kategori_Material_Jasa'];
echo $value['Nama_Material_Jasa'];
}
return $value;
}
else
{
return null;
}
}
and controllers
function index()
{
$data['kirim'] = $this->m_kategorimaterial->read();
echo $data; //returns KKMJ001batuKKMJ002batuKKMJ003batuKKMJ004batuKKMJ005batuArray
$this->load->view('v/vkategorimaterial',$data);
}
and the views
<?php
if ( !empty($kirim) ) {
$no = 1;
foreach ($kirim as $row) { ?>
<tr id="row">
<td id="no"><?php echo $no;?></td>
<td id="judul"><?php echo $row->Kode_Kategori_Material_Jasa;?></td>
<td id="kategori"><?php echo $row->Nama_Material_Jasa;?></td>
</tr> ?>
<?php
$no++;
}
} else { ?>
<tr id="row">
<td colspan="6" align="center">Data tidak ditemukan</td>
//the screen shows 'data tidak ditemukan' </tr>
<?php
}
?>
this is so confusing, since i can see the data in the controller, then pass them into the views, views said no data received

In your model
if($query->num_rows()>0)
{
foreach ($query->result_array() as $value) {
echo $value['Kode_Kategori_Material_Jasa'];
echo $value['Nama_Material_Jasa'];
}
return $value;
}
it's wrong, it should be
if($query->num_rows()>0)
{
return $query->result();
}
and in your controller you should have
function index()
{
$this->load->model('m_kategorimaterial'); // load it if it's not autoloaded
$data['kirim'] = $this->m_kategorimaterial->read();
$this->load->view('v/vkategorimaterial',$data);
}

Just reread your code. You're returning $value but never populating it. See below.
{
$query = $this->db->get('ms_kategori_material');
if($query->num_rows()>0)
{
$value = $query->result(); // or result_array() but you're using it as an object in the view not an array.
return $value;
}
else
{
return null;
}
}

You are actually not returning the results in your model,
replace
foreach ($query->result_array() as $value)
{
echo $value['Kode_Kategori_Material_Jasa'];
echo $value['Nama_Material_Jasa'];
}
with
if($query->num_rows()>0)
{
return $query->result();
}

Related

Update field using foreach loop in codeigniter not working

I have an issue I´m trying to update a field from a column using codeigniter , I display results from database using a foreach loop then I want to add a button called "reactivate" to change the status of that column, the PROBLEM is that it updates all the fields I just want to update the row I´m clicking in but it changes the 'Status' of all the other accounts, I don´t know how to solve this issue. Thanks in advance.
This is my view:
div class='container'>
<h5>Histórico</h5>
<table id='table' class='table'>
<thead>
<tr>
<th>INTERNO</th>
<th>CLABE</th>
<th>FECHA_ALTA</th>
<th>FECHA_BAJA</th>
<th>STATUS</th>
<th>ACCIÓN</th>
</thead>
<tbody>
<?php
foreach ($mos-> result() as $row) {
echo "<tr>";
echo "<td>".$row->Interno;"</td>";
echo "<td>".$row->Clabe;"</td>";
echo "<td>".$row->Fecha_alta;"</td>";
echo "<td>".$row->Fecha_baja;"</td>";
echo "<td>".$row->Status;"</td>";
echo "<td><input type='submit' id='ReActivar' name='ReActivar' class='btn btn-success' value='ReActivar' onclick='return validacion();' disabled><span id='msg'></span></td>";
"</tr>";
}
?>
</tbody>
</table>
</div>
This is my controller:
function decision(){
if (!empty($_POST['actualizar'])) {
$this->validaclabe();
}
if (!empty($_POST['Desactivar'])) {
$this->update_Status();
}
if (!empty($_POST['ReActivar'])) {
$this->update_Status_again();
}
}
function update_Status_again()
{
$this->load->helper('date');
date_default_timezone_set('America/Mexico_City');
$now = date('Y-m-d H:i:s');
$Interno = $this->input->post('Interno');
$Status = $this->input->post('activo');
$data = array(
'Status' => ! empty($Status) ? $Status : 'A',
'Fecha_alta' => $now,
);
//$data['status'] = ! empty($Status) ? $Status : 'I';
if ( ! empty($Interno))
{
$updated = $this->consultas_M->update_Status($Interno, $data);
if($updated)
{
//echo " update successful...";
$this->logueado();
//redirect('index.php/Datos/search2');
}
else
{ echo "update not successful...";}
} else
{ echo "Interno not found ..."; }
}
This is the model:
function muestra_inactivos($Interno)
{ $Status = 'A';
//$Interno = $this->input->get('Interno');
$this->db->select('Interno, Clabe, Fecha_alta, Fecha_baja, Status');
$this->db->where('Status !=', $Status);
$this->db->where('Interno =', $Interno);
$q= $this->db ->get('cuentas');
if($q -> num_rows() >= 0){
return $q;
}else{
return false;
}
}
function update_Status_again($Interno,$data)
{
$this->db->where('Interno', $Interno);
return $this->db->update('cuentas', $data);
if ( $this->db->affected_rows() > 0 )
{
return TRUE;
}
else
{
return FALSE;
}
}
This is also a picture I also don´t know why one button is disabled and the other is not.

why not load view when i call a function? codeigniter

I have a problem in codeigniter.
I call a funtion in controller Articles from the view.
<li><a href="index.php/articles/Category/1" ><?php echo $item['CATEGORY'] ?></a></li>
The function
function getCategories() {
$this->db->select('ID_CAT,CATEGORY');
$this->db->from('CATEGORY');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return FALSE;
}
}
function Category($id_cat) {
if (!$id_cat) {
$id_cat = 0;
}
$this->load->model('graf/home_model');
$data = array();
$data['categories'] = $this->home_model->getCategories();
$filtro_cat = $id_cat;
$data['article'] = $this->home_model->getArticles($filtro_cat);
$this->load->view('graf/articles', $data, TRUE);
}
home_model
function getArticles($filtro_cat) {
$this->db->select('*');
$this->db->from('ARTICLES');
if ($filtro_cat != 0) {
$this->db->where("ID_CAT=".$filtro_cat);
}
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return FALSE;
}
}
When i select the button the url changes to http://localhost/Graf/index.php/articles/Category/1 but does not load
http://localhost/Graf/index.php/articles/
Why?
When you set third argument of load->view() to true it returns string value and does not output anything
Try changing:
$this->load->view('graf/articles', $data, TRUE);
to
$this->load->view('graf/articles', $data);
set the routes.php in folder config, add this code;
$route['articles/Category/(.*)'] = 'articles/$1';
and change the link
<li><a href="<?php echo site_url('index.php/articles/'.$item['ID_CAT']); ?>" ><?php echo $item['CATEGORY'] ?></a></li>
and change your controller to this
$this->load->view('graf/articles', $data);

Can not run a foreach loop

i have a trouble with my foreach loop, it always shows warning that is: Invalid argument supplied for foreach(), can you have a look and help me solve this problem.
This is my controller:
function prod_detail($id_sp){
$this->load->model('product_model');
$data['prod_detail'] = $this->product_model->getProdDetailByProdId($id_sp);
$data['prod_errors'] = $this->product_model->getProdDataError($id_sp);
$data['error_repairing'] = $this->product_model->getProdDataErrorRepairing($id_sp);
$data['rows']= $this->membership_model->getUserData();
$data['main_content'] = 'backend/home/manproduct/prod_detail_view';
$this->load->view('includes/admin/template', $data);
}
This is my model:
function getProdDataError($id_sp){
$this->db->where('id_sp', $id_sp);
$this->db->where('status', 0);
$query = $this->db->get('loi');
if($query->num_rows()>0){
foreach ($query->result() as $row){
$data[]=$row;
}
return $data;
}
}
function getProdDataErrorRepairing($id_sp){
$this->db->where('id_sp', $id_sp);
$this->db->where('status', 1);
$query = $this->db->get('loi');
if($query->num_rows()>0){
foreach ($query->result() as $row){
$data[]=$row;
}
return $data;
}
}
And here is my view:
if($ud->status==0){
echo 'Hoạt động';
}else if($ud->status==1){
echo '<b>Lỗi<br><ul></b>';
foreach ($prod_errors as $err) {
echo '<li>'.$err->ten_loi.'</li>';
}
echo '</ul>';
}else if($ud->status==2){
echo '<b>Đang sửa lỗi<br><ul></b>';
foreach ($error_repairing as $err) {
echo '<li>'.$err->ten_loi.'</li>';
}
echo '</ul>';
}else if($ud->status==3){
echo 'Chưa lắp đặt';
}
It's ok when $ud->status==1 but it shows a warning message when $ud->status==2. btw, i use codeigniter to develop my web, can you help?
try this (adding checks on $prod_errors/$error_repairing before trying to iterate them)
if($ud->status==0)
{
echo 'Hoạt động';
}else if($ud->status==1)
{
echo '<b>Lỗi<br><ul></b>';
// first check $prod_errors exists and is not null before iterating it
if (isset($prod_errors))
{
foreach ($prod_errors as $err)
{
echo '<li>'.$err->ten_loi.'</li>';
}
}
else{
echo "No Data found";
}
echo '</ul>';
}else if($ud->status==2)
{
echo '<b>Đang sửa lỗi<br><ul></b>';
// first check $error_repairing exists and is not null before iterating it
if (isset($error_repairing))
{
foreach ($error_repairing as $err)
{
echo '<li>'.$err->ten_loi.'</li>';
}
}
else{
echo "No Data found";
}
echo '</ul>';
}else if($ud->status==3)
{
echo 'Chưa lắp đặt';
}

read more function with codeigniter

i am trying to create a readmore function in codeigniter where the readmore link will be linked to a controller which would show all the data about that particular id....i am kind of confused on how to go about it... i tried...
<?php
$research_detail_url = site_url()."/research/research_details";
//echo json_encode($research)
if($research)
{
foreach ($research as $_research) {
$author = $_research->author;
$content = $_research->content;
$dsubmitted = $_research->dsubmitted;
echo "<div class='menu-collapse'> <h5>$author</h5>";
echo "<p>";
echo "<span class='support_text'>$content <span><br />";
echo "<span class='support_text'>$dsubmitted <span><br />";
echo "<a href='$research_detail_url' target='_blank' style='text-decoration:underline; color:#0088cc;'>
read more » </a>";
echo "</p> </div>";
}
}
?>
but i seem not be getting any results...i need help...
and this is my controller function.....
public function research_details($id='')
{
if(!$id)
{
echo "Project Id required";
return;
}
$_result = $this->projects_model->get_project($id);
if($_result)
{// success in fetching data hurray
$result['projects'] = $_result;
$users_ids = $this->users_model->get_user_ids(); //return available user id's
$groups_ids = $this->groups_model->get_group_ids(); //return available group id's
//echo json_encode($users_ids);
//echo json_encode($groups_ids);
$group_record = $this->map_names_to_ids($users_ids , $groups_ids );
$result['group_record'] = $group_record;
//load the view
$this->load->view('__includes__/header');
$this->load->view('__includes__/boostrap_responsive');
$this->load->view('projects/project_panel', $result);
$this->load->view('__includes__/footer_scripts');
$this->load->view('__includes__/wijmo_file_jquery');
$this->load->view('__includes__/footer');
}
else
{
exit("An Error occured in fetching the requested project");
}
}
and this is my model.....
<?php
class research_model extends CI_Model {
function add()
{
$this->db->insert('research',$_POST);
if($this->db->_error_number())
{
return $this->db->_error_number();
}
}
function update($article_id, $data_fields = NULL){
if($data_fields == NULL)
{
$this->db->where("article_id =".$article_id);
$this->db->update('research',$_POST);
}
else
{
$this->db->where("article_id =".$article_id);
$this->db->update('research',$data_fields);
}
$is_error = $this->db->_error_number();
if($is_error){
echo $is_error;
}
return TRUE;
}
function delete($id){
$this->db->where("article_id =".$id);
return $this->db->delete('research');
}
//return the research with this id
function get_research($id){
$this->db->where("article_id =".$id);
$query = $this->db->get('research');
if ($query->num_rows() > 0){
return $query->row_array();
}
else
echo $this->db->_error_message();
return FALSE;
}
//return the available research in the table
function get_research_all(){
$query = $this->db->get('research');
if ($query->num_rows() > 0)
{
foreach($query->result() as $row)
{
$result[] = $row;
}
return $result;
}
}
}
and my entire controller.....
<?php
class Research extends Public_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('research_model');
}
function index()
{
if($this->ion_auth->is_admin())
{
$result = $this->research_model->get_research_all();
$data = array(
'main_content' => 'research/index',
'research' => $result
);
$this->load->view("loader", $data);
}
else
{
redirect('home');
}
}//END INDEX
// public view
function current()
{
$result = $this->research_model->get_research_all();
$data = array('research' => $result);
$this->load->view('__includes__/header');
$this->load->view('__includes__/navbar');
$this->load->view('research/current', $data);
$this->load->view('__includes__/footer');
}
function add()
{
if($this->ion_auth->is_admin())
{
$this->load->view("loader",array('main_content'=>"research/add_research"));
}
}//END ADD
function edit($id='')
{
if(! $id)
{
echo "research Id required";
return;
}
$result = $this->research_model->get_research($id);
if( ! $result)
{
echo "Nothing to edit";
return;
}
$result['main_content'] = "research/add_research";
$this->load->view("loader",$result);
}//END EDIT
function delete($id='')
{
if(! $id)
{
echo "Id required";
return;
}
$this->research_model->delete($id);
$this->get_research();
}//END DELETE
function submit($id='')
{
//validate form [perform validation server-side to make sure of fields]
$this->load->library('form_validation');
$this->form_validation->set_rules('author', 'Author', 'trim|required|min_length[4]');
if ($this->form_validation->run() == FALSE){
//ajax data array
$data = array(
'server_validation' => validation_errors()
);
echo str_replace('\\/', '/', json_encode($data));
}
else{
if($id){
$result = $this->research_model->update($id);
$content = "article has been UPDATED successfully";
//$retArr["content"] = $content;
//echo json_encode($retArr);
}
else{
$result = $this->research_model->add();
$content = "article has been CREATED successfully";
//$retArr["content"] = $content;
//echo json_encode($retArr);
}
//if duplicate key
if($result == 1062){
//ajax data array
$data = array();
$data['is_valid'] = 0;
echo json_encode($data);
}else{
//ajax data array
$data = array(
'is_valid' => 1,
'content' => $content
);
echo json_encode($data);
}
}//end ELSE form valid
}//END SUBMIT
public function research_details($id='')
{
if(!$id)
{
echo "Project Id required";
return;
}
$_result = $this->research_model->get_research($id);
if($_result)
{// success in fetching data hurray
$result['article'] = $_result;
//load the view
$this->load->view('__includes__/header');
$this->load->view('__includes__/boostrap_responsive');
$this->load->view('research/research_details', $Aresult);
$this->load->view('__includes__/footer_scripts');
$this->load->view('__includes__/wijmo_file_jquery');
$this->load->view('__includes__/footer');
}
else
{
exit("An Error occured in fetching the requested project");
}
}//END EDIT
}
?>
my public controller
<?php
abstract class Public_Controller extends CI_Controller
{
public $about_data;
function __construct()
{
parent::__construct();
//Making This variable availale for the whole site
$this->load->model('about_model');
$this->load->model('captcha_model');
//get your data
$this->about_data = $this->about_model->get_abouts();
}
}
?>
I see a couple of problems:
In your view, you are not closing the span tags. You need
</span>
In your view, you are creating the link, but you are not
including an ID, which you need in your controller. What I mean by this is the following:
First,you create this $research_detail_url = site_url()."/research/research_details";.
Then echo "<a href='$research_detail_url' target='_blank' style=''>read more</a>";
As you can see, your URL does not have an ID when created. What you should do is something like this: $research_detail_url = site_url()."/research/research_details/31"; where 31 is a random ID. You need to come up with the right ID needed in order to pass it to your controller. Your controller is currently assigning a blank ID since none is being passed which is why you end up with a result of "Project Id required"

i want to use for-each on my code igniter but i am using conditione of (id) and my other conditione

on the view i want to use foreach but with the condition of the id i get from mypost
model
public function get_rows($id= FALSE)
{
if ($id === FALSE)
{
$query = $this->db->get('table1');
return $query->result_array();
$config['per_page'];
$this->uri->segment(3);
}
$query = $this->db->get_where('table1', array('id'=> $id,'type'=> 2));
return $query->row_array();
}
Controller
public function index($id){
$data['rows'] = $this->model->get_rows($id);
if (empty($data['rows']))
{
show_404();
}
$data['id'] = $data['rows']['id'];
$this->load->view('view', $data);
}
View
<?php foreach ($rows as $row): ?>
<li><?php echo $row->title; ?></li>
<?php endforeach; ?>
Here i want to use for each on my view but with the condition of(ID) i get from the post and the type i set my self
thanks in advance...
rafiq__
In view use $row['title']; not $row->title;
View
<?php foreach ($rows as $row): ?>
<li><?php echo $row['title']; ?></li>
<?php endforeach; ?>
and may be you need to change return $query->row_array(); to return $query->result_array(); in your model.
Model
public function get_rows($id = 0){
if ($id > 0){
$this->db->where(array('id'=> $id,'type'=> 2));
}
return $this->db->get('table1')->result_array();
}

Categories