Jump to content

Recommended Posts

Hey guys,

 

I'm having an issue. I'm trying to write a script that features a random user.

I wrote it this way first:

 

Featured(Feat_ID, User_ID)

 

featureMe.php

$feat_res = mysql_query("SELECT DISTINCT User_ID FROM Featured WHERE User_ID='$_SESSION[userID]'", $connection);
if (mysql_num_rows($feat_res) == 0) {
if (!$feat_res = mysql_query("INSERT INTO Featured (User_ID) VALUES ('$_SESSION[userID]')", $connection)) {
	die("Error:".mysql_error());
} else {
	echo "You have been added to our list. Thank you for sharing your story!";
	header("refresh:2; url='profile.php'");
}
} else {
echo "You have already been added to our <b>Feature Me!</b> list";
header("refresh:2; url='profile.php'");
}

 

featuredUser.php

$num_users = mysql_num_rows(mysql_query("SELECT User_ID FROM Featured", $connection));

$rand_num = mt_rand(1, $num_users);

$users_res = mysql_query("SELECT User_ID FROM Featured WHERE Feat_ID=$rand_num", $connection);
if (mysql_num_rows($users_res) == 0) {
	echo "Could not complete query. Error:".mysql_error();
} else {
	while ($row_u = mysql_fetch_assoc($users_res)) {
		echo '<b>'.$row_u[user_ID]."</b><br />\r\n";
		$user = $row_u[user_ID];
		$bio_res = mysql_query("SELECT F_NAME, L_Name, Email FROM User WHERE User_ID='$user'", $connection);
		while ($row_b = mysql_fetch_assoc($bio_res)) {
			foreach ($row_b as $key => $value) {
				echo $value.'<br />';
			}
		}
	}

}

 

but when I delete a user from the "Featured" table, the code is just as likely to break as get a featured user (I think because of the way I got a random user)

 

So then I thought I could add a field in the User table ("Featured") with a Bool value for yes or no

Here's that code:

featuredUser(2).php

 

$feat_res = mysql_query("SELECT User_ID FROM User WHERE Featured=1", $connection);

while ($row_f = mysql_fetch_assoc($feat_res)) {
echo $row_f[user_ID].'<br />';
}

 

but am having issues getting a random user. Maybe I've been over this too many times, I'm not sure, but if anyone has any thoughts, ideas, suggestions, I'm all ears.

 

Hope I've provided enough info.

 

Thanks for reading

Link to comment
https://forums.phpfreaks.com/topic/203547-trouble-getting-a-random-featured-user/
Share on other sites

Ok, here's what I came up with for the second solution...let me know if anyone sees anything wrong, because I think it works...

 

$feat_res = mysql_query("SELECT User_ID FROM User WHERE Featured=1 ORDER BY RAND() LIMIT 1", $connection);
while ($row_f = mysql_fetch_assoc($feat_res)) {
echo $row_f[user_ID].'<br />';
}

 

(solution found here:

http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand)

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.