retrieve studentinformation in another page in codeigniter - codeigniter

I have a problem about retrieving again in another page of student information.
I can view all students using this through model and view:
public function viewdata()
{
$this->db->order_by("Idnumber", "asc");
$query= $this->db->get('studentinformation');
return $query->result();
}
<table class=" table table-hover ">
<thead>
<tr>
<th>ID Number</th>
<th>Firstname</th>
<th>Middlename</th>
<th>Lastname</th>
<th>Sex</th>
</tr>
</thead>
<?php
foreach($this->model_adminlogin->viewdata() as $row){
?>
<tbody>
<tr>
<td><?php echo $row->Idnumber?></td>
<td><?php echo $row->Firstname?></td>
<td><?php echo $row->Middlename?></td>
<td><?php echo $row->Lastname?></td>
<td><?php echo $row->Sex ?></td>
<td>
Option
</td>
</tr>
</tbody>
<?php
}
?>
</table>
also I can view them every student information using this in model and controller and view:
function getonerow($id)
{
$this->db->where('Id',$id);
$query=$this->db->get('studentinformation');
return $query->row();
}
public function viewspecific($id)
{
if($this->session->userdata('logged_in'))
{
$this->output->set_header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
$this->output->set_header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0');
$this->output->set_header('Cache-Control: post-check=0, pre-check=0', FALSE);
$this->output->set_header('Pragma: no-cache');
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$row=$this->model_adminlogin->getonerow($id);
$data['r']=$row;
$this->load->view('individualviewstudent',$data);
} else{
redirect('welcome', 'refresh');
}
if((!isset($session_data) || $session_data !=TRUE)) {
redirect('welcome', 'refresh');
}
}
and view using $r
<label class="z label-primary">Name:</label> <span class="z"> <?php echo $r -> Firstname." ". $r-> Middlename." ".$r-> Lastname; ?></span><br>
</div>
<div class="spacesz">
<label class="z label-primary">Id Number: </label><span class="z"> <?php echo $r->Idnumber; ?></span>
</div>
<div class="spacesz">
<label class="z label-primary">Age:</label><span class="z"> <?php echo $r-> Age; ?></span>
</div>
etc.......................................................................................
my problem now how can I retrieve again to another view page with that student I click for my updating purpose?? this is my image of the problem ->
retrieve in another page of that student

Use the id available in the object $r.
<?php echo site_url('student/update/'.$r->Id) ?>
Place the above code in your Update Student link. Like below:
Update Student

Related

Adding student in only specific class list using codeigniter

I have a problem in viewing only a class list that I choose and add student but when I select other class it will also appear/view that I select in previous class
here I select the class that I select
Here is the class page that I select and then I add student
but when I choose another class it will also appear the student
here is my controller:
public function viewspecificclass($id)
{
if($this->session->userdata('logged_in'))
{
$this->output->set_header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
$this->output->set_header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0');
$this->output->set_header('Cache-Control: post-check=0, pre-check=0', FALSE);
$this->output->set_header('Pragma: no-cache');
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$row=$this->model_adminlogin->getonerowclass($id);
$data['r']=$row;
$data['classid'] = $id;
$this->load->view('individualviewclass',$data);
} else{
redirect('welcome', 'refresh');
}
if((!isset($session_data) || $session_data !=TRUE)) {
redirect('welcome', 'refresh');
}
}
public function addstudentnow($classid, $studentid){
$data= array(
'ClassId'=>$classid,
'Id'=> $studentid
);
$this->db->insert('enroll',$data);
$this->session->set_flashdata('category_success', 'Successfully Add!');
redirect('enrollment/viewspecificclass ');
}
here is my model:
function statusofenrollment(){
$this->db->select('*');
$this->db->from('studentinformation');
$this->db->join('statusofenrollment', 'statusofenrollment.Id = studentinformation.Id', 'left');
$query = $this->db->get();
return $query->result();
}
function studentinclasslist(){
$this->db->select('*');
$this->db->from('statusofenrollment');
$this->db->join('studentinformation', 'studentinformation.Id = statusofenrollment.Id', 'right');
$this->db->join('enroll', 'enroll.Id = studentinformation.Id', 'right');
$query = $this->db->get();
return $query->result();
}
here is the view:
<div class="z table-responsive">
<table class=" table table-striped ">
<thead>
<tr>
<th class="text-center">LRN</th>
<th class="text-center">First name</th>
<th class="text-center"> Middle name</th>
<th class="text-center">Last name</th>
<th class="text-center">Gender</th>
<th class="text-center">Status of Enrollment</th>
<th class="text-center"> </th>
</tr>
</thead>
<?php
foreach($this->model_adminlogin->studentinclasslist() as $row){
?>
<tr>
<td class="text-capitalize text-center"><?php echo $row->Idnumber ?></td>
<td class="text-capitalize text-center"><?php echo $row -> Firstname ?></td>
<td class="text-capitalize text-center"><?php echo $row->Middlename ?></td>
<td class="text-capitalize text-center"><?php echo $row->Lastname ?></td>
<td class="text-capitalize text-center"><?php echo $row->Sex ?></td>
<td class="text-capitalize text-center"><?php echo $row->Statusofenrollment ?></td>
<td class="text-capitalize text-center">
</td>
</tr>
<?php
}
?>
</table>
</div>
<div id="myModalpick" class="modal fade" role="dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title text-center">Student Status</h4>
</div>
<div class="modal-body">
<div class="z table-responsive">
<table class=" table table-striped ">
<thead>
<tr>
<th class="text-center">LRN</th>
<th class="text-center">First name</th>
<th class="text-center"> Middle name</th>
<th class="text-center">Last name</th>
<th class="text-center">Gender</th>
<th class="text-center">Status of Enrollment</th>
<th class="text-center"> </th>
</tr>
</thead>
<?php
foreach($this->model_adminlogin->statusofenrollment() as $row){
?>
<tr>
<td class="text-capitalize text-center"><?php echo $row->Idnumber ?></td>
<td class="text-capitalize text-center"><?php echo $row -> Firstname ?></td>
<td class="text-capitalize text-center"><?php echo $row->Middlename ?></td>
<td class="text-capitalize text-center"><?php echo $row->Lastname ?></td>
<td class="text-capitalize text-center"><?php echo $row->Sex ?></td>
<td class="text-capitalize text-center"><?php echo $row->Statusofenrollment ?></td>
<td class="text-capitalize text-center">
Add To Class List
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-default" >Ok</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
because of the fact that your code is totally unorganized it makes hard to help you - but i'll give it a shot
first of all in your models Function studenClassList you've to pass an id from the specific class because other wise you'll get on any request the students of all of your classes - change your model like this
function studentinclasslist($classId){
$query = $this->db
->select('studentinformation.*')
->from('statusofenrollment')
->join('studentinformation', 'studentinformation.Id = statusofenrollment.Id', 'right')
->join('enroll', 'enroll.Id = studentinformation.Id', 'right')
->where("class.Id",$classId)
->get();
return $query->result();
}
be aware that you have to change in the where construct the actual class.Id field because i wasn't able to see where it is
and after that in your view simply pass the ID to your model function
foreach($this->model_adminlogin->studentinclasslist($classId) as $row){
but in any case i must say, in my opinion it isn't a good idea in CI to use a model in your view because the controller should pass the prepared model data to the view

Remove array from multidimensional array yii ajaxlink

I have a page to add items to a table that is rendered with renderpartial via ajax. Every time the user adds an item, I render the table with all the items. I also have an ajaxlink to delete the item the user chooses. The problem is that I can only delete the last record of the table, if I click on other ajaxlink, nothing happens.
here is the view file to search and add items
<div class="row">
<?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name' => 'test_autocomplete',
'source'=>$this->createUrl('insumos/ver'),
'value' => "",
'options' => array(
'minChars'=>1,
'autoFill'=>false,
'focus'=> 'js:function( event, ui ) {
$( "#test_autocomplete" ).val( ui.item.label );
return false;
}',
'select'=>'js:function( event, ui ) {
$( "#Pedidos_codigoinsumo").val(ui.item.id);
$( "#Pedidos_codigo").val(ui.item.codigo);
$( "#Pedidos_nombre").val(ui.item.label);
$( "#Pedidos_cantidad").focus();
$( "#cantidades").show();
return false;
}',
),
'htmlOptions'=>array( 'autocomplete'=>'off'),
)); ?>
</div>
<div id="cantidades">
<div class="row">
<?php echo $form->labelEx($model,'codigo'); ?>
<?php echo $form->textField($model,'codigo',array('size'=>60,'maxlength'=>300)); ?>
<?php echo $form->hiddenField($model,'codigoinsumo'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'nombre'); ?>
<?php echo $form->textField($model,'nombre',array('size'=>60,'maxlength'=>300)); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'cantidad'); ?>
<?php echo $form->textField($model,'cantidad',array('size'=>60,'maxlength'=>300)); ?>
<?php echo $form->error($model,'codigoinsumo'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'observaciones'); ?>
<?php echo $form->textArea($model,'observaciones',array('rows'=>6, 'cols'=>50)); ?>
</div>
<?php echo CHtml::ajaxButton('Agregar', array('pedidos/adicion'), array('type'=>'POST','data'=>'js:$("#pedidoalmacen-form").serialize()','update'=>'#req_res02'));
echo CHtml::ajaxButton('Cancelar Pedido', array('pedidos/cancelar'), array('update'=>'#req_res02'));
?>
<?php $this->endWidget(); ?>
<div id="req_res02"></div>
the view that has the table is:
if($ver==1)
{
?>
<table>
<tr>
<td align="center">Borrar</td>
<td align="center">Código</td>
<td align="center">Insumo</td>
<td align="center">Cantidad</td>
<td align="center">Observaciones</td>
</tr>
<?php
$codigo= Yii::app()->session['pedido-codigo'];
$contado=count($codigo);
$i=0;
while($i<$contado)
{
?>
<tr>
<td align="left"><?php echo CHtml::ajaxLink('Quitar', Yii::app()->createUrl('pedidos/quitar'),array('update'=>'#req_res02','type'=>'POST','data'=>array('dato'=>$codigo[$i]['contador'])),array('id'=>'quitar-'. uniqid())); ?></td>
<td align="left"><?php echo $codigo[$i]['contador']; ?></td>
<td align="left"><?php echo $codigo[$i]['insumo']; ?></td>
<td align="left"><?php echo $codigo[$i]['cantidad']; ?></td>
<td align="left"><?php echo $codigo[$i]['observaciones']; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
<div class="row buttons">
<?php echo CHtml::submitButton('Agregar'); ?>
</div>
<?php
}
?>
and the controller is
public function actionAdicion()
{
$model=new Pedidos;
if(isset($_POST['Pedidos']))
{
$model->attributes=$_POST['Pedidos'];
if(isset(Yii::app()->session['pedido-codigo']))
{
$codigo= Yii::app()->session['pedido-codigo'];
$contador=count($codigo)+1;
$auxiliar=array('contador'=>$contador,'id'=>$model->codigoinsumo,'codigo'=>$model->codigo,'insumo'=>$model->nombre,'cantidad'=>$model->cantidad,'observaciones'=>$model->observaciones);
}
else
{
$auxiliar=array('contador'=>1,'id'=>$model->codigoinsumo,'codigo'=>$model->codigo,'insumo'=>$model->nombre,'cantidad'=>$model->cantidad,'observaciones'=>$model->observaciones);
}
$codigo[]=$auxiliar;
Yii::app()->session['pedido-codigo']=$codigo;
$this->renderPartial('insumosporpedir',array('ver'=>'1'),FALSE,TRUE);
//Yii::app()->end();
//echo $_POST['data1'];//CHtml::encode(print_r("hola", true));
}
}
public function actionCancelar()
{
unset(Yii::app()->session['pedido-codigo']);
$this->renderPartial('insumosporpedir',array('ver'=>'0'),FALSE,TRUE);
}
public function actionQuitar()
{
if(isset($_POST['dato']))
{
$codigo= Yii::app()->session['pedido-codigo'];
$compara=$_POST['dato'];
foreach ($codigo as $subkey => $subarray)
{
if($subarray['contador']==$compara)
{
unset($codigo[$subkey]);
//echo $codigo[$subkey];
}
}
$ver=1;
if(count($codigo)>0)
{
Yii::app()->session['pedido-codigo']=$codigo;
}
else
{
unset(Yii::app()->session['pedido-codigo']);
$ver=0;
}
//$this->layout='';
$this->renderPartial('insumosporpedir',array('ver'=>$ver),false,true);
//Yii::app()->end();
}
thank you!
I found the problem. the problem wasnt the unset method, it was in the view where I showed the values in the array. insted of using
while($i<$contado)
{
?>
<tr>
<td align="left"><?php echo CHtml::ajaxLink('Quitar', Yii::app()->createUrl('pedidos/quitar'),array('update'=>'#req_res02','type'=>'POST','data'=>array('dato'=>$codigo[$i]['contador'])),array('id'=>'quitar-'. uniqid())); ?></td>
<td align="left"><?php echo $codigo[$i]['contador']; ?></td>
<td align="left"><?php echo $codigo[$i]['insumo']; ?></td>
<td align="left"><?php echo $codigo[$i]['cantidad']; ?></td>
<td align="left"><?php echo $codigo[$i]['observaciones']; ?></td>
</tr>
<?php
$i++;
}
I replaced it with
foreach ($codigo as $key => $value)
{
?>
<tr>
<td align="left"><?php echo CHtml::ajaxLink('Quitar', Yii::app()->createUrl('pedidos/quitar'),array('update'=>'#req_res02','type'=>'POST','data'=>array('dato'=>$value['contador'])),array('id'=>'quitar-'. uniqid())); ?></td>
<td align="left"><?php echo $value['codigo']; ?></td>
<td align="left"><?php echo $value['insumo']; ?></td>
<td align="left"><?php echo $value['cantidad']; ?></td>
<td align="left"><?php echo $value['observaciones']; ?></td>
</tr>
<?php
}
the error occurred because I lost the continuity in the keys everytime I delete a middle array

ajax delete using codeigniter

Hi i have this delete thing, and im using it on ajax to delete the data. My problem is it wont delete on the database, when i refresh the browser the data remains, Can someone help me out figured this thing??
Here's my controller below
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class News_and_events extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->model('admin_model', 'am');
}
public function index(){
if($this->session->userdata('logged_in')){
$this->data['title'] = 'News and Events | Spring Rain Global Consultancy Inc Admin Panel';
$this->data['logout'] = 'Logout';
$session_data = $this->session->userdata('logged_in');
$this->data['id'] = $session_data['id'];
$this->data['username'] = $session_data['username'];
$this->data['allData'] = $this->am->getAllData();
$this->load->view('pages/admin_header', $this->data);
$this->load->view('content/news_and_events', $this->data);
$this->load->view('pages/admin_footer');
}else{
redirect('login', 'refresh');
}
}
public function add(){
if($this->session->userdata('logged_in')){
$this->form_validation->set_rules('date', 'Date', 'trim|required|xss_clean');
$this->form_validation->set_rules('event', 'Event', 'trim|required|xss_clean');
$this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean');
if($this->form_validation->run() == FALSE){
$this->data['title'] = 'News and Events | Spring Rain Global Consultancy Inc Admin Panel';
$this->data['logout'] = 'Logout';
$session_data = $this->session->userdata('logged_in');
$this->data['id'] = $session_data['id'];
$this->data['username'] = $session_data['username'];
$this->data['allData'] = $this->am->getAllData();
$this->load->view('pages/admin_header', $this->data);
$this->load->view('content/news_and_events', $this->data);
$this->load->view('pages/admin_footer');
}else{
$array = array(
'Date' => $this->input->post('date'),
'Event' => $this->input->post('event'),
'Description' => $this->input->post('description')
);
$this->am->saveData($array);
$this->session->set_flashdata('add_another',1);
redirect('news_and_events', 'refresh');
}
}else{
redirect('homepage', 'refresh');
}
}
public function delete(){
$id = $this->uri->segment(2);
$this->am->delete($id);
redirect(base_url().'news-and-events');
}
}
and my views
<script type="text/javascript">
$(document).ready(function(){
$("#add_another").click(function(){
});
});
function goDelete(id){
var agree = confirm("Are you sure you want to delete this?");
if(agree){
$("#news-and-event"+id).fadeOut('slow');
$.post('<?php echo base_url().'news-and-events/delete/'?>', {id:id}, function(){
});
}else{
return false;
}
}
</script>
<div class="container" >
<br />
<br />
<br />
<ul id="nav">
<li><h4>Home</h4></li>
<li><h4>News and Events</h4></li>
<li><h4>Activities</h4></li>
</ul>
<div class="starter-template">
<h1>News And Events</h1>
<?php if(!$this->session->flashdata('add_another')):?>
<form action="<?php echo base_url().'news-and-events/add'?>" method="post">
<?php echo validation_errors('<div class="error">', '</div>');?>
<table class="table-striped">
<tr>
<td>Date: </td>
<td><input type="text" id="datepicker" name="date" value="<?php echo set_value('date');?>" /></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td >Event: </td>
<td ><input type="text" name="event" value="<?php echo set_value('event');?>" /></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td width="20%">Description: </td>
<td><textarea cols="30" rows="5" name="description" ><?php echo set_value('description');?></textarea></td>
</tr>
<tr>
<td width="20%"> </td>
<td><input type="submit" value="Add" class="btn btn-success" /></td>
</tr>
</table>
</form>
<?php else: ?>
<div id="add_another" style="float:left;">
<input type="button" value="Add Another" class="btn btn-primary" />
</div>
<?php endif; ?>
<br />
<br />
<table class="table" >
<tr>
<th>Date</th>
<th width="47%" >Event</th>
<th width="32%">Description</th>
<th>Options</th>
</tr>
<?php foreach($allData as $x => $allDatas): ?>
<tr id="news-and-event<?php echo $allDatas->id; ?>">
<td width="10%"><?php echo $allDatas->Date; ?></td>
<td style="text-align:left;"><?php echo $allDatas->Event; ?></td>
<td style="text-align:left;"><?php echo $allDatas->Description; ?></td>
<td width="10%">
Edit |
<a href="javascript:;" onclick="return goDelete('<?php echo $allDatas->id;?>');" >Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div><!-- /.container -->
<script>
var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
$('#datepicker').datepicker({
minDate: new Date(currentYear, currentMonth, currentDate),
dateFormat: "yy-mm-dd"
});
</script>
and my model to delete the database
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Admin_model extends CI_Model{
public function saveData($array){
$this->db->insert('news_and_updates', $array);
}
public function getAllData(){
return $this->db->order_by("Date", "desc")
->get('news_and_updates')
->result_object();
}
public function delete($id){
$this->db->where('id', $id)->delete('news_and_updates');
}
}
?>
im using the $this->uri->segment(2); any help? is much greatly appreciated
thanks
You are sending data via POST but you are trying to delete based on the uri segment.
Try this instead in your controller:
public function delete(){
$id = $this->input->post('id');
$this->am->delete($id);
redirect(base_url().'news-and-events');
}

