| | |
How to display query results under headings derived from another field
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
Hi all,
Please I'm a newbie in PHP and MySQL, I have results from a GROUP BY (grouped by col2) query in the format.
col1 | col2
-----------
a1 | a
a2 | a
a3 | a
a4 | a
b1 | b
b2 | b
b3 | b
I wish to display the results in the format
----
a
----
a1
a2
a3
a4
----
b
----
b1
b2
b3
I'll appreciate any help on how to go about this.
Please I'm a newbie in PHP and MySQL, I have results from a GROUP BY (grouped by col2) query in the format.
col1 | col2
-----------
a1 | a
a2 | a
a3 | a
a4 | a
b1 | b
b2 | b
b3 | b
I wish to display the results in the format
----
a
----
a1
a2
a3
a4
----
b
----
b1
b2
b3
I'll appreciate any help on how to go about this.
0
#2 29 Days Ago
Hey.
Try something like:
If you are trying to do this sort of formatting using MySQL alone... don't. MySQL is a database system, meant to store data and return it in a very basic way.
Formatting the data for output is a job for the front-end application. (PHP, in this case.)
Try something like:
php Syntax (Toggle Plain Text)
<?php $dbLink = new mysqli('host', 'user', 'pwd', 'db'); $result = $dbLink->query("... Put your query here ..."); if($result) { $data = array(); // Loop through all the returned rows and group the values // into the $data array based on the 'col2' field. while($row = $result->fetch_assoc($result) { $data[$row['col2']][] = $row['col1']; } // Loop through the 'col2' values and print a list of // 'col1' values for each of them. foreach($data as $_col2 => $_col1_list) { echo "----\n{$_col2}\n----\n"; foreach($_col1_list as $_col1) { echo $_col1, "\n"; } } } else { echo "Your query phailed! " . $dbLink->error; } ?>
If you are trying to do this sort of formatting using MySQL alone... don't. MySQL is a database system, meant to store data and return it in a very basic way.
Formatting the data for output is a job for the front-end application. (PHP, in this case.)
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
![]() |
Similar Threads
- Differences between MS SQL and Sybase query results (MS SQL)
- displaying query results in a generated pdf doc (PHP)
- Displaying SQL query results in a GUI (C#)
- Trying to show SQL query results in Excel sheet (Legacy and Other Languages)
- CFLoop Display query results (ColdFusion)
- display query in textbox (VB.NET)
- Query of Query - Using Count (ColdFusion)
- Displaying query results dynamically in table (PHP)
- pagination of mysql query results (PHP)
Other Threads in the PHP Forum
- Previous Thread: Creating dynamic links with smarty.
- Next Thread: Forget Password in PHP
| Thread Tools | Search this Thread |
# .htaccess 5.2.10 ajax apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date display dissertation dynamic echo email error file files folder form forms function functions google href htaccess html image images include insert integration ip java javascript joomla ldap legislation limit link login loop mail memberships menu mlm mod_rewrite multiple mysql mysqlquery oop open paypal pdf persist php problem query radio random recursion regex remote rss script search server sessions sms soap sockets source space spam sql syntax system table tutorial update upload url validation validator variable video web xml youtube





