Table numbering next to data that was selected in the database
I would like to put the numbering along with the fetched data from the database using codeigniter.
I created a table row for data and wanted to have numbering in front of the actual description of the data.
This is my opinion: Pay attention <td>#</td>
to <tbody></tbody>
. This is where I wanted to put the numbering.
<div class="table-responsive">
<table class="table table-hover table-bordered table-striped table-condensed">
<caption><h3><strong>Master List of Suppliers</strong></h3></caption>
<thead>
<tr>
<th>#</th>
<th>Supplier Code</th>
<th>Description</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
foreach ($view_masterlist_suppliers as $row) {
echo
'<tr>
<td>#</td>
<td><a href="'.site_url("site/itemspersupplier?i=".$row->SUP_CODE).'">'.$row->SUP_CODE.'</a></td>
<td>'.$row->SUP_DESC.'</td>';
if(empty($row->SUP_ADD1)){ echo '<td><strong>- NO ADDRESS HAS BEEN ENCODED -</strong></td>';}
else if(empty($row->SUP_ADD2)){ echo '<td>'.$row->SUP_ADD1.'</td>';}
else{ echo '<td>'.$row->SUP_ADD1.', '.$row->SUP_ADD2.'</td>';}
echo '</tr>';
}
?>
</tbody>
</table>
</div>
This is my controller:
public function itemspersupplier(){
$data["title"] = "Supplier Master List";
$i = $_GET['i'];
$this->load->model("get_model");
$data["view_items_per_suppliers"] = $this->get_model->view_items_per_suppliers($i);
$this->load->view("templates/header", $data);
$this->load->view("templates/nav", $data);
$this->load->view("pages/items_per_supplier", $data);
$this->load->view("templates/footer", $data); }
This is my model: Note: I have databases, but there is nothing to worry about :)
function view_items_per_suppliers($i){
//Load db2 and run query
$CI = &get_instance();
//Seeting the second parameter to TRUE (Boolean) the function will
//return the database object.
$this->db2 = $CI->load->database('db2', TRUE);
$this->db2->select('TBL_SUPPLIER.SUP_CODE,TBL_SUPPLIER.SUP_DESC,TBL_SUPPLIER.SUP_ADD1,TBL_SUPPLIER.SUP_ADD2,TBL_SUPPLIER.SUP_TEL1,TBL_SUPPLIER.SUP_TEL2,TBL_SUPPLIER.SUP_FAX1,TBL_SUPPLIER.SUP_FAX2,TBL_SUPPLIER.SUP_CONT,TBL_MASTER_ITEMS.IN_CODE,TBL_MASTER_ITEMS.ITE_DESC,TBL_MASTER_ITEMS.UNIT,TBL_MASTER_ITEMS.UNT_COST');
$this->db2->from('TBL_SUPPLIER');
$this->db2->join('TBL_MASTER_ITEMS', 'TBL_SUPPLIER.SUP_CODE = TBL_MASTER_ITEMS.SUP_CODE', 'inner');
$this->db2->where('TBL_MASTER_ITEMS.SUP_CODE', $_GET['i']);
//$this->db->limit('25');
$view_items_per_suppliers = $this->db2->get();
return $view_items_per_suppliers->result();
}
Thank.
+3
source to share
2 answers
Just update your code. This will work for you
<tbody>
<?php
$i = 1;
foreach ($view_masterlist_suppliers as $row) {
echo
'<tr>
<td>'.$i++.'</td>
<td><a href="'.site_url("site/itemspersupplier?i=".$row->SUP_CODE).'">'.$row->SUP_CODE.'</a></td>
<td>'.$row->SUP_DESC.'</td>';
if(empty($row->SUP_ADD1)){ echo '<td><strong>- NO ADDRESS HAS BEEN ENCODED -</strong></td>';}
else if(empty($row->SUP_ADD2)){ echo '<td>'.$row->SUP_ADD1.'</td>';}
else{ echo '<td>'.$row->SUP_ADD1.', '.$row->SUP_ADD2.'</td>';}
echo '</tr>';
}
?>
can simply be achieved as
foreach ($view_masterlist_suppliers as $key => $row) {
echo
'<tr>
<td>'.$key+1.'</td>
OR
foreach ($view_masterlist_suppliers as $key => $row) {
echo
'<tr>
<td>'.++$key.'</td>
+1
source to share