Hello.

Table table, id fields (preferably not to use it), type (type), url (address). According to one previous topic, I learned how to group data in a table, add it to an array. And nothing complicated with the conclusion ... but, there is one not realizable Wishlist, tell me how to implement this.

We take a label, group the data by type, and output - type 1, a dash, and enumerate everything that belongs to type 1 ... then type two, and so on ... I tried to write a cycle in a cycle - beyond the first cycle "comp tupit "8-)

Tell me how to write?

  • Bring your cycle here - dogmar
  • Ie you want to have all the data grouped by type? if yes, then you don’t need 2 cycles, you just need to do the "grouping" directly in php - Alex Kapustin
  • Yes, we need data grouped by type, and there must be a signature from the beginning, what kind of type (described above). if it's not a secret, can you write an example of such a grouping in php? - frank

2 answers 2

if I understand you correctly - something like this:

$res = query('select type, url from table order by url'); $data = array(); while ($r = fetch($res)) { $data[$r['type']][] = $r['url']; } foreach ($data as $type => $urls) { echo $type . " - " . join(', ', $urls) . "<br/>"; } 
  • thanks, it helped - frank

Make an order by type , and then check whether the type field has changed from the previous value.

Added code

 // $ar - массив полученных данных (id, type, url) $type = ''; $html = ''; foreach($ar as $v){ if($v['type'] != $type){ $html .= 'type "'.$v['type'].'":<br>'; } $html .= ' - '.$v['url'].'<br>'; } 
  • thanks, it helped. I forgot about ORDER BY while I was thinking how)) - frank