td rowspan when number of rows are unknown

I am building a table in a php script using foreach. eg:
foreach {
   echo "<tr> ";
   echo "<td>...";
   echo "<td>...";
   echo "</tr> ";
}

Open in new window

so I don't know how many rows there will be until the foreach code runs.

I would like to add a column starting at the first row <td rowspan="??"> spanning all the rows created by the foreach code.

I suppose I could run the foreach twice, once to count the number of rows, a second time to build the table but is there a simpler way?

TIA
LVL 39
thenelsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Cornelia YoderArtistCommented:
If the source of the table is an array, can't you just do a count() on the array?
0
thenelsonAuthor Commented:
No, the foreach code looks up several items on another site to build the table so it takes several seconds to run. It would run faster if I look up just one item in the first counting foreach run. This would be the row count foreach code (1st run of the foreach):
foreach ($client->account->recordings as $recording) 
{
	$call = $client->account->calls->get($recording->call_sid);
        $rowcount++
}

Open in new window

Another way of wording my question: Is there a way to specify the td rowspan tag to span all the rows?
0
Chris StanyonWebDevCommented:
Instead of echoing out the table in your foreach loop, create a variable that holds the table HTML. Inside the loop, check to see if you're on row 1, so you can add the colspan attribute, and instead of a value put in %s. You can then replace this at the end with the row count using the printf() function

$rowNum = 0;
$table = "<table>";

foreach(something) {
    $rowNum ++;

    $table .= "<tr>";
    $table .= ($rowNum == 1) ? "<td rowspan='%s'> some value </td>": "";
    $table .= "<td>Some Text</td>";
    $table .= "</tr>
}

$table .= "</table>";

printf($table, $rowNum);

Open in new window

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Cornelia YoderArtistCommented:
Depending on the size of the table, you could start by creating an array to hold the information, then use count() on that array and build the table from the array.

Sort of like ChrisStanyon suggested, but you don't need to put all the HTML into the array, just the data.

Either way, if there is not a way to count the size up front, you will have to process through it twice.   However, you won't have to access the second site twice, the second time through will be the array right in your code.
0
Slick812Commented:
greetings  thenelson, , , I do not think that your - "Is there a way to specify the td rowspan tag to span all the rows? " except to use the numeric value for the actual number of rows, which you have already thought of as getting the number by doing your - "I could run the foreach twice, once to count the number of rows, a second time to build the table but is there a simpler way?"
There may be another way, but as far as a "simpler way" would depend on how you want the visual presentation to look like. . . . PHP usually does a very fast "foreach" roll through for a single counting like -
  $rowcount = 0;
foreach ($client->account->recordings as $r)   $rowcount++;

but it would be more efficient if you could get some property of  $client->account->recordings  that held the Count-Amount and use that, such as -
<td rowspan="$client->account->recordings->total">
or
<td rowspan="$client->account->recordings->totalCount">
of course using the correct $client->account->recordings property AND the correct PHP syntax inside the two " " of <td rowspan=" ">

ALSO there may be HTML-CSS structure that could go outside the html <table> </table> that surrounds the <table> </table>, like a DIV or something that could accomplish your visual display for the ROW that spans the entire table thing? ? but I do not know how you want this thing to look like?
0
Dave BaldwinFixer of ProblemsCommented:
What is the point of that first <td> with 'rowspan'?  I think I'd make the data rows another table in that <td> and avoid the problem altogether.  Or make the outside table have two <td>s with the row data table in the second one.
0
Chris StanyonWebDevCommented:
@slick812 / @yodercm - my method only loops through the data once.

As for passing a count back fromt he object, that would definintely help, but you still have the issue that you only need the rowspan on the first iteration through the loop - not every one.
0
Cornelia YoderArtistCommented:
Well, that's not quite true, Chris, because print_f does it.  You just don't see it in the code. :)
0
Chris StanyonWebDevCommented:
@yodercm - What? printf() doesn't loop through anything - it simply outputs a formatted string!
0
Cornelia YoderArtistCommented:
You know, one simple way to do this would be to create a table with one row, max width, for the header, close that table, then start a second table with no space between, also max width, for the rows.

The only drawback to this would be a double line if you are bordering your table, and even that wouldn't be too hard to eliminate.

Ooops, I'll leave this, but I see that DaveBaldwin already suggested something similar.
0
thenelsonAuthor Commented:
A lot of good ideas here.

Slick812,
"but it would be more efficient if you could get some property of $client->account->recordings  that held the Count-Amount"
I am getting the data from my Twilio account and I have an excellent tech support guy at Twilio. So I sent him an email if this is available.

DaveBaldwin,
Are you suggesting imbedding the table with the data in another 2 column table? That makes a lot of sense.

yodercm,
"You know, one simple way to do this would be to create a table with one row, max width, for the header, close that table, then start a second table with no space between, also max width, for the rows."
I am not understanding this. Wouldn't that create one table above the other instead of adding a seventh column to the right of the six columns I already have? (Which is what I am trying to achieve.)

BTW: I have borders turned off in the table.
0
Dave BaldwinFixer of ProblemsCommented:
Yes, like this.  I turned on the border and spacing just so you could see where things were.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Tables in Tables</title>
</head>
<body>
<h1>Tables in Tables</h1>
<table border="1" cellpadding="2" cellspacing="0" summary="">
<tr><td>First TD</td><td>
<table border="1" cellpadding="2" cellspacing="0" summary="">
<tr><td>second table</td><td>1</td></tr>
<tr><td>second table</td><td>1</td></tr>
<tr><td>second table</td><td>1</td></tr>
<tr><td>second table</td><td>1</td></tr>
</table>

</td></tr>
</table>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cornelia YoderArtistCommented:
Oh, I hadn't realized you were trying to span vertically, duh.

Dave's suggestion of using a table within a table is a clearcut favorite.  Very simple and should work without any trouble.

<table>
<tr>
<td>span column stuff here </td>
<td>
    <table>
    <tr>
    <td>row stuff here</td>  repeated as necessary
    </tr>
    <table>
</td>
</tr>
</table>
0
Chris StanyonWebDevCommented:
Ooooh. Tables within tables - I'm feeling nostalgic ;)
0
Dave BaldwinFixer of ProblemsCommented:
Well, he is asking about tables and if that gives you 'warm fuzzies'...  It is the simplest answer to the question.
0
thenelsonAuthor Commented:
With these mods, I get the additional cell on the right instead of the left:
<table border="0" cellpadding="2" cellspacing="0" summary="">
<tr><td>
<table border="0" cellpadding="2" cellspacing="0" summary="">
<tr><td>second table</td><td>1</td></tr>
<tr><td>second table</td><td>1</td></tr>
<tr><td>second table</td><td>1</td></tr>
<tr><td>second table</td><td>1</td></tr>
</table>
</td>
<td valign="top">First TD</td></tr>
</table>

Thanks DaveBaldwin!

BTW: Slick812,
I discovered I can get a count from Twilio. That may be useful for something else - thanks!
0
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad it works for you.  Thanks for the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.