I have this code:

$sql=mysql_query("select distinct one.ime_kategorije AS ime_kat,count, two.kategorija_g AS kategorija_g
FROM (SELECT *, COUNT(DISTINCT kategorija_g) AS count FROM galerija_slik LEFT JOIN kategorija ON galerija_slik.id_kat=kategorija.id_kategorija GROUP BY id_kat) AS one
JOIN (
select *
from galerija_slik
group by kategorija_g) AS two
WHERE one.id_kat = two.id_kat;");
while ($row=mysql_fetch_array($sql)) {
    $kategorija=$row['ime_kat'];
    $id_kat=$row['id_kat'];
    $st=$row['count'];
    $kategorija=html_entity_decode($kategorija);
    $kat=$row['kategorija_g'];
    $kat=html_entity_decode($kat);
    
    echo "<li>"; 
    echo $kategorija." "."(".$st.")";    
    echo "<ul>";
    echo "<li>".$kat."</li></ul>";
    echo "<li>";
}

But I have problem how to display this on my site.

I want to have like:

<li>Clanice (3)
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</li>
<li>Ostalo (1)
<ul>
<li>turnir</li>
</ul>
</li>

I think that mysql part is good, but I don't know how to do this in php to display on this way.

With this code I get this:

<li>Clanice(3)
<ul><li>1</li></ul></li>
<li>Clanice(3)
<ul><li>2</li></ul></li>
<li>Clanice(3)
<ul><li>3</li></ul></li>
<li>Ostalo
<ul><li>turnir</li></ul></li>

That means threetimes Clanice and under this 1,2,3. But I like category Clanice only once and under this 1,2,3 like subcategories.

I forgot to write database tables:

galerija_slik:
g_id, link_slike, kategorija_g, ime_g, thumb_slike, datum, id_kat

id_kat is in relation with id_kategorija from kategorija table

kategorija:
id_kategorija, ime_kategorije
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.