Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-09-2010, 03:04 AM   PM User | #1
Crisp
New Coder

 
Join Date: Feb 2009
Posts: 96
Thanks: 8
Thanked 0 Times in 0 Posts
Crisp has a little shameless behaviour in the past
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!
Crisp is offline   Reply With Quote
Old 01-09-2010, 03:10 AM   PM User | #2
chump2877
Senior Coder

 
chump2877's Avatar
 
Join Date: Dec 2004
Location: the U.S. of freakin' A.
Posts: 2,530
Thanks: 15
Thanked 128 Times in 121 Posts
chump2877 is on a distinguished road
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 Web Conversion Software on Facebook !! :)
chump2877 is offline   Reply With Quote
Old 01-09-2010, 03:14 AM   PM User | #3
Crisp
New Coder

 
Join Date: Feb 2009
Posts: 96
Thanks: 8
Thanked 0 Times in 0 Posts
Crisp has a little shameless behaviour in the past
Ok, this is what I get:
array(1) { [0]=> string(74) "Crisp" }
Crisp, meaning my username.

Thanks.
Crisp is offline   Reply With Quote
Old 01-09-2010, 03:19 AM   PM User | #4
chump2877
Senior Coder

 
chump2877's Avatar
 
Join Date: Dec 2004
Location: the U.S. of freakin' A.
Posts: 2,530
Thanks: 15
Thanked 128 Times in 121 Posts
chump2877 is on a distinguished road
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 Web Conversion Software on Facebook !! :)
chump2877 is offline   Reply With Quote
Old 01-09-2010, 03:26 AM   PM User | #5
Crisp
New Coder

 
Join Date: Feb 2009
Posts: 96
Thanks: 8
Thanked 0 Times in 0 Posts
Crisp has a little shameless behaviour in the past
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.
Crisp is offline   Reply With Quote
Old 01-09-2010, 04:56 AM   PM User | #6
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
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 :)
JAY6390 is offline   Reply With Quote
Old 01-09-2010, 05:13 AM   PM User | #7
Crisp
New Coder

 
Join Date: Feb 2009
Posts: 96
Thanks: 8
Thanked 0 Times in 0 Posts
Crisp has a little shameless behaviour in the past
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.
Crisp is offline   Reply With Quote
Old 01-09-2010, 05:16 AM   PM User | #8
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
Show the code above the loop, including the query.
__________________
Are you a Help Vampire?
tomws is offline   Reply With Quote
Old 01-09-2010, 02:46 PM   PM User | #9
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
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 :)
JAY6390 is offline   Reply With Quote
Old 01-09-2010, 05:31 PM   PM User | #10
Crisp
New Coder

 
Join Date: Feb 2009
Posts: 96
Thanks: 8
Thanked 0 Times in 0 Posts
Crisp has a little shameless behaviour in the past
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");
    } 
Crisp is offline   Reply With Quote
Old 01-09-2010, 05:56 PM   PM User | #11
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
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?
tomws is offline   Reply With Quote
Old 01-09-2010, 06:33 PM   PM User | #12
Crisp
New Coder

 
Join Date: Feb 2009
Posts: 96
Thanks: 8
Thanked 0 Times in 0 Posts
Crisp has a little shameless behaviour in the past
Only one comes out of it.
I want everything to come out.

Thanks.
Crisp is offline   Reply With Quote
Old 01-09-2010, 07:40 PM   PM User | #13
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
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?
tomws is offline   Reply With Quote
Old 01-09-2010, 11:59 PM   PM User | #14
Crisp
New Coder

 
Join Date: Feb 2009
Posts: 96
Thanks: 8
Thanked 0 Times in 0 Posts
Crisp has a little shameless behaviour in the past
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.
Crisp is offline   Reply With Quote
Old 01-10-2010, 12:39 AM   PM User | #15
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
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:
Quote:
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?
tomws is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:42 PM.


Advertisement
Log in to turn off these ads.