Jump to content

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,

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.