| | |
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 19 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: Wordpress Online Booking System
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube





