Attendance report print in HTML table format [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to print shift in horizontally and name/id in vertically. I have an attendance table like this =>>.
attendance table:
I want to print shift value in this table format html table structure:
<?php
$d=cal_days_in_month(CAL_GREGORIAN,date("n"),date("Y"));
$totdate = $d;
$month = date("F");
echo '<table class="table table-striped table-bordered" id="table_content">';
echo "<thead>";
echo "<tr>";
echo "<th>Sl/No</th>";
echo "<th>Emp.ID</th>";
echo "<th>Name</th>";
for ($i=1; $i <= $totdate; $i++) {
echo "<th>".$i."</th>";
}
echo "</tr>";
echo "</thead>";
$qry = "SELECT * FROM attendance WHERE `month` = '$month'";
$cnt=1;
$query = mysqli_query($conn, $qry);
foreach ($query as $q) {
$sid = $q['empid'];
echo "<tr>";
echo "<td>".$cnt++ ."</td>";
$st_name_id = "SELECT `emp_id`, `name` FROM `emp` WHERE `id`='$sid'";
$re = mysqli_query($conn, $st_name_id);
while ($result = mysqli_fetch_array($re)) {
echo "<td>".$result['emp_id']."</td>";
echo "<td>".$result['name']."</td>";
}
for ($i=1; $i <= $totdate ; $i++) {
$qry2 = "SELECT * FROM `attendance` WHERE `empid`='$sid' AND `month`='$month' AND `date`='$i'";
$re2 = mysqli_query($conn, $qry2);
echo "<td>";
foreach ($re2 as $value => $q) {
$dt = $q['date'];
$s = $q['shift'];
if ($i == $dt) {
echo $s;
echo "</td>";
}else{
echo "<td>x</td>";
}
}
}
echo "</tr>";
}
echo "</table>";
?>

$qry ="SELECT * FROM emp";

HI guys here is the answer. Thank you for support
<?php
$d=cal_days_in_month(CAL_GREGORIAN,date("n"),date("Y"));
$totdate = $d;
$month = date("F");
echo '<table class="table table-striped table-bordered" id="table_content">';
echo "<thead>";
echo "<tr>";
echo "<th>Sl/No</th>";
echo "<th>Emp.ID</th>";
echo "<th>Name</th>";
for ($i=1; $i <= $totdate; $i++) {
echo "<th>".$i."</th>";
}
echo "</tr>";
echo "</thead>";
// $qry = "SELECT * FROM attendance WHERE `month` = '$month'";
$qry ="SELECT * FROM `emp`";
$cnt=1;
$query = mysqli_query($conn, $qry);
foreach ($query as $q) {
$sid = $q['id'];
echo "<tr>";
echo "<td>".$cnt++ ."</td>";
$st_name_id = "SELECT `emp_id`, `name` FROM `emp` WHERE `id`='$sid'";
$re = mysqli_query($conn, $st_name_id);
while ($result = mysqli_fetch_array($re)) {
echo "<td>".$result['emp_id']."</td>";
echo "<td>".$result['name']."</td>";
}
for ($i=1; $i <= $totdate ; $i++) {
$ii = sprintf("%02d", $i);
$qry2 = "SELECT * FROM `attendance` WHERE `empid`='$sid' AND `month`='$month' AND `date`='$ii'";
$re2 = mysqli_query($conn, $qry2);
echo "<td>";
foreach ($re2 as $q) {
$dt = $q['date'];
$s = $q['shift'];
if ($dt == $ii) {
echo $s;
echo "</td>";
}else{
echo "x</td>";
}
}
// echo sprintf("%02d", $i) ."</td>";
}
echo "</tr>";
}
echo "</table>";
as accepted result

Related

PHP echo mysqli data table from the last

i need help.. im trying to echo all the data from my database but from the last to the first.
im a college student and i need ur help.
while (($row = mysqli_fetch_assoc($result)) && ($i < 6)) { // my lecture ask me to echo 5 data only
echo "<tr>";
foreach ($row as $field => $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
$i = $i + 1;
}
that is my code for now, and its only echoing from the first table to the last. my lecture told me i need to echo from the last.
edit :
i think i haven't give all the information.. sorry, this is the first time im here :(.
<?php
$i = 1;
include_once("function/helper.php");
include_once("function/koneksi.php");
$query = mysqli_query($koneksi, "SELECT * FROM transaksi ");
$pemilik = mysqli_fetch_assoc($query);
?>
<?php
$i = 1;
$sql = "SELECT mutasi, waktu_tanggal,tujuan FROM transaksi WHERE user_id='$user_id' ORDER BY waktu_tanggal";
$result = mysqli_query($koneksi, $sql);
if(mysqli_num_rows($result) == 0) {
?>
<h1>Anda belum melakukan transaksi apapun</h1>
<?php
}else {
echo "<br>";
echo "<table border='1'>";
?>
<tr>
<th>Mutasi</th>
<th>Waktu</th>
<th>Keterangan</th>
</tr>
<?php
while (($row = mysqli_fetch_assoc($result)) && ($i < 6)) {
echo "<tr>";
foreach ($row as $field => $value) { line like this: foreach($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
$i = $i + 1;
}
echo "</table>";
}
?>
that is the entire page.
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
The sql [ORDER BY] command is used to sort the result set in ascending or descending order.
SELECT column1, column2 FROM table_name ORDER BY column1 DESC;
Try php function mysqli_fetch_all () --- >> you don't need a while-loop.
Then create a new array and populate it with the required number of records.
Like this:
$query = "SELECT TOP 5*FROM `MyTable`";
// Or alternative query: "SELECT*FROM `MyTable LIMIT 5`
$result = mysqli_query($connection, $query);
if($result != 0) {
$newDataArray = mysqli_fetch_all($result, MYSQLI_BOTH);
}

How to integrate array_column() and array_filter() into dynamic table generation of sql results

I want to read out data from an sql-database an show them in a table. This works well. Now, I would like to show only those columns with at least one value in it and not the empty ones (containing NULL, 0, empty string). This works with the following example:
enter code here
<TABLE width="500" border="1" cellpadding="1" cellspacing="1">
<?php
$query = mysql_query("SELECT * FROM guestbook", $db);
$results = array();
while($line = mysql_fetch_assoc($query)){
$results[] = $line;
$Name = array_column($results, 'Name');
$Home = array_column($results, 'Home');
$Date = array_column($results, 'Date');
$Emptycolumn = array_column($results, 'Emptycolumn');
$Comment = array_column($results, 'Comment');
$City = array_column($results, 'City');
}
echo "<TR>";
if(array_filter($Name)) {echo "<TH>Name</TH>";}
if(array_filter($Home)){echo "<TH>Home</TH>";}
if(array_filter($Date)){echo "<TH>Date</TH>";}
if(array_filter($Emptycolumn)){echo "<TH>Emptycolumn</TH>";}
if(array_filter($Comment)){echo "<TH>Comment</TH>";}
if(array_filter($City)){echo "<TH>City</TH>";}
echo "</TR>";
$query = mysql_query("SELECT * FROM guestbook", $db);
while($line = mysql_fetch_assoc($query)){
echo "<TR>";
if(array_filter($Name)) {echo "<TD>".$line['Name']."</TD>";}
if(array_filter($Home)) {echo "<TD>".$line['Home']."</TD>";}
if(array_filter($Date)) {echo "<TD>".$line['Date']."</TD>";}
if(array_filter($Emptycolumn)) {echo "<TD>".$line['Emptycolumn']."</TD>";}
if(array_filter($Comment)) {echo "<TD>".$line['Comment']."</TD>";}
if(array_filter($City)) {echo "<TD>".$line['City']."</TD>";}
echo "</TR>";
}
?>
</TABLE>
Since the column-names of my table are highly variable (depending on the query), the table is generated by looping through the result-array, first the column-names, then the values in the rows:
enter code here
$sql = "SELECT DISTINCT $selection FROM $tabelle WHERE
$whereclause"; //will be changed to PDO
$result = mysqli_query($db, $sql) or die("<b>No result</b>"); //Running
the query and storing it in result
$numrows = mysqli_num_rows($result); // gets number of rows in result
table
$numcols = mysqli_num_fields($result); // gets number of columns in
result table
$field = mysqli_fetch_fields($result); // gets the column names from the
result table
if ($numrows > 0) {
echo "<table id='myTable' >";
echo "<thead>";
echo "<tr>";
echo "<th>" . 'Nr' . "</th>";
for($x=0;$x<$numcols;$x++){
$key = array_search($field[$x]->name, $custom_column_arr);
if($key !== false){
echo "<th>" . $key . "</th>";
}else{
echo "<th>" . $field[$x]->name . "</th>";
}
}
echo "</tr></thead>";
echo "<tbody>";
$nr = 1;
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $nr . "</td>";
for ($k=0; $k<$numcols; $k++) { // goes around until there are no
columns left
echo "<td>" . $row[$field[$k]->name] . "</td>"; //Prints the data
}
echo "</tr>";
$nr = $nr + 1;
} // End of while-loop
echo "</tbody></table>";
}
}
mysqli_close($db);
Now, I tried to integrate the array_column() and array_filter()-blocks of the example above into the loops, but unfortunately, it didn´t work. I´m sure, this is easy for a professional and I would be very grateful, if someone could help me with this problem!
Thank you very much in advance!!

Writing the attributes of a database in PHP

I am writing an application in which user can enter a database name and I should write all of its contents in table with using PHP.I can do it when I know the name of database with the following code.
$result = mysqli_query($con,"SELECT * FROM course");
echo "<table border='1'>
<tr>
<th>blablabla</th>
<th>blabla</th>
<th>blablabla</th>
<th>bla</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['blablabla'] . "</td>";
echo "<td>" . $row['blabla'] . "</td>";
echo "<td>" . $row['blablabla'] . "</td>";
echo "<td>" . $row['bla'] . "</td>";
echo "</tr>";
}
echo "</table>";
In this example I can show it since I know the name of table is course and it has 4 attributes.But I want to be able to show the result regardless of the name the user entered.So if user wants to view the contents of instructors there should be two columns instead of 4.How can I accomplish this.I get the table name with html.
Table:<input type="text" name="table">
Edit:Denis's answer and GrumpyCroutons' answer are both correct.You can also ask me if you didnt understand something in their solution.
Quickly wrote this up, commented it (This way you can easily learn what's going on, you see), and tested it for you.
<form method="GET">
<input type="text" name="table">
</form>
<?php
//can be done elsewhere, I used this for testing. vv
$config = array(
'SQL-Host' => '',
'SQL-User' => '',
'SQL-Pass' => '',
'SQL-Database' => ''
);
$con = mysqli_connect($config['SQL-Host'], $config['SQL-User'], $config['SQL-Pass'], $config['SQL-Database']) or die("Error " . mysqli_error($con));
//can be done elsewhere, I used this for testing. ^^
if(!isSet($_GET['table'])) { //check if table choser form was submitted.
//In my case, do nothing, but you could display a message saying something like no db chosen etc.
} else {
$table = mysqli_real_escape_string($con, $_GET['table']); //escape it because it's an input, helps prevent sqlinjection.
$sql = "SELECT * FROM " . $table; // SELECT * returns a list of ALL column data
$sql2 = "SHOW COLUMNS FROM " . $table; // SHOW COLUMNS FROM returns a list of columns
$result = mysqli_query($con, $sql);
$Headers = mysqli_query($con, $sql2);
//you could do more checks here to see if anything was returned, and display an error if not or whatever.
echo "<table border='1'>";
echo "<tr>"; //all in one row
$headersList = array(); //create an empty array
while($row = mysqli_fetch_array($Headers)) { //loop through table columns
echo "<td>" . $row['Field'] . "</td>"; // list columns in TD's or TH's.
array_push($headersList, $row['Field']); //Fill array with fields
} //$row = mysqli_fetch_array($Headers)
echo "</tr>";
$amt = count($headersList); // How many headers are there?
while($row = mysqli_fetch_array($result)) {
echo "<tr>"; //each row gets its own tr
for($x = 1; $x <= $amt; $x++) { //nested for loop, based on the $amt variable above, so you don't leave any columns out - should have been <= and not <, my bad
echo "<td>" . $row[$headersList[$x]] . "</td>"; //Fill td's or th's with column data
} //$x = 1; $x < $amt; $x++
echo "</tr>";
} //$row = mysqli_fetch_array($result)
echo "</table>";
}
?>
$tablename = $_POST['table'];
$result = mysqli_query($con,"SELECT * FROM $tablename");
$first = true;
while($row = mysqli_fetch_assoc($result))
{
if ($first)
{
$columns = array_keys($row);
echo "<table border='1'>
<tr>";
foreach ($columns as $c)
{
echo "<th>$c</th>";
}
echo "</tr>";
$first = false;
}
echo "<tr>";
foreach ($row as $v)
{
echo "<td>$v</td>";
}
echo "</tr>";
}
echo "</table>";
<?php
$table_name = do_not_inject($_REQUEST['table_name']);
$result = mysqli_query($con,'SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME='. $table_name);
?>
<table>
<?php
$columns = array();
while ($row = mysql_fetch_assoc($result)){
$columns[]=$row['COLUMN_NAME'];
?>
<tr><th><?php echo $row['COLUMN_NAME']; ?></th></tr>
<?php
}
$result = mysqli_query($con,'SELECT * FROM course'. $table_name);
while($row = mysqli_fetch_assoc($result)){
echo '<tr>';
foreach ($columns as $column){
?>
<td><?php echo $row[$column]; ?></td>
<?php
}
echo '</tr>';
}
?>
</table>

Display images from database into a Table on PHP

I have some problems creating a table that will display pictures from a php database
This is my code:
$query = "SELECT * FROM images ORDER BY name ASC ";
$result = $db->query($query);
$num_result = $result->num_rows;
echo "<h1> Images</h1>";
$array = array();
for ($i = 0; $i < $num_result; $i++){
$row = $result->fetch_assoc();
$name = $row['name'];
$URL = $row['imageURL'];
$array[] = $URL;
}
//this loop is printing the images correctly in order
foreach ($array as $image){
echo '<img src="'.$image.'"/>';
}
What I am trying to accomplish is to create a table with 2 Columns that will print the images there, something like this
echo '<table>';
echo ' <tr>';
echo ' <td>image 1</td>';
echo ' <td>image 2</td>';
echo ' </tr>';
echo ' <tr>';
echo ' <td>image 3</td>';
echo ' <td> image 4</td>';
echo ' </tr>';
// and so on if there is more images
echo '</table>';
Any suggestions will help , Thanks!
i think you want something like
$i=0;
echo"<table>";
echo"<tr>";
foreach ($array as $image)
{
if($i%2==0 && $i>0 )
echo"</tr><tr>";
echo"<td>";
echo '<img class="coupons" src="'.$image.'"/>';
echo"</td>";
$i++;
}
echo"</tr>";
echo"</table>";

mysql_fetch_array dropping first entry

I am trying to display a list of links based upon mysql output. The code works except that it drops the first record. I know it is because of the duplicate [ #row3 = mysql_fetch_array($result3) ] entries but if i remove one the code fails. Can anyone suggest a fix?
Thanks
<?php
$sql3 = "SELECT `Record_ID`, `Name` FROM `rides` WHERE `Rating` = 3";
$result3=mysql_query($sql3)or die(mysql_error());
//var_dump ($result3);
$num = mysql_num_rows($result3);
while ($row3 = mysql_fetch_array($result3))
{
echo "<table>";
for ($i = 0; $i < $num; $i++){
$row3 = mysql_fetch_array($result3);
//var_dump($row3);
$ridesid = $row3[0];
$rides = $row3[1];
echo "<tr>";
echo "<a href='attraction_page.php?rideID=". urlencode($ridesid) ."'>$rides</a>";
echo "<br />";
echo "</tr>";
}
echo '</table>';
}
?>
You have fetch same thing twice!
Try this:
<?php
$sql3 = "SELECT `Record_ID`, `Name` FROM `rides` WHERE `Rating` = 3";
$result3=mysql_query($sql3)or die(mysql_error());
//var_dump ($result3);
$num = mysql_num_rows($result3);
echo "<table>";
while ($row3 = mysql_fetch_array($result3))
{
$ridesid = $row3[0];
$rides = $row3[1];
echo "<tr>";
echo "<a href='attraction_page.php?rideID=". urlencode($ridesid) ."'>$rides</a>";
echo "<br />";
echo "</tr>";
}
echo '</table>';
?>
Call mysql_fetch_array() only once...

Categories