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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts

    While outside of loop

    Hey,
    I've wanted to be able to have a value in the while loop, and echo it outside of it.
    For example, here is what I have:
    PHP Code:
    while($user mysql_fetch_array($users))
    {
        
    $username $user['username'];
    }
    echo 
    $username
    I want username to be whatever usernames there are in there.
    The way it works now (As you can guess) is it only echos one.
    I want all.

    See, I know the problem, but I'm not sure how I can fix it.
    I hope you can help, thanks!

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,746
    Thanks
    18
    Thanked 155 Times in 146 Posts
    Try this:

    PHP Code:
    while($user mysql_fetch_array($users)) 

        
    $usernames[] = $user['username']; 

    var_dump($usernames); 
    Regards, R.J.

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

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Ok, this is what I get:
    array(1) { [0]=> string(74) "Crisp" }
    Crisp, meaning my username.

    Thanks.

  • #4
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,746
    Thanks
    18
    Thanked 155 Times in 146 Posts
    That means you only have one username in your database. That is why you are only getting a single user. If you had more than one user, var_dump would display an array with all of your usernames.

    Check your MySQL query to make sure it is correct. Or add users to your database.
    Regards, R.J.

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

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #5
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I've got 5 users in the database.
    I'll be looking at the code to see if I find anything, if you figure out anything, please reply and let me know.
    Thanks man.

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    You may have 5 users in your database. It might be that your query limits the value using either the LIMIT keyword or a WHERE clause to narrow the list down. Need more code
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #7
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I'm using no limits or wheres.
    All I need to know, is how to get the total users in the while loop, and then echo it outside of the loop.

  • #8
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Show the code above the loop, including the query.
    Are you a Help Vampire?

  • #9
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Yeah show the rest of your code
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #10
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Ok, here is my query class:
    PHP Code:
    class Quueries
    {
    function 
    userQuery($prefix ""$limit ""$other "")
        {
            if(
    $prefix == true)
                
    $where "WHERE ".$prefix."";
            else
                
    $where NULL;
            if(
    $limit == true)
                
    $limit "LIMIT $limit";
            else
                
    $limit == NULL;
            
    $users_query mysql_query(
                
    "SELECT *
                FROM
                `"
    .PREFIX."_users`
                $where
                $limit
                $other"
            
    )or die(mysql_error());    
            return 
    $users_query;
        }
    }
    function 
    online()
        {
            global 
    $queries;
            global 
    $category;
            global 
    $features;
            echo 
    "<br />".D_TABLE;
            
    $category->Title("0","5","Who's Online");
            
    //Values
            
    $online_bit "<img src='".PATH.P_BITS."/".P_ONLINE."/online.png' alt='Online' />";
            
    //The whole function
            
    $users $queries->userQuery();
            while(
    $user mysql_fetch_array($users))
            {
                
    //Usergroups
                
    $usergroups $queries->groupQuery("`id`='".$user['usergroup']."'");
                
    $usergroup mysql_fetch_array($usergroups);
                
    //Values
                
    $id mysql_real_escape_string($user['uid']);
                
    $ug_prefix $usergroup['prefix'];
                
    $ug_suffix $usergroup['suffix'];
                if(
    time()-900>$user['time']);
                else
                {
                    
    $who_online "<a href='user.php?id=".$id."'>".
                    
    $ug_prefix.htmlentities($user['username']).$ug_suffix."</a>";
                }
            }
            
    $online_notice "<span style='font-size:11px;'><b>Users online within the past 15 minutes.</b></span>";
            
    defaultTags("0","1","5","MAIN","","$online_notice","1","0");
            
    defaultTags("0","1","1","MAIN_L","width:5px;","$online_bit","0","0");
            
    defaultTags("0","0","2","MAIN","","$who_online","1","1");
        } 

  • #11
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Does the online() function only print one user or all five? If all, then the query is working fine and you can just collect the usernames in an array. If only one, something is broken in the db or the query itself (though it looks okay).
    Are you a Help Vampire?

  • #12
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Only one comes out of it.
    I want everything to come out.

    Thanks.

  • #13
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    The query is straightforward, so it's something else. Toss in a test line to see how many it thinks it's getting back. Use mysql_num_rows(). Try something like this:
    PHP Code:
    // in function online()
            
    $users $queries->userQuery();
            
    // add here
            
    die('rows returned: '.mysql_num_rows($users));
            
    //
            
    while($user mysql_fetch_array($users))
            { 
    Or change it to an echo/print. Just get the number. Since you're only getting one result printed, it should show 1. If so, check your data again. Then do some debugging around the query. Echo/print it, dump the vars inside, etc.
    Are you a Help Vampire?

  • #14
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts
    It shows I'm getting 5, as said.
    See, when I echo it inside the loop, I'm getting all of it.
    But outside, I only get one.

    This is because the value outside the loop, is all it gets because that value isn't in the while loop.
    do I make sense?
    Thanks.

  • #15
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by Crisp View Post
    It shows I'm getting 5, as said.
    See, when I echo it inside the loop, I'm getting all of it.
    That's not what you said before:
    Only one comes out of it.
    You need to decide which is really happening. I'll assume that you didn't know what you were talking about last time and that you really mean the online() function does in fact print all five entries - as you've just said.

    In that case, the method already suggested should work for you, though it might need a slight modification).
    PHP Code:
            // in function online()
            
    $users $queries->userQuery();
            
    // add here
            
    $usernames = array();
            
    //
            
    while($user mysql_fetch_array($users))
            {  
              
    // add here
              
    $usernames[] = $user['username'];
              
    //
              /* .. other stuff .. */
            
    }
            
    // add here
            
    var_dump($usernames);
            
    // 
    Are you a Help Vampire?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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