serial number on the table in codeigniter pagination libarary - php

I have problems when numbering the tables using CodeIgniter framework with libraary pagination. below are the table..
+------------------------------------------+
| num| NIP | Name | Category | Type |
+------------------------------------------+
| 1 | 12345 | udin | A | A1 |
+------------------------------------------+
| | | | B | B1 |
+------------------------------------------+
| | | | C | C1 |
+------------------------------------------+
| 2 | 54321 | JHON | B | B2 |
+------------------------------------------+
| | | | C | C1 |
+------------------------------------------+
the issue is on the next page in the table below..
+------------------------------------------+
| num| NIP | Name | Category | Type |
+------------------------------------------+
| 1 | 54321 | JHON | F | F2 |
+------------------------------------------+
| | | | G | G3 |
+------------------------------------------+
| | | | G | G4 |
+------------------------------------------+
| | | | I | I3 |
+------------------------------------------+
| | | | J | J1 |
+------------------------------------------+
I want the next page numbering sequential numbering starting from the end of the previous value. Supposedly in figure 2 numbering number "2" instead of "1".
code in the controller:
$this->load->library('pagination');
$data['judul'] = 'Report Data Attachment';
$start_row = $this->uri->segment(3);
$per_page = 10;
if (trim($this->uri->segment(3)) == '')
{
$start_row = '0';
}
$config['base_url'] = base_url().'report/index/';
$config['total_rows'] = $this->lampiran_m->lampiran_karyawan_semua_page_conter()->num_rows();
//$config['uri_segment'] = '3';
$config['per_page'] = $per_page;
$config['full_tag_open'] = '<p class="page">';
$config['full_tag_close'] = '</p>';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['lampiran'] = $this->lampiran_m->lampiran_karyawan_semua_page($start_row,','.$per_page);
$data['no'] = '';
$data['last'] = '';
$this->template->display('tabel_report',$data);
and code in my Model :
public function lampiran_karyawan_semua_page($offset='',$limit='')
{
$qry = "select distinct lampiran.id_category,name_category,tipe,lampiran.nip from lampiran
left join employee on (lampiran.nip = employee.nip)
left join category on (lampiran.id_category=category.id_category) where lampiran.id_category = category.id_category and lampiran.nip=employee.nip order by lampiran.nip desc limit $offset $limit";
return $this->db->query($qry);
}
and this code in view :
<table border="1" width="100%" cellpadding="0" cellspacing="0" id="product-table" >
<tr>
<th class="table-header-repeat line-left minwidth-1"> No</th>
<th class="table-header-repeat line-left minwidth-1"> NIP</th>
<th class="table-header-repeat line-left minwidth-1"> Name</th>
<th class="table-header-repeat line-left minwidth-1"> Category Attachment</th>
<th class="table-header-repeat line-left minwidth-1">Type Attachment </th>
<th class="table-header-repeat line-left minwidth-1">Page </th>
</tr>
<?php
foreach($lampiran->result_array() as $k) {
$now=$k['nip'];
if($last!=$now ) {
$no++;
echo "<tr><td>$no</td><td>$k[nip]</td><td>".$this->employee_m->get($k['nip'])->nama."</td><td>$k[name_category]</td><td>$k[tipe]</td><td>"; ?>
<?php foreach($this->lampiran_m->conter_tabel_semua($k['id_category'],$k['tipe'],$k['nip'])->result() as $row) :?>
<?php echo $row->nilai;?>
<?php endforeach;
echo "</td></tr>";
}else{
echo "<tr><td> </td><td> </td><td> </td><td>$k[name_category]</td><td>$k[tipe]</td><td>"; ?>
<?php foreach($this->lampiran_m->conter_tabel_semua($k['id_category'],$k['tipe'],$k['nip'])->result() as $row) :?>
<?php echo $row->nilai;?>
<?php endforeach;
echo "</td></tr>";
}
$last = $k['nip'];
}
?>
<tr><td colspan="5" style="text-align: center;"><b>Jumlah Total</b></td><td>
<?php foreach($this->lampiran_m->jumlah_semua_lampiran()->result() as $row) :?>
<?php echo "<b>$row->jumlah</b>";?>
<?php endforeach; ?>
</td></tr>
</table>
I hope someone can help me on this issue .. thanks

before foreach you have to add
$no = $this->uri->segment(3)+1;
No
NIP
Name
Category Attachment
Type Attachment
Page
</tr>
<?php
//before foreach you have to add
$no = $this->uri->segment(3)+1;
foreach($lampiran->result_array() as $k) {
$now=$k['nip'];
if($last!=$now ) {
$no++;
echo "<tr><td>$no</td><td>$k[nip]</td><td>".$this->employee_m->get($k['nip'])->nama."</td><td>$k[name_category]</td><td>$k[tipe]</td><td>"; ?>
<?php foreach($this->lampiran_m->conter_tabel_semua($k['id_category'],$k['tipe'],$k['nip'])->result() as $row) :?>
<?php echo $row->nilai;?>
<?php endforeach;
echo "</td></tr>";
}else{
echo "<tr><td> </td><td> </td><td> </td><td>$k[name_category]</td><td>$k[tipe]</td><td>"; ?>
<?php foreach($this->lampiran_m->conter_tabel_semua($k['id_category'],$k['tipe'],$k['nip'])->result() as $row) :?>
<?php echo $row->nilai;?>
<?php endforeach;
echo "</td></tr>";
}
$last = $k['nip'];
}
?>
<tr><td colspan="5" style="text-align: center;"><b>Jumlah Total</b></td><td>
<?php foreach($this->lampiran_m->jumlah_semua_lampiran()->result() as $row) :?>
<?php echo "<b>$row->jumlah</b>";?>
<?php endforeach; ?>
</td></tr>
</table>

please put below line before foreach statement
$currentffset = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$slno_start = (($currentffset / $per_page) * per_page) + 1;
after that use $slno_start as serial number for each item inside foreach at last line of foreach just increment it
$slno_start++;

Minor improvement from above answers.Thanks both of u guys
$count =$this->uri->segment(3)+1;
foreach($records as $rec){echo $count; $count++;}

Related

How Set date in month to coloum table with codeigniter

I try to create a table with a date column within a month, I want to try to enter the date that I have into the table but I do not understand its logic ..
<?php
$Month = date('m');
$Year = date('Y');
$day = cal_days_in_month(CAL_GREGORIAN, $Month ,$Year);
$q = $this->db->select('nm_py, dt_start,dt_finish')->from('tb_payment')->get();
$data = $q->result_array();
//example data nm_py : Mobile, dt_start: 2018-05-02, dt_finish: 2018-05-05
//example table output as expected
// | NO| Payment | Month
// |___|_________| 1 | 2 | 3 | 4 | 5 | 6 | 7 | xxxxx
// | 1 | Mobile | - | Y | Y | Y | Y | - | - | xxxxxx
?>
<table>
<thead>
<tr>
<th class="text-center">No</th>
<th class="text-center">Payment</th>
<th class="text-center" colspan="<?php echo $day; ?>">Month</th>
</tr>
<tr>
<th class="text-left"></th>
<th class="text-left"></th>
<?php
for ($x = 1; $x <= $day; $x++) {
echo "<th class='text-center'>".$x."</th>";
}
?>
</tr>
<thead>
<tbody>
<?php $no = 1; foreach($data as $row) { ?>
<?php $no++; }?>
</tbody>
</table>
You can use date_create to get day see working code bellow :
<?php
$q = $this->db->select('nm_py, dt_start,dt_finish')->from('tb_payment')->get();
$data = $q->result_array();
//example data nm_py : Mobile, dt_start: 2018-05-02, dt_finish: 2018-05-05
//example table output as expected
// | NO| Payment | Month
// |___|_________| 1 | 2 | 3 | 4 | 5 | 6 | 7 | xxxxx
// | 1 | Mobile | - | Y | Y | Y | Y | - | - | xxxxxx
?>
<table>
<thead>
<tr>
<th class="text-center">No</th>
<th class="text-center">Payment</th>
<th class="text-center" colspan="<?php echo $day; ?>">Month</th>
</tr>
<tr>
<th class="text-left"></th>
<th class="text-left"></th>
<?php
for ($x = 1; $x <= $day; $x++) {
echo "<th class='text-center'>".$x."</th>";
}
?>
</tr>
<thead>
<tbody>
<?php $no=1; foreach($data as $row) {
echo "<tr>";
echo "<td class='text-center'>".$no."</td>";
echo "<td class='text-center'>".$row['nm_py']."</td>";
// days values
$day_start=date_create($row['dt_start']);
$day_end=date_create($row['dt_finish']);
for ($x = 1; $x <= $day; $x++) {
if($x >=$day_start->format('d') and $x <=$day_end->format('d'))
echo "<td class='text-center'>Y</td>";
else
echo "<td class='text-center'>-</td>";
}echo "</tr>";
$no++;
}?>
</tbody>
</table>

PHP MYSQL HighScores list not working

I have a table name "scores"
+----+-------+-------+-------------------+
| id | score | level | username |
+----+-------+-------+-------------------+
| 1 | 65 | one | dustyrydberg |
| 2 | 56 | one | dustyrydberg |
| 3 | 34 | one | pointedshrill |
| 4 | 50 | one | witchrage |
| 5 | 41 | one | spacetimeken |
| 6 | 44 | one | harperhetton |
| 7 | 65 | one | witchrage |
| 8 | 70 | one | paladino |
| 9 | 50 | one | chimpanzer |
| 10 | 22 | one | yahooligan |
| 11 | 672 | one | blueCrow |
| 12 | 120 | one | sleepyDolphin |
| 13 | 43 | one | gothicNightingale |
| 14 | 22 | one | phonyPuppy |
| 15 | 23 | one | phonyPuppy |
| 16 | 24 | one | phonyPuppy |
| 17 | 31 | one | darlingFairy |
| 18 | 36 | one | starkVoid |
+----+-------+-------+-------------------+
I am trying to list the top 10 highscores without duplication of usernames. I am doing this using php and mysql. I have a functions.php file which includes the following function:
function find_all_scores() {
global $connection;
$query = "SELECT MIN(score), username ";
$query .= "FROM scores ";
$query .= "GROUP BY username ";
$query .= "ORDER BY MIN(score) ASC";
$score_set = mysqli_query($connection, $query);
confirm_query($score_set);
return $score_set;
}
Which is called on in the all_highscores.php file
<?php require_once("../includes/session.php"); ?>
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php
$score_set = find_all_scores();
$ranking = 1;
?>
<?php $layout_context = "score"; ?>
<?php include("../includes/layouts/header.php"); ?>
<div id="main">
<div id="navigation">
</div>
<div id="page">
<?php echo message(); ?>
<h2>Your Highscores</h2>
<p>Here are <?php echo htmlentities($_SESSION["username"]); ?>'s best scores.</p>
<table>
<tr>
<th style="text-align: left; width: 200px;">Position</th>
<th style="text-align: left; width: 200px;">Username</th>
<th style="text-align: left; width: 200px;">Level</th>
<th colspan="2" style="text-align: left;">Score</th>
</tr>
<?php while($score = mysqli_fetch_assoc($score_set)) { ?>
<tr>
<td><?php echo $ranking; ?></td>
<td><?php echo htmlentities($score["username"]); ?></td>
<td><?php echo htmlentities($score["level"]); ?></td>
<td><?php echo htmlentities($score["score"]); ?></td>
<?php
$ranking = $ranking + 1; /* INCREMENT RANKING BY 1 */
?>
</tr>
<?php } ?>
</table>
</div>
</div>
<?php
// 4. Release returned data
//mysqli_free_result($result);
?>
<?php include("../includes/layouts/footer.php"); ?>
However I keep ending up with either a query failure or nothing displays in the HTML browser. Please help!

Trying to create a schedule for a GYM web

I finished my studies but now I need to develop a project and I thought about creating a web app for a gym, but I'm stuck on the schedule module. I need it to get the info from a database like this:
+--------+------------+--------+----------+------+-----------+------------+
| ID_Act | HoraInicio | Dia | Duracion | Sala | DNI_Prof | Dificultad |
+--------+------------+--------+----------+------+-----------+------------+
| 1 | 09:00:00 | LUNES | 45 | 1 | 12345678A | 2 |
| 1 | 09:00:00 | MARTES | 45 | 1 | 12345678A | 3 |
| 1 | 10:30:00 | LUNES | 45 | 1 | 12345678A | 1 |
+--------+------------+--------+----------+------+-----------+------------+
ID_Act: Activity ID
HoraInicio: Start time
Dia: Day
Duración: Duration
Sala: Room
DNI_Prof: Teacher ID
Dificultad: Level
I've been trying to create the whole schedule using a for loop so I don't have to copy&paste 50 rows. I can create the whole schedule with the loop but I can not print the values that I need to have in the proper cell.
This is the code of what I've been trying:
<?php session_start(); ?>
<?php
include("connbd.php");
$consulta="SELECT actividades.nombre AS Actividad, ID_Act, HoraInicio, Dia, Duracion, Sala, Dificultad FROM horarios
JOIN actividades WHERE actividades.id LIKE ID_Act;";
$resultado=mysqli_query($conn,$consulta);
$hora = array("09:00","09:15","09:30","09:45","10:00","10:15","10:30","10:45","11:00","11:15","11:30",
"11:45","12:00","12:15","12:30","12:45","13:00","13:15","13:30","13:45","14:00","14:15","14:30","14:45",
"15:00","15:15","15:30","15:45","16:00","16:15","16:30","16:45","17:00","17:15","17:30","17:45","18:00",
"18:15","18:30","18:45","19:00","19:15","19:30","19:45","20:00","20:15","20:30","20:45","21:00");
?>
<div id=contenidoHorarios class="contenidoHorarios">
<div id=contenidoHorariosTabla class="contenidoHorariosTabla">
<div id=contenidoHorariosTablaHoras class="contenidoHorariosTablaHoras">
<table border="1px" id="contenidoHorariosTablaTabla" class="contenidoHorariosTablaTabla">
<tr>
<th width="5%"></th>
<th width="13.57%">Lunes</th>
<th width="13.57%">Martes</th>
<th width="13.57%">Miércoles</th>
<th width="13.57%">Jueves</th>
<th width="13.57%">Viernes</th>
<th width="13.57%">Sábado</th>
<th width="13.57%">Domingo</th>
</tr>
<?php
foreach ($hora as $horatupla) {
?>
<tr>
<th><?php echo $horatupla; ?></th>
<td id="<?php
while ($row=$resultado->fetch_assoc())
{
$id = $row['ID_Act'];
$horainicio = substr($row['HoraInicio'],0,-3);
$dia = $row['Dia'];
$duracion = $row['Duracion'];
$sala = $row['Sala'];
$dificultad = $row['Dificultad'];
$actividad = $row['Actividad'];
if($dia=='LUNES'){echo $dia . $horatupla;} ?>" rowspan="<?php if($duracion=='15' || $duracion==''){echo '1';} if($duracion=='30'){echo '2';} if($duracion=='45'){echo '3';} ?>"
<?php
if ($horatupla==$horainicio)
{
echo $actividad;
}
}
?>>
</td>
</tr>
<?php } ?>
</table>
</div>
</div>
</div>
I hope you understand what I'm saying, but to help you, I give you a example of what I do want to do (no spam).
http://www.go-fit.es/Horarios/Index

