SimplePortal

Customization => Custom Coding => Topic started by: KahneFan on February 03, 2009, 02:50:40 AM

Title: Random URL Block
Post by: KahneFan on February 03, 2009, 02:50:40 AM
It would be nice if we could highlite a random member's URL each day by adding a block to our homepage with a random URL pulled from a random member's profile within a set period of time (24 hours).
Title: Re: Random URL Block
Post by: [SiNaN] on February 03, 2009, 03:56:44 AM
+ Gets a random member id
+ Changes it every 24 hours
+ Does not choose same member following day
+ Caches member name

Code: [Select]
global $db_prefix, $scripturl, $modSettings;

if (!empty($modSettings['sp_random_member']))
$member = explode(',', $modSettings['sp_random_member']);

if (empty($member) || $member[2] + 86400 < time())
{
$request = db_query("
SELECT ID_MEMBER, realName
FROM {$db_prefix}members" . (!empty($member[0]) ? "
WHERE ID_MEMBER != $member[0]" : "") . "
ORDER BY RAND()
LIMIT 1", __FILE__, __LINE__);
list ($id, $username) = mysql_fetch_row($request);
mysql_free_result($request);

if (empty($id))
return 'No members.';

updateSettings(array(
'sp_random_member' => $id . ',' . $username . ',' . time()
));
}

if (!empty($id) && !empty($username))
$member = array($id, $username);

echo '
<a href="', $scripturl, '?action=profile;u=', $member[0], '" title="', $member[1], '">', $member[1], '</a>';
Title: Re: Random URL Block
Post by: KahneFan on February 03, 2009, 03:46:49 PM
I have not built a php block yet. Would I enter the code just as you have it listed?
Title: Re: Random URL Block
Post by: [SiNaN] on February 04, 2009, 02:37:55 AM
Yeah, just a create a PHP block and use the codes I gave as the content.
Title: Re: Random URL Block
Post by: KahneFan on February 04, 2009, 10:02:34 AM
When I try it as is, I receive the following error...

Quote
Fatal error: Call to undefined function db_query() in .../Sources/SPortal2.php(1863) : eval()'d code on line 8
Title: Re: Random URL Block
Post by: KahneFan on February 05, 2009, 04:30:49 PM
(I hope a 24 hour bump is OK)

BD, any thought on the error?

Thank you for your help so far BTW.
Title: Re: Random URL Block
Post by: [SiNaN] on February 06, 2009, 03:13:35 AM
The code I gave was for SMF 1.x. I wish you metioned you use SMF 2.x. Use the following codes:

Code: [Select]
global $smcFunc, $scripturl, $modSettings;

if (!empty($modSettings['sp_random_member']))
$member = explode(',', $modSettings['sp_random_member']);

if (empty($member) || $member[2] + 86400 < time())
{
$request = $smcFunc['db_query']('', '
SELECT id_member, real_name
FROM {db_prefix}members'  . (!empty($member[0]) ? '
WHERE id_member != {int:current_member}' : '') . '
ORDER BY RAND()
LIMIT 1',
array(
'current_member' => !empty($member[0]) ? $member[0] : 0,
)
);
list ($id, $username) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);

if (empty($id))
return 'No members.';

updateSettings(array(
'sp_random_member' => $id . ',' . $username . ',' . time()
));
}

if (!empty($id) && !empty($username))
$member = array($id, $username);

echo '
<a href="', $scripturl, '?action=profile;u=', $member[0], '" title="', $member[1], '">', $member[1], '</a>';
Title: Re: Random URL Block
Post by: KahneFan on February 06, 2009, 03:22:46 AM
I should have mentioned that, sorry. Now the codes out there for both though... right  :-[

Again, it worked perfectly, thanks!!!
Title: Re: Random URL Block
Post by: KahneFan on February 06, 2009, 03:27:50 AM
BD, sorry for bugging again. I just realized, that gives a user of the day, it linked to a user profile instead of to a site. I was hoping to have something like...

{linkable-URLtitle} by {deadtext-username}

(the "by {username}" isn't a must, but it would be nice)

If you take a peek at the top left block on my site, you'll see it currently links to a member's profile.
Title: Re: Random URL Block
Post by: KahneFan on February 06, 2009, 03:34:15 AM
I think I see the issue, it's the way I phrased the request. I did say member's URL, but I meant the URL from a member's profile. For instance, if it highlighted me for a day it would say...

I Post You Post (http://ipostyoupost.com) by KahneFan
Title: Re: Random URL Block
Post by: [SiNaN] on February 06, 2009, 03:35:29 AM
You mean you want to show users' website in that block?
Title: Re: Random URL Block
Post by: KahneFan on February 06, 2009, 03:48:20 AM
Just the link to their site. Since I Post You Post is about helping to promote forums, it would be a way to spotlite a forum of the day. So, no graphics or anything (screenshot), just the link they have listed in their profile.

 You know, I just looked at the profile listings here and I don't see the "website" field like I used to see on 1.1.x. In our profiles there is a "Website Title" and "Website URL" field which USED to come together to give a profile line of:

"Website:  {linked titled}"

I no longer see that in 2.x, but that was the effect I was looking for was to be able to place that linked title in a box so I could highlight a user's forum each day.


Again, to use me as an example; in my profile I have listed:
Website Title: I Post You Post
Website URL: http://ipostyoupost.com

and I as hoping the block would be able to display

I Post You Post (http://ipostyoupost.com) by KahneFan

Title: Re: Random URL Block
Post by: [SiNaN] on April 03, 2009, 02:48:53 AM
Sorry for delayed response.

Code: [Select]
global $smcFunc, $scripturl, $modSettings;

if (!empty($modSettings['sp_random_site']))
   $member = explode('~', $modSettings['sp_random_site']);

if (empty($member) || $member[4] + 86400 < time())
{
   $request = $smcFunc['db_query']('', '
      SELECT id_member, real_name, website_title, website_url
      FROM {db_prefix}members
  WHERE website_url != {string:empty_string}'  . (!empty($member[0]) ? '
         AND id_member != {int:current_member}' : '') . '
      ORDER BY RAND()
      LIMIT 1',
      array(
         'current_member' => !empty($member[0]) ? $member[0] : 0,
         'empty_string' => '',
      )
   );
   list ($id, $username, $title, $url) = $smcFunc['db_fetch_row']($request);
   $smcFunc['db_free_result']($request);

   if (empty($id))
   {
      echo 'No members.';
      return;
   }

   updateSettings(array(
      'sp_random_site' => implode('~', array($id, $username, $title, $url, time())),
   ));
}

if (!empty($id) && !empty($username))
   $member = array($id, $username, $title, $url);

echo '
<a href="', $member[3], '">', !empty($member[2]) ? $member[2] : $member[3], '</a> by <a href="', $scripturl, '?action=profile;u=', $member[0], '" title="', $member[1], '">', $member[1], '</a>';
Title: Re: Random URL Block
Post by: KahneFan on April 03, 2009, 03:35:24 PM
Perfect, thanks!

What length of time is that?
Title: Re: Random URL Block
Post by: Nathaniel on April 03, 2009, 08:30:14 PM
That code should change the website once a day. Is that what you were refering to?

You can change the '86400' value (in seconds) if you want to change the length of time that a website is shown for. ;)
Title: Re: Random URL Block
Post by: KahneFan on April 04, 2009, 01:08:40 AM
... value (in seconds)...

That's what I was wondering. Thanks :D
Title: Re: Random URL Block
Post by: KahneFan on April 06, 2009, 12:26:01 AM
Is it possible to make the site open in a new window?
Title: Re: Random URL Block
Post by: Eliana Tamerin on April 06, 2009, 01:04:42 AM
Certainly, change this code:

Code: [Select]
echo '
<a href="', $member[3], '">', !empty($member[2]) ? $member[2] : $member[3], '</a> by <a href="', $scripturl, '?action=profile;u=', $member[0], '" title="', $member[1], '">', $member[1], '</a>';

To this:
Code: [Select]
echo '
<a href="', $member[3], '" target="_blank">', !empty($member[2]) ? $member[2] : $member[3], '</a> by <a href="', $scripturl, '?action=profile;u=', $member[0], '" title="', $member[1], '">', $member[1], '</a>';
Title: Re: Random URL Block
Post by: KahneFan on April 06, 2009, 01:18:24 AM
(to self) DUH!

I should have seen that one. Thanks  :D
SimplePortal 2.3.8 © 2008-2024, SimplePortal