If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > PHP MySQL Count

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-18-10, 12:25
vladakg vladakg is offline
Registered User
 
Join Date: Aug 2010
Posts: 9
Question PHP MySQL Count

I have questions, how to count rows by id. I have some lines of code

$query = mysql_query("SELECT * FROM $tbl_name WHERE Left(menu_name, 1) BETWEEN 'A' AND 'M' ORDER BY menu_name ASC");

And in next line wanna count rows of each one categories, How ?
Reply With Quote
  #2 (permalink)  
Old 08-18-10, 12:42
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,002
Code:
SELECT Count(*)
FROM   ...
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 08-18-10, 13:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
Quote:
Originally Posted by vladakg View Post
... wanna count rows of each one categories, How ?
Code:
SELECT LEFT(menu_name,1) AS menu_letter
     , COUNT(*) AS rows
  FROM $tbl_name 
 WHERE LEFT(menu_name,1) BETWEEN 'A' AND 'M' 
GROUP
    BY menu_letter
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 09-05-10, 08:57
vladakg vladakg is offline
Registered User
 
Join Date: Aug 2010
Posts: 9
Anyone can help me?

<?php
$tbl_name="menu";
$kategorije = mysql_query("SELECT * FROM $tbl_name WHERE Left(menu_name, 1) BETWEEN 'A' AND 'M' ORDER BY menu_name ASC");
if (!$kategorije) {
die("Database query failed: " . mysql_error());
}

while ($row=mysql_fetch_array( $kategorije )) {
echo "<div id=lista-header><h4>{$row["menu_name"]}</h4></div>";
$id_sub=$row['id_menu'];
$podkategorije = mysql_query("SELECT * FROM submenu WHERE id_menu=$id_sub ORDER BY sub_name ASC", $connection);
if (!$podkategorije) {
die("Database query failed: " . mysql_error());
}
echo "<ul class=\"pod\">";
while ($pod=mysql_fetch_array( $podkategorije )) {
echo "<li><a href=index.php?=podkate?".$pod["id_sub"]." class=black>{$pod["sub_name"]}</a><hr size=1 align=left width=100px color=#cccccc></li>";
}
echo "</ul>";
}


?>

In this way, I get list with categories and hes subcategories. How to count how many subcategories have each categories, and count how many articles have each categories?

Example (I wanna get this kind of look):

Categories name (3)
subcategoriesname (2)
subcategoriesname (4)
subcategoriesname (7)

Categories name (5)
subcategoriesname (1)
subcategoriesname (14)
subcategoriesname (9)
subcategoriesname (2)
subcategoriesname (8)


Categories name (2)
subcategoriesname (28)
subcategoriesname (17)

Where the numbers represent how many categories and sub-items have articles
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On