[PHP]Link sets $user

Joined
Mar 26, 2006
Messages
603
Reaction score
7
Heres the script
PHP:
$users = mysql_query("SELECT username FROM userinfo WHERE status=1") or die(mysql_error());
echo("Click the user to add them to your friends list");
echo("<br />");
while($row = mysql_fetch_array($users))
    {
    	$user = $row["username"];
    	echo "<a href='?act=addfriend=$user'>".$row["username"].'</a><br>';
    }
My problem comes in when multiple users are displayed because $user becomes the last user displayed (of course) but i cant figure out a way where when i click the link the username clicked can be set to $user. Sorry if this is confusing
 
ok the script shows a list of users who are currently online, when you click the link i want it to add that user to there friends list, the adding friends is complete the problem comes in with the users links. I want when i click the username it will set $user to the user name. so if i ran the script above and the users name was Test the link would be shown as
a href = ?act=addfriend=Test
Thats where this part of the script is
PHP:
echo "<a href='?act=addfriend=$user'>".$row["username"].'</a><br>';
But the problem is $user is always set to the last user displayed in the list. I want when i click the link $user becomes the username the link is set to in this case i $user = Test; but i cant think of a way to do it
 
If I were you I would add a field in your mysql table named "id" defined as a primary key, with auto_increment turned on. This way you can use this query:

PHP:
$users = mysql_query("SELECT username FROM userinfo WHERE status=1 ORDER BY id DESC") or die(mysql_error());
 
If I were you I would add a field in your mysql table named "id" defined as a primary key, with auto_increment turned on. This way you can use this query:

PHP:
$users = mysql_query("SELECT username FROM userinfo WHERE status=1 ORDER BY id DESC") or die(mysql_error());

I didnt even think about doin that already had userid set in the db but usin it instead of using a while didnt cross my mind, thanks ima go try and see what I can get done now :nosweat:
 
Maybe you can make an array of users, so instead of using a "user" variable, use an array index, i.e.
Code:
$i = 0;
while( etc... ) {
        $user[$i] = $row["username"];
        echo "<a href='?act=addfriend=$user[$i]'>".$row["username"].'</a><br>';
        $i++;
}

Or if $row["username"] gives the correct value, then replace the $user variable entirely with $row["username"] (since you're already using it as the html text for the link, and they are the same value).

I don't really know php but I think that makes sense.
 
Maybe you can make an array of users, so instead of using a "user" variable, use an array index, i.e.
Code:
$i = 0;
while( etc... ) {
        $user[$i] = $row["username"];
        echo "<a href='?act=addfriend=$user[$i]'>".$row["username"].'</a><br>';
        $i++;
}

Or if $row["username"] gives the correct value, then replace the $user variable entirely with $row["username"] (since you're already using it as the html text for the link, and they are the same value).

I don't really know php but I think that makes sense.

na because then m y add friend script wouldnt, work but i fixed it last night using $_GET thanks though
 
Back