...

View Full Version : While outside of loop



Crisp
01-09-2010, 04:04 AM
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:


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!

chump2877
01-09-2010, 04:10 AM
Try this:


while($user = mysql_fetch_array($users))
{
$usernames[] = $user['username'];
}
var_dump($usernames);

Crisp
01-09-2010, 04:14 AM
Ok, this is what I get:
array(1) { [0]=> string(74) "Crisp" }
Crisp, meaning my username.

Thanks.

chump2877
01-09-2010, 04:19 AM
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.

Crisp
01-09-2010, 04:26 AM
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.

JAY6390
01-09-2010, 05:56 AM
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

Crisp
01-09-2010, 06:13 AM
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.

tomws
01-09-2010, 06:16 AM
Show the code above the loop, including the query.

JAY6390
01-09-2010, 03:46 PM
Yeah show the rest of your code

Crisp
01-09-2010, 06:31 PM
Ok, here is my query class:


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");
}

tomws
01-09-2010, 06:56 PM
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).

Crisp
01-09-2010, 07:33 PM
Only one comes out of it.
I want everything to come out.

Thanks.

tomws
01-09-2010, 08:40 PM
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:

// 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.

Crisp
01-10-2010, 12:59 AM
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. :)

tomws
01-10-2010, 01:39 AM
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. :rolleyes:

In that case, the method already suggested should work for you, though it might need a slight modification).

// 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);
//

Crisp
01-12-2010, 09:16 PM
Not working. :(
Ok, I've made a very simple one.
Here it is:


<?php
mysql_connect("localhost","root");
mysql_select_db("abb");
$users = mysql_query(
"SELECT *
FROM
`revbb_users`"
);
while($user = mysql_fetch_array($users))
{
$users1 = $user['username'];
}
echo $users1;

It only gets 1 user.
And why is this?
Because I am echoing the value ($users1) Outside the while loop.

If I echo it inside the while loop, it gets every one.
Any help?

Thanks.

tomws
01-12-2010, 09:25 PM
In that example, you're assigning a new value each time. It's doing just what you've told it to do. Reproduce the array usage from either my last post or chump's first post and try again.

JAY6390
01-12-2010, 09:30 PM
<?php
mysql_connect("localhost","root");
mysql_select_db("abb");
$users = mysql_query(
"SELECT *
FROM
`revbb_users`"
);
$users = array();
while($user = mysql_fetch_array($users))
{
$users[] = $user['username'];
}
echo $users[0];

JAY6390
01-12-2010, 09:37 PM
to view them all use print_r($users); which will show all the users in the array

Crisp
01-13-2010, 06:25 AM
@Tomws,
Ok that var_dump($usernames); works right, but I don't want all of the array(=>) stuff there.
Do you know how to fix that?
Thanks!

abduraooft
01-13-2010, 08:04 AM
echo implode(",",$usernames);

JAY6390
01-13-2010, 01:46 PM
The array stuff is to show you the current structure of the array. That doesn't exist in the actual data. For example, use

echo $users[2];
that will show you the third username (arrays have base 0 so it goes 0 1 2 ... and so on)
you can also use a foreach

foreach($users as $user) {
echo 'Username: '. $user.'<br />';
}

tomws
01-13-2010, 02:58 PM
It's an array! Use it like an array! :rolleyes:

Crisp
01-13-2010, 08:59 PM
echo implode(",",$usernames);
That works!
Thank you so much! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum