Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Manipulando while loop e agrupando valores PHP MYSQL


Tente evitar mysql_* funções. Use mysqli_* . Você só precisa agrupá-los primeiro. Considere este exemplo:
<?php

$link = new mysqli('localhost', 'test', 'test', 'test');
$query = mysqli_query($link, 'SELECT * FROM author_master ORDER BY author_id');
$books = array();
while($row = $query->fetch_assoc()) {
    $books[$row['author_id']][] = $row;
}
?>

<table border="0" cellpadding="10">
    <thead>
        <tr>
            <th>Author ID</th>
            <th>Author Book</th>
            <th>Rating</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($books as $author_id => $values): ?>
            <tr>
                <td><?php echo $author_id; ?></td>
                <td><?php foreach($values as $author_book) {echo $author_book['author_book'] . '<br/>';} ?></td>
                <td><?php foreach($values as $author_book) {echo $author_book['rating'] . '<br/>';} ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>