how to group result in subgroups in php - php

Hey everyone. While I'm trying to learn some PHP and mySQL, I ran into a problem that I've had some difficulty in solving. I need a PHP script that queries mySQL database for country, books, book_price. Here is my dbtable:
+----+-------+----------+------------------------+
| id |Country|Books | Book_price |
+----+-------+----------+------------------------+
| 1 | USA | Zorro | 10 |
| 2 | USA | Zorro | 20 |
| 3 | USA | Zorro | 50 |
| 4 | USA | Leon | 200 |
| 5 | USA | Leon | 240 |
| 6 | ITALY| Tarzan | 70 |
| 7 | ITALY| Tarzan | 30 |
| 8 | ITALY| Tarzan | 100 |
| 9 | ITALY| Cobra | 300 |
| 10 | ITALY| Cobra | 320 |
| 11 | ITALY| Cobra | 350 |
+----+------+-----------+------------------------+
I want to organize the results based upon the country, books, total book, total country and TOTAL GEN (which is sum of all total country) and the result to show like this:
+----------------------------------------------------+
| USA |
+----------------------------------------------------+
| Zorro 10 |
| 20 |
| 50 |
+----------------------------------------------------+
| Total Zorro: 80 |
+----------------------------------------------------+
| Leon 200 |
| 240 |
+----------------------------------------------------+
| Total Leon:440 |
+----------------------------------------------------+
|Total USA: 520 |
+----------------------------------------------------+
|ITALY |
+----------------------------------------------------+
| Tarzan 70 |
| 30 |
| 100 |
+----------------------------------------------------+
| Total Tarzan:200 |
+----------------------------------------------------+
| Cobra 300 |
| 320 |
| 350 |
+----------------------------------------------------+
| Total Cobra: 970 |
+----------------------------------------------------+
|Total ITALY: 1170 |
+----------------------------------------------------+
|TOTAL GEN: 1690 |
+----------------------------------------------------+
Thank you

$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("mydbname");
$sql = 'SELECT * FROM table_name';
$result = mysql_query($sql);
$data = array();
while ($row = mysql_fetch_assoc($result)) {
if ( empty($data[ $row['Country'] ]) ) {
$data[ $row['Country'] ] = array();
}
if ( empty( $data[ $row['Country'] ][ $row['Books'] ] ) ) {
$data[ $row['Country'] ][ $row['Books'] ] = array();
}
$data[ $row['Country'] ][ $row['Books'] ][] = $row['Book_price'];
}
$totalSum = 0;
foreach ( $data as $country => $books ) {
echo '<b>' . $country . '</b><br/>';
$totalCountry = 0;
foreach ( $books as $book => $prices ) {
$sum = array_sum( $prices );
echo '<u>' . $book . '</u><br/>';
echo implode(',', $prices) . '<br/>;
echo 'Total ' . $book . ':' . $sum . '<br/>';
$totalCountry += $sum;
}
echo 'Total ' . $country . ':' . $totalCountry . '<br/>';
echo '<hr/>';
$totalSum += $totalCountry;
}
echo 'TOTAL GEN: ' . $totalSum;

Use additional variables that hold the last country/book and their subtotals:
$last = array('Country' => null, 'Books' => null);
$subtotals = array('Country' => 0, 'Books' => 0);
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
if ($row['Books'] !== $last['Books']) {
if (!is_null($last['Books'])) {
echo '<tr><td colspan="2">Total '.$last['Books'].': '.$subtotals['Books'].'</td></tr>';
}
$last['Books'] = $row['Books'];
$subtotals['Books'] = $row['Book_price'];
} else {
$subtotals['Books'] += $row['Book_price'];
}
if ($row['Country'] !== $last['Country'])) {
if (!is_null($last['Country'])) {
echo '<tr><td colspan="2">Total '.$last['Country'].': '.$subtotals['Country'].'</td></tr>';
}
echo '<tr><th colspan="2">'.$row['Country'].'</th></tr>';
$last['Country'] = $row['Country'];
$subtotals['Country'] = $row['Book_price'];
} else {
$subtotals['Country'] += $row['Book_price'];
}
echo '<tr><td>'.$row['Books'].'</td><td>'.$row['Book_price'].'</td></tr>';
}
echo '</table>';

Try:
SELECT country, books, SUM(price)
FROM sales
GROUP BY country, books WITH ROLLUP;
Rows where books is null will return total for each country
Row where Country and Books are null return total for whole query

Check this out !
<?php
include ("config.php");
function render($price3) {
$output = "<td align='right'>".number_format($price3->book_price, 0, ',', '.')."</td>";
$output .= "</tr>";
return $output;
}
echo "<table border='1'>";
echo "<tr>
<th>books</th>
<th>price</th>
</tr>";
$result = mysql_query("select id, country, books, book_price from test") or die(mysql_error());
$set = array();
while ($record = mysql_fetch_object($result)) {
$set[$record->country][$record->books][] = $record;
}
$sum_country = 0;
foreach ($set as $country => $price1) {
echo "<tr>
<td align='center'>{$country}</font></td>
<td></td>
</tr>";
foreach ($price1 as $books => $price2 ) {
echo "<tr>
<td>{$books}</td>";
foreach ($price2 as $price3) {
echo render($price3);
}
$sum_books = 0;
foreach($price2 as $grbooks){
$sum_books += $grbooks->book_price;
}
echo "<tr><td>Total {$books}</td>
<td align='right'>".number_format($sum_books, 0, ',', '.')."</td>
</tr>";
}
$sum_country += $sum_books;
echo "<tr<td>Total {$country}</td>
<td align='right'>".number_format($sum_country, 0, ',', '.')."</td>
</tr>";
}
echo "</table>";

Related

Dividing table rows to different columns according to their value

I have a mysql table id, gra, grb integers and NOT NULLS and contain DIFFERENT numbers between 1 and 10.
I'd like to divide the data from gra and grb to different columns according to their value.
example: mysql table:
+------------+------------+------------+
| id | gra | grb |
+------------+------------+------------+
| 1 | 2 | 6 |
+------------+------------+------------+
| 2 | 10 | 8 |
+------------+------------+------------+
| 3 | 9 | 5 |
+------------+------------+------------+
etc
The expected table output would be like this:
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
| gr1 | gr2 | gr3 | gr4 | gr5 |gr6 | gr7 |gr8 |gr9 |gr10 |
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
| | 2 | | | | 6 | | | | |
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
| | | | | | | | 8 | | 10 |
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
| | | | | 5 | | | | 9 | |
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
Is there an easy way to do this? I figured I could do this using mysql with CASE function but it'd be too long. I think there should be an easier way. Can I get help with this, please? thanks.
You can get the desired result using this approach
// Create connection
$conn = new mysqli('hostname', 'username', 'password', 'dbname');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT gra ,grb FROM `tablename`";
$result = $conn->query($sql);
$rows = array();
$i = 0;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
foreach(range(1,10) as $key => $value){
$_key = 'gr'.$value;
$rows[$i][$_key] = ($row['gra'] == $value) ? $row['gra'] : ( ($row['grb'] == $value) ? $row['grb'] : null);
}
$i++;
}
}
echo '<pre>';
print_r($rows);
You can print in a table like this
$sql = "SELECT gra ,grb FROM `grade`";
$result = $conn->query($sql);
$rows = array();
$i = 0;
$html = '<table border="1"><tr>';
foreach(range(1,10) as $v){
$html.= '<td>gr'.$v.'</td>';
}
$html .= '</tr>';
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$html .= '<tr>';
foreach(range(1,10) as $key => $value){
$_key = 'gr'.$value;
$rows[$i][$_key] = ($row['gra'] == $value) ? $row['gra'] : ( ($row['grb'] == $value) ? $row['grb'] : null);
$html .= '<td> '.$rows[$i][$_key].' </td>';
}
$html .= '</tr>';
print_r($rows[$i]);echo '<hr>';
$i++;
}
}
$html .= '</table>';
echo $html;

how to display data in two column with php with header

I have database
---------------------------------------------
no | name | code | grade
---------------------------------------------
1 | john | A1 | C
2 | john | A2 | D
3 | john | A3 | B
4 | tom | A1 | A
5 | john | A4 | A
6 | alice | A1 | C
7 | alice | A2 | D
8 | john | A5 | D
9 | john | A6 | C
---------------------------------------------
when I want show data with name john, I want result with 2 column:
---------------------------------------------------------------
no | name | code | grade | no | name | code | grade |
---------------------------------------------------------------
1 | john | A1 | C | 5 | john | A5 | D |
2 | john | A2 | D | 6 | john | A6 | C |
3 | john | A3 | B | | | | |
4 | john | A4 | A | | | | |
---------------------------------------------------------------
this my code that I already try
$result = mysql_query("select * from grade ");
echo "<table border='1'><tr>";
echo "<td>no</td>";
echo "<td>name</td>";
echo "<td>code</td>";
echo "<td>grade</td></tr><tr>";
$no = 1;
$count = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
extract($row);
echo "<td>$no</td>";
echo "<td>$row[name]</td>";
echo "<td>$row[code]</td>";
echo "<td>$row[grade]</td>";
if ($count++ % 2 == 0)
{
echo "</tr><tr>";
$no++;
}
}
echo "</tr></table>";
But the result not what I want, this is the result when I run my code
no | name | code | grade | | | | |
---------------------------------------------------------------
1 | john | A1 | C | 1 | john | A4 | A |
2 | john | A2 | D | 2 | john | A5 | D |
3 | john | A3 | B | 3 | john | A6 | C |
---------------------------------------------------------------
Can somebody help me...thank you
This is representation problem, so it can be solved it two ways.
1. You can make this columns using modern HTML:
<section style="-webkit-column-count:2; -webkit-column-gap:15;">
<div class='columns'>
<table>
<tr>
<th>no</th>
<th>name</th>
<th>code</th>
<th>grade</th>
</tr>
<?php
$result = mysql_query("select * from grade ");
$no = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>';
echo "<td>{$row['no']}</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['code']}</td>";
echo "<td>{$row['grade']}</td>";
echo '</tr>';
$no++;
}
?>
</table>
</div>
</section>
2. If you want to stick to plain tables, you have to do some calculations:
First, you need to know full amount of grades you gonna show. You can get it with SQL query(more efficient way), I'll do it by preloading all data to array. HTML footer and header are omitted for brevity.
<?php
$data = array();
$result = mysql_query('SELECT * FROM grade;');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$data[] = $row;
}
$rows = (int) ceil(count($data)/2);
for ($i=0; $i < $rows; $i++) {
$no = $i + 1;
echo '<tr>';
echo "<td>{$no}</td>";
echo "<td>{$data[$i]['name']}</td>";
echo "<td>{$data[$i]['code']}</td>";
echo "<td>{$data[$i]['grade']}</td>";
if (array_key_exists($i + $rows, $data)) {
$no = $i + $rows + 1;
echo "<td>{$no}</td>";
echo "<td>{$data[$i + $rows]['name']}</td>";
echo "<td>{$data[$i + $rows]['code']}</td>";
echo "<td>{$data[$i + $rows]['grade']}</td>";
} else {
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
}
echo "</tr>\n";
}
I have not tested this nor checked for errors, but I think it is on the lines of what you are trying to accomplish.
$result = mysql_query("select * from grade ");
$orig_count = mysql_num_rows($result);
if ( $orig_count % 2 != 0 )
$count = $orig_count + 1;
$cols = $count / 2;
//draw header
echo "<table border='1'><tr>";
echo "<td>no</td>";
echo "<td>name</td>";
echo "<td>code</td>";
echo "<td>grade</td></tr><tr>"
for ( $i = 0; $i < $cols; i++ )
{
if ( $orig_count == 0 )
break; //zero results
//first column
echo "<td>$i</td>";
echo "<td>".mysql_result( $result, $i, "name" )."</td>";
echo "<td>".mysql_result( $result, $i, "code" )."</td>";
echo "<td>".mysql_result( $result, $i, "grade" )."</td>";
//second column
if ( $i > $orig_count )
{
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
}
else
{
echo "<td>".$i+($count/2)."</td>";
echo "<td>".mysql_result( $result, $i+($count/2), "name" )."</td>";
echo "<td>".mysql_result( $result, $i+($count/2), "code" )."</td>";
echo "<td>".mysql_result( $result, $i+($count/2), "grade" )."</td>";
}
}
//finish off
echo "</tr></table>";

PHP: Troubles on indexing a nested for-loop

I am trying to print a 3-dimensional array into a table. But the indexes are kinda fked up. When I use the following (psuedo)code:
...
<<print headers and stuff>>
for ( $i = 0; $i < count( $array ); i++) {
$itemArray = $array[i];
for ( $j = 0; $j < count( $itemArray; j++) {
$innerItem = $itemArray[j];
echo <<tr start + both indexes in td>>
foreach ($innerItem as $spec) {
echo <<td with item>>
}
echo <<tr stop>>
}
}
In this example I am using i as index for the outer array and j as an index for the inner array (pretty obvious).
The result I am getting from this is as follows:
| index i | index j | title1 | title2 |
| 0 | 0 | | |
| 1 | 0 | | |
| 2 | 0 | | |
| ... | ... | | |
Whilst I would expect:
| index i | index j | title1 | title2 |
| 0 | 0 | | |
| 0 | 1 | | |
| 1 | 0 | | |
| 1 | 1 | | |
| 1 | 2 | | |
| 2 | 0 | | |
| ... | ... | | |
The (original) full code is:
echo "<h1>Combat analysis</h1>";
echo '<table cellspacing="0" cellpadding="4" border="1"><tbody>';
echo "<tr><td>#Mon</td><td>#Att</td><td>DungLVL</td><td>CharLVL</td><td>Health</td><td>Weapon</td><td>No. potions</td></tr>";
for ($battleIndex = 0; $battleIndex < count($this->combatLog); $battleIndex++) {
$battle = $this->combatLog[$battleIndex];
for ($attackIndex = 0; $attackIndex < sizeof($battle); $attackIndex++) {
$attack = $battle[$attackIndex];
echo "<tr><td>" . $battleIndex . "</td><td>" . $attackIndex . "</td>";
foreach ($attack as $stat) {
echo "<td>" . $stat . "</td>";
}
echo "</tr>";
}
}
echo "</tbody></table>";
What is going wrong?
Tested your code and runs as expected. You should do a echo '<pre>'.print_r($this->combatLog).'</pre>'; and debug the array contents.
Also I would recommend you the following:
1) You can use foreach instead of for, example: foreach ($this->combatLog as $battleIndex => $battle)
2) If you're not sure that a array contains values you should first do a: if (is_array($this->combatLog) && count($this->combatLog) > 0)
3) For simplicity and code maintenance I would first loop the multi-dimensional array and turn it into a one dimension called $attacks containing a array per each attack indexed by keys that you can recognize, ej:
$attacks=array();
$attacks[]=array(
'Mon'=>$battleIndex,
'Att'=>$attackIndex,
'DungLVL'=>isset($stat[0])?$stat[0]:null,
'CharLVL'=>isset($stat[1])?$stat[1]:null,
'Health'=>isset($stat[2])?$stat[2]:null,
'Weapon'=>isset($stat[3])?$stat[3]:null,
'Potions'=>isset($stat[4])?$stat[4]:null,
);
Then you could define some columns for example:
$columns=array(
'Mon',
'Att',
'DungLVL',
'CharLVL',
'Health',
'Weapon',
'Potions',
);
Then print the table header like this:
echo '<tr>';
foreach ($columns as $column) {
echo '<td>'.$column.'</td>';
}
echo '</tr>';
And print rows like this:
foreach ($attacks as $attack) {
echo '<tr>';
foreach ($columns as $column) {
echo '<td>'.$attack[$column].'</td>';
}
echo '</tr>';
}

how to make simple php and mysql query to make table of product and stock

i need a help
assumptions that i have 2 table
table1 : product
-----------------------------
| id | Code | any else field |
-----------------------------
| 10 | GA 12 | -------------- |
| 11 | GA 1 | -------------- |
-----------------------------
table2 : stock
---------------------------------------------
| id | product_id | color | size | stock |
---------------------------------------------
| 1 | 10 | Black | M | 12 |
| 2 | 10 | Yellow | L | 10 |
| 3 | 10 | Black | S | 12 |
| 4 | 10 | Yellow | XL | 10 |
| 5 | 10 | Red | S | 10 |
| 6 | 10 | Black | XL | 12 |
| 7 | 10 | Yellow | M | 10 |
| 8 | 10 | Red | L | 12 |
| 9 | 10 | Red | XL | 12 |
| 10 | 10 | Gray | M | 12 |
---------------------------------------------
from the table we can see
for product_id 10 have 4 colors
Black with size S M and XL
Red with size S L and XL
yellow M L and XL
Gray M
that mean size range for product_id 10 is S - XL
from that 2 table i need a result on my website as below
and i have try using php and mysql query as below:
function getProduct(){
$Condb = #new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$Condb->connect_errno and die ('Connect Failed : '.$Condb->connect_error);
$sql = "
SELECT product.*
FROM product
";
$res2 = $Condb->query($sql);
while ($row = $res2->fetch_assoc()){
$return_data[] = $row;
}
$res2->close();
$Condb->close();
$return ['data'] = $return_data;
return $return;
}
--
function list_stock($product_id){
$Condb = #new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$Condb->connect_errno and die ('Connect Failed : '.$Condb->connect_error);
//Distinct to get size range from product_id
$sql = "
SELECT DISTINCT size FROM stock WHERE product_id='$product_id'
ORDER BY uk ASC
";
$res2 = $Condb->query($sql);
$return_size_range = array();
while ($row = $res2->fetch_assoc()){
$return_size_range[] = $row['size'];
}
$res2->close();
$return_stock[] = array();
$return_color = array();
$sql = "
SELECT * FROM stock
WHERE product_id='$product_id'
GROUP BY product_id, color, size
ORDER BY warna, uk ASC
";
$res2 = $Condb->query($sql);
$n = 0;
$w = 0;
while ($row = $res2->fetch_assoc()){
$size_index = 0;
if($n%count($return_size_range)== 0){
$color=$row['color'];
$return_color[$w]=$color;
$w=$w+1;
$n=0;
}
while($size_index == 0){
if($return_size_range[$n] == $row['size']){
$sz = $row['size'];
$return_stock[$n]['size' . $color] = $sz;
$return_stock[$n]['stock' . $color . $sz] = $row['stock'];
$size_index=1;
}else{
$size_index=0;
}
$n += 1;
}
}
$res2->close();
$Condb->close();
$return ['color'] = $return_color;
$return ['size'] = $return_size_range;
$return ['stock'] = $return_stock;
return $return;
}
for html i use below code :
<?php
$product = getProduct();
$product_detail = $product['data'];
?>
<table>
<tr>
<!-- header of table -->
<tr>
<?php
for($p=0 ; $p<count($produk_detail); $p++){
?>
<tr>
<!-- produk_detail -->
<tr>
<tr>
<td> <!-- with colspan from produk detail column -->
<?php
$stock_detail = list_stock($product_detail[$p]['id']);
$size = $stock_detail['size'];
$color = $stock_detail['color'];
$stock = $stock_detail['stock'];
?>
<table>
<tr>
<td>color</td>
<?php
for($s=0; $s<count($size); $s++){
echo '<td>' . $size[$s] . '</td>';
}
?>
</tr>
<?php
for($c=0; $c<count($color); $c++){
?>
<tr>
<td><?php echo $color[$c]; ?></td>
<?php
for($st=0; $st<count($size); $st++){
echo '<td>';
echo isset($stock[$st]['stock' . $color[$c] . $size[st]]) ? $stock[$st]['stock' . $color[$c] . $size[st]] : 0;
echo '</td>';
}
?>
</tr>
<?php
}
?>
<tr>
</tr>
</table>
</td>
<tr>
<?php
}
?>
<table>
based on above source code, i success to get the result as image i attached,
but the code is very long, and make loading slow to get the product and stock detail,
is this possible to make more simple of source code(php) and mysql query from my source code??
or is this possible to get result as image attched with simple php and mysql query??
thanks

foreach for groups and subgroups in php

i have table in database:
Group:
| id | Category | title |
| 1 | 1 | group1 |
| 2 | 2 | group2 |
| 3 | 1 | group3 |
| 4 | 3 | group4 |
| 5 | 2 | group5 |
| 6 | 1 | group6 |
News:
| id | Group | title | body |
| 1 | 3 | title1 | body1 |
| 2 | 2 | title2 | body2 |
| 3 | 1 | title3 | body3 |
| 4 | 4 | title4 | body4 |
| 5 | 1 | title5 | body5 |
| 6 | 5 | title6 | body6 |
| 7 | 3 | title7 | body7 |
| 8 | 2 | title8 | body8 |
| 9 | 1 | title9 | body9 |
| 10 | 6 | title10| body10 |
| 11 | 1 | title11| body11 |
| 12 | 5 | title12| body12 |
how can i show this as:
-GROUP1, GROUP3 and GROUP6
//GROUP1 (category1)
--title3
--title5
--title9
//GROUP3 (category1)
--title1
--title7
//GROUP6 (category1)
--title10
-GROUP2 and GROUP5
//GROUP2 (category2)
--title2
--title8
//GROUP5 (category2)
--title6
--titl12
-GROUP4
//GROUP4 (category3)
--title4
i will make this in foreach. thanks for help!
Your exact requested output makes this complicated.
$sql = 'SELECT n.title, n.Group AS group_id, g.Category AS cat_id
FROM News AS n
JOIN Group AS g ON g.id = group_id
ORDER BY cat_id, group_id, n.id';
$result = mysql_query($query);
$categories = array();
while ($row = mysql_fetch_assoc($result)) {
$catID = $row['cat_id'];
$groupID = $row['group_id'];
$title = $row['title'];
$categories[$catID]['groups'][$groupID]['titles'][] = $title;
}
foreach ($categories as $catID => $groups) {
$catGroups = '-GROUP'.implode(', GROUP',array_keys($groups)).PHP_EOL;
$lastComma = strrpos($catGroups,',');
if ($lastComma !== false) {
$catGroups = substr($catGroups,0,$lastComma-1).
' AND ' .substr($catGroups,$lastComma+1);
}
echo $catGroups;
foreach ($groups as $groupID => $titles) {
echo "//GROUP$groupID (category$catID)".PHP_EOL;
foreach ($groups as $group => $titles) {
echo '--'.$title.PHP_EOL;
}
}
}
If you didn't need such fancy output, this would be much simpler.
$sql = 'SELECT n.title, n.Group AS group_id, g.Category AS cat_id
FROM News AS n
JOIN Group AS g ON g.id = group_id
ORDER BY cat_id, group_id, n.id';
$result = mysql_query($query);
$lastCatID = null;
$lastGroupID = null;
while ($row = mysql_fetch_assoc($result)) {
$catID = $row['cat_id'];
$groupID = $row['group_id'];
$title = $row['title'];
if ($catID !== $lastCatID){
echo "*** CATEGORY $catID\n";
$lastCatID = $catID;
}
if ($groupID !== $lastGroupID){
echo "GROUP $groupID\n";
$lastGroupID = $groupID;
}
echo "-- $title\n";
}
You told, you have your values in the database. So you have to get them first, e.g. with the following database query:
SELECT
g.`title` AS `group_title`
, n.`title` AS `news_title`
FROM
`Group` AS g
INNER JOIN
`News` AS n
ON
g.`id` = n.`Group`
ORDER BY
g.`Category`
, n.`Group`
, n.`title`
Store the data in an array. Now you can use a foreach loop to iterate over the array.
===
Here my update:
First fill the array while reading from the database (example query see above).
<?php
$data = array();
$res = mysql_query('SELECT ...');
while (($row = mysql_fetch_assoc($res)) !== false) {
$data[$row['group_title']][] = $row['news_title'];
}
?>
Then write the array to the screen:
<?php
foreach ($data as $group_title => $groups) {
echo $group_title . "\n";
foreach ($groups as $news) {
echo "\t" . $news . "\n";
}
}
?>

Categories