I'm new to php and I'm stuck. I have a report that allows a supervisor to show the working hours of the operators. I can show all the hours from the db, but I can't make a sum of all the hours. I want to display one more row that shows the total hours. How can I do that?
<?php
if(isset($_SESSION['username'])) {
$username = $_SESSION['username'];
} else {
header('Location: index.php');
die();
}
include('connect.php');
#$oret_e_punes = $_POST['oret_e_punes'];
#$grupi = $_POST['grupi'];
#$data_e_inserimit = $_POST['data_e_inserimit'];
#$data_e_inserimit_2 = $_POST['data_e_inserimit_2'];
$sql = "select grup_name from grupi";
$result = mysqli_query($dbCon, $sql);
if(!$result) {
die("Error");
}
?>
//html form
<?php
if(isset($_POST['insert'])) {
$insert = "select * from ore where grupi='$grupi' and (data between '$data_e_inserimit' and '$data_e_inserimit_2' and ore !=0)";
$result_insert = mysqli_query($dbCon, $insert);
if(!$result_insert) {
die("Error");
}
echo "<table id='table'>
<tr id='main'>
<td>Operatori</td>
<td>Grupi</td>
<td>Oret e punes</td>
<td>Data</td>
</tr>";
while ($row = mysqli_fetch_assoc($result_insert)) {
$id = $row['id'];
echo "<tr id='sub'>
<td>".$row['usr']."</td>
<td>".$row['grupi']."</td>
<td>".$row['ore']."</td> ---->working hours
<td>".$row['data']."</td>
</tr>";
$id++;
}
echo "</table>";
$_SESSION['$id'] = #$id;
}
?>
have a variable for hours before loop starts. Add in it hour of each record. After loop will end you will have your total hours
echo "<table id='table'>
<tr id='main'>
<td>Operatori</td>
<td>Grupi</td>
<td>Oret e punes</td>
<td>Data</td>
</tr>";
$hours = 0;
while ($row = mysqli_fetch_assoc($result_insert)) {
$id = $row['id'];
echo "<tr id='sub'>
<td>".$row['usr']."</td>
<td>".$row['grupi']."</td>
<td>".$row['ore']."</td> ---->working hours
<td>".$row['data']."</td>
</tr>";
$hours += $row['ore'];
$id++;
}
echo "<tr><td colspan='4'>Total</td><td>$hours</td></tr>";
echo "</table>";
Related
I inserted the data in the table very well and it's showing in phpMyadmin but when i try to display data from the database only one single item is displayed. I need help.
Code and screenshoots below
<?php
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
$count = mysqli_num_rows($result);
if($count > 0){
while($row = mysqli_fetch_assoc($result)){
$incidentId = $row['id'];
$icTypeName = $row['inctype_name'];
$addedBy = $row['added_by'];
$addedOn = $row['added_on'];
}
?>
<tr class="tableData">
<td><?php echo $incidentId;?></td>
<td><?php echo $icTypeName;?></td>
<td><?php echo $addedBy;?></td>
<td><?php echo $addedOn;?></td>
<td><i class="uil uil-edit icon editIcon"></i></td>
</tr>
<?php
} else {
$_SESSION['noData'] = 'No incidents to show!';
}
} else {
die('Failed to Connect!');
}
?>
Images below
I expect to get all the data in the database tables displayed with the "SELECT * FROM nw"
Simply move the } that ends the while loop to after the code that is using the variables. You are currently consuming all the resultset before outputting anything, so you will only see the last rows data.
<?php
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
$count = mysqli_num_rows($result);
if($count > 0){
while($row = mysqli_fetch_assoc($result)){
$incidentId = $row['id'];
$icTypeName = $row['inctype_name'];
$addedBy = $row['added_by'];
$addedOn = $row['added_on'];
//} REMOVED
?>
<tr class="tableData">
<td><?php echo $incidentId;?></td>
<td><?php echo $icTypeName;?></td>
<td><?php echo $addedBy;?></td>
<td><?php echo $addedOn;?></td>
<td><i class="uil uil-edit icon editIcon"></i></td>
</tr>
<?php
} // END OF WHILE LOOP
} else {
$_SESSION['noData'] = 'No incidents to show!';
}
} else {
die('Failed to Connect!');
}
?>
This are my db table:
But my query only get 1 row for each table like this:
As you can see, there are 2 tables for 1003 because it has 2 rows. It should be only one (1) table of 1003 with 2 rows. How do I fix this? EXPECTED RESULT:
// Attempt select query execution
$query = "SELECT model, brand_code FROM smartphone GROUP BY model";
if($result = mysqli_query($db, $query))
{
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
?>
<?php echo $row["brand_code"]?>
<table id="table_stock" class="">
<thead>
<tr>
<th>Model</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row["model"]?></td>
</tr>
</tbody>
</table><br>
<?php
}
/// Free result
mysqli_free_result($result);
}
else
{
echo "<td class='no_record' colspan='7'>No records found.</td>";
}
}
else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
You have at least 5 problems here,
[edit: problem 1 removed & changed sample based on extended answer]
Inside your while { ... } loop, you're printing an entire table, when you should only be printing the <tr>...</tr> part there. This is what causes additional table(s).
And 3rd problem: your "no_record" line is a loose <td>. Not only isn't it inside the table (which is covered in problem #2), it's also not wrapped with a <tr>.
4th problem: You're randomly printing the echo $row["brand_code"] outside of the table.
5th problem: you're printing raw data from the database as if it is valid html, it more than likely is not. it has to be probably encoded with htmlentities/htmlspecialchars.
Quick & dirty fixed version:
function tableOpen($row) {
printf( '<h1>%s</h1>', htmlentities($row["brand_code"]) );
echo '<table id="table_stock" class="">';
echo '<thead>';
echo '<tr>';
echo '<th>Model</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
}
function tableClose() {
echo '</tbody>';
echo '</table><br>';
}
// Attempt select query execution
$query = "SELECT model, brand_code FROM smartphone ORDER BY brand_code";
$lastBrand = null;
if ($result = mysqli_query($db, $query)) {
if (mysqli_num_rows($result) > 0) {
if ($lastBrand !== $row["brand_code"] && !is_null($lastBrand)) tableClose();
if ($lastBrand !== $row["brand_code"]) tableOpen($row);
$lastBrand = $row["brand_code"];
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
printf( '<td>%s</td>', htmlentities($row["model"]) );
echo '</tr>';
}
tableClose();
/// Free result
mysqli_free_result($result);
} else {
echo '<p class="no_record">No records found.</p>';
}
} else {
echo "ERROR: Not able to execute \$query: <br>" . htmlentities($query) . '<br>' . htmlentities(mysqli_error($link));
}
you need additional loop. Also in the first query you need to use group by codes.
$query = "SELECT model, brand_code FROM smartphone GROUP BY brand_code";
if($result = mysqli_query($db, $query))
{
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
?>
<?php echo $row["brand_code"]?>
<table id="table_stock" class="">
<thead>
<tr>
<th>Model</th>
</tr>
</thead>
<tbody>
<?php
if ($result1 = mysqli_query($db, "SELECT DISTINCT model, brand_code FROM smartphone WHERE brand_code={$row["brand_code"]}"))
{
while ($row1 = mysqli_fetch_array($result1))
{
// get count for each model within brand_code
$cnt = ($result2 = mysqli_query($db, "SELECT COUNT(*) AS cnt FROM smartphone WHERE brand_code={$row["brand_code"]} AND model='{$row1["model"]}'")) && ($row2 = mysqli_fetch_array($result2)) ? $row2["cnt"] : "---";
?>
<tr>
<td><?php echo $row1["model"] ({$cnt})?></td>
</tr>
<?php
}
mysqli_free_result($result1);
}
?>
</tbody>
</table><br>
<?php
}
/// Free result
mysqli_free_result($result);
}
else
{
echo "<td class='no_record' colspan='7'>No records found.</td>";
}
}
else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
I currently have this code set up:
$sql = "SELECT * FROM homework WHERE class = '$class'";
$result = mysqli_query($conn, $sql);
$data_exist = false;
if (mysqli_num_rows($result) > 0) {
// output data of each row
$data_exist = true;
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$teacher_set = $row["teacher_set"];
$class = $row["class"];
$name = $row["name"];
$description = $row["description"];
}
}
And then:
<?php if ($data_exist){?>
<p><?php echo $id ?></p>
<p><?php echo $teacher_set?></p>
<p><?php echo $name?></p>
<p><?php echo $description?></p>
<?php
}?>
However, the issue is if there is multiple results in the database it only outputs one of them, how can I prevent this from happening and output two?
I want to make it so every row has their own section, like this: http://prntscr.com/hcgtqn so if there is only one result, one one will show etc.
You have to echo data in a loop. Right now you are reassigning values in while($row = mysqli_fetch_assoc($result)) iterations and printing just the last one.
You need to print each time you read a row from the database.
about the styles, you can represent it in many ways. In the code below I present it in a table.
<table>
<thead>
<tr>
<th>id</th>
<th>teacher set</th>
<th>name</th>
<th>description</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM homework WHERE class = '$class'";
$result = mysqli_query($conn, $sql);
$data_exist = false;
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_array($result)) {
$id = $row["id"];
$teacher_set = $row["teacher_set"];
$class = $row["class"];
$name = $row["name"];
$description = $row["description"];
// you need to print the output now otherwise you will miss the row!
// now printing
echo "
<tr>
<td>".$id."</td>
<td>".$teacher_set."</td>
<td>".$name."</td>
<td>".$description."</td>
</tr>";
}
}
else // no records in the database
{
echo "not found!";
}
?>
</tbody>
</table>
</body>
</html>
I want to create a student time table having 5 subject, 5 teacher, 5 class, per day 5 hour and 5 day in a week. I have completed everything, but now the issue I am facing is if there is no data inserted for specific one day (suppose for Monday there is no class) and i am inserting data for Tuesday. Then the Tuesday data are showing on Monday.
Here's some images:
Output TimeTable(Here the No Class field's time is 2pm-3pm and day is Thursday. But it is showing in Friday and in 10am-11am)
My TimeTable database table
I want to show if there is no row inserted for a day. Then for that day data should be show as 'No Class' in front end time table.
Here I am placing my logic:
<body>
<?php include 'sidenav.php';?>
<article>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<tr>
<th><b>TIME</b></th>
<th><b>Monday</b></th>
<th><b>Tuesday</b></th>
<th><b>Wednesday</b></th>
<th><b>Thursday</b></th>
<th><b>Friday</b></th>
</tr>
<tbody>
<h1>Class Wise Time Table</h1>
<?php
error_reporting(1);
session_start();
if ((isset($_SESSION['login'])) && (isset($_REQUEST['cls']))) {
$classname = $_REQUEST['cls'];
$conn = mysqli_connect("localhost", "root", "", "finaltask");
$sql = "select DISTINCT (time) as time from time_table where classname = "."'$classname'";
$rest = mysqli_query($conn, $sql);
while($t = mysqli_fetch_assoc($rest)){
$sql = "select * from time_table where classname = "."'$classname'". " AND time = '".$t['time']."'";
//print_r($sql);exit;
$res = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($res)){
//echo '<pre>'; print_r ($row);
if(($row['day']=='Monday')&&($row['time']==$t['time'])){
echo"<tr> <td>{$row['time']}</td>";
if(($row['subject']==' ')&&($row['teacher']== ' ')){
echo "<td>No Class</td>";//die('');
}else{
echo "<td>".$row['subject']."<br>".$row['teacher']."</td>";
}
}
if(($row['day']=='Tuesday')&&($row['time'])){
if(($row['subject']=='')&&($row['teacher']=='')){
echo "<td>No Class</td>";
}else{
echo "<td>".$row['subject']."<br>".$row['teacher']."</td>";
}
}
if(($row['day']=='Wednesday')&&($row['time'])){
if(($row['subject']=='')&&($row['teacher']=='')){
echo "<td>No Class</td>";
}else{
echo "<td>".$row['subject']."<br>".$row['teacher']."</td>";
}
}
if(($row['day']=='Thurshday')&&($row['time'])){
if(($row['subject']=='')&&($row['teacher']=='')){
echo "<td>No Class</td>";
}else{
echo "<td>".$row['subject']."<br>".$row['teacher']."</td>";
}
}
if(($row['day']=='Friday')&&($row['time'])){
if(($row['subject']=='')&&($row['teacher']=='')){
echo "<td>No Class</td>";
}else{
echo "<td>".$row['subject']."<br>".$row['teacher']."</td></tr>";
}
}
}
}
}
?>
</tbody>
</table>
This is my suggestion for you to go with. You can define a new array, and store the values from while loop organised by week day names and then use create another array with name of week days you want to look through, and with foreach loop you can check if you get any value for the day that is in current loop and then print No class for day with no data and time tableĀ for the day that have data, look below.
<?php
$days_data = array();
while($row = mysqli_fetch_assoc($res)){
$days_data[$row['day']] == $row;
}
if($days_data):
$week_days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
foreach($week_days as $wday){
if(!empty($days_data[$wday])){
$row_data = $days_data[$wday]; // $row_data contains all of the data from that specific day time,name,teacher and etc...
// there is data for this day
} else {
// no data for the day currently in loop
}
}
endif;
Class Wise Time Table
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<tr>
<th><b>TIME</b></th>
<th><b>Monday</b></th>
<th><b>Tuesday</b></th>
<th><b>Wednesday</b></th>
<th><b>Thursday</b></th>
<th><b>Friday</b></th>
</tr>
<tbody>
<?php
error_reporting(1);
session_start();
if ((isset($_SESSION['login'])) && (isset($_REQUEST['cls']))) {
$classname = $_REQUEST['cls'];
$conn = mysqli_connect("localhost", "root", "", "finaltask");
$sql = "select DISTINCT (time) as time from time_table where classname = "."'$classname'";
$rest = mysqli_query($conn, $sql);
while($t = mysqli_fetch_assoc($rest)){
$sql = "select * from time_table where classname = "."'$classname'". " AND time = '".$t['time']."'";
//print_r($sql);exit;
$res = mysqli_query($conn, $sql);
?>
<tr>
<td><?php echo $t['time'] ?></td>
<?php
$week_days = array('Monday','Tuesday','Wednesday','Thurshday','Friday');
$classes = array();
while($row = mysqli_fetch_assoc($res)) {
$classes[$row['day']] = $row;
}
foreach ($week_days as $day) {
?>
<?php if (array_key_exists($day, $classes)) { $row = $classes[$day]; ?>
<td><?php echo $row['subject'] . '<br />' . $row['teacher'] ?></td>
<?php } else { ?>
<td>No Class</td>
<?php } ?>
<?php
}
?>
</tr>
<?php
}
?>
</tbody>
</table>
I need some help with php code.
I want to put data that I get with this query into simple php/html table.
Can you guys help me with this?
<?php
$konekcija=mysqli_connect("111.111.111.111","test","test123","publishers_instagram_accounts");
if (mysqli_connect_errno())
die ("Error:".mysqli_connect_error());
$select = "select * from publishers_instagram_accounts where followed_id <= (select followed_id from publishers_instagram_accounts where username='zika')*1.2 and followed_id >= (select followed_id from publishers_instagram_accounts where username='zika')*0.8";
$stmt = mysqli_prepare($konekcija, $select);
if (mysqli_error($konekcija))
die ("Error:" . mysqli_error($konekcija));
mysqli_stmt_bind_param($stmt, "ss", $trazi1, $trazi2);
if (!mysqli_stmt_execute($stmt))
{
die ("Error:" . mysqli_stmt_error($stmt));
}
else
{
$rezultat = mysqli_stmt_get_result($stmt);
while ($red = mysqli_fetch_array($rezultat, MYSQL_ASSOC))
{
var_dump($red);
}
}
while ($red = mysqli_fetch_array($rezultat, MYSQL_ASSOC)) {
echo "
<table>
<thead>
<td>Username</td>
<td>Password</td>
</thead>
<tbody>
<td>$red['username']</td>
<td>$red['password']</td>
</tbody>
</table>
";
}
Something like this.. Hope it helps you.
Hello #filipche you can try by replacing your 'while' loop with following code:
echo "<table><thead><tr><th>Field 1</th><th>Field 2</th><th>Field 3</th></tr></thead>";
while ($red = mysqli_fetch_array($rezultat, MYSQL_ASSOC)) {
echo "<tbody><tr>";
echo "<td>".$red["field1"]."</td>";
echo "<td>".$red["field2"]."</td>";
echo "<td>".$red["field3"]."</td>";
echo "</tr><tbody>";
}
echo "</table>";
Hope this is what you are looking for...
$qstring = "Your query goes here";
$result = mysql_query($qstring);
if($result)
{
while($row = mysql_fetch_array($result))
{
echo "<table>";
echo "<td>".$row[column name]."</td>";echo "</table>";
}