Jump to content

put into an array from database


brem13

Recommended Posts

i'm trying to figure out how to put data(users) into an array from a database, i'd also like to make it so that array doesnt have duplicates. any help?

$result = mysql_db_query($database, "select * from $table ORDER BY rand()") or die (mysql_error());
while($qry = mysql_fetch_array($result))
{ 
$user = $qry['username'];
$pic = $qry['mainPic'];
$lastOnline = $qry['lastLogin'];
$lookingFor = $qry['lookingFor'];
}//end while
$users = array($user);
$distinct = array_unique($users);

Link to comment
Share on other sites

one more option can be:

 

$users=array();

while($qry = mysql_fetch_array($result))

{

    if(!in_array($qry['username'],$users)

    {

              array_push($users,$qry['username']);

    }

}//end while

 

Link to comment
Share on other sites

I don't know why you'd have duplicate users in that table, but you just need to modify your SQL. Try adding a group by clause:

 

select * from $table GROUP BY username ORDER BY rand()

Link to comment
Share on other sites

yes, here is the full code

while($i < 10)
{
if($i == 4 || $i == 7)
	echo "</tr><tr>";

echo "<td width=225px height=125px align=left valign=top>";
include("config.php");
mysql_connect($server, $db_user, $db_pass) or die (mysql_error()); 
$result = mysql_db_query($database, "select * from $table GROUP BY username ORDER BY rand()") or die (mysql_error());
$users=array();
while($qry = mysql_fetch_array($result))
{ 
	$user = $qry['username'];
	$pic = $qry['mainPic'];
	$lastOnline = $qry['lastLogin'];
	$lookingFor = $qry['lookingFor'];
	if(!in_array($qry['username'],$users))
	{
	     array_push($users,$qry['username']);
	}
}//end while
list($width, $height, $type, $attr) = getimagesize("userImages/thumbs/".$pic);

if($height>=101)
{
	echo "<a href=viewProfile.php?user=$users style='text-decoration:none;'><font face=tahoma size=2 color=#FFFFFF>$users</font><br><img src=userImages/thumbs/$pic height=75px></a><br>";
}//end if $height
else
{
	echo "<a href=viewProfile.php?user=$users style='text-decoration:none;'><font face=tahoma size=2 color=#FFFFFF>$users</font><br><img src=userImages/thumbs/$pic width=75px></a><br>";
}
echo "<font face=tahoma size=1>$lastOnline - $lookingFor</font>";
echo "</td>";

$i++;
}//end while

Link to comment
Share on other sites

ok, tried that, and changed the code around a bit, took it out of the while loop and now i'm getting the same user for all of them

 

modified code:

$result = mysql_db_query($database, "select * from $table ORDER BY rand() LIMIT 9") or die (mysql_error());

while($qry = mysql_fetch_array($result))
{ 
$user = $qry['username'];
$pic = $qry['mainPic'];
$lastOnline = $qry['lastLogin'];
$lookingFor = $qry['lookingFor'];
list($width, $height, $type, $attr) = getimagesize("userImages/thumbs/".$pic);
if($height>=101)
{
	$display = "<a href=viewProfile.php?user=$user style='text-decoration:none;'><font face=tahoma size=2 color=#FFFFFF>$user</font><br><img src=userImages/thumbs/$pic height=75px></a><br>";
}//end if $height
else
{
	$display = "<a href=viewProfile.php?user=$user style='text-decoration:none;'><font face=tahoma size=2 color=#FFFFFF>$user</font><br><img src=userImages/thumbs/$pic width=75px></a><br>";
}

}//end while
$i = 1;
while($i < 10)
{
	if($i == 4 || $i == 7)
	echo "</tr><tr>";

	echo "<td width=225px height=125px align=left valign=top>";
	echo $display;
	echo "<font face=tahoma size=1>$lastOnline - $lookingFor</font>";
	echo "</td>";
	$i++;
}//end while

Link to comment
Share on other sites

ok, finally got it to work, got rid of the while loop, inserted limit 9, changed around code a bit, result below

mysql_connect($server, $db_user, $db_pass) or die (mysql_error()); 
$result = mysql_db_query($database, "select * from $table ORDER BY rand() LIMIT 9") or die (mysql_error());
$i=1;							
while($qry = mysql_fetch_array($result))
{ 
$user = $qry['username'];
$pic = $qry['mainPic'];
$lastOnline = $qry['lastLogin'];
$lookingFor = $qry['lookingFor'];
list($width, $height, $type, $attr) = getimagesize("userImages/thumbs/".$pic);
echo "<td width=225px height=125px align=left valign=top>";
if($height>=101)
{
echo "<a href=viewProfile.php?user=$user style='text-decoration:none;'><font face=tahoma size=2 color=#FFFFFF>$user</font><br><img src=userImages/thumbs/$pic height=75px></a><br>";
}//end if $height
else
{
echo "<a href=viewProfile.php?user=$user style='text-decoration:none;'><font face=tahoma size=2 color=#FFFFFF>$user</font><br><img src=userImages/thumbs/$pic width=75px></a><br>";
}
echo "<font face=tahoma size=1>$lastOnline - $lookingFor</font>";
echo "</td>";
$rows = mysql_num_rows($result);  
if($i==3 || $i==6)
	echo "</tr><tr>";
$i++;
}//end while

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.