Display results in multiple html tables - php

I have been making a volunteer sign up sheet for our hockey club. I display the results in a table using a while loop.
Everything works good, but I am now wanting to make the output a little more user friendly. I order the query by the date and display it in a single table. What I would like to do is display a new table for each different date. The heading of that table would be the date and the rows would have all events for that date ordered by time. I think this would make it easier for people to find events on a specific date.
Here is how my table is set up: id, date, time, game, name1, name2, name3, name4, name5
Here is the query on my main page:
<?php
//displays the table
$result = mysql_query("SELECT * FROM namestable ORDER BY date, time ASC");
displayTable($result);
?>
This is my function:
<?php
function displayTable($result){
echo "
<table border='1'>
<tr class='top'>
<th class='date'>Date</th>
<th class='time'>Time</th>
<th class='game'>Game</th>
<th class='name'>Name</th>
<th class='name'>Name</th>
<th class='name'>Name</th>
<th class='name'>Name</th>
<th class='name'>Name</th>
<th class='sign'>Sign Up</th>
</tr>";
while($row = mysql_fetch_array($result))
{
//change the date format here
$fdate = date('M jS, Y l', strtotime($row['date']));
echo "<tr>";
echo "<td class='date'>" . $fdate . "</td>";
echo "<td class='time'>" . $row['time'] . "</td>";
echo "<td class='game'>" . $row['game'] . "</td>";
echo "<td class='nameA'>" . $row['name1'] . "</td>";
echo "<td class='nameB'>" . $row['name2'] . "</td>";
echo "<td class='nameA'>" . $row['name3'] . "</td>";
echo "<td class='nameB'>" . $row['name4'] . "</td>";
echo "<td class='nameA'>" . $row['name5'] . "</td>";
$id = $row['id'];
echo "<td class='sign'>" . "<form name='input' action='process.php' method='POST'>
<input type='text' name='name' maxlength='25' value='Enter Name' onfocus=\"if(this.value=='Enter Name') this.value='';\"/>
<input type='hidden' name='id' value='$id'/>
<input type='submit' value='Sign Up' />
<input type='submit' name='dname' value='Remove Name' /></form>" .
"<form name='delete' action='delete.php' method='POST'>
<input type='hidden' name='id' value='$id'/>";
if(isset($_SESSION['user_id'])){
echo "<input type='submit' name='delete' value='Delete Event' /></form>" .
"</td>";
}else{
echo "</form>" .
"</td>";
}
echo "</tr>";
}
echo "</table>";
echo "<br />";
}
?>
Users can enter a name to sign up and delete their name.
The admins can add and delete events.
Any suggestions?

Since your results are already ordered by date, you can just store the previous date in a variable and create a new table every time this changes.
$olddate = '';
while($row = mysql_fetch_array($result))
{
$fdate = date('M jS, Y l', strtotime($row['date']));
if ( $olddate != $fdate ) { // date has changed:
// end the previous table (if it exists)
if ( $olddate != '' ) {
echo "</table>"
}
// start the new table. Do something with $fdate here if you like
echo "
<h3>$fdate</h3>
<table border='1'>
<tr class='top'>
...
</tr>";
}
// print a row as before.
echo "<tr>";
....
}
// end the last table
echo "</table>";
Basically the only thing that has changed is that the $fdate is stored also in $olddate. When we process a new row, if the date has changed (ie $olddate != $fdate), we create a new table.
For each row in the mysql result we still generate a table row as before (you may want to make some changes like not including the Date column any more).

$getdates = mysql_query("SELECT DISTINCT date FROM namestable ORDER BY date DESC");
$dates = mysql_num_rows($getdate);
if($dates > 0){
echo "<table width='100%' cellspacing='0' cellpadding='0'>";
while ($rowdates = mysql_fetch_assoc($getdates)) {
$date = $rowdates['date'];
$getevents = mysql_query("SELECT * FROM namestable WHERE date = '$date' ORDER BY time ASC");
$events = mysql_num_rows($getevents);
if($events > 0){
echo "<tr><td>$date</td></tr>";
while ($rowevents = mysql_fetch_assoc($getevents)) {
$event_time = $rowevents['time'];
// all other info you want to pull here
echo "<tr><td>ROW WITH EVENT INFO HERE</td></tr>";
} // end event loop
} // end if events > 0
echo "</table>";
} // end if dates > 0
} // end date loop