Message: Undefined property: stdClass::$Date using codeigniter

my code runs well but im getting this error Message: Undefined property: stdClass::$Date and i dont know where this error came from, as far as ive seen my codes are correct. The problem is in my foreach code
here's my controller below
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class News_and_events extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->model('admin_model', 'am');
}
public function index(){
if($this->session->userdata('logged_in')){
$this->data['title'] = 'News and Events | Spring Rain Global Consultancy Inc Admin Panel';
$this->data['logout'] = 'Logout';
$session_data = $this->session->userdata('logged_in');
$this->data['id'] = $session_data['id'];
$this->data['username'] = $session_data['username'];
$this->data['allData'] = $this->am->getAllData();
$this->load->view('pages/admin_header', $this->data);
$this->load->view('content/news_and_events', $this->data);
$this->load->view('pages/admin_footer');
}else{
redirect('login', 'refresh');
}
}
}
my model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Admin_model extends CI_Model{
public function saveData($array){
$this->db->insert('news_and_updates', $array);
}
public function getAllData(){
return $this->db->select(
'news_and_updates.Event',
'news_and_updates.Description',
'news_and_updates.Date'
)
->from('news_and_updates')
->order_by("Date", "desc")
->get()->result_object();
}
}
?>
and my views
<script type="text/javascript">
$(document).ready(function(){
$("#add_another").click(function(){
alert('test');
});
});
</script>
<div class="container" >
<br />
<br />
<br />
<ul id="nav">
<li><h4>Home</h4></li>
<li><h4>News and Events</h4></li>
<li><h4>Activities</h4></li>
</ul>
<div class="starter-template">
<h1>News And Events</h1>
<?php if($this->session->flashdata('add_another')):?>
<div id="add_another" style="float:left;">
<input type="button" value="Add Another" class="btn btn-primary" />
</div>
<?php else: ?>
<form action="<?php echo base_url().'news-and-events/add'?>" method="post">
<?php echo validation_errors('<div class="error">', '</div>');?>
<table class="table-striped">
<tr>
<td>Date: </td>
<td><input type="text" id="datepicker" name="date" value="<?php echo set_value('date');?>" /></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td >Event: </td>
<td ><input type="text" name="event" value="<?php echo set_value('event');?>" /></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td width="20%">Description: </td>
<td><textarea cols="30" rows="5" name="description" ><?php echo set_value('description');?></textarea></td>
</tr>
<tr>
<td width="20%"> </td>
<td><input type="submit" value="Add" class="btn btn-success" /></td>
</tr>
</table>
</form>
<?php endif; ?>
<br />
<br />
<table class="table" >
<tr>
<th>Date</th>
<th width="51%">Event</th>
<th>Description</th>
<th>Options</th>
</tr>
<?php foreach($allData as $x => $allDatas): ?>
<tr>
<td><?php echo $allDatas->Date; ?></td>
<td><?php echo $allDatas->Event; ?></td>
<td></td>
<td></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div><!-- /.container -->
<script>
var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
$('#datepicker').datepicker({
minDate: new Date(currentYear, currentMonth, currentDate),
dateFormat: "yy-mm-dd"
});
</script>
in my foreach code the error is this one $allDatas->Date; it says Message: Undefined property: stdClass::$Date
and that's the field name in my table Date
can someone help me figured this out??
any help is much appreciated! thanks! can't find the error
In your model or controller - after the getAllData method is executed:
echo the result of $this->db->last_query()
Manually execute the returned query in your database, then fix your query
As the comments started, The issue is that Date isn't generated from the query, which means something is wrong in that level, not the output itself.
Make sure the query is correct, dump it before you load the view directly from the controller to see what it gets, if it doesn't get a Date, your issue is elsewhere, probably in the query itself.

