user8190974
user8190974

Reputation: 23

display each record one below the other in html table

I have the following table table with fields: name,surname,field,class. The table have the following records:

  name        surname  field        class
 William       Smith    History      A1
 Mary          Adams    Maths        A2
 Stev          Goth     Literature   C2
 Helen         Maguire  Chemistry    A1

I want to display the aforemantioned elements with the following figure

History(A1) William Smith
Maths(A2) Mary Adams
Literature(C2) Stev Goth
Chemistry(A1) Helen Maguire

I try this in order to recover my data from db:

SELECT GROUP_CONCAT(field SEPARATOR '<br>') AS field, 
GROUP_CONCAT(class SEPARATOR '<br>') AS field,
GROUP_CONCAT(name SEPARATOR '<br>') AS field,
GROUP_CONCAT(surname SEPARATOR '<br>') AS field  
FROM teacher 

And display my data into table like this

<td>".$row['field']." ( ".$row['class']." ) ".$row['name]." ".$row['surname']."</td></tr>";

But display each data in a new line. Also I try this: SELECT GROUP_CONCAT(CONCAT(field,'(', class, ')',' ',name,' ',surname) SEPARATOR '<br>') without result Any idea to resolve this problem?

Upvotes: 0

Views: 56

Answers (2)

user8190974
user8190974

Reputation: 23

Finally I use regular SELECT. I put td before while loop and close td after while loop. To display each record one under the other I use special character \n. The whole code:

$result=SELECT name, surname, field, class FROM teacher;
echo "<td>";
while ($row=mysql_fetch_assoc($result)){
echo $row['field']." ( ".$row['class'].") "  .$row['name']." ".$row['surname'].  "\n";
}
echo "</td>";

Upvotes: 0

wogsland
wogsland

Reputation: 9518

You don't need GROUP_CONCAT. Just do a regular SELECT:

SELECT name, surname, field, class FROM teacher;

and then loop through your rows in PHP.

Upvotes: 1

Related Questions