Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    68
    Thanks
    3
    Thanked 5 Times in 4 Posts

    Count elements in an array

    I'm trying to count how many elements are in an array - I've tried to use "count()" and other methods but they just don't work (always returns 1)

    The only way I can get it to count it right is by doing this :

    PHP Code:
    $exclude $vbulletin->db->query_read("SELECT threadid FROM " TABLE_PREFIX "thread WHERE visible=1 $excluded_forums_query LIMIT 0,".$howmany_latestposts."");

             while (
    $value $vbulletin->db->fetch_array($exclude))
             {
                       
    $pcount++;
             }
             echo 
    "$pcount"
    That echo's "5" which is correct.

    My question is - how do I take that variable and include it in my next "while" loop? When I try to use it in my "while" loop, the variable is blank. Outside of the loop, it echos "5" just fine.

    Here is my whole script :



    PHP Code:
    // query the database and collect which "threads" we are allowed to read

    $exclude $vbulletin->db->query_read("SELECT threadid FROM " TABLE_PREFIX "thread WHERE visible=1 $excluded_forums_query LIMIT 0,".$howmany_latestposts."");

             while (
    $value $vbulletin->db->fetch_array($exclude))
             {
                       
    $pcount++;
             }
           

    // build a string of the allowed threads and inject it into the mysql query string at the bottom of the code.

             
    while ($value $vbulletin->db->fetch_array($exclude))
             {
                      
    $qcount++;
                      if (
    $qcount $pcount)
                      {
                             
    $id .= "threadid=$value[threadid] or ";
                      }
                      else
                      {
                             
    $id .= "threadid=$value[threadid]";
                      }
             }
      
    // What happens here is that the string ends with "Threadid=15 or" because the variable $pcount is blank in that while loop.

             
    $latestposts $vbulletin->db->query_read("SELECT * FROM " TABLE_PREFIX "post WHERE $id ORDER BY postid DESC LIMIT 0,".$howmany_latestposts.""); 
    Anyone know how to solve this issue?

  • #2
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    You could use the classes equivalent of mysql_num_rows to get the number of row returned by the query. Then you wouldn't have to use the first while loop.

    You may also want to initialize you $pcount and $qcount variables outside of the loops and set them to 0 so that if someone else reads your code they aren't wondering where the variables magically appeared from.

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    68
    Thanks
    3
    Thanked 5 Times in 4 Posts
    Quote Originally Posted by ninnypants View Post
    You could use the classes equivalent of mysql_num_rows to get the number of row returned by the query. Then you wouldn't have to use the first while loop.

    You may also want to initialize you $pcount and $qcount variables outside of the loops and set them to 0 so that if someone else reads your code they aren't wondering where the variables magically appeared from.
    Thank you! The mysql_num_rows + get rid of the "while" loop did the trick!



  •  

    Posting Permissions

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