LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP show query results (https://www.linuxquestions.org/questions/programming-9/php-show-query-results-180197/)

Gerardoj 05-11-2004 01:03 AM

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

david_ross 05-11-2004 12:31 PM

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.


All times are GMT -5. The time now is 01:50 PM.