0

Edit: hey people, i'm already using php, i didn't mean to ask that. I was asking how can i display 3-columned mysql result in an 8-column html table

--work table--                                               --crew table--

date           |   hours                                     date   |    name   
2013-02-03(Sun)     8                                    2013-02-03     john
2013-02-04(Mon)     7                                    2013-02-03     sam 
                                                         2013-02-03     peter
                                                         2013-02-04     john
                                                         2013-02-04     sam

i have these 2 tables to keep record of the hours worked by employees.
i need to show this data as a timesheet.

But how do i show 8 columns in this html table below ?

i mean suppose if you run this query:

SELECT hours, name, DAYNAME(work.date) day
FROM work 
LEFT JOIN crew ON work.date = crew.date
WHERE WEEK(work.date) = 5

it'll show 3 columns. So how do i build this html table?

This mysql result to

hours    name    day
  8      john   Sunday
  8      Sam    Sunday
  8      Peter  Sunday
  7      John   Monday
  7      Sam    Monday

this html table (How?)

Name    Sunday   Monday  Tuesday..(day name can be written in html)
John      8        7       ...
Sam       8        7       ...
Peter     8        -       ...
NestedWeb
  • 1,657
  • 2
  • 15
  • 31
  • Write some PHP/Perl/whatever language you choose to fetch the output and add the appropriate HTML tags. – Ed Heal Feb 07 '13 at 06:26
  • when the result has just 3 columns then why do you want 8 columns in view? Anyways, if you want it then whats wrong with having them blank? Just put the tds blank. If i am going wrong way then you can surely ask me back. – Bhavik Shah Feb 07 '13 at 08:00
  • hey thanks bhavik, i want to split the mysql result rows into different columns in html, please look at my updated question – NestedWeb Feb 07 '13 at 08:20

2 Answers2

0

Modify this....... if u using php

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

while($row = mysql_fetch_array($result))
  {
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br />";
  }

mysql_close($con);
?>
0

Warning: I am no PHP programmer, so the following is pseudocode at best, and contains a lot of syntax errors (not to mention any missing error handling, or even html layout). I hope one gets the idea anyway. Anyone feel free to edit

$result = mysql_query("query here");

// a double nested array
// $weekdays['Sunday']['Peter'] will be the hours peter working on sunday 
$weekdays = [];
// a list of all persons in the table
$persons = [];

while($row = mysql_fetch_array($result))
{
  $hours = $row[0];
  $person = $row[1];
  $weekday= $row[2];

  if (!$weekdays[$person]) {
     $weekdays[$person] = [ $person => $hours ];
  } else {
     $weekdays[$person][$weekday] = $hours;
  }
  if (! $person in $persons) {
    $persons[] = $person;
  }
}

// then unroll in two loops
for ($person in $persons) {
  print '<tr><td>'.$person.'</td>'
  for ($weekday in [ "Sunday", "Monday", ...]) {
     print '<td>'.$weekdays[$weekday][$person].'</td>'
  }
  print '</tr>'
}
Clemens Klein-Robbenhaar
  • 3,457
  • 1
  • 18
  • 27