简体   繁体   中英

MYSQL fetching rows to different tables

I'm here with following problem. I have table with multiple records. Each row represents news of some kind, so I need to distinguish one from the another. The way to do that, is to make a table for each of these news(or a wrapper of some kind), however I encoutred a problem with fetching each result to each table(wrapper).
I can't figure out how to tell SQL to put the record separatley. Below is chunk of my code, as well as the design for the table.

 .news{ width:400px; height:auto; border: 1px solid red; float:left; } .news tr:first-child{ font-weight: bold; height:40px; text-align: center; } .news tr:last-child{ background-color: whitesmoke; height: 200px; padding-bottom: auto; text-align: center; } .details td{ width:200px; height: 40px; text-align: center; } 
 echo "<table class='news'>"; while ($row = mysqli_fetch_array($result)) { echo "<tr> <td colspan='2'> $row[0] </td> </tr> <tr class='details'> <td> $row[1] </td> <td> $row[2] </td> </tr> <tr> <td colspan='2'> $row[3] </td> </tr>"; echo "</table>"; } 

$query = " SELECT headline, date, concat (firstName,' ',lastName), body FROM "
        . "school_info natural join teachers ORDER BY id_school_inf DESC LIMIT 2;";
$result = mysqli_query($conn, $query);

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^This is my query. There is a limit, so i can always fetch latest (let's say last 5) news.
Any help would be much appreciated

You need to create a separate table for every type of record you have? Simple enough, if you order your records by the type:

SELECT * FROM whatever ORDER BY type_of_record

then

$previous = null;
while(... fetch from db ...) {
    if ($row['type'] != $previous) {
         ... got new record type, start new table 
    }
    ... display row
    $previous = $row['type']; // store current type for comparison later
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM