Frank Helligberger Frank Helligberger - 1 month ago 6
MySQL Question

Fetch navbar elements from SQL with PHP

I have a mySQL table containing stores. It looks like this:

id | name | city
1 | H&M | New York
2 | Gucci | New York
3 | H&M | Chicago
4 | H&M | Boston
5 | Gucci | Las Vegas
6 | Cavalli| New York
7 | H&M | Phoenix


I have a drop-down menu that I want to be looking like this:


  • Home

  • Stores


    • New York

    • Chicago

    • Boston

    • Las Vegas

    • Phoenix




My question is: How do I fetch, without repeating, the cities I have stores in so I can make each of them a hyperlink in my drop-down menu like this:
mypage.com/city/new-york
. The page it will lead to, will obviously contain the stores
where city='$_GET['city']'
.

Answer

use DISTINCT this will help to optimize data MORE INFO

Syntax:

SELECT DISTINCT column FROM Table_name

also you can use multiple columns

Syntax:

SELECT DISTINCT c1, c2, c3 FROM t1