Jump to content

How to list this table?


lopes_andre

Recommended Posts

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

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'];
}

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,

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.