Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-11-2004, 02:03 AM   #1
Registered: May 2003
Location: Somewhere over the Rainbow
Distribution: Slackware 9.x, Knoppix, Damn Small Linux, RedHat.
Posts: 465

Rep: Reputation: 30
PHP show query results

Hi Im currently showing my results as the below code, but that code display me the results exactly as my table on my DB. I would like to display the data on one row and down one row until some field name of X column change.

if the 4 fields on column A are the same print all the columns on one row and then if the next 5 fields on column A are differents as the first 4 fields down one row until a field name of A column change.. Thanks lot..

PHP Code:
/* Connecting, selecting database */
$link mysql_connect("mysql_host""mysql_user""mysql_password")
   or die(
"Could not connect : " mysql_error());
"Connected successfully";
mysql_select_db("my_database") or die("Could not select database");

/* Performing SQL query */
$query "SELECT * FROM my_table";
$result mysql_query($query) or die("Query failed : " mysql_error());

/* Printing results in HTML */
echo "<table>\n";
while (
$line mysql_fetch_array($resultMYSQL_ASSOC)) {
   foreach (
$line as $col_value) {

/* Free resultset */

/* Closing connection */
This is my currently output.
Champs 1 2
Champs 3 4
Champs 5 16
TETE 1 2
TETE 3 4
TETE 5 16

but I would like to print as:
Champs 1 2 3 4 5 16
TETE 1 2 3 4 5 16

Last edited by Gerardoj; 05-11-2004 at 03:38 AM.
Old 05-11-2004, 01:31 PM   #2
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 66
You should just be able to change the while loop and using a hash array:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  if($lastrow=$row["col_one"] && $lastrow){
    echo "\n\t</td>\n\t</tr>\n\t<tr>\n\t<th>".$row["col_one"]."</th>\n\t<td>\n";
    echo "\t<tr><th>".$row["col_one"]."</th><td>\n\t\t";
  echo "$row["col_two"] ";
echo "\t</td>\n\t</tr>\n";
You'll need to change "col_one" and "col_two" to the names of the first and second columns.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL results to html forms using PHP xemous Programming 3 08-15-2005 04:27 PM
OpenOffice Slide Show Query erraticassassin Linux - Software 6 03-13-2005 11:21 AM
rpm -Va should show no results on a good system right? dr_zayus69 Linux - Software 1 01-06-2005 11:50 AM
why dont seach results show search string? Fascistchicken LQ Suggestions & Feedback 6 04-25-2004 03:29 PM
Wierd PHP results Andy@DP Programming 2 09-14-2003 12:55 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:03 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration