| | |
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 Nov 8th, 2009
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 |
Tag cloud for PHP
.htaccess access ajax alerts apache api array beginner binary broken cakephp checkbox class cms code convert cron curl database date directory display download dynamic echo email error file files folder form forms function functions google hack href htaccess html htmlspecialchars image include insert integration ip java javascript joomla limit link login loop mail menu methods mlm mod_rewrite multiple mysql network object oop overwrite parse paypal pdf php problem query radio random recursion redirect regex remote script search securephp server sessions sms soap source space sql structure syntax system table tutorial update upload url validation validator variable video web xml youtube