Displaying serial numbers with query result

I am trying to fetch data from my database and when I display it I want to show serial numbers (Just like a list) , example:
**Serial Number** Name Country
1. John USA
2. Srijon UK
I have tried something with PHP Loops, but I couldn't make it work. Would you please kindly help me? please note that by serial numbers I don't mean values retrieved from database.
Thanks in Advance :)
Here's my Model
//Function To Create All Batch List
function batch_list($perPage,$uri) {
$this->db->select('*');
$this->db->from('batch');
$this->db->join('teacher', 'batch.batchinstructor = teacher.teacherid');
$this->db->order_by('batchid','DESC');
$getData = $this->db->get('', $perPage, $uri);
if($getData->num_rows() > 0)
return $getData->result_array();
else
return null;
}
//End of Function To Create All Batch List
Here's mY Controller
function index(){
$this->load->library('pagination');
$config['base_url'] = base_url().'batchlist/index';
$config['total_rows'] = $this->db->get('batch')->num_rows();
$config['per_page'] = 20;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination" align="center">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$this->load->model('mod_batchlist');
$data['records']= $this->mod_batchlist->batch_list($config['per_page']
,$this->uri->segment(3));
$data['main_content']='view_batchlist';
$this->load->view('includes/template',$data);
}
Here's My View
<?php if(count($records) > 0) { ?>
<table id="table1" class="gtable sortable">
<thead>
<tr>
<th>Batch Name</th>
<th>Class</th>
<th>Batch Instructor</th>
<th>Edit/Delete</th>
</tr>
</thead>
<tbody>
<?php foreach ($records as $row){ ?>
<tr>
<td><?php echo $row['batchname'];?> </td>
<td><?php echo $row['class'];?></td>
<td><?php echo $row['teachername'];?></td>
<td> <img src="<?php echo base_url(); ?> support/images/icons/edit.png" alt="Edit" />
<img src="<?php echo base_url(); ?>support/images/icons/cross.png" alt="Delete" />
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
<div class="tablefooter clearfix">
<div class="pagination">
<?php echo $this->pagination->create_links(); ?>
</div>
</div>
<?php if(count($records) > 0) { ?>
<table id="table1" class="gtable sortable">
<thead>
<tr>
<th>Serial</th>
<th>Batch Name</th>
<th>Class</th>
<th>Batch Instructor</th>
<th>Edit/Delete</th>
</tr>
</thead>
<tbody>
<?php $i = $this->uri->segment(3); foreach ($records as $row){ $i++; ?>
<tr>
<td><?php echo $i; ?>.</td>
<td><?php echo $row['batchname'];?> </td>
<td><?php echo $row['class'];?></td>
<td><?php echo $row['teachername'];?></td>
<td> <img src="<?php echo base_url(); ?> support/images/icons/edit.png" alt="Edit" />
<img src="<?php echo base_url(); ?>support/images/icons/cross.png" alt="Delete" />
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
<div class="tablefooter clearfix">
<div class="pagination">
<?php echo $this->pagination->create_links(); ?>
</div>
</div>
In my case the fourth and fifth parameters where page number and rows per page respectively
ie: example.com/category/page/1/10
first page with 10 rows per page.
so in this case just use this before the loop
$i = $this->uri->segment(4)? (
$this->uri->segment(4) + $this->uri->segment(5)? $this->uri->segment(5):0
)
:0;
and from inside the loop just increment the value of i.
foreach ($result_obj as $key => $value)
{
$i++;
...
}
Try this..work like this..1,2,3,4,5
$i = 0
while($condition)
{
echo '<td>'.$i++.'</td>';
}

Resources