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
}
Related
if the date is same with database date just display one time only.Example:database date 16/2 then all 16/2 must display in a box, but other date also need to display. who help me to solve this problem.
<h1>Payment Record</h1>
<?php
$user_check=$_SESSION['login_user'];
$query = "SELECT * FROM `confirm_order` where customer = '$user_check'";
$result=mysqli_query($mysqli,$query);
while($row=mysqli_fetch_array($result))
{
?>
<table style="width:100%;border:1px solid black;margin-bottom:20px;">
<tr>
<td>Product Name: <?php echo $row['product_name'];?></td>
</tr>
<tr>
<td>Quantity: <?php echo $row['quantity']?></td>
</tr>
<tr>
<td>Receiver Name: <?php echo $row['receiver_name']?></td>
</tr>
<tr>
<td>Receiver Address: <?php echo $row['receiver_address']?></td>
</tr>
<tr>
<td>Receiver Contact: <?php echo $row['receiver_contact']?></td>
</tr>
<tr>
<td style="float:right">Date: <?php echo $row['date']?></td>
</tr>
</table>
<?php
}
?>
Example
id | product | date |
---|---------|------|
1 | book | 16/2 |
2 | pencil | 16/2 |
3 | shoe | 18/2 |
Result
-------
book |
pencil |
-------
shoe |
-------
Here is the complete solution:
$user_check=$_SESSION['login_user'];
$query = "SELECT * FROM `confirm_order` where customer = '$user_check'";
$result=mysqli_query($mysqli,$query);
$current_date=NULL;
$previous_date=NULL;
$table_open="<table border='1'>";
$table_close="</table><hr>";
$output=NULL;
while($row=mysqli_fetch_array($result))
{
$current_date=$row['date'];
if ($current_date==$previous_date) {
// SAME DATE
$output.= "<tr><td>SAME DATE</td><td>".$current_date."</td></tr>";
} else {
// NEW DATE
if ($previous_date<>NULL) {
$output.= $table_close;
}
$output.= $table_open."<tr><td>NEW DATE</td><td>".$current_date."</td></tr>";
}
$previous_date=$row['date'];
}
echo $output;
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
I have a table like this in my database
orderID| productID | qty
1 5 1
1 2 1
1 6 1
1 4 1
2 9 1
2 5 2
2 6 1
I want to output it into my HTML/php page in a similar way to this
Order ID:1
Product ID | Qty
5 | 1
2 | 1
6 | 1
4 | 1
Order ID:2
Product ID | Qty
9 | 1
5 | 2
6 | 1
Is this even possible to do? I have tried to use DISTINCT but that only outputs 1 row for each orderID. Here is the closest way I have outputted using the ORDER BY statement at the moment but this makes the headers for every row from the database.
<?php
if ($query = "SELECT * FROM productOrders ORDER BY orderID"){
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<table>
<tr>
<th>Order ID: <?php echo $row['orderID'] ?></th>
</tr>
<tr>
<th>Product ID</th>
<th>Qty</th>
</tr>
<tr>
<td><?php echo $row['productID'] ?></td>
<td><?php echo $row['qty'] ?></td>
</tr>
</table>
<?php
}
}
?>
Try this
<?php
$query = "SELECT * FROM productOrders ORDER BY orderID";
if ($query){
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$id=$row['orderID'];
$sql="SELECT orderID, qty FROM productOrders WHERE orderID=$id";
$q=mysqli_query($connection,$sql);
if(!$q){ echo "error"; }
else { while($row1=mysqli_fetch_assoc($q)){
?>
<table>
<tr>
<th>Order ID <?php echo $id; ?></th>
</tr>
<tr>
<th>Product ID</th>
<th>Qty</th>
</tr>
<tr>
<td><?php echo $row1['productID'] ?></td>
<td><?php echo $row1['qty'] ?></td>
</tr>
</table>
<?
}
}
}
}
}
?>
select orderid, productid, sum(qty)
from productOrders
group by orderid, productid
which will give you layout of...
OrderID | ProductID | Qty
1 | 5 | 1
1 | 2 | 1
1 | 6 | 1
1 | 4 | 1
2 | 9 | 1
2 | 5 | 2
2 | 6 | 1
Then just loop through each row and display as needed in PHP
I have applied query to show order details in a table according to names and dates. It shows data correct.Right now its displaying data like this:
But now i want my data to be shown in format where i can see all the orders from same client according to date something like this:
ABC ordered on Date:20/05/15
ItemName | Size | Quantity | Color
item1 | XL | 7 | yellow
item2 | L | 3 | pink
item3 | S | 1 | green
XYZ ordered on Date:13/09/15
ItemName | Size | Quantity | Color
item1 | L | 8 | brown
item2 | L | 3 | pink
item3 | S | 4 | green
Any suggestions or help will be appreciable
Code
echo" <table border='3px' bordercolor='#333333' >";
$query="select orders.date,order_detail.quantity,order_detail.price,order_detail.color,order_detail.size,customers.name,products.product_name,products.product_image from order_detail JOIN orders on orders.serial=order_detail.orderid Join customers on customers.serial=orders.customerid Join products on products.productid=order_detail.productid ";
$sql=mysqli_query($con,$query);
while($row=mysqli_fetch_array($sql))
{
?>
<tr>
<td><?php echo $row['name'] ?> </td>
<td><?php echo $row['date'] ?></td>
</tr>
<th>Product</th>
<th>Name & quantity</th>
<th>Color</th>
<th>Price</th>
<th>Size</th>
<tr>
<td><image width="80px" height="90px" src="\images<?php echo $row['product_image'] ?>"/></td>
<td><?php echo $row['product_name']. "*". $row['quantity']?></td>
<td><?php echo $row['color'] ?></td>
<td><?php echo $row['price'] ?></td>
<td><?php echo $row['size'] ?></td>
</tr>
<?php
} }
?>
</table>
Try adding order by this way
$query="select orders.date,order_detail.quantity,
order_detail.price,order_detail.color,
order_detail.size,customers.name,products.product_name,
products.product_image from order_detail
JOIN orders on orders.serial=order_detail.orderid
Join customers on customers.serial=orders.customerid
Join products on products.productid=order_detail.productid
order by orders.date ASC, product.name ASC";
Use Group BY Order_date. If there are multiple orders from same customer Use SUM(quantity) and distinct(Itemname).
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++;}