/    Sign up×
Community /Pin to ProfileBookmark

Table grouping nightmare

Good morning,

I have been agonizing over this, what feels like forever. I am thissss close! I have one more peice I can’t seem to make work. Using the code below I get all the Facilities in a list then the table as one big table. What I WANT is the facility the table data associated with that facility then start another table list the facility name then table data associated with that facility repeating the process until there are no more facilities.

Can anyone help me!!! Thanks in advance.

Laura

[CODE]
// first set some basic vars:
$label = “”;
$type = ”;
$data = “”;
$allocation = ”;
$diff = ”;
include (“UpLoad/connect.php”);

//load it all into the associative array
$sql = “select * from dbo.vwAllByFacility”;
$result = mssql_query($sql);
echo $type;
echo “<table border=1>”;
while($row = mssql_fetch_row($result))
{
if($row[1] <> $label)
{
if($allocation != ”)
{
echo $allocation;
}
if($diff != ”)
{
echo $diff;
}
if($type != ”)
{
echo $type;
}

//header row
echo “<TR class =dkblue>
<th class=header>”.$row[1].”</td></tr>”;
$label = $row[1];

//facility name
$type = “Facility: “.$row[0];

//allocations row
$allocation = “<tr class=blue>”.
“<td>Allocation</td>”.
“<td>$row[23]</td>”.
“</tr>”;
//difference row
$diff = “<tr class=blue>”.
“<td>Difference</td>”.
“<td>$row[41]</td>”.
“</tr>”;
}
//data row
echo “<tr class=tr>
<td>$row[2]</td>
<td>$row[3]</TD>
</tr>”;
}
echo $allocation;
echo $diff;
echo “</table><br>”;
[/CODE]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 18.2005 — If I'm understanding the situation correctly, I might do something along this line:
[code=php]
# do DB connection and query stuff here, then...
$currentFacility = "";
$tableStarted = FALSE;
while($row = mssql_fetch_row($result))
{
if($row[0] != $currentFacility)
{
$currentFacility = $row[0];
if($tableStarted)
{
echo "</table>n";
}
$tableStarted = TRUE;
echo "<h3>Facility: $row[0]</h3>n";
echo "<table border=1>n";
}
# output row data here...
}
if($tableStarted)
{
echo "</table>n";
}
[/code]
Copy linkTweet thisAlerts:
@lmayer3authorAug 18.2005 — Yea hah! ? That is the closest I've been yet. Thanks so much.

Laura
Copy linkTweet thisAlerts:
@aznchong91Aug 18.2005 — So is it fixed or not?
Copy linkTweet thisAlerts:
@lmayer3authorAug 18.2005 — Hi there,

Thanks for the great idea! Almost got it. The tables show up right now the tricky part is getting the diff and allocation rows to show up in the right place. What needs to happen is after each group of data is presented for each bodytype the diff and allocation rows are shown. What IS happening is the first two show then it skips to the next table and shows up as the first row. I'm very confused. If you you take a quick look and maybe tell me where I've went wrong I would much appreciate it.

Thanks so much

Laura

<i>
</i>// first set some basic vars:
$label = "";
$data = "";
$allocation = '';
$diff = '';
$currentFacility = "";
$BodyType = "";
$tableStarted = FALSE;

include ("UpLoad/connect.php");

//load it all into the associative array
$sql = "select * from dbo.vwAllByFacility";
$result = mssql_query($sql);
while($row = mssql_fetch_row($result))
{
if($row[0] != $currentFacility)
{
$currentFacility = $row[0];
if($tableStarted)
{
echo "&lt;/table&gt;n";
}
$tableStarted = TRUE;
echo "&lt;h3&gt;Facility: $row[0]&lt;/h3&gt;n";
echo "&lt;table border=1&gt;n";
}
if($row[1] != $BodyType)
{
$BodyType = $row[1];
if($allocation != '')
{
echo $allocation;
}
if($diff != '')
{
echo $diff;
}
//header row
echo "&lt;TR class =dkblue&gt;
&lt;th class=header&gt;".$row[1]."&lt;/td&gt;&lt;/tr&gt;";
$label = $row[1];
//allocations row
$allocation = "&lt;tr class=blue&gt;".
"&lt;td&gt;Allocation&lt;/td&gt;".
"&lt;td&gt;$row[23]&lt;/td&gt;".
"&lt;/tr&gt;";
//difference row
$diff = "&lt;tr class=blue&gt;".
"&lt;td&gt;Difference&lt;/td&gt;".
"&lt;td&gt;$row[41]&lt;/td&gt;".
"&lt;/tr&gt;";
}
//data row
echo "&lt;tr class=tr&gt;
&lt;td&gt;$row[2]&lt;/td&gt;
&lt;td&gt;$row[3]&lt;/TD&gt;
&lt;td&gt;$row[4]&lt;/td&gt;
&lt;/tr&gt;";
}
if($tableStarted)

{
echo $allocation;
echo $diff;
echo "&lt;/table&gt;n";
}
Copy linkTweet thisAlerts:
@lmayer3authorAug 18.2005 — YESSS!!!! Got the little bugger ?

Code is below if anybody needs such a horrible thing....

<i>
</i>// first set some basic vars:
$label = "";
$data = "";
$allocation = '';
$diff = '';
$currentFacility = "";
$tableStarted = FALSE;

include ("UpLoad/connect.php");

//load it all into the associative array
$sql = "select * from dbo.vwAllByFacility";
$result = mssql_query($sql);
while($row = mssql_fetch_row($result))
{
if($row[1] &lt;&gt; $label)
{
if($allocation != '')
{
echo $allocation;
}
if($diff != '')
{
echo $diff;
}
if($row[0] != $currentFacility)
{
$currentFacility = $row[0];
if($tableStarted)
{
echo "&lt;/table&gt;n";
}
$tableStarted = TRUE;
echo "&lt;h3&gt;Facility: $row[0]&lt;/h3&gt;n";
echo "&lt;table border=1&gt;n";
}

<i> </i> //header row
<i> </i> echo "&lt;TR class =dkblue&gt;
<i> </i> &lt;th class=header&gt;".$row[1]."&lt;/td&gt;
<i> </i> &lt;th&gt;MT&lt;/th&gt;&lt;th&gt;Strip&lt;/th&gt;&lt;/tr&gt;";
<i> </i> $label = $row[1];

<i> </i> //allocations row
<i> </i> $allocation = "&lt;tr class=blue&gt;".
<i> </i> "&lt;td&gt;Allocation&lt;/td&gt;".
<i> </i> "&lt;td&gt;$row[23]&lt;/td&gt;".
<i> </i> "&lt;td&gt;$row[24]&lt;/td&gt;".
<i> </i> "&lt;/tr&gt;";
<i> </i> //difference row
<i> </i> $diff = "&lt;tr class=blue&gt;".
<i> </i> "&lt;td&gt;Difference&lt;/td&gt;".
<i> </i> "&lt;td&gt;$row[41]&lt;/td&gt;".
<i> </i> "&lt;td&gt;$row[42]&lt;/td&gt;".
<i> </i> "&lt;/tr&gt;";
<i> </i> }


<i> </i> //data row
<i> </i> echo "&lt;tr class=tr&gt;
<i> </i> &lt;td&gt;$row[2]&lt;/td&gt;
<i> </i> &lt;td&gt;$row[3]&lt;/TD&gt;
<i> </i> &lt;td&gt;$row[4]&lt;/td&gt;
<i> </i> &lt;td&gt;$row[5]&lt;/td&gt;
<i> </i> &lt;/tr&gt;";

}

if($tableStarted)
{
echo $allocation;
echo $diff;
echo "&lt;/table&gt;n";
}
Copy linkTweet thisAlerts:
@NogDogAug 18.2005 — It's only horrible if it doesn't work. ?

Glad I was able to provide some help. Please send my consulting fee to... ?
×

Success!

Help @lmayer3 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...