Related

Date record from MYSQL to display in a html table based from the dates Month

I have a table in mysql with 4 columns. 2 VARCHAR fields and 1 date field with NULL field to "Yes" and the default field "NULL" in phpmyadmin. I want to be able to display on a html table that has 12 columns one for each month (eg Jan,feb etc) that date Month in the MYSQL table to the correct month column. For example if my date field has 12/8/2019 i want that to be displayed on the August Column.
I have tried using a date field for every month and the use a form with 12 date pickers but i found that i could not have any empty date fields.
I do not know what code to use to display the date month in a month column but this is the html
<table class="table table-bordered table-sm small">
<thead class="thead-dark">
<tr>
<th>Owner</th>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
<th>T1 Break</th>
<th>Apr</th>
<th>May</th>
<th>Jun</th>
<th>T2 Break</th>
<th>Jul</th>
<th>Aug</th>
<th>Sep</th>
<th>T3 Break</th>
<th>Oct</th>
<th>Nov</th>
<th>Dec</th>
<th>T4 Break</th>
</thead>
</tr>
</Table>
For example if my date field has 12/8/2019 i want that to be displayed on the August Column.
<?php
require 'db.php';
$sqlview="SELECT * FROM booking";
$myData = mysqli_query($con, $sqlview);
while($viewrecord = mysqli_fetch_array($myData))
{
echo "<form action=overseebooking.php method=POST>";
echo "<tr>";
echo "<td>" . $viewrecord['booking_owner'] . " </td>";
echo "<td>" . $viewrecord['booking_jan'] . " </td>";
echo "<td>" . $viewrecord['booking_feb'] . " </td>";
echo "<td>" . $viewrecord['booking_march'] . " </td>";
echo "<td>" . $viewrecord['booking_t1hols'] . " </td>";
echo "<td>" . $viewrecord['booking_april'] . " </td>";
echo "<td>" . $viewrecord['booking_may'] . " </td>";
echo "<td>" . $viewrecord['booking_june'] . " </td>";
echo "<td>" . $viewrecord['booking_t2hols'] . " </td>";
echo "<td>" . $viewrecord['booking_july'] . " </td>";
echo "<td>" . $viewrecord['booking_august'] . " </td>";
echo "<td>" . $viewrecord['booking_september'] . " </td>";
echo "<td>" . $viewrecord['booking_t3hols'] . " </td>";
echo "<td>" . $viewrecord['booking_oct'] . " </td>";
echo "<td>" . $viewrecord['booking_nov'] . " </td>";
echo "<td>" . $viewrecord['booking_dec'] . " </td>";
echo "<td>" . $viewrecord['booking_t4hols'] . " </td>";
echo "<td>" . "<input type=submit class=form-control name=update value=update" . " </td>";
echo "</form>";
}
?>
I don't know exactly how you are getting the data back but here is some idea:
Loop thru all results, get the number of the month number if this date, for each result add a tr in each tr add 12 td's for each month column, when $i in the loop is === to your month number, add it to $html
See code example:
<table class="table table-bordered table-sm small">
<thead class="thead-dark">
<tr>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
<th>Apr</th>
<th>May</th>
<th>Jun</th>
<th>Jul</th>
<th>Aug</th>
<th>Sep</th>
<th>Oct</th>
<th>Nov</th>
<th>Dec</th>
</thead>
</tr>
<?php
$html = "";
require 'db.php';
$sqlview="SELECT * FROM booking";
$myData = mysqli_query($con, $sqlview);
//loop thru the dates (in your case the SQL results)
while($viewrecord = mysqli_fetch_array($myData)){
//get the month number (Jan = 1, Feb = 2 etc.)
$monthNumber = date_parse_from_format("y-m-d", $viewrecord['date'])['month'];
//return A tr for each result
$html .= '<tr>';
//a td for each month
for($i = 0; $i < 12; $i++){
$html .= '<td>';
//if column is current month, display date
$html .= ($monthNumber === ($i+1)) ? $viewrecord['date'] : "";
$html .= '</td>';
}
$html .= '<tr>';
}
echo $html;
?>
<table>
Assuming you are using PDO to query your data and you already selected all records,
all you need to do is to process the main query result with PHP normally like any array like below:
1- Your query:
$data = $pdo->query("SELECT * FROM TABLE")->fetchAll();
2- Creating month array (Put all the months you want):
$myMonth = array('January' => 'Jan','February' => 'Feb','March' => 'Mar');
3- Create arrays for each month based on $data
$January = array_filter($data ,function($val){
if (in_array($val["Jan"], $myMonth)) {
return $val;
}
});
I didn't actually test the code above but the important thing is that I hope you got the idea.
After repeating that for each month, you will end up with 12 separate arrays includes all records you need for each one of them, then you can loop through them in your table into each month.

