SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    count elements in an array

    Can somebody help with something which I feel should be really simple.

    I have a set of dates created from my database like this

    Code PHP:
    while ($new = mysql_fetch_array($sqlnew))	{
    		$newdates = $new['dates'];

    I can 'echo' the dates no problem.

    I need to count the number of dates so had this as my next line
    Code PHP:
    echo count($newdates);
    .

    This just returns 1's the number of 1's reflecting the number of times the loop has run

    What should I be doing?

  2. #2
    SitePoint Addict
    Join Date
    Apr 2006
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_fetch_array is pulling each record in sequence that matched your query, so it's not going to give you the total number of records that matched. Try mysql_num_rows.

  3. #3
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    mysql_num_rows($sqlnew)

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Texas
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you including the count() statement within your while loop? You would want it to be outside the loop.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,184
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    mysql_num_rows() is the best solution.

    PHP Code:
    $newdates = array();
    while (
    $new mysql_fetch_array($sqlnew))   {
            
    $newdates[] = $new['dates'];


  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Texas
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you've already loaded the elements into an array then there's no reason to use mysql_num_rows which makes a second database call.

    Instead, count the number of values in the array, or use an incrementing integer ($i++) in your while statement.

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Keeton View Post
    If you've already loaded the elements into an array then there's no reason to use mysql_num_rows which makes a second database call.
    Er, I don't think it does.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Keeton View Post
    If you've already loaded the elements into an array then there's no reason to use mysql_num_rows which makes a second database call.
    mysql_num_rows does not make a second call, the number of rows returned is part of the result resource returned from calling mysql_query. mysql_num_rows just looks in memory, there's no extra communication with the database.

  9. #9
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that. mysql_num_rows has done the trick.


Bookmarks

Posting Permissions

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