Need to display retrieved data from mysql table in table format, but not getting the right display i planned for. This is how i want it to look like
Target display
But this is what am getting based on my code
Current display
This is the html code
<div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$staff_set = find_all_employee();
while ($staff = mysqli_fetch_assoc($staff_set)) {
//display staff first name, last name, department and action (edit and delete links)
?>
<tr>
<td>
<?php
echo $staff["first_name"];
}
?>
</td>
<?php
$staff_set = find_all_employee();
while ($staff = mysqli_fetch_assoc($staff_set)) {
//display staff last name
?>
<td>
<?php
echo $staff["last_name"];
}
?>
</td>
<?php
$staff_set = find_all_employee();
while ($staff = mysqli_fetch_assoc($staff_set)) {
//display staff department
?>
<td>
<?php
echo $staff["department"];
}
?>
</td>
<td>
<span class="glyphicon glyphicon-pencil"> Edit</span>
 
<span class="glyphicon glyphicon-trash"> Delete</span>
</td>
</tr>
</tbody>
</table>
</div>
Here is the function that am using to find the list of all employee from the my staff table
function find_all_employee() {
global $connection;
$query = "select * from staff";
$staff_set = mysqli_query($connection, $query);
confirm_query($staff_set);
return $staff_set;
}
Is there a better way to write the loop and display my data in the right way? I have looked up similar threads but still not able to grasp.
I dont understand why you calling so much tie function and also why making so much loops. Let me try fi your code:
<div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$staff_set = find_all_employee();
while ($staff = mysqli_fetch_assoc($staff_set)) {
//display staff first name, last name, department and action (edit and delete links)
?>
<tr>
<td>
<?php echo $staff["first_name"]; ?>
</td>
<td>
<?php echo $staff["last_name"]; ?>
</td>
<td>
<?php echo $staff["department"]; ?>
</td>
<td>
<span class="glyphicon glyphicon-pencil"> Edit</span>
 
<span class="glyphicon glyphicon-trash"> Delete</span>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
Since OP asked for a varying technique in a comment:
<?php
// Select all staff first name, last name, and department info
$staff_set = find_all_employee();
$staff_results = array();
while ($staff = mysqli_fetch_assoc($staff_set)) {
$staff_results[] = $staff;
}
?>
<div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php if (!empty($staff_results)): ?>
<?php foreach($staff_results as $staff_member): ?>
<tr>
<td>
<?= $staff_member["first_name"]; ?>
</td>
<td>
<?= $staff_member["last_name"]; ?>
</td>
<td>
<?= $staff_member["department"]; ?>
</td>
<td>
<span class="glyphicon glyphicon-pencil"> Edit</span>
 
<span class="glyphicon glyphicon-trash"> Delete</span>
</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr><td colspan="4">No Staff Members Found.</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>
This also introduces the short tag syntax (<?=) for replacing calls to echo. Read this thread if your PHP version is < 5.4. This also introduces control structure alternate syntax, for better readability with HTML. The above approach separates the concern of selecting data from the DB from the presentation of the HTML. This allows for easier debugging, as well as future adaptation and modularization of your application.
Related
I am updating a plugin for WordPress using .php files. I have an update that requires a table and pagination. I'm trying to use jQuery for this.
I followed the datatable instructions but I am not seeing any changes applied. What am I doing wrong? I have added the jQuery to use the correct table id. Nothing seems to work doing this.
I am attempting to run through the data and allow the page to paginate. I do not care about having the option for user to select number of items on page at given time.
I am looking for an easy way to paginate a HTML table. Nothing I've tried seems to work
jQuery(document).ready(function() {
jQuery('#engine-search-table').DataTable();
});
<div class="parts-table">
<table id="engine-search-table" class="display">
<thead>
<tr id="header-row">
<th class="table-header">
<h4>Manufacturer</h4>
</th>
<th class="table-header">
<h4>Model</h4>
</th>
<th class="table-header">
<h4>Description</h4>
</th>
<th class="table-header">
<h4>Series</h4>
</th>
<th class="table-header">
<h4>Engine</h4>
</th>
<th class="table-header">
<h4>Years</h4>
</th>
<th class="table-header">
<h4>Begin Serial</h4>
</th>
<th class="table-header">
<h4>Engine Serial</h4>
</th>
</tr>
</thead>
<tbody>
<?php
if (!empty($parts)) {
foreach ($parts as $part) {
$meta = get_post_meta($part->ID); ?>
<tr id="data" class="parts-row">
<td class="part-container-title">
<?php printCFTVal($meta, 'manufacturer');?>
</td>
<td class="part-container-manufacturer">
<a href='<?php echo ($part->guid); ?>'>
<?php printCFTVal($meta, 'model'); ?>
</a>
</td>
<td class="part-container-category">
<?php printCFTVal($meta, 'description'); ?>
</td>
<td class="part-container-excerpt">
<?php echo str_replace('OVERVIEW', '', get_the_excerpt($part)); ?>
</td>
<td class='part-container-button'>
<?php echo ($part->guid); ?>'>
<?php printCFTVal($meta, 'enginemodel'); ?>
</td>
<td>
<?php printCFTVal($meta, 'years');?>
</td>
<td>
<?php printCFTVal($meta, 'beginSerial');?>
</td>
<td>
<?php printCFTVal($meta, 'engineserial');?>
</td>
</tr>
<?php }
} else {
echo '<p>No parts found.</p>';
} ?>
</tbody>
</table>
</div>
Right now I am writing a program in core PHP, but I am not able to write a perfect oops concept as I m rewriting the code many times.
for Eg.
public function all()
{
$init = new Database;
$sql = "SELECT * FROM categories";
// Result
$result = $init->conn->query($sql);
if ($result->num_rows > 0) {
?>
<table class="table table-hover">
<thead>
<tr>
<th>Action</th>
<th scope="col">id</th>
<th scope="col">Category name</th>
<th scope="col">Category description</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td>
<form action="update_categories.php?update_id=<?php echo $row['id'] ?>" method="post">
<button type="submit" class="btn btn-primary" name="update">Update</button>
</form>
<form action="delete_categories.php?id=<?php echo $row['id'] ?>" method="post">
<button type="submit" name="delete" class="btn btn-danger">Delete</button>
</form>
</td>
<td><?php echo $row["id"] ?></td>
<td><?php echo $row["category_name"] ?></td>
<td><?php echo $row["category_description"] ?></td>
</tr>
<?php
} ?>
</tbody>
<?php
} else {
?>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">Category name</th>
<th scope="col">Category description</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">no result</th>
<td>no result</td>
<td>no result</td>
</tr>
</tbody>
<?php
}
}
So here you can see I have to fetch the data from the database but in this function, I have included Html too so if you want to add database data to another place I have to write the function for that again and fetch the values.
Is there any way I can only fetch the value and i can use the function to print it wherever I want.
Not sure if this is the best way to do this.. But..
I've got a PHP file calling an API and doing a foreach loop to get all the data I want. Previously I was calling this from my html file and putting it into a table, the problem was that it was all appearing as technically the same row and that made formatting hard.
This is what I used to have:
<div class="bgimg2 w3-container">
<table class="w3-table w3-medium w3-text-white">
<tr class="w3-blue-gray">
<th> <h5> Name </h5> </th>
<th> <h5> Size </h5> </th>
<th> <h5> % </h5> </th>
</tr>
<tr>
<th> <h4> <div id=queue> </h4> </th>
<th> <h4> <div id=queueSize> </h4> </th>
<th> <h4> <div id=queuePercent> </h4> </th>
</tr>
</thead>
</table>
</div>
I want to have each item appearing as it's own row, so I'm trying to do a php foreach loop directly in the table.
This is what I've got currently for the table:
<table class="w3-table w3-medium w3-text-white">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php
require_once ('/api/sabapiQueue.php');
foreach ($apiResultDataShift as $row){
?>
<tr>
<td> <?php $row['filename'] ?> </td>
</tr>
<?php } ?>
</tbody>
</table>
It's not working and the file isn't even loading. Not sure where I've gone wrong.
Any help would be greatly appreciated.
Seems you missed a detail in not echoing out your variable. I would also suggest, and perhaps introduce you to alternative PHP syntax which is good for when you want to tightly integrate a few php blocks into html as you are doing here:
<table class="w3-table w3-medium w3-text-white">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php
require_once ('/api/sabapiQueue.php');
foreach ($apiResultDataShift as $row):
?>
<tr>
<td><?= $row['filename'] ?></td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
Alternatively, as per the echo manual page you could just change this line:
<td><?php echo $row['filename']; ?></td>
<?php session_start();
require_once('SessionSet.php');
require_once('connection.php');
include('top.php');
if(isset($_GET['ClassID']) && isset($_GET['SectionId']) )
{
$ClassID = $_GET['ClassID'];
$SectionId = $_GET['SectionId'];
$ClassName = $_GET['ClassName'];
$SectionName = $_GET['SectionName'];
$GetCurrentMonth = date('M');
/* select latest Academic Year*/
$GetAcademicQ = "select * from study_year order by StudyYearId desc limit 1";
$GetAcademicQR = mysqli_query($con,$GetAcademicQ);
$GetAcademicRow = mysqli_fetch_assoc($GetAcademicQR);
$AcademciyearId = $GetAcademicRow['StudyYearId'];
$YearName = $GetAcademicRow['YearName'];
/* Get Students and Students Class + Fee Records */
$GetStudentClassQ = "select * from studentclass where
AcademicYearId='$AcademciyearId' and StudentClassId='$ClassID'
and StudentSectionId='$SectionId';";
$GetStudentClassQR = mysqli_query($con,$GetStudentClassQ);
$GetStudentClassNum = mysqli_num_rows($GetStudentClassQR);
if($GetStudentClassNum>0)
{
while($GetStudentClassRow = mysqli_fetch_assoc($GetStudentClassQR))
{
$StudentID = $GetStudentClassRow['StudentID'];
$RollNumber = $GetStudentClassRow['RollNumber'];
$RollNumber = $GetStudentClassRow['RollNumber'];
$StuentClassFee = $GetStudentClassRow['StuentClassFee'];
/* Get Stduent Name and bio data */
$GetStudentQ = "select * from students where Student_ID='$StudentID';";
$GetStudentQR = mysqli_query($con,$GetStudentQ);
$GetStudentRow = mysqli_fetch_assoc($GetStudentQR);
$Name = $GetStudentRow['Name'];
$FatherName = $GetStudentRow['FatherName'];
$StudentPhoto = $GetStudentRow['StudentPhoto'];
$Student_ID = $GetStudentRow['Student_ID'];
/* Get Stduent Name and bio data */
$GetfeeQ = "select * from fee where FeeStudentID='$StudentID';";
$GetfeeQR = mysqli_query($con,$GetfeeQ);
$GetfeeRow = mysqli_fetch_assoc($GetfeeQR);
$FeeAmount = $GetfeeRow['FeeAmount'];
$FeePaid = $GetfeeRow['FeePaid'];
?>
<div id="page-wrapper">
<div class="container-fluid">
<div class="row">
<table class="table table-hover table-bordered print-table" style="width:100% !important" align="center">
<!--Office Copy-->
<tr class="warning">
<th>Student ID</th>
<td>
<?php echo $Student_ID;?>
</td>
<th>Class</th>
<td>
<?php echo $ClassName;?>
</td>
<th>Section Name</th>
<td>
<?php echo $SectionName;?>
</td>
<th>Roll Number</th>
<td>
<?php echo $RollNumber;?>
</td>
<th>Academic Year </th>
<td>
<?php echo $YearName;?>
</td>
<!--td rowspan="5" style="text-align:center"><img src="StudentImages/<?php/* echo $StudentPhoto;*/?>"/ alt="Stdudent Image not found" style="width:150px; height:200px"></td-->
</tr>
<tr class="warning">
<th>Student Name</th>
<td colspan="4">
<?php echo $Name;?>
</td>
<th>Father Name</th>
<td colspan="4">
<?php echo $FatherName;?>
</td>
</tr>
<tr class="warning">
<th>Fee Month </th>
<td>
<?php echo $GetCurrentMonth;?>
</td>
<th>Fee Amount</th>
<td style="font-size:20px;">
<?php echo $StuentClassFee;?>
</td>
<th>Previous Dues</th>
<td style="font-size:20px;">
<?php echo $FeeAmount-$FeePaid;?>
</td>
<th>Due Date</th>
<td>12 -
<?php echo $GetCurrentMonth;?>
</td>
<th>After Due Date</th>
<td>
<?php echo $StuentClassFee+50;?>
</td>
</tr>
<hr>
<!--Student Copy-->
</table>
<table class="table table-hover table-bordered print-table" style="width:100% !important" align="center" >
<tr class="warning">
<th>Student ID</th>
<td>
<?php echo $Student_ID;?>
</td>
<th>Class</th>
<td>
<?php echo $ClassName;?>
</td>
<th>Section Name</th>
<td>
<?php echo $SectionName;?>
</td>
<th>Roll Number</th>
<td>
<?php echo $RollNumber;?>
</td>
<th>Academic Year </th>
<td>
<?php echo $YearName;?>
</td>
<!--td rowspan="5" style="text-align:center"><img src="StudentImages/<?php/* echo $StudentPhoto;*/?>"/ alt="Stdudent Image not found" style="width:150px; height:200px"></td-->
</tr>
<tr class="warning">
<th>Student Name</th>
<td>
<?php echo $Name;?>
</td>
<th>Father Name</th>
<td>
<?php echo $FatherName;?>
</td>
<th>Fee Month </th>
<td>
<?php echo $GetCurrentMonth;?>
</td>
<th>Fee Amount</th>
<td style="font-size:20px;">
<?php echo $StuentClassFee;?>
</td>
<th>Previous Dues</th>
<td style="font-size:20px;">
<?php echo $FeeAmount-$FeePaid;?>
</td>
</tr>
</table>
</div>
<!-- row end here -->
</div>
</div>
<!-- page-wrapper end here -->
<?php
}
}
else
{
}
?>
<?php
}
?>
i am building a php school management application. when I try to print report after pressing ctrl+p one recored is being shown on each page. but i want to show atleast 3 records on a single page after giving printing command. I have attached snapshots of records before and after print command.
enter image description here
here is picture after giving print command
enter image description here
Your question has nothing to do with PHP, this is a question about CSS, specifically print styles.
In the absence of an example of plain HTML and CSS, take a look at the CSS properties page-break-before, page-break-after and page-break-inside. In this case you may want to look at something like:
#media print {
.row, table {
page-break-before: avoid;
page-break-after: avoid;
}
}
Learn a little bit more about the page-break-* properties at this CSS Tricks article. If you are generally new to CSS print styles, I wrote a primer for .NET magazine.
Bear in mind that whatever styles you use, these are only suggestions to the browser. Paper size, page orientation, font size, zoom level, user margins, etc. will all work together to mess with your plans. Content that you force to be too wide for the page may also be an issue.
Finally, don't use any inline styles. Use your CSS file for that (so get rid of that width:100% inline style I saw.
If you want more help, post the raw HTML output and your CSS.
This is the code, and as written in the title, I'm trying to have each column with the same width:
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>N°</th>
<th>Data</th>
<th>Compratore</th>
<th>Venditore</th>
<th>Merce</th>
<th>Quantita'</th>
<th>Prezzo</th>
<th>Pagamento</th>
<th>Azioni</th>
</tr>
</thead>
<tbody>
<?php
$result = query("SELECT numero_contratto, anno_contratto, data, C.denominazione as compratore, V.denominazione as venditore, merce, quantita, info_quantita, prezzo, info_prezzo, pagamento FROM Contratti, Societa V, Societa C WHERE compratore = C.id_societa and venditore = V.id_societa ORDER BY data DESC", $conn);
foreach($result as $r)
{
?>
<tr>
<td>
<?php echo $r['numero_contratto']."/".$r['anno_contratto']; ?>
</td>
<td>
<?php
$data = explode("-", $r['data']);
echo $data[2]."-".$data[1]."-".$data[0];
?>
</td>
<td>
<?php echo $r['compratore']; ?>
</td>
<td>
<?php echo $r['venditore']; ?>
</td>
<td>
<?php echo $r['merce']; ?>
</td>
<td>
<?php echo $r['quantita']." ".$r['info_quantita']; ?>
</td>
<td>
<?php echo $r['prezzo']." ".$r['info_prezzo']; ?>
</td>
<td>
<?php echo $r['pagamento']; ?>
</td>
<td>
<span class="glyphicon glyphicon-remove"></span>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
And this is the output on my web page:
But if you see this Fiddle with static values putted into the table, the problem doesn't show!
http://jsfiddle.net/brVa9/1/
So the problem is inside the db schema or structure.
What I should do to have all the columns with the same width?