Question

Here is the problem:

I need to know if there is a way to do an upward rowspan on a <th> element on a form.

I am reading some rows inside a DB that I need to put inside an html table. I am doing something like:

echo "<table>";
while($result = $resultSet->fetch())
{
    echo "<tr>";
        echo "<td>$result['Name']</td>";
        echo "<td>$result['Job']</td>";
    echo "</tr>";
}
ehco "</table>";

First steps are easy until I needed to 'span' together two adjacent cells that contains the same value.

For exemple if someone have the same name but had two jobs I would like them to have something like :

echo "<tr>";
    echo "<td rowspan='2'>$result['Name']</td>";
    echo "<td>$result['Job']</td>";
echo "</tr>";

But I can't predict how many jobs someone have (except they all have at least one and that they are all in order). exemple of record in MySQL table

Name/**/Jobs

Paul/**/Jobs1

Simon/**/Jobs23

Simon/**/Jobs45

Roger/**/Jobs67

(All simon's jobs are 'behind one another', they are grouped. If Paul had a second job it would be the second record in the table 'pushing' simons jobs down).

So I need to change the rowspan value of the first element to fit how many jobs that person have.

This is why I would need to know if it is possible to do a upward span because i could just display every on of them and count how many jobs each person have and do a rowspan that would merge the table cell upward.

It would certainly be easier then fetching every row in the DB then looping throught them all and checking how many jobs a person have to 'span' the cells now and then display them all and then skipping them. and so on.

Thanks

Was it helpful?

Solution

A situation like this calls for pre-processing. Like so:

$people = array();
while($result = $resultSet->fetch())
{
    if( !isset($people[$result['Name']])) $people[$result['Name']] = array();
    $people[$result['Name']][] = $result['Job'];
}

echo "<table>";
foreach($people as $name=>$jobs)
{
    echo "<tr>";
        echo "<td rowspan=\"".count($jobs)."\">".$name."</td>";
        echo "<td>".array_shift($jobs)."</td>";
    echo "</tr>";
    foreach( $jobs as $otherjob)
    {
        echo "<tr><td>".$otherjob."</td></tr>";
    }
}
echo "</table>";

Done!

OTHER TIPS

What you're looking for doesn't exist in HTML. But you can do it like this:

$currName =  $result['Name'];
echo "<tr>";
    if($currName==$prevName)
    {
      echo "<td>&nbsp;</td>";
    }
    else
    {
      echo "<td>$result['Name']</td>";
    }
    echo "<td>$result['Job']</td>";
echo "</tr>";
$prevName = $currName;

Rather than using rowspan, just nest a table in the second column with all of the jobs for that person.

echo "<tr>";
echo "<td>$result['Name']</td>";
echo "<td>";
echo "<table>"
foreach($jobs as $job): //$jobs holds the job values for the current name
  echo "<tr><td>";
  echo $job;
  echo "</td></tr>";
endforeach;
echo "</table>";
echo "</td>";
echo "</tr>";

Note: you will need to preprocess the result set to get a 2D array for this method (see answer from Niet the Dark Absol).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top