LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 05-11-2004, 01:03 AM   #1
Gerardoj
Member
 
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:
<?php
/* Connecting, selecting database */
$link mysql_connect("mysql_host""mysql_user""mysql_password")
   or die(
"Could not connect : " mysql_error());
echo 
"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)) {
   echo 
"\t<tr>\n";
   foreach (
$line as $col_value) {
       echo 
"\t\t<td>$col_value</td>\n";
   }
   echo 
"\t</tr>\n";
}
echo 
"</table>\n";

/* Free resultset */
mysql_free_result($result);

/* Closing connection */
mysql_close($link);
?>
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 02:38 AM.
 
Old 05-11-2004, 12:31 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
You should just be able to change the while loop and using a hash array:
Code:
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";
  if($lastrow=$row["col_one"]){
    echo "\t<tr><th>".$row["col_one"]."</th><td>\n\t\t";
  }
  echo "$row["col_two"] ";
  $lastcol=$row["col_one"];
}
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.
 
  


Reply


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 03:27 PM
OpenOffice Slide Show Query erraticassassin Linux - Software 6 03-13-2005 10:21 AM
rpm -Va should show no results on a good system right? dr_zayus69 Linux - Software 1 01-06-2005 10:50 AM
why dont seach results show search string? Fascistchicken LQ Suggestions & Feedback 6 04-25-2004 02:29 PM
Wierd PHP results Andy@DP Programming 2 09-13-2003 11:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:57 PM.

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