I have comma-separated field base_users in my database. How do I query to count the totaluser of that group? I have no problem to calculate the totaluser if the data is not in comma-separated field.
SELECT COUNT(base_u_id) AS totaluser
FROM base_users
WHERE base_u_group =".$row['base_gp_id']."
1)base_users
|base_u_id | base_u_name | base_u_group |
------------------------------------------
| 1 | username1 | 1, 2, 4 |
| 2 | username2 | 3 |
| 3 | username3 | 3, 4 |
| 4 | username4 | 1, 4 |
2)base_groups
| base_gp_id | base_gp_name |
------------------------------
| 1 | group1 |
| 2 | group2 |
| 3 | group3 |
| 4 | group4 |
| 5 | group5 |
From the sample database above, my expected result will be:
Total User of group1 = 2
Total User of group2 = 1
Total User of group3 = 2
Total User of group4 = 3
Total User of group5 = 0
This is what I have tried so far:
<?php
$getUser = base_executeSQL("SELECT * FROM base_users");
while($row_getUser = base_fetch_array($getUser))
{
$explodeData = explode(", ",$row_getUser['base_u_group']);
foreach($explodeData as $data)
{
$getUserGroupSQL = base_executeSQL("SELECT COUNT(base_u_id) AS totaluser FROM base_users as user, base_groups as gp WHERE gp.base_gp_id ='".$data."' ");
while($UserGroupProfile_row = base_fetch_array($getUserGroupSQL))
if (base_num_rows($getUserGroupSQL)!= 0)
$totaluser = $UserGroupProfile_row["totaluser"];
elseif (base_num_rows($getUserGroupSQL)== 0)
$totaluser = 0;
}
}
?>
Use below logic: Just HINT
$group = array(1=>0,2=>0,3=>0,4=>0,5=>0);
$base_u_group = array('1,2,4','3','3,4','1,4');
foreach($base_u_group as $gr) {
$split = explode(',', $gr);
if (!empty($split)) {
foreach($split as $val) {
if ($val) {
$group[$val] = $group[$val] + 1;
}
}
}
}
var_dump($group);
Try this:
SELECT COUNT(u.base_u_id), g.base_gp_name FROM base_users u
INNER JOIN base_groups g ON IF(POSITION(',' IN u.base_u_group) > 0, u.base_u_group LIKE ('".$row['base_gp_id'].",%') OR u.base_u_group LIKE ('%, ".$row['base_gp_id'].",%') OR u.base_u_group LIKE ('%, ".$row['base_gp_id']."'), u.base_u_group = '".$row['base_gp_id']."')
WHERE g.base_gp_id = ".$row['base_gp_id']."
Related
i have a datastructure similar to this
+---------+---------+
| id | value |
+---------+---------+
| 1 | value |
1 | value |
| 1 | value |
| 1 | value |
| 1 | value |
| 2 | value |
| 2 | value |
| 2 | value |
| 3 | value |
| 3 | value |
| 3 | value |
| | |
+---------+---------+
I am trying to update this table to look something like this
+---------+---------+
| id | value |
+---------+---------+
| 1 | value 0 |
1 | value 1 |
| 1 | value 2 |
| 1 | value 3 |
| 1 | value 4 |
| 2 | value 0 |
| 2 | value 1 |
| 2 | value 2 |
| 3 | value 0 |
| 3 | value 1 |
| 3 | value 2 |
| | |
+---------+---------+
To achieve this, i have written php script that looks like this
$query = "select count(*) as count,id, value from foo group by id";
$sql=$con->prepare($query);
$sql->execute();
$sql->setFetchMode(PDO::FETCH_ASSOC);
while($row=$sql->fetch()){
$id[] = $row['id'];
$count[] = $row['count'];
$value[] = $row['value'];
echo "<pre>";
}
$c=array_combine($id, $count);
foreach ($c as $key=>$value){
for($i=0;$i<=$value;$i++){
$postid = $key;
if($i==0){
$multiple = "multiple";
$newvalue= $value;
}
else{
$x=$i-1;
$multiple = "multiple_".$x;
echo $multiple . "<br>";
$query2 = "update foo set value = :multiple";
$sql2=$con->prepare($query2);
$sql2->bindValue(':multiple', $multiple);
$sql2->execute();
}
}
}
The problem is that the code returns the following results
+---------+---------+
| id | value |
+---------+---------+
| 1 | value_1 |
1 | value_1 |
| 1 | value_1 |
| 1 | value_1 |
| 1 | value_1 |
| 2 | value_1 |
| 2 | value_1 |
| 2 | value_1 |
| 3 | value_1 |
| 3 | value_1 |
| 3 | value_1 |
| | |
+---------+---------+
What can i be possibly be doing wrong?
Thanks #Shadow
Your query runs fine but returns the following results
+------+-----------------------------------------------+
| id | value |
+------+-----------------------------------------------+
| 1 | multiple_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 |
| 1 | multiple_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1 |
| 1 | multiple_1_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2 |
| 1 | multiple_1_3_3_3_3_3_3_3_3_3_3_3_3_3_3_3_3 |
| 2 | multiple_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 |
| 2 | multiple_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1 |
| 2 | multiple_1_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2 |
| 2 | multiple_1_3_3_3_3_3_3_3_3_3_3_3_3_3_3_3_3 |
| 3 | multiple_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 |
| 3 | multiple_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1 |
| 3 | multiple_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 |
+------+-----------------------------------------------+
You can do the update iterating and creating data in such a way:
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $pdo->prepare("SELECT * FROM foo");
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
$response = array();
foreach ($data as $dataIndex => $dataValue) {
if (!isset($response[$dataValue["id"]]["count"])) {
$response[$dataValue["id"]]["count"] = 0;
} else {
$response[$dataValue["id"]]["count"] ++;
}
$response[$dataValue["id"]]["values"][$dataValue["pid"]] = "value_" . $response[$dataValue["id"]]["count"];
$sth = $pdo->prepare("UPDATE foo SET value = '{$response[$dataValue["id"]]["values"][$dataValue["pid"]]}' WHERE pid = {$dataValue["pid"]}");
$sth->execute();
}
?>
But try to do an update using the least iteration not to create as many database queries , example:
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $pdo->prepare("SELECT * FROM foo");
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
$response = array();
$update = array();
foreach ($data as $dataIndex => $dataValue) {
$response[$dataValue["id"]]["id"] = $dataValue["id"];
if (!isset($response[$dataValue["id"]]["count"])) {
$response[$dataValue["id"]]["count"] = 0;
} else {
$response[$dataValue["id"]]["count"] ++;
}
$response[$dataValue["id"]]["values"][$dataValue["pid"]] = "value_" . $response[$dataValue["id"]]["count"];
$update[] = "UPDATE foo SET value = '{$response[$dataValue["id"]]["values"][$dataValue["pid"]]}' WHERE pid = {$dataValue["pid"]};";
}
$update = implode("",$update);
$sth = $pdo->prepare($update);
$sth->execute();
?>
Your update query
$query2 = "update foo set value = :multiple";
does not contain any where criteria, each time you call this query it updates the value field's value in all records.
Honestly, I would not really involve php in this update, would do it purely in sql using user defined variables and multi-table join syntax in the update:
update foo inner join (select #i:=0, #previd:=-1) as a
set foo.value=concat(foo.value,'_',#i:=if(id=#previd,#i+1,0),if(#previd:=id,'',''))
The subquery in the inner join initialises #i and #previd user defined variables. The 3rd parameter of the concat function determines the value #i to be concatenated to the value field. The 4th parameter of concat sets the #previd variable and returns an empty string not to affect the overall concatenation. Unfortunately, I do not have access to MySQL to test the query, but it should be a good starting point anyway.
UPDATE
The OP claims in the updated question that the query I provided creates the below resultset:
+------+-----------------------------------------------+
| id | value |
+------+-----------------------------------------------+
| 1 | multiple_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 |
| 1 | multiple_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1 |
| 1 | multiple_1_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2 |
| 1 | multiple_1_3_3_3_3_3_3_3_3_3_3_3_3_3_3_3_3 |
| 2 | multiple_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 |
| 2 | multiple_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1 |
| 2 | multiple_1_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2 |
| 2 | multiple_1_3_3_3_3_3_3_3_3_3_3_3_3_3_3_3_3 |
| 3 | multiple_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 |
| 3 | multiple_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1 |
| 3 | multiple_1_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0 |
+------+-----------------------------------------------+
Tested my solution in sqlfiddle. I had to remove the order by clause, otherwise the query produced the results in line with the requirements stated in the question. See sqlfiddle for details.
The results in the updated question are likely the result of running the query in a loop multiple times. In simple words: you just copy pasted the query into your code and did not remove the loop, even when I pointed out, that this may be the reason of the results you received.
below is my table data
+-------------+-----------+----------------+
| customer_id | parent_id | node_direction |
+-------------+-----------+----------------+
| 1 | 0 | T |
| 2 | 1 | L |
| 3 | 1 | R |
| 4 | 2 | L |
| 5 | 2 | R |
| 6 | 4 | L |
+-------------+-----------+----------------+
Which represents the following tree
1
|
---------
| |
2 3
|
-------
| |
4 5
|
-----
|
6
I need to find the position for insertion by parent id
For Example:
1) if parent id is 1 then insert position will be root-3 position-L
2) if parent_id is 2 then insert position will be root-4 position-R
3) if parent_id is 3 then insert position will be root-3 position-L
The thing is it need to follow the binary structure
I also need to have count of sub nodes by parent node for example:
1 - 5
2 - 3
3 - 0
4 - 1
5 - 0
I need to accomplish this in php and mysql.
Can anyone suggest to me the easiest way to achieve this?
function getNodeInsertPostionByParentId($parentId){
$position = array('status'=>false);
$parents = array();
foreach($parentId as $parent){
$qry = "select customer_id,node_direction from mlm_nodes where parent_id =".$parent." order by node_direction";
$rst = mysql_query($qry);
$count = mysql_num_rows($rst);
if($count==2){
while($row = mysql_fetch_assoc($rst)){
$parents[$parent][] = $row['customer_id'];
}
}elseif($count==1){
$position['status'] = true;
$position['parentId'] = $parent;
$position['node'] = 'R';
//echo '<pre>1';print_r($position);echo '</pre>';
return $position;
}else{
$position['status'] = true;
$position['parentId'] = $parent;
$position['node'] = 'L';
//echo '<pre>2';print_r($position);echo '</pre>';
return $position;
}
}
return $this->searchByParents($parents);
}
function searchByParents($parents){
foreach($parents as $parent){
return $this->getNodeInsertPostionByParentId($parent);
}
}
echo '<pre>';print_r($this->getNodeInsertPostionByParentId(array('4')));die;
This works as expected for finding node position by parent id
I have the following scenario.
I have 2 tables as follow
Table 1
ID, Name, Desc, Seo
Table 2
ID, Table1_ID, Relation_Table1_ID
in Table 1 I have all my data that I need as:
-----------------------------------
|ID | Name |Desc |Seo |
-----------------------------------
| 1 | Smith |Father |f |
| 2 | Jonh |Son |j |
| 3 | Margat |Mother |m |
| 4 | Bis3 |son |b1 |
| 5 | Bis2 |son |b2 |
| 6 | Bis1 |son |b3 |
| 7 | Lanos |Brother |l |
-----------------------------------
And then we have our table 2 as follow
-------------------------------------
|ID | Table1_ID |Relation_Table1_id|
--------------------------------------
| 1 | 1 | 4 |
| 2 | 1 | 5 |
| 3 | 3 | 6 |
| 4 | 3 | 2 |
| 5 | 7 | 0 |
--------------------------------------
So far I have my first table dump with jSON() as follow:
<?php
include ('config.php');
$dump1 = mysql_query("SELECT * FROM Table1 ") or die(mysql_error());
$getList = array();
while ($row = mysql_fetch_assoc($dump1)) {
$getList[] = $row;
}
print json_encode($getList); exit;
?>
That code will give me the following:
[
{
"ID":"1",
"Name":"Smith",
"Desc":"Father",
"Seo":"f"
},
{
"ID":"2",
"Name":"Jonh",
"Desc":"Son",
"Seo":"j"
},
{
"ID":"3",
"Name":"Margat",
"Desc":"Mother",
"Seo":"m"
}... ... ...
]
What I can't figure is how do I get the following
[
{
"ID":"1",
"Name":"Smith",
"Desc":"Father",
"Seo":"f",
"Relations":[
{
"ID":"4",
"Name":"Bis3",
"Desc":"Son",
"Seo":"b1"
}
]
},
{
"ID":"3",
"Name":"Margat",
"Desc":"Father",
"Seo":"f",
"Relations":[
{
"ID":"6",
"Name":"Bis2",
"Desc":"Son",
"Seo":"b2"
},
{
"ID":"2",
"Name":"Jonh",
"Desc":"Son",
"Seo":"j"
}
]
}... ... ...
]
In plain text it would be something like
ID 1 Smith
| |_ID 4 Bis3
|
|_ ID 3 Margat
|_ID 5 Bis2
|_ID 2 Jonh
I'm learning how to use json and I just got the first part as you can see, but my lack of knowledge of SQL and php wont let me get the what I really want, so please can any one help me achieve this scenario please.
Thank you.
you can loop through $getlist
then query
for($i = 0; $i <= count($getlist); $i++){
"SELECT * FROM Table2 WHERE Table1_ID = $getlist[$i]['ID']"
// get the result however way
$getlist[$i]['something'] = $result;
}
then take the result of that, inside the loop, and assign it to whichever key of getlist you want
for example
hopefully thats specific enough. let me know if you have problems with that.
You need to join across the two tables. First, join the second table with the first table. This can be done with the following code:
select * from table_2 as t2 join table_1 as t1 on t1.id = t2.rel_id;
which results in:
+------+-----------+--------+------+------+------+------+
| ID | table1_id | rel_id | ID | name | desc | seo |
+------+-----------+--------+------+------+------+------+
| 4 | 3 | 2 | 2 | John | Son | j |
| 1 | 1 | 4 | 4 | Bis3 | son | b1 |
| 2 | 1 | 5 | 5 | Bis2 | son | b2 |
| 3 | 3 | 6 | 6 | Bis1 | son | b3 |
+------+-----------+--------+------+------+------+------+
now we want to join the columns if the table1_id is the same. This can be done with the GROUP_CONCAT command:
select table1_id,group_concat(t1.name) as name_rel,
group_concat(t1.desc) as desc_rel, group_concat(seo) as seo_rel,
group_concat(rel_id) as id_rel from table_2 as t2 join table_1 as t1 on
t2.rel_id = t1.id group by t2.table1_id
which results in:
+-----------+-----------+----------+---------+--------+
| table1_id | name_rel | desc_rel | seo_rel | id_rel |
+-----------+-----------+----------+---------+--------+
| 1 | Bis2,Bis3 | son,son | b2,b1 | 5,4 |
| 3 | John,Bis1 | Son,son | j,b3 | 2,6 |
+-----------+-----------+----------+---------+--------+
The as command can be used to rename columns in the output. Try running the query without it and you should see column names like group_concat(t1.name) instead of name_rel.
Now we want to join this selection with table_1 where the table1_id is the same as the id in table1. This can be done with the following query:
select * from (
select table1_id,group_concat(t1.name) as name_rel,
group_concat(t1.desc) as desc_rel, group_concat(seo) as seo_rel,
group_concat(rel_id) as id_rel from table_2 as t2 join table_1 as t1 on
t2.rel_id = t1.id group by t2.table1_id
) as joined_table
join table_1 as t3 on t3.id = joined_table.table1_id;
which results in:
+-----------+-----------+----------+---------+--------+------+--------+--------+------+
| table1_id | name_rel | desc_rel | seo_rel | id_rel | ID | name | desc | seo |
+-----------+-----------+----------+---------+--------+------+--------+--------+------+
| 1 | Bis2,Bis3 | son,son | b2,b1 | 5,4 | 1 | Smith | Father | f |
| 3 | John,Bis1 | Son,son | j,b3 | 2,6 | 3 | Margat | Mother | m |
+-----------+-----------+----------+---------+--------+------+--------+--------+------+
As you can see, you know have all the data. All that is left is to turn the *_rel columns into separate objects, which can be done in php. Go ahead and give that a shot. I've gotta go for now, but I'll edit this post later if you're still having trouble.
Alright, I'm back. My php is rusty, but something along these lines should work:
while ($row = mysql_fetch_assoc($dump1)) {
$name_rel = explode("," , $row["name_rel"]);
$desc_rel = explode("," , $row["desc_rel"]);
$seo_rel = explode("," , $row["seo_rel"]);
$id_rel = explode("," , $row["id_rel"]);
$relations = array();
for($i = 0; $i < count($id_rel); ++$i){
$temp = array("ID" => $id_rel[$i],
"Name" => $name_rel[$i],
"Desc" => $desc_rel[$i],
"Seo" => $seo_rel[$i],);
$relations[] = $temp ;
}
unset($row["id_rel"]);
unset($row["name_rel"]);
unset($row["desc_rel"]);
unset($row["seo_rel"]);
$row["Relations"] = $relations ;
$getList[] = $row;
}
print json_encode($getList); exit;
This will create each of the Relations objects and add them to the row. The unset commands should remove the keys we no longer care about (id_rel, name_rel, desc_rel, and seo_rel). We no longer care about them because their data should have been moved to the Relations object.
I have this scenario.
I input $groupid="1";
main table
----------------------
| groupid | postid |
|---------------------|
| 1 | 1 |
| 2 | 2 |
| 1 | 3 |
$query = "SELECT postid FROM `mainl` WHERE groupid='$groupid'";
$result = mysql_query($query);
// a group of postids belonging to that groupid which should hold [1, 3] for groupid=1
while($row = mysql_fetch_array($result)) {
$postids[] = $row["postid"];
}
second table
-------------------------------------------
| postid | commentid | comment |
-------------------------------------------
| 1 | 1 | testing 1 |
| 1 | 2 | testing 2 |
| 1 | 3 | what? |
| 2 | 1 | hello |
| 2 | 2 | hello world |
| 3 | 1 | test 3 |
| 3 | 2 | begin |
| 3 | 3 | why? |
| 3 | 4 | shows |
$query = "SELECT * FROM `second`";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
if (in_array($row["postid"], $postids)) {
$comments[$row["postid"]] = $row["comment"];
But how should I take care of commented
I want the postid array to be [1,3] and my comment array to be
[commentid: comment] [1:testing1, 2: testing2, 3: what?] for postid=1
and
[1:test3, 2:begin, 3: why? 4:shows] for postid=3
how should be arrange everything such comment are associated with commentid and postid?
First I would follow rokdd suggestion and make 1 query
SELECT m.groupid , s.postid, s.commentid, s.comment FROM `main1` m JOIN `second` s USING (postid) where m.groupid = 1
Then I would make a multi-dimensional array
while ($row = mysql_fetch_array($result))
$groups[$row['groupid'][$row['postid']][$row['commentid']=$row['comment'];
then to iterate through the array
foreach($groups as $group)
foreach($group as $post)
foreach($post as $comment)
echo $comment;
This will keep track of groups also (if you ever want to select by more than 1 group.
If you don't care about groups just drop off the first part of the array.
while ($row = mysql_fetch_array($result))
$posts[$row['postid']][$row['commentid']=$row['comment'];
foreach($posts as $post)
foreach($post as $comment)
echo $comment;
I guess to use the join in sql so that you will have one statement:
SELECT * FROM second as second_tab LEFT join main as main_table ON main_table.post_id=second_table.post_id WHERE main_table.group_id="3"
Well not tested now but thats a way to solve some of your problems!
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";
}
}
?>