how to customize this ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

how to customize this ?

Post by PHPycho »

Hello forums !!
I am not getting the solution for the following case:
My query results gives the following results:
--------------------
| field1 | field 2 |
---------------------
| name1 | value1 |
| name1 | value2 |
| name1 | value3 |
| name2 | value4 |
| name2 | value5 |
:
:
etc.

Code: Select all

while($row = ..){
	//show results  here..
}
I would like to show the results as:
name1
-value1
-value2
-value3

name2
-value4
-value5
without displaying like
name1
-vaue1
name1
-value2
name1
-value3 etc..
Please help me in this case..
Thanks in advance to all of you
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Read through the first two links of Useful Posts.

Please try to be a bit more specific in topic titles next time.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

<pre>
<?php

$data = array();

$data['item1'] = range('a', 'f');
$data['item2'] = range('g', 'm');

foreach ($data as $parent => $children)
{
    echo "$parent\n";
    foreach ($children as $child)
    {
        echo " -$child\n";
    }
}
Outputs:

Code: Select all

item1
 -a
 -b
 -c
 -d
 -e
 -f
item2
 -g
 -h
 -i
 -j
 -k
 -l
 -m
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Maybe you'd be interested in GROUP BY and GROUP_CONCAT() in MySQL.
Post Reply