How to echo the results in PHP

I need to be able to display the results of the submitted form with radio buttons. I want the ID to display with either of the results '1' or '2' from the radio button values.
i.e. ID: 13 - Value: 2
if the row id is set within name the radio buttons work fine but I'm not sure how to display the results from it
if the name of the radio button is set to 'ans' it links all radio buttons together, when I want them working per id
Please see my below code to explain a little better - I'm just not sure how to echo this
<?php
$sql = "SELECT * FROM 'tz_todo' ORDER BY 'position' ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table border='1' width='100%' bordercolor='#000000' style='border-collapse: collapse'><tr class='t1' bgcolor='#00204F'><th>
<font color='#FFFFFF'>ID</font></th><th><font color='#FFFFFF'>CHECK</font></th>
<th width='50'>
<font color='#FFFFFF'>OK</font></th><th width='50'><font color='#FFFFFF'>FAIL</font></th></tr>";
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "<tr><td>
<p align='center'>" . $row["id"] . "</td><td>
<p align='center'>" . $row["text"] . "</td><td align='center' width='50'><input type='radio' name='" . $row["id"] . "' value='1'><br></td>
<td align='center' width='50'><input type='radio' name='" . $row["id"] . "' value='2'><br></td></tr>";
}
echo "</table><input type='submit' value='Submit' name='submit' class='buttons'> ";
} else {
echo "0 results";
}
$conn->close();
?>
</form>
<?php
if (isset($_POST['submit'])) {
echo("????????????????????????????????????");
}
?>
I've now amended it slightly and have it generating results but now the IDs aren't linked to the two radio buttons (1 and 2)
<?php
$sql = "SELECT * FROM `tz_todo` ORDER BY `position` ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table border='1' width='100%' bordercolor='#000000' style='border-collapse: collapse'><tr class='t1' bgcolor='#00204F'><th>
<font color='#FFFFFF'>ID</font></th><th><font color='#FFFFFF'>CHECK</font></th>
<th width='50'>
<font color='#FFFFFF'>OK</font></th><th width='50'><font color='#FFFFFF'>FAIL</font></th></tr>";
// output data of each row
while ($row = $result->fetch_assoc()) {
$rowid = $row["id"];
echo "<tr><td>
<p align='center'>" . $row["id"] . "</td><td>
<p align='center'>" . $row["text"] . "</td><td align='center' width='50'><input type='radio' name='ans' value='1'><br></td>
<td align='center' width='50'><input type='radio' name='ans' value='2'><br></td></tr>";
}
echo "</table><input type='submit' value='Submit' name='submit' class='buttons'> ";
} else {
echo "0 results";
}
$conn->close();
?>
</form>
<?php
if (isset($_POST['ans'])) {
$answer = $_POST['ans'];
if ($answer == "1") {
echo 'OK';
} elseif ($answer == "2") {
echo 'FAIL';
}
}
?>
Please let me know if you would like me to explain a little better or differently
Thanks,
Tom
I think I understand your problem. You should name the radio button this way for example
name='ans" . $row["id"] . "'
This way, radio buttons will work together for each row.
Instead of using the name field within the radio button, I've merged it all into the value field:
<input type='radio' name='radio' value='".$rowid." - 1'>
but then I have the problem that you can only select 1 radio button for all rows

MySQL UPDATE statement is not updating database data through web form

Scenario: I have a web form where the user can manually enter data (see below). The user will submit the form and the data will be automatically added to the database.
date, order_ref, first_name, last_name, postcode, country, quantity, scott_packing, packing_price, courier_price, dispatch_type, job_status
On another page, the user will be able to only view all the jobs that are currently being (this data is taken from the database) processed and add the tracking number and edit the packing_price,courier_price and job_status and submit the new data.
http://i754.photobucket.com/albums/xx182/rache_R/Screenshot2014-04-23at104045_zps2a628d50.png
Issue: When the user clicks the 'submit all' button, the user is supposed to be redirected to the thank you page which simply notifies the user that their entry has been successful however at the moment, the user is only directed to a blank page which contains the navigation menu. I have checked the database to see if the data has been updated but nothing has changed. How do i get my update statement to work so that the user can update the existing jobs?
This is the code for the page that displays all the jobs:
<?
session_start();
if(!session_is_registered(myusername))
{
header("location:../index.php");
}
include("../template/header.php");
include("../controllers/cn.php");
$sql = "SELECT * FROM Jobs";
$qry = mysql_query($sql);
echo "<div class='content'>";
echo "<form class='form_edit' method='post' action='updatejob.php'>";
echo "<table id='job_list' cellpadding='0' cellspacing='0'>
<tr>
<th>Job No</th>
<th>Date</th>
<th>Qty</th>
<th>Postcode</th>
<th>Country</th>
<th>Packed by Scott</th>
<th>Packing Price</th>
<th>Courier Price</th>
<th>Tracking No</th>
<th>Dispatch Type</th>
<th>Job Status</th>
</tr>";
while($row = mysql_fetch_array($qry))
{
echo "<tr>";
echo "<td width='80' style='text-align: center;'>" . $row['order_ref'] . "</td>";
echo "<td width='100' style='text-align: center;'>" . $row['date'] . "</td>";
echo "<td width='100' style='text-align: center;'>" . $row['quantity'] . "</td>";
echo "<td width='100' style='text-align: center;'>" . $row['postcode'] . "</td>";
echo "<td width='100' style='text-align: center;'>" . $row['country'] . "</td>";
echo "<td width='100' style='text-align: center;'><input type='CHECKBOX' id='scott_packing' name='scott_packing' value='". $row['scott_packing'] . "'></td>";
echo "<td width='100' style='text-align: center;'><input type='text' id='packing_price' name='packing_price' value='". $row['packing_price'] . "'/></td>";
echo "<td width='100' style='text-align: center;'><input type='text' id='courier_price' name='courier_price' value='". $row['courier_price']."'/></td>";
echo "<td width='100' style='text-align: center;'><input type='text' id='tracking_number' name='tracking_number' value='". $row['tracking_number'] . "'/></td>";
echo "<td width='100' style='text-align: center;'>" . $row['dispatch_type'] . "</td>";
echo "<td width='100' style='text-align: center;'><select name='job_status' id='job_status'>
<option value='". $row['job_status'] ."'>". $row['job_status']. " <option value='dispatched'>Dispatched</td>";
//echo "<td width='100' style='text-align: center;'><a href='editjob.php'>edit</td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' name='submit' value='submit all'/>";
echo "</form>";
mysql_close();
?>
And here is the code that is supposed to update the data:
<?
session_start();
if(!session_is_registered(myusername)){
header("location:../index.php");
}
include("../template/header.php");
include("../controllers/cn.php");
if (isset($_POST['submit']))
{
$order_ref = $_POST['order_ref'];
$packing_price = $_POST['packing_price'];
$courier_price = $_POST['courier_price'];
$tracking_number = $_POST['tracking_number'];
$job_status = $_POST['job_status'];
$sql_qry = "UPDATE Jobs SET '$packing_price, $courier_price, $tracking_number, $job_status' WHERE order_ref = '$order_ref'";
$query = mysql_query($sql_query);
if(!$query)
{
die('Could not update data' .mysql_error());
} else
{
header("location: updatesuccess.php");
exit;
}
mysql_close();
}
?>
Your update query is wrong. The query of update should be like
UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]
In this case it will be something like
UPDATE Jobs SET packing_price='$packing_price',courier_price='$courier_price',tracking_number='$tracking_number',job_status='job_status' WHERE order_ref = '$order_ref'";
and also you missed out the name property for input fields. If you don't specify it you can't access it like $_POST['packing_price'] where packing_price is be the name of the input field.
Also add method="post" to the form like
echo "<form class='form_edit' action='updatejob.php' method='post'>";
Try with this query
$sql_qry = "UPDATE Jobs SET column1 = '$packing_price', column2 ='$courier_price', .... WHERE order_ref = '$order_ref'";
And like Roland Jansen stated you are missing the name attributes on your input tags

item to hide once overdue by 1 day

Ok, I am fairly new to the PHP scene so please correct anything I might have done wrong ect. I am trying to do a calender list, which as soon as it get to the day after the due date it hides it from view. I currently have it getting the information that is submitted however not sure how to continue from here to make the overdue items hide? I have attached my code below for feedback also.
<?php
include 'db-connect.php';
$result = mysqli_query($con,"SELECT * FROM tasks");
echo "<table border='0' align='center'>
<tr>
<th>Task Title:</th>
<th>Task Description:</th>
<th>Due Date:</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['task'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['datedue'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
You could do it in your MYSQL query
SELECT * FROM tasks WHERE datedue >= CURDATE()
This would only return the rows that were higher or equal to the current date.

show checkbox data mysql

I am having some minor errors and was wondering if anyone could help!
I am creating a attendance system for a college.
This Scenario:
Student logs in successfully and then wants to view his/her attendance for a particular course.
Problem: I want it to show me both checked and uncheked data from mysql, it currently shows an empty checkbox (when its meant to be checked) also at the moment it is showing numerous duplicated data, is it possible i could limit that, say for example show one record per week , it shows all data and duplicates it.
<?php
$q3= "SELECT attendance.week_number_id, attendance.week_number, courses.course_id, students.student_id, course_attendance.present, course_attendance.notes
FROM courses, course_attendance, attendance, students
WHERE course_attendance.student_id= ".$_SESSION['student_id']." AND course_attendance.course_id= courses.course_id AND course_attendance.week_id= attendance.week_number_id AND courses.course_id='101'
";
$result = mysql_query($q3) or die(mysql_error());
echo "<table border='1' align='center'><tr> <th><strong>Week Number</strong></th> <th><strong>Present</strong></th> <th><strong>Notes</strong></th> </tr> ";
while($row = mysql_fetch_assoc($result))
{
extract($row);
echo
"</td><td width='200' align='center'>" .$row['week_number'].
"</td><td width='400' align='center'><input type='checkbox' name='present'" .$row['present'].
"</td><td width='400' align='center'>" .$row['notes'].
"</td><tr>";
}
echo "</table>";
?>
Note: I am connected successfully to database, mysql is up and running, i am using sessions, currently it does show data for the student but does not show the existing checked or uncheked value, the checkbox is empty.
Can anyone help
In your code, you're not properly defining the checkbox to be checked. Make a code that adds checked="true" if the 'present' field is 1.
<?php
$q3 = " SELECT attendance.week_number_id, attendance.week_number, courses.course_id, students.student_id, course_attendance.present, course_attendance.notes
FROM courses, course_attendance, attendance, students
WHERE course_attendance.student_id= ".$_SESSION['student_id']." AND course_attendance.course_id= courses.course_id AND course_attendance.week_id= attendance.week_number_id AND courses.course_id='101'";
$result = mysql_query($q3) or die(mysql_error());
echo "
<table border='1' align='center'>
<tr>
<th><strong>Week Number</strong></th>
<th><strong>Present</strong></th>
<th><strong>Notes</strong></th>
</tr>
";
while($row = mysql_fetch_assoc($result)) {
$checked = '';
if($row['present'] == 1) {
$checked = ' checked="true"';
}
echo "
<tr>
<td width='200' align='center'>" . $row['week_number'] . "</td>
<td width='400' align='center'>
<input type='checkbox' name='present'" .$checked . "/>
</td>
<td width='400' align='center'>" . $row['notes'] . "</td>
</tr>
";
}
echo "</table>";
?>
Your
echo
"</td><td width='200' align='center'>" .$row['week_number'].
"</td><td width='400' align='center'><input type='checkbox' name='present'" .$row['present'].
"</td><td width='400' align='center'>" .$row['notes'].
"</td><tr>";
statement should be
$present = "";
if(.$row['present']==1)
{
$present = "checked =checked/>";
}
else
{
$present = "/>";
}
echo
"" .$row['week_number'].
"" .$row['notes'].
"";
Hope this helps you. Thanks
$checked = '';
if($present == 1) {
$checked = 'checked="checked"';
}
echo "</td><td width='200' align='center'>" .$row['week_number'].
"</td><td width='400' align='center'><input type='checkbox' name='present'" .$checked . "/>" .
"</td><td width='400' align='center'>" .$row['notes'].
"</td><tr>";
}
echo "</table>";
Try.

Categories