PHP While loop only fetching first row results

i am fetching data from db in datatable , but my while loop is only fetching first row results . i can guess that i am wrong somewhere but not exactly know where . find below the details
Tables :
user :
user_id | user_role_id | user_name | fname | lname | profile
:-----: | :----------: | :-------: | :---: | :---: | :-----:
1 | 2 | schin | sam | chin | t1.png
2 | 2 | mlouis | mark | louis| t2.png
teachers :
t_id | classes | subjects | pri_classes | primary_subjects | email
:-----: | :----------: | :-------: | :---------: | :--------------: | :-----:
1 | eight,nine | math,phy | nine | maths | 1#gmail.com
2 | two | science | two | science | 2#gmail.com
Php Query :
<?php
$sql3 = "select * from user where user_role_id = 2";
$result3 = $dbh->query($sql3);
$row3 = mysqli_fetch_assoc($result3);
$user_id = $row3['user_id'];
$sql4 = "select * from teachers where t_id = '$user_id'";
$result4 = $dbh->query($sql4);
?>
Table head :
<table id="datatable-table" class="table table-striped table-hover">
<thead>
<tr>
<th>S.no</th>
<th class="no-sort hidden-sm-down">Image</th>
<th >Name</th>
<th >User Name</th>
<th >Classes Handled</th>
<th >Subjects Handled</th>
<th >Primary Class</th>
<th >Primary Subjects</th>
<th >Email</th>
</tr>
</thead>
<tbody>
While loop :
<?php
while(($row4 = mysqli_fetch_assoc($result4))&&($row3)){
?>
<tr align="center">
<td>
<?php
$i = 1;
echo $i;
$i++;
?>
</td>
<td>
<span >
<img src="../img/<?php echo $row3['profile'];?>" style="width:40px; height:40px;">
</span>
</td>
<td>
<span class="fw-semi-bold">
<?php echo $row3['fname'].' '.$row3['lname']; ?>
</span>
</td>
<td>
<span class="fw-semi-bold">
<?php echo $row3['user_name']; ?>
</span>
</td>
<td><span class="fw-semi-bold">
<?php
$classes=explode(',', $row4['classes']);
$prefix = '';
foreach($classes as $cout)
{
echo $prefix . '' . wordsToNumber($cout);
$prefix = ', ';
}
?>
</span>
</td>
<td><span class="fw-semi-bold">
<?php
$subjects=explode(',', $row4['subjects']);
$prefix = '';
foreach($subjects as $sout) {
echo $prefix . '' . str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($sout))));
$prefix = ', ';
}
?>
</span>
</td>
<td>
<span class="fw-semi-bold">
<?php
echo wordsToNumber($row4['pri_classes']);
?>
</span></td>
<td><span class="fw-semi-bold">
<?php
echo str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($row4['primary_subjects']))));
?>
</span></td>
<td><span class="fw-semi-bold"><?php echo wordwrap($row4['email'],10, "<br>\n"); ?></span></td>
</tr>
<?php
}
?>
</tbody>
</table>
For wordsToNumber i use this function
Your first query is only retrieving a single user. You need nested loops.
while ($row3 = mysqli_fetch_assoc($result3)) {
$user_id = $row3['user_id'];
$result4 = $dbh->query("select * from teachers where t_id = '$user_id'");
while($row4 = mysqli_fetch_assoc($result4)){
// display etc
}
}
You may want to consider some joins in MySQL to avoid so many queries http://dev.mysql.com/doc/refman/5.7/en/join.html

How to display tables by school year and semester using php?

I am trying to display a table by school year using PHP. I it seems not working very well because the school year displays redundantly.
How to simply display it like this?
No | SY | Student ID | Student Name | Gender | Section Code | Subject
-----------------------------------------------------------------------------
2015-2016 SEMESTER 1
-----------------------------------------------------------------------------
1 | 2 | 0011941 | Cocos, Scrappy S. | Male | IT15B | IT15
-----------------------------------------------------------------------------
1 | 2 | 1212211 | asasas, sasas a. | Male | IT15B | IT15
-----------------------------------------------------------------------------
2015-2016 SEMESTER 2
-----------------------------------------------------------------------------
1 | 2 | 0011941 | Cocos, Scrappy S. | Male | IT15B | IT15
-----------------------------------------------------------------------------
1 | 2 | aSAsaSA | dsdsadsad.ssasasas | Male | IT15B | IT15
-----------------------------------------------------------------------------
2016-2017 SEMESTER 1
-----------------------------------------------------------------------------
1 | 2 | 0011941 | Doo, Scooby D. | Male | IT15B | IT15
-----------------------------------------------------------------------------
2016-2017 SEMESTER 2
-----------------------------------------------------------------------------
1 | 2 | 0011941 | Doo, Scooby D. | Male | IT15B | IT15
here's my code:
<tr>
<th>No</th>
<th>SY</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Gender</th>
<th>Section Code</th>
<th>Subject</th>
<th colspan="2">Update</th>
</tr>
$sql= "SELECT sts.stud_id, sts.fname, sts.lname, sts.mi, sts.gender, sub.section_code, sub.subject_name, ets.sem, ets.enroll_num, ets.sy
FROM students sts
JOIN enrollments ets ON(sts.stud_id = ets.stud_id)
JOIN subjects sub ON (sub.section_code = ets.section_code)
GROUP BY sts.stud_id, sub.section_code ORDER BY ets.stud_id ASC";
$sql_sel=mysql_query($sql);
$num_rows = mysql_num_rows($sql_sel);
if($num_rows==0)
{
echo "No results found. Please try again";
}
$i=0;
while($row=mysql_fetch_array($sql_sel)) //for the first query
{
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<th colspan="8"><?php echo $row['sy'];?></th> //this thing here
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['sem'];?></td>
<td><?php echo $row['stud_id'];?></td>
<td width="200"><?php echo $row['lname'].", ".$row['fname']." ".$row['mi'].".";?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['section_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<td align="center" width="82"><img src="picture/update.png" /></td>
<!----<td align="center"><img src="picture/delete.png" /></td>-------->
</tr>
<?php
}
?>
First you need to order by year and semester in your query:
$sql= "SELECT sts.stud_id, sts.fname, sts.lname, sts.mi, sts.gender, sub.section_code, sub.subject_name, ets.sem, ets.enroll_num, ets.sy
FROM students sts
JOIN enrollments ets ON(sts.stud_id = ets.stud_id)
JOIN subjects sub ON (sub.section_code = ets.section_code)
GROUP BY sts.stud_id, sub.section_code ORDER BY ets.sy ASC, ets.sem ASC, ets.stud_id ASC";
Then you only write the year row when you detect a change of year:
$i=0;
$yr=null;
while($row=mysql_fetch_array($sql_sel)) //for the first query
{
if ($yr != ($row['sy'] . ' SEMESTER ' . $row['sem'])) {
$yr = $row['sy'] . ' SEMESTER ' . $row['sem'];
print ('<th colspan="8">' . $yr . '</th>');
}
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['sem'];?></td>
<td><?php echo $row['stud_id'];?></td>
<td width="200"><?php echo $row['lname'].", ".$row['fname']." ".$row['mi'].".";?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['section_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<td align="center" width="82"><img src="picture/update.png" /></td>
<!----<td align="center"><img src="picture/delete.png" /></td>-------->
</tr>
<?php
}

Categories