PHP/MySql HTML Table Grouping

Kevin Johnson

I have a problem I can't seem to to wrap my head around.

I have a mySQL table setup similar to this:

userid          date          rating1        rating2

1               5/1/2013      5              5
2               5/1/2013      4              4
3               5/1/2013      3              3
2               5/7/2013      5              5
1               5/7/2013      5              5
3               5/7/2013      2              2

I have my php code to query the data:

$con=mysqli_connect("localhost","root","password","db");
$result = mysqli_query($con,"SELECT userid,date,rating1,rating2 FROM db");

But when I try and output to a table using:

while($row = mysqli_fetch_array($result))
  {
echo '<table>';
    echo '<thead>';
    echo '<tr>';
        echo '<th>UserID</th>';
        echo '<th>Date</th>';
        echo '<th>First Rating</th>';
        echo '<th>Second Rating</th>';
            echo '</tr>';
    echo '</thead>';
    echo '<tbody>';
    echo '<tr>';
        echo '<td>'.$row['userid'].'</td>';
        echo '<td>'.$row['date'].'</td>';
        echo '<td>'.$row['rating1'].'</td>';
        echo '<td>'.$row['rating2'].'</td>';
            echo '</tr>';
    echo '</tbody>';
 echo '</table>';
  }

It outputs a unique table for each entry. What I need is 1 HTML table per UserID Ex:

UserID 1

Date          First Rating        Second Rating

5/1/2013      5                   5
5/7/2013      5                   5



UserID 2

Date          First Rating        Second Rating

5/1/2013      4                   4
5/7/2013      5                   5



UserID 3

Date          First Rating        Second Rating

5/1/2013      3                   3
5/7/2013      2                   2

I am honestly stuck... Thanks in advance for your help!

weenoid
$con=mysqli_connect("localhost","root","password","db");
$result = mysqli_query($con,"SELECT userid,date,rating1,rating2 FROM db ORDER BY userid");
$id = -1;

while($row = mysqli_fetch_array($result)) {
    if ($id != -1 && $id != $row['userid']) {
        echo '</tbody>';
        echo '</table>';
    }

    if ($id != $row['userid']) {
        echo '<table>';
        echo '<thead>';
        echo '<tr>';
        echo '<th>UserID</th>';
        echo '<th>Date</th>';
        echo '<th>First Rating</th>';
        echo '<th>Second Rating</th>';
        echo '</tr>';
        echo '</thead>';
        echo '<tbody>';

        $id = $row['userid'];
    }

    echo '<tr>';
    echo '<td>'.$row['userid'].'</td>';
    echo '<td>'.$row['date'].'</td>';
    echo '<td>'.$row['rating1'].'</td>';
    echo '<td>'.$row['rating2'].'</td>';
    echo '</tr>';
 }

echo '</tbody>';
echo '</table>';

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Add grouping column to ordered table

분류에서Dev

ASP.net MVC Table Grouping

분류에서Dev

Grouping Regressors in Anova Table for Multiple Linear Regression

분류에서Dev

HTML table with database values

분류에서Dev

HTML Table with images

분류에서Dev

read html table in R

분류에서Dev

Convert table HTML to JSON

분류에서Dev

Undefined href in a html table

분류에서Dev

Using jQuery to build HTML table

분류에서Dev

How to extract text in html table

분류에서Dev

Scraping embeded html table in R

분류에서Dev

Hide cell border in HTML table

분류에서Dev

Html.DropDownList For grouping by bool-show string instead of bool value

분류에서Dev

echo MySQL table to HTML table displaying text for null fields

분류에서Dev

Error Appears When Trying to Display MySQL Table Data in HTML Table

분류에서Dev

Inserting two arrays into columns of a html table perl

분류에서Dev

CanJS view return @@!!@@ if view contain HTML table

분류에서Dev

Displaying data based on checkboxes selection to html table

분류에서Dev

Formating Mail Body data into html table format

분류에서Dev

add html tag to table using jquery

분류에서Dev

HTML/CSS/Javascript - Align Text In Table

분류에서Dev

Generate a HTML table report using AWK

분류에서Dev

HTML Table, Right Justify Cell (Not Contents)

분류에서Dev

JavaScript and HTML Formatting - Table Needs to Call Variable

분류에서Dev

update html table when dropdown value change

분류에서Dev

HTML How to create a group of boxes using table?

분류에서Dev

How Do I Parse this html table with BeautifulSoup

분류에서Dev

HTML & bootstrap/ text is override another table

분류에서Dev

How to make html table made by list responsive?