lopes_andre Posted January 16, 2010 Share Posted January 16, 2010 Hi, I have a table like this one: id_cidade n_anuncio 1106 Maria Sameiro 1512 Mara Ferreira 1512 Joao Pires 1106 Alexandra Alves 1106 Paula Abrantes 1106 Filipa Nunes 1106 Paulo Pires I need to list this in a PHP page like: 1106 Maria Sameiro Alexandra Alves Paula Abrantes Filipa Nunes Paulo Pires 1512 Mara Ferreira Joao Pires How can I do that? Best Regards, Link to comment https://forums.phpfreaks.com/topic/188702-how-to-list-this-table/ Share on other sites More sharing options...
wildteen88 Posted January 16, 2010 Share Posted January 16, 2010 By table I am assuming you mean a MySQL table. $sql = 'SELECT id_cidade, n_anuncio FROM your_table ORDER BY id_cidade'; $result = mysql_query($sql); $prevID = 0; while($row = mysql_fetch_assoc($result)) { if($prevID != $row['id_cidade']) echo '<h1>' . $row['id_cidade'] . '</h1>'; echo $row['n_anuncio'] . '<br />'; $prevID = $row['id_cidade']; } Link to comment https://forums.phpfreaks.com/topic/188702-how-to-list-this-table/#findComment-996213 Share on other sites More sharing options...
lopes_andre Posted January 17, 2010 Author Share Posted January 17, 2010 Hi, Thanks for your reply. It is solved! I have used this code, as my application is using CodeIgniter <?php $prevCidade = ''; foreach ($result as $row) { if ($prevCidade != $row->n_cidade){ echo $row->n_cidade . '<br />'; } echo ' ' . $row->n_anuncio . '<br />'; $prevCidade = $row->n_cidade; } ?> Best Regards, Link to comment https://forums.phpfreaks.com/topic/188702-how-to-list-this-table/#findComment-996622 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.