Jump to content
php.lv forumi

Ierakstu saskaitīšana


Indian

Recommended Posts

Sveiki,

 

Man ir tabula ar ierakstiem.

 

Name | Category

---------|------------

ābols | augli

----------------------

apelsīns| augli

----------------------

burkans| darzenis

----------------------

 

<?

$sql = mysql_query("SELECT * FROM products");

while($pr = mysql_fetch_array($sql)) {

 

echo $pr['category'];

}

?>

 

Kā man izdarīt lai izvadītos šādi:

Augli 2

Darzeni 1

Link to comment
Share on other sites

SELECT Category, COUNT(*) FROM products GROUP BY Category

 

Sevis paša labuma dēļ padomā vai tiešām gribi tādu DB struktūru. Daudz prātīgāk būtu taisīt divas tabulas:

Kategorijas (id, nosaukums) - kurā likt iekša tos Category tekstus: auglis, dārzenis, oga, ...

Produkti (id, nosaukums, kategorijas_id) - kurā likt iekšā tos visus produktus (ābols, apelsīns, ..) ar atbilstošu kategorijas id vērtību (integer skaitlis).

Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...