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>
Related
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 need solution for rows conditional formatting (background) based on time now and time difference, before 48 hours, 84
For example:
if in cell is today's date and time 13:00 /background color =#ffffff
if in cell value is -48 hours different color
if in cell value is -84 different color.
Here is what I have
<?php
$con = mysqli_connect("localhost", "test", "test");
if (!$con) {
die("Database Connection failed".mysqli_error());
} else {
$db_select = mysqli_select_db($con, 'test2');
if (!$db_select) {
die("Database selection failed".mysqli_error());
} else {
}
}
$records = mysqli_query($con, "SELECT * FROM test2 ORDER by user_Name");
$timenow = new DateTime();
?>
<!DOCTYPE html>
<html>
<body>
<table width="500" border="2" cellpadding="2" cellspacing='1'>
<tr bgcolor="#2ECCFA">
<th>User</th>
<th>Device</th>
<th>Last Contact</th>
</tr>
<?php
if ($records) {
while ($row = mysqli_fetch_assoc($records)) {
$calc = $timenow - $row['date'];
if ($calc > 12000) {
$rowClass = "oldOrder";
} else {
$rowClass = "normalOrder";
}
echo '<tr class="'.$rowClass.'"><td>';
echo $row['user_name'] . '';
echo '</td><td>';
echo $row['Device_ID'] . '';
echo '</td><td>';
echo $row['Last_Contact'] . '';
echo '</td></tr>';
}
}
?>
<a href=”rowClass.css” rel=”stylesheet” type=”text/css”>
</table>
Field in DB date is additional column that is set as DATETIME
I keep getting error
Notice: Object of class DateTime could not be converted to int in bla/bal line 50 that is this part
while ($row = mysqli_fetch_assoc($records)) {
$calc = $timenow - $row['date'];
if ($calc > 3) {
$rowClass = "oldOrder";
} else {
$rowClass = "normalOrder";
You can convert all the time information in the Unix Time format, using the time() function in PHP and the UNIX_TIME() function in MySQL and work this way:
<?php
$con = mysqli_connect("localhost","test","test" 'test2');
if ($con->connect_error) {
die('Connect Error: ' . $con->connect_error);
}
$records = mysqli_query($con,"SELECT *, UNIX_TIMESTAMP(date) AS u_date FROM test2 ORDER by user_Name");
$timenow = time();
?>
<!DOCTYPE html>
<html>
<body>
<table width="500" border="2" cellpadding="2" cellspacing='1'>
<tr bgcolor="#2ECCFA">
<th>User</th>
<th>Device</th>
<th>Last Contact</th>
</tr>
<?php
if ($records) {
while ($row = mysqli_fetch_assoc($records)) {
$calc = $timenow - $row['u_date'];
if ($calc > 12000) {
$rowClass = "oldOrder";
} else {
$rowClass = "normalOrder";
}
echo '<tr class="'.$rowClass.'"><td>';
echo $row['user_name'] . '';
echo '</td><td>';
echo $row['Device_ID'] . '';
echo '</td><td>';
echo $row['Last_Contact'] . '';
echo '</td></tr>';
}
}
?>
<a href="rowClass.css" rel="stylesheet" type="text/css">
</table>
You need to compare apples to apples.
If your MySQL date field is of type date/datetime, you can just pass it as argument to DateTime and compare the objects:
$dateToCompare = new \DateTime("-3 days"); // 3 days ago
while ($row = mysqli_fetch_assoc($records)) {
if ($dateToCompare > (new \DateTime($row['date']))) {
$rowClass = "oldOrder";
} else {
$rowClass = "normalOrder";
Hello my first question here, I have a bad of trouble getting this code to work or better said how to proceed.
I have 2 tables, table 1 holds a string separated by commas, after exploding the array I want to compare the strings to another table that holds product prices related to the string.
<?php
function packages()
{
global $db;
$query = "SELECT * FROM product_package WHERE package_id > 1; ";
$result = mysqli_query($db, $query) or die('<h3>Query failed</h3>');
$rowcount = mysqli_num_rows($result);
if ($rowcount < 1)
{
return;
}
else
{
while ($row = mysqli_fetch_array($result))
{
$singles = explode(',', $row["product_ids"]);
$query2 = "SELECT product_price FROM products WHERE product_id = $single; ";
$result2 = mysqli_query($db, $query2);
?>
<tr>
<td><?php echo $row["package_id"] ?></td>
<td><?php echo $row["package_name"] ?></td>
<td><?php echo $row["product_ids"] ?></td>
<td>
EDIT
</td>
<td>
<?php
foreach($singles as $single)
{
echo $single . '<br />';
}
?>
</td>
</tr>
<?php
}
}
}
?>
How do I echo the prices that are overlapping with $single?
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>";
Ok I have this code:
<?
$name=$_POST['name'];
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("juliver", $con);
$result = mysql_query("SELECT * FROM items WHERE id='$name'");
$ss = ""
while($row = mysql_fetch_array($result))
{
$ss .= "<div style='border:1px solid red; float:left; width:100px;'><img src="Images/media'.$row['name'].'" />";
$ss .= "<p>".$row['title']."</p>";
$ss .= "<p>".$row['description']."</p>";
$ss .= "<a href='".$row['link']."'>".$row['link']."</a></div>";
}
mysql_close($con);
?>
<? echo $ss; ?>
Now, I want to organize the display, therefore, I want the record to be set by 3 but the only problem where im stuck is I dont know how to make it to display in by set of 3. Im open for a suggestion, please help me. Thank you.
a function
function sqlArr($sql){
$ret = array();
$res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
if ($res) {
while($row = mysql_fetch_array($res)){
$ret[] = $row;
}
}
return $ret;
}
a code
mysql_connect("localhost","root","");
mysql_select_db("juliver");
$name = mysql_real_escape_string($_POST['name']);
$data = sqlArr("SELECT * FROM items WHERE id='$name'");
$data = array_chunk($data,3);
include 'template.tpl.php';
a template
<table border='1'>
<? foreach ($data as $row): ?>
<tr>
<? foreach ($row as $cell): ?>
<td><?=$cell['id']?><?=$cell['title']?></td>
<? endforeach ?>
</tr>
<? endforeach ?>
</table>
In while loop you can put the condition to know when it executes 3 times then it would go automatically on other line. have a look
int $a=0;
while($row = mysql_fetch_array($result))
{
if($a%3==0){
this will executes only if when the no of record dividable by 3. means 3,6,9,12..
do this..
}else {
do this..
}
$a++;
}