Each value is displayed in a new row HTML table - php

I'm currently working on a small website where my colleagues can see their work times.
My "rooster" table looks like this:
rooster_id int(2) Auto_increment primary key.
personeel_id int(2) //personeel means staff in the Dutch language
(I'm using inner join to get the person's name).
dag varchar(10) //dag means day.
start varchar(5) //start is the time when the employee has to start
working.
eind varchar(5) //eind means end.
datum date
weeknummer int(2) //weeknummer means weeknumber.
Display:
---------------------------------------------------------------------
|rooster_id|personeel_id|dag |start|eind |datum |weeknummer|
|1 |1 |Tuesday |12:00|21:00|2016-05-10|19 |
|1 |1 |Wednesday|15:00|21:00|2016-05-11|19 |
|1 |2 |Monday |08:00|18:30|2016-05-10|19 |
---------------------------------------------------------------------
My php code above HTML tag:
include_once 'config.php';
$people = "SELECT DISTINCT * FROM personeel INNER JOIN rooster ON rooster.personeel_id = personeel.id";
$result = mysql_query($people);
My php code inside the table tag:
<table cellpadding="1" width="100%" cellspacing="1" class="box-inhoud">
<?php
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['naam']."</td>";
if($row['dag'] === "maandag"){
echo "<td class='td-midden'>".$row['start']." - ".$row['eind']."</td>";
} else {
echo "<td class='td-midden'>Vrij</td>";
}
if($row['dag'] === "dinsdag"){
echo "<td class='td-midden'>".$row['start']." - ".$row['eind']."</td>";
} else {
echo "<td class='td-midden'>Vrij</td>";
}
if($row['dag'] === "woensdag"){
echo "<td class='td-midden'>".$row['start']." - ".$row['eind']."</td>";
} else {
echo "<td class='td-midden'>Vrij</td>";
}
if($row['dag'] === "donderdag"){
echo "<td class='td-midden'>".$row['start']." - ".$row['eind']."</td>";
} else {
echo "<td class='td-midden'>Vrij</td>";
}
if($row['dag'] === "vrijdag"){
echo "<td class='td-midden'>".$row['start']." - ".$row['eind']."</td>";
} else {
echo "<td class='td-midden'>Vrij</td>";
}
if($row['dag'] === "zaterdag"){
echo "<td class='td-midden'>".$row['start']." - ".$row['eind']."</td>";
} else {
echo "<td class='td-midden'>Vrij</td>";
}
if($row['dag'] === "zondag"){
if($row['start'] === "00:00:00"){
echo "<td class='td-midden'>Feestdag</td>";
} else {
echo "<td class='td-midden'>".$row['start']." - ".$row['eind']."</td>";
}
} else {
echo "<td class='td-midden'>Vrij</td>";
}
echo "</tr>";
}
?>
</table>
This displays the data like this:
------------------------------------------------------
|Employee's|Monday |Tuesday |Wednesday |
|Tom |Vrij |12:00 - 21:00|Vrij |
|Tom |Vrij |Vrij |15:00 - 21:00|
|Jack |08:00 - 18:30|Vrij |Vrij |
------------------------------------------------------
It also displays Thursday, Friday, Saturday, Sunday but I didn't want to write all of the days here on stackoverflow since those will have "vrij" as value.
But the thing is, I want to have it like this:
------------------------------------------------------
|Employee's|Monday |Tuesday |Wednesday |
|Tom |Vrij |12:00 - 21:00|15:00 - 21:00|
|Jack |08:00 - 18:30|Vrij |Vrij |
------------------------------------------------------
So every employee does only have 1 row with all their times listed in it.
I'm new on this website so I hope I explained everything good, so you guys can easily help me.
And yes I know, I'm using MySQL because I want to keep it simple and I have never worked with MySQLi nor PDO.

Here we go entire example:
<?php
$con = mysqli_connect('localhost', 'root', '', 'test') or die(mysqli_error($con));
$query = "SELECT * FROM personeel INNER JOIN rooster ON rooster.personeel_id = personeel.id";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
if (mysqli_num_rows($result) > 0) {
$arr = array();
$nam = array();
$day = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
while ($row = mysqli_fetch_assoc($result)) {
$nam[$row['id']] = $row['naam'];
$arr[$row['id']][$row['dag']] = $row['start'] . ' - ' . $row['eind'];
}
echo '<table border="1">';
echo '<tr>';
echo '<td>Employees</td>';
foreach($day as $d){
echo '<td>'.$d.'</td>';
}
echo '</tr>';
foreach ($nam as $k=>$n){
echo '<tr>';
echo '<td>'.$n.'</td>';
foreach ($day as $d){
if(isset($arr[$k][$d])){
echo '<td>'.$arr[$k][$d].'</td>';
}else{
echo '<td>Virj</td>';
}
}
echo '</tr>';
}
echo '</table>';
}
?>
Output is:
Some details:
$nam array stores all fetched names from personeel together with his id relation.
Array
(
[1] => Tom
[2] => Jack
)
$arr is main array which contains relation between personeel id and his data:
Array
(
[1] => Array
(
[Tuesday] => 12:00 - 21:00
[Wednesday] => 15:00 - 21:00
)
[2] => Array
(
[Monday] => 08:00 - 18:30
)
)
$day is static array with week days.
Walking trough both $nam and $arr arrays gives you desired table without any duplicates. In addition provided solution uses mysqli_* functions, so you can see how to use them.
Hope this will help you to solve your problem. :)

Related

how to print query mysql in php like this

I am newbie in php.
I have one table in mysql (example)
id id_pro name
1 2 budi
2 1 adi
3 1 steve
4 1 jono
5 2 galang
I would like to print like this
id_pro name
1 adi, steve, jono
2 budi, galang
I have made,but look like this. (Please help me)
id_pro name
1 adi
1 steve
1 jono
2 budi
2 galang
<?php
$a = mysql_connect("localhost","root","admin");
mysql_select_db("aa");
$result = mysql_query("select * from member");
echo '<table>
<tr><th>id_pro</th><th>name</th></tr>';
while($data = mysql_fetch_array($result))
{
echo '<tr>';
echo '<td> '. $data["id_pro"] .'</td>';
echo '<td> '. $data["name"] .'</td>';
echo '</tr>';
}
?>
<?php
$result = array();
$data = array(array("id_pro"=>1,"name"=>"adi"),array("id_pro"=>2,"name"=>"budi"),array("id_pro"=>1,"name"=>"steve"));
foreach($data as $value){
$result[$value["id_pro"]][] = $value["name"];
}
foreach($result as $key => $value){
echo $key . "----" . implode("," , $value);
echo "\n";
}
?>
or you can change your sql like this :
select id_pro,group_concat(name) from member group by id_pro;

php two dimensional array with sums to table

im in serach for a generic piece of code that uses and array
$arr[$key1][$key2] = $value;
output should be like this where "SUM" is not part of the array.
| 1st key2 | 2nd key2 | 3rd key2 | SUM
1st key1 | 10 | 10 | 10 | 30
2nd key1 | 10 | 10 | 10 | 30
3rd key1 | 10 | 10 | 10 | 30
SUM | 30 | 30 | 30 | 90
so i startet an output to see how far i get:
echo '<table width="100%"><thead>';
foreach($arr as $linekey => $line)
{
echo '<tr>';
echo '<th align="center">';
echo '</th>';
foreach($line as $key => $value)
{
echo '<th align="center">';
echo $key;
echo '</th>';
}
echo '<th align="center">';
echo 'SUM'; //adding the SUM column
echo '</th>';
echo '</tr>';
break;
}
echo '</thead><tbody>';
foreach($arr as $key1 => $value1)
{ echo '<tr>';
echo'<td>'.$key1.'</td>';
$sumRow = 0; //reset sumRow
foreach($arr[$key1] as $key2 => $value2)
{
echo'<td>'.round($arr[$key1][$key2],0).'</td>';
$sumRow += $arr[$key1][$key2]; //summing up rows
$sumAll += $arr[$key1][$key2]; //summing up everything
$sumCol += $arr[$key1][$key2]; //where should be this?
}
echo'<td>'.round($sumRow,0).'</td>'; //echo $sumRow
echo '</tr>';
}
echo '</tbody></table>';
this alaredy works but im not sure where to sum the columns
You should use an array $sumCol to gather columns sums:
$sumCol[$key2] += $arr[$key1][$key2];
It size should be as number of columns.
You cannot do it in one loop without an array because you loop over columns index internally, so you could gather only sumRow in one temporary variable (without array).
Then, at the end:
echo '<tr><td>SUM</td>';
foreach($sumCol as $key2 => $value2)
{
echo'<td>'.round($sumCol[$key2],0).'</td>'; //echo $sumCol
}
echo '</tr>';
echo '</tbody></table>';
The other way is to define the second loop where you iterate first over columns and at second, internally, over rows.

Date sorting from input in php from sql?

I'm doing a sales recording for my own small shop using the combination of html and php.
I want to have a time selecting input (something like March 2014, April 2014 when selecting)Here is my index.php
<?php
$con=mysqli_connect("192.168.1.248","a","a","services");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM pcsales");
echo "<h3><marquee><b>====PC Sales====</b></marquee></h3>";
echo "<button type='button' name='add' onClick='add()'>Add</button>";
echo "<script type='text/javascript'>
function add()
{
window.location='./edit';
}
</script>";
echo "<button type='button' name='edit' onClick='edit()'>Edit</button>";
echo "<script type='text/javascript'>
function edit()
{
window.location='./edit/edit.html';
}
</script>";
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Type</th>
<th>If Others</th>
<th>Brand</th>
<th>Description</th>
<th>Date Sold</th>
<th>Serial No.</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td><p style='font-size:12px'>" . $row['ID'] . "</p></td>";
echo "<td>" . $row['Type'] . "</td>";
echo "<td>" . $row['Types'] . "</td>";
echo "<td>" . $row['Brand'] . "</td>";
echo "<td><p style='font-size:12px'>" . $row['Description'] . "</p></td>";
echo "<td><p style='font-size:12px'>" . $row['Selldate'] . "</p></td>";
echo "<td><p style='font-size:12px'>" . $row['Serial'] . "</p></td>";
echo "</tr>";
}
echo "</table>";
echo "<h3><marquee direction=right><b>====PC Sales====</b></marquee></h3>";
mysqli_close($con);
?>
I'm using TIMESTAMP in mysql.
Here is the output
ID Type If Others Brand Description Date Sold Serial No.
1 Notebook Acer E1 Add 2GB DDR3 1600 RAM 2014-06-25 11:57:58 123456789
2 Others AIO Asus N/A 2014-07-25 12:52:12 987654321
3 Desktop Trendsonic Full spec listed. 2014-07-30 09:55:10 N/A
When I put a range on a textbox (more expedient if selectable)
Example:
July 2014
Then the output shall be
ID Type If Others Brand Description Date Sold Serial No.
2 Others AIO Asus N/A 2014-07-25 12:52:12 987654321
3 Desktop Trendsonic Full spec listed. 2014-07-30 09:55:10 N/A
If only2014 is inserted, then it shall output everything from Year 2014
Is it possible to do that?
Note: I don't care about exploits, as it is used internally.
Better to take datepicker and fetch date from there in variable and make query some how like this.
SELECT *
FROM TABLENAME
WHERE MONTH(dateColumnname) = $month AND YEAR(dateColumnname) = $year;
EXAMPLE: SELECT *
FROM testerpractice
WHERE MONTH(datess) = '03' AND YEAR(datess) = '2017';
Best OF Luck...
Better use dropdown lists ( tag) for day, month and year (with defaults 01 for day, Jan for month, current year for the year) , and concatenate their values to get the search string. You'll have expediency and will be able to construct the query easy.
-Problem Solved-
My solution(not that "professional")
I do a sort.html and a sort.php
The sort.html contains
Year:<input type="text" value="" name="year"/>
Month:<input type="text" value="" name="month"/>
While the sort.php contains
$year = mysqli_real_escape_string($con, $_POST['year']);
$month = mysqli_real_escape_string($con, $_POST['month']);
$result = mysqli_query($con,"SELECT * FROM pcsales WHERE Selldate >= '$year-$month-01 00:00:00' AND Selldate <= '$year-$month-31 23:59:59'");
This might be a reference for other people who might encounter the same problem as me~

Custom Table using SQL query and php

I am trying to create a table from a sql query but I need to only have 3 results per table row.
I have a list of about 47 names so I have no problem printing them on a new line but how would I go about creating a table where the while loop would print a table row then print 3 table data cells with the query then create a new row for the next 3 values?
Exa:
result_1 | result_2 | result_3
result_4 | result_5 | result_6
result_7 | result_8 | result_9
Current While Loop:
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td><input type='checkbox'> ".$row['name']."</td>";
echo "<td><input type='checkbox'> ".$row['name']."</td>";
echo "<td><input type='checkbox'> ".$row['name']."</td>";
echo "</tr>";
}
Database Structure:
id | name
1 | result_1
2 | result_2
3 | result_3
4 | result_4
Thanks in advance!
You can use the modulus operator (%) to check if you're ready for a new line.
$number_of_names = count($names);
$number_of_columns = 3; //you can change this at any point
echo "<table><tr>";
for($i=0;$ i<$number_of_names; $i++){
echo "<td>" . $names[$i] . "</td>";
if ($i % $number_of_columns == ($number_of_columns - 1) && $i<$number_of_names-1){
echo "</tr><tr>";
}
}
echo "</tr></table>";
Try looping through the results using a for loop, then compare the remainder of dividing the iterator by 3, using the modulus operator.
echo "<tr>";
for($i = 0; $i<mysql_num_rows($result); $i++) {
if($i%3==0) {
echo "</tr><tr>";
}
$row = mysql_fetch_assoc($result)
echo "<td><input type='checkbox'> ".$row['name']."</td>";
}
echo "</tr>";

mysql php columns

<?php
$say = array("ann","brenda","charles","david",
"edward","florence","geoff","harry",
"ingrid","james","kelly","liam");
$columns = 5;
for ($p=0; $p<count($say); $p++) {
// Start of table or line?
if ($p==0) { // Start of table
print "<table border=0><tr>";
} elseif ($p%$columns == 0) { // Start of row
print "<tr>";
}
print "<td>".htmlspecialchars($say[$p])."</td>";
// End of table or line?
if (($p+1)%$columns == 0) { // End of row
print "</tr>";
}
if ($p==count($say)-1) { // End of table
$empty = $columns - (count($say)%$columns) ;
if ($empty != $columns) {
print "<td colspan=$empty> </td>";
}
print "</tr></table>";
}
}
?>
The result:
ann brenda charles david edward
florence geoff harry ingrid james
kelly liam
I'm trying to do the same with mysql
so far i got
<?php
$con = mysql_connect("localhost","root","lol");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM test");
while($row = mysql_fetch_array($result))
{
$id=$row['id'];
$nam=$row['nam'];
$columns = 3;
for ($p=0; $p<count($id); $p++) {
// Start of table or line?
if ($p==0) { // Start of table
print "<table border=0><tr>";
} elseif ($p%$columns == 0) { // Start of row
print "<tr>";
}
print "<td>".$nam."</td>";
// End of table or line?
if (($p+1)%$columns == 0) { // End of row
print "</tr>";
}
if ($p==count($nam)-1) { // End of table
$empty = $columns - (count($nam)%$columns) ;
if ($empty != $columns) {
print "<td colspan=$empty> </td>";
}
print "</tr></table>";
}
}
}
mysql_close($con);
?>
Result:
ann
brenda
charles
david
edward
florence
geoff
harry
ingrid
james
kelly
liam
Question: what's wrong?
Dabase table
id nam
1 ann
2 brenda
3 charles
4 david
5 edward
6 florence
7 geoff
8 harry
9 ingrid
10 james
11 kelly
12 liam
I'd suggest that you split your code into two distinct functions.
One function will read information from the database or the array, the other will format the output.
Right now, it looks an awful lot like you took your first chunk of code and put it into the middle of the while loop in the second piece.
MySQL is returning results to you, one result row at a time. So what you should do is collect all those results first and then print them out second (either that, or make a counter on the number of rows returned). In your second piece of code, you're treating each result row as you were the entire array of results in the first piece.
That is, the line while($row = mysql_fetch_array($result)) returns a single row from the table.
Because of this, the line $id=$row['id']; does not assign an array to $id.
Because of this, the line for ($p=0; $p<count($id); $p++) { iterates over a single item, resulting in what you're seeing.
My code still looks a little hackish, but it may give you an idea. I'm afraid I haven't tested it.
print "<table><tr>";
$p=0;
$columns=3;
while( $row = mysql_fetch_array($result) ) {
if ( $p>0 && ($p % $columns)==0 )
print "</tr><tr>";
print "<td>{$row['nam']}</td>";
$p++;
}
for(true;($p % $columns)!=0;$p++) //Finish off $p from above
print "<td> </td>";
print "</tr></table>";
To do this in a more modular way:
function display($stuff,$cols){
//Make sure the table is some multiple of $cols to eliminate special cases
//Hackish
while( (count($stuff) % $cols)!=0 )
$stuff.push_back(" ");
//Start table and first row, eliminating another special case
print "<table><tr>";
for($i=0;$i<count($stuff);$i++){
if($i>0 && ($i % $cols)==0)
print "</tr><tr>";
print "<td>{$stuff[$i]}</td>";
}
print "</tr></table>";
}
$names=array()
while( $row=mysql_fetch_array($result) )
$names.push_back($row['nam']);
display($names,5);

Categories