Topic: Random Profile and Search

I'd like an option to show a random profile, not any sensitive information, just their

avatar
nickname
total posts
join date
location

I've seen a php-plugin do that so Im sure PUNbb can cuz its better wink

Also I'd like a forum search box that would work from a front page would be cool too

2 (edited by s0me0ne 2006-03-18 21:18)

Re: Random Profile and Search

I attempted to make a random profile but its not working, the code is complicated kind of but its a efficient random picker (which I found at- http://jan.kneschke.de/projects/mysql/order-by-rand/ )

//random profile
$result = $db->query('SELECT id, username, num_posts, registered, location FROM '.$db->prefix.'users AS r1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM users)) AS id) AS r2 WHERE r1.id >= r2.id ORDER BY r1.id ASC LIMIT 1')
 or error('Unable to fetch user data', __FILE__, __LINE__, $db->error());
while ($data = $db->fetch_assoc($result))
{
echo "\t\t\t\t\t\t".'<a href="http://mysite.com/profile.php?id='.$data['id'].'">'.pun_htmlspecialchars($data['username']).'</a><br />Total Posts: '.$data['num_posts']."<br />Member since:".$data['registered']."<br />Location:".$data['location']."<br />\n";
}

Unfortunately it isnt working sad and Im not sure why I'm sure someone that knows Mysql can figure it out, anyway this is a good starting point

3 (edited by elbekko 2006-03-18 22:58)

Re: Random Profile and Search

This query should do the trick:

SELECT id, username, num_posts, registered, location FROM ".$db->prefix."users WHERE username!='Guest' ORDER BY RAND() LIMIT 1

Tested it, works fine for me...


EDIT:
This code will show what you want:

$query = "SELECT id, username, num_posts, registered, location FROM ".$db->prefix."users WHERE username!='Guest' ORDER BY RAND() LIMIT 1";
$result = $db->query($query) or die(mysql_error());

$data = $db->fetch_assoc($result);

if ($pun_config['o_avatars'] == '1' && $pun_user['show_avatars'] != '0')
        {
            if ($img_size = @getimagesize($pun_config['o_avatars_dir'].'/'.$data['id'].'.gif'))
                $user_avatar = '<img src="'.$pun_config['o_avatars_dir'].'/'.$data['id'].'.gif" '.$img_size[3].' alt="" />';
            else if ($img_size = @getimagesize($pun_config['o_avatars_dir'].'/'.$data['id'].'.jpg'))
                $user_avatar = '<img src="'.$pun_config['o_avatars_dir'].'/'.$data['id'].'.jpg" '.$img_size[3].' alt="" />';
            else if ($img_size = @getimagesize($pun_config['o_avatars_dir'].'/'.$data['id'].'.png'))
                $user_avatar = '<img src="'.$pun_config['o_avatars_dir'].'/'.$data['id'].'.png" '.$img_size[3].' alt="" />';
        }
        else
            $user_avatar = '';

// echo "<br />",$query,"<br /><b>Id: </b>",$data['id'],"<br /><b>Username: </b>",$data['username'],"<br />","<br />";
echo "<br />";
echo "<b>Id:</b> " . $data['id'];
echo "<br />";
echo "<b>Username:</b> " . $data['username'];
echo "<br />";
echo "<b>Posts:</b> " . $data['num_posts'];
echo "<br />";
echo "<b>Registered on:</b> " . $data['registered'];
echo "<br />";
echo "<b>Location:</b> " . $data['location'];
echo "<br />";
// echo "<b>Avatar:</b> <img src=\"img/avatars/" . $data['avatar'] . "\" alt=\"" . $data['avatar'] . "\" />";
echo "<b>Avatar: </b> " . $user_avatar;
echo "<br />";
echo "<br />";
?>

Yes, I know I used die(), it was just for testing some stuff smile

Re: Random Profile and Search

Yeh I knew i could do it that first way but that website I linked to showed it wasnt the most efficient way, but thanks for figuring out that avatar stuff man smile I learned quite a bit from your code too wink

I think you were running some avatar mod/plugin so I had to edit mine since my install is rather plain, I changed the layout some to my liking and fixed the date on it, but if someone is running PunBB without avatar plugins/mods this is some sample code, I replaced the die() also

<p>
Random Profile<br>
<?php
//random profile
$query = "SELECT id, username, num_posts, registered, location FROM ".$db->prefix."users WHERE username!='Guest' ORDER BY RAND() LIMIT 1";
$result = $db->query($query) or error('Unable to fetch user data', __FILE__, __LINE__, $db->error());

$data = $db->fetch_assoc($result);

if ($pun_user['show_avatars'] != '0')//$pun_config['o_avatars'] == '1' && 
        {
        $avatars_dir="forums/img/avatars/";//avatars are located here
        
            if ($img_size = @getimagesize($avatars_dir.'/'.$data['id'].'.gif'))
                $user_avatar = '<img src="'.$avatars_dir.'/'.$data['id'].'.gif" '.$img_size[3].' alt="" />';
            else if ($img_size = @getimagesize($avatars_dir.'/'.$data['id'].'.jpg'))
                $user_avatar = '<img src="'.$avatars_dir.'/'.$data['id'].'.jpg" '.$img_size[3].' alt="" />';
            else if ($img_size = @getimagesize($avatars_dir.'/'.$data['id'].'.png'))
                $user_avatar = '<img src="'.$avatars_dir.'/'.$data['id'].'.png" '.$img_size[3].' alt="" />';
        }
        else
            $user_avatar = '';


// echo "<br />",$query,"<br /><b>Id: </b>",$data['id'],"<br /><b>Username: </b>",$data['username'],"<br />","<br />";

//echo "<b>Id:</b> " . $data['id'];
echo "<strong>" . $data['username'] . "</strong><br />";
echo $user_avatar . "<br />";
echo "Posts: <strong>" . $data['num_posts'] . "</strong><br />";
echo "Registered on: <strong>" . date('M j, Y' , $data['registered']) . "</strong><br />";
echo "Location: <strong>" . $data['location'] . "</strong>";

//echo "<b>Avatar:</b> <img src=\"forums/img/avatars/" . $data['avatar'] . "\" alt=\"" . $data['avatar'] . "\" />";
//echo "<b>Avatar: </b> " . $user_avatar;
?>
</p>

Now all thats left is a search box and I think i think i know how, I'd still like to figure out to do it the efficient way but I guess its not that vital. I plan to learn MySQL soon. This punBB rocks man smile I've got almost everything I need for a simple CMS

Re: Random Profile and Search

now for the search code

<p>
Forum Search - <a href="http://FORUMS.YOURSITE.COM/search.php">more options</a><br />
<form id="search" method="get" action="FORUMS.YOURSITE.COM/search.php">
<input type="hidden" name="action" value="search" />
<input id="author" type="hidden" name="author" size="25" maxlength="25" value="" />
<input type="hidden" class="conl" value="-1" />
<input type="hidden" id="search_in" name="search_in" value="all" />
<input type="hidden" name="sort_by" value="0" />
<input type="hidden" name="sort_dir" value="DESC" />
<input type="hidden" name="show_as" value="topics" />
<label class="conl">Search<br /><input type="text" name="keywords" size="40" maxlength="100" /></label>
<input type="submit" name="search" value="Submit" accesskey="s" />
</form>
</p>

Simplier than I thought, I just had to put in those hidden fields, which you can feel free to edit to change the results of your search, also be sure to change the paths of these to whatever yoursite is smile

Re: Random Profile and Search

As far as I know, I have no specific avatar plugins/mods installed... I made the query, copied the avatar code from viewtopic.php and that was about it tongue