Results 1 to 4 of 4

Thread: PHP MySQL Count

  1. #1
    Join Date
    Aug 2010
    Posts
    9

    Question Unanswered: 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 ?

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Code:
    SELECT Count(*)
    FROM   ...
    George
    Home | Blog

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    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
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  4. #4
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •