Facebook  Twitter 

SMFHacks.com

+-

SMFHacks.com

+- User Information

Welcome, Guest.
Please login or register.
 
 
 
Forgot your password?

+- Forum Stats

Members
Total Members: 4257
Latest: Alex998.
New This Month: 1
New This Week: 0
New Today: 0
Stats
Total Posts: 43295
Total Topics: 7523
Most Online Today: 212
Most Online Ever: 2482
(April 09, 2011, 07:02:45 pm)
Users Online
Members: 0
Guests: 203
Total: 203

Author Topic: How do I randomize the related gallery images?  (Read 4597 times)

0 Members and 1 Guest are viewing this topic.

Offline shuban

  • Hero Member
  • *****
  • Posts: 665
    • View Profile
    • Biology Forums
How do I randomize the related gallery images?
« on: September 22, 2014, 01:39:11 pm »
Take this topic, for instance - http://biology-forums.com/index.php?topic=139886

Notice on the right it shows the related images. I'm sure there are a lot more related images, but it only shows the top three. What can I do to the following code to randomize them?

Code: [Select]
function Gallery_AddRelatedPicture($pictureID, $title = '')
{
global $db_prefix;

if (empty($title))
return;

db_query("INSERT IGNORE INTO {$db_prefix}gallery_title_cache
(ID_PICTURE, title)
VALUES ($pictureID, '$title')", __FILE__, __LINE__);


$relatedPics = Gallery_FindRelated($title, true);

if (empty($relatedPics))
return false;


$relatedPics = array_slice($relatedPics, 0, 10);

$picsSQL = '';

foreach ($relatedPics as $id)
if ($pictureID != $id[0])
$picsSQL .= '
(' . min($pictureID, $id[0]) . ', ' . max($pictureID, $id[0]) . ', ' . $id[1] . '),';

if (empty($picsSQL))
return false;

db_query("INSERT IGNORE INTO {$db_prefix}gallery_related_pictures
(id_picture_first, id_picture_second, score)
VALUES" . substr($picsSQL, 0, -1), __FILE__, __LINE__);

}

function Gallery_FindRelated($title, $score = false)
{
global $db_prefix;

$result = db_query("SELECT ID_PICTURE, MATCH(title) AGAINST('$title') AS score
FROM {$db_prefix}gallery_title_cache
WHERE MATCH(title) AGAINST('$title')
ORDER BY MATCH(title) AGAINST('$title') DESC", __FILE__, __LINE__);

$data = array();

while ($row = mysql_fetch_assoc($result))
{
if ($score == false)
$data[] = $row['ID_PICTURE'];
else
$data[] = array($row['ID_PICTURE'], $row['score']);
}

return $data;
}

Offline SMFHacks

  • Administrator
  • Hero Member
  • *****
  • Posts: 16452
    • View Profile
Re: How do I randomize the related gallery images?
« Reply #1 on: September 22, 2014, 01:45:26 pm »
It would probably be this part of code
$relatedPics = array_slice($relatedPics, 0, 10);

You would need before it randomize the $relatedPics array
Get your Forum Ranked! at https://www.forumrankings.net - find out how your forum compares with others!

Like What I do? Support me at https://www.patreon.com/vbgamer45/

Offline shuban

  • Hero Member
  • *****
  • Posts: 665
    • View Profile
    • Biology Forums
Re: How do I randomize the related gallery images?
« Reply #2 on: September 22, 2014, 01:52:13 pm »
It would probably be this part of code
$relatedPics = array_slice($relatedPics, 0, 10);

You would need before it randomize the $relatedPics array

I thought I'd need to use RAND somewhere...

Offline SMFHacks

  • Administrator
  • Hero Member
  • *****
  • Posts: 16452
    • View Profile
Re: How do I randomize the related gallery images?
« Reply #3 on: September 22, 2014, 02:14:56 pm »
In the mysql query normally use order by rand
but for php data array you can use shuffle command http://php.net/manual/en/function.shuffle.php
Get your Forum Ranked! at https://www.forumrankings.net - find out how your forum compares with others!

Like What I do? Support me at https://www.patreon.com/vbgamer45/

Offline shuban

  • Hero Member
  • *****
  • Posts: 665
    • View Profile
    • Biology Forums
Re: How do I randomize the related gallery images?
« Reply #4 on: September 22, 2014, 02:36:52 pm »
I tried this, but it didn't work.

Code: [Select]
$relatedPics = shuffle($relatedPics);

$relatedPics = array_slice($relatedPics, 0, 10);
   $picsSQL = '';

Offline shuban

  • Hero Member
  • *****
  • Posts: 665
    • View Profile
    • Biology Forums
Re: How do I randomize the related gallery images?
« Reply #5 on: September 22, 2014, 02:38:18 pm »
Wait this worked:

Code: [Select]
shuffle($relatedPics);

Offline shuban

  • Hero Member
  • *****
  • Posts: 665
    • View Profile
    • Biology Forums
Re: How do I randomize the related gallery images?
« Reply #6 on: September 22, 2014, 02:38:43 pm »
You should have a mark as solved button on here.

 

Related Topics

  Subject / Started by Replies Last post
0 Replies
3911 Views
Last post December 14, 2007, 03:53:28 am
by Vincent Volmer
2 Replies
3445 Views
Last post November 21, 2011, 03:43:36 pm
by Vincent Volmer
3 Replies
4014 Views
Last post June 09, 2012, 08:58:43 am
by SMFHacks
3 Replies
3205 Views
Last post January 06, 2016, 10:25:28 am
by SMFHacks
3 Replies
2070 Views
Last post August 09, 2020, 01:52:57 pm
by SMFHacks

+- Recent Topics

Please Help! by SMFHacks
April 17, 2024, 08:04:55 am

Rate own images by fvlog19
April 11, 2024, 10:56:53 am

Tidy Child Boards on 2.1.4 by SMFHacks
April 04, 2024, 03:54:12 pm

Problems SMF 2.0.19 > 2.1.4 SMF Gallery Pro - Recents Images to overall header by Michel68
March 30, 2024, 12:41:08 pm

Can't DROP 'id_member'; check that column/key exists Datei: by SMFHacks
March 30, 2024, 11:58:20 am

No thumbnails on new uploads by Tonyvic
March 29, 2024, 06:26:18 am

Display the Contact Page for guests by SMFHacks
March 27, 2024, 10:55:43 am

is it possible to add support for odysee.com by fvlog19
March 21, 2024, 08:47:51 am

Request for admin notification by davejo
March 10, 2024, 01:31:59 am

I need help with torrent upload by Ineedsmfhelp
March 09, 2024, 10:01:13 pm

Powered by EzPortal