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
https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/
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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.