Page 1 of 1

Group results by field data ...

Posted: Fri Jan 07, 2011 10:22 pm
by pepe_lepew1962
[text]Hello, I am having problems with grouping records while showing the group data only once. I am trying to get them in a html table that would look like:

+------------------------+----------------------+------------------------+
| tblTable1.fldField1 | tblTable1.fldField2 | tblTable1.fldField3 |
+------------------------+----------------------+------------------------+
| 12653 | PBXL | 396.52 |
| | ABCD | 426.28 |
| | GEDL | 385.20 |
+------------------------+----------------------+------------------------+
| 12654 | ABCD | 515.18 |
| | FGHL | 520.00 |
+------------------------+----------------------+------------------------+
| 12659 | PBXL | 612.35 |
| | FGHL | 619.20 |
| | XTRL | 420.39 |
+------------------------+----------------------+------------------------+


<?php
//
require("connect.php");
//
$mydata101 = "SELECT tblTable1.fldField1 as 'group', tblTable1.fldField2, tblTable1.fldField3 as 'name' FROM tblTable1 GROUP BY tblTable1.fldField1";
$mydata102 = mysql_query($mydata101);
//
//
$group = null;
while($row = mysql_fetch_array($mydata102))
{
if($row['group'] != $group)
{
echo $row['group'];
$group = $row['group'];
//
$row['name'];
//
}
//
}
?>


Any help would be great appreciated.[/text]

Re: Group results by field data ...

Posted: Sat Jan 08, 2011 7:46 pm
by jankidudel
Here it is, I think you will work little yourself on style :)

Code: Select all

<?php

mysql_connect('localhost', 'root', '');
mysql_select_db('test');

$query = "SELECT  help.field1 , help.field2, help.field3 from help order by help.field1";

$result = mysql_query($query) or die(mysql_error());;

$first_written = false;
if(mysql_num_rows($result) > 0)
{
	echo "<table class='result'  border='1'>";
	while($row = mysql_fetch_assoc($result))
	{	
		echo "<tr>";
		if(!$first_written || $first_written != $row['field1'])
		{
			echo "<td>{$row['field1']}</td>";
			$first_written = $row['field1'];
		}
		else
		{
			echo "<td></td>";
		}
		echo "<td>{$row['field2']}</td>";
		echo "<td>{$row['field3']}</td>";
		echo "</tr>";
	}
	echo "</table>";
}
?>



Re: Group results by field data ...

Posted: Sun Jan 09, 2011 2:49 pm
by pepe_lepew1962
PERFECT !!!
Thank you so much !!!