SimplePortal
Customization => Custom Coding => Topic started by: Cyst on September 18, 2008, 06:49:09 AM
-
I was wondering if there is a way to write a custom Block to pull not only a random image from the SMF Gallery, but also the description of the image under it?
Cheers :)
-
By the way if it's not possible, this might be more useful in the feature request section :)
-
Maybe we can add the description field too, not sure how it looks.
Try creating a php block with this content:
global $modSettings, $context, $user_info, $scripturl, $sc, $ID_MEMBER, $db_prefix, $txt;
$GD_Installed = function_exists('imagecreate');
$dbresult = db_query("
SELECT p.ID_PICTURE
FROM {$db_prefix}gallery_pic as p
ORDER BY ID_PICTURE DESC
LIMIT 1", __FILE__, __LINE__);
$max = mysql_fetch_assoc($dbresult);
$check = 0;
while($check == 0) {
$idpic = rand(1, $max['ID_PICTURE']);
$dbresult = db_query("
SELECT p.ID_PICTURE, p.commenttotal, p.filesize, p.views, p.thumbfilename, p.filename, p.height, p.width, p.title, p.ID_MEMBER,
m.memberName, m.realName, p.date, p.description
FROM {$db_prefix}gallery_pic as p
LEFT JOIN {$db_prefix}members AS m on ( p.ID_MEMBER = m.ID_MEMBER)
WHERE p.approved = 1 AND p.ID_PICTURE = $idpic
ORDER BY ID_PICTURE DESC", __FILE__, __LINE__);
$gallery_picture = array();
while($row = mysql_fetch_assoc($dbresult))
{
$gallery_picture = array (
'id' => $row['ID_PICTURE'],
'title' => $row['title'],
'desc' => $row['description'],
'views' => $row['views'],
'senderid' => $row['ID_MEMBER'],
'sendername' => $row['realName'],
'thumbfilename' => $row['thumbfilename'],
'filename' => $row['filename']
);
}
mysql_free_result($dbresult);
if(!empty($gallery_picture) || empty($max))
$check = 1;
}
if (!empty($gallery_picture)) {
echo '<div align="center">';
echo '
<a href="' . $scripturl . '?action=gallery;sa=view;id=' . $gallery_picture['id'] . '"><img ' . ($GD_Installed == true ? 'src="' . $modSettings['gallery_url'] . $gallery_picture['thumbfilename'] . '" ' : 'src="' . $modSettings['gallery_url'] . $gallery_picture['filename'] . '" height="78" width="120" ') . ' border="0" /></a>
<br />' . $gallery_picture['title'] . '<span class="smalltext"><br />
' . $gallery_picture['desc'] . '<br />
<b>' . $txt['sp-grpictureviews'] . '</b> ' . $gallery_picture['views'] . '<br />';
if ($gallery_picture['sendername'] != '')
echo '<b>' . $txt['sp-grpicturesender'] . '</b> <a href="' . $scripturl . '?action=profile;u=' . $gallery_picture['senderid'] . '">' . $gallery_picture['sendername'] . '</a><br />';
else
echo '<b>' . $txt['sp-grpicturesender'] . '</b> ' . $txt['sp-grpictureguest'] . '<br />';
echo '</span>';
echo '</div>';
}
else
echo 'There is no picture in the gallery.';
-
Wow, it worked a treat. With small sentences it does a great job.
It has a little trouble with long descriptions which is a shame though (links aren't formatted either which is a shame) is there anyway to force it to stretch the block vertically instead of horizontally and to format links? I feel like I'm asking for a bit too much, sorry! :wave:
edit: Or perhaps a sort of cut off on the amount of characters shown and a small '...' link to the rest in the gallery would be better?
-
We can cut some part of the description if you want, just tell me the char limit. And what you mean by "format links"?
Feel free to ask whatever you want, we are here to answer them. ;)
-
Hmm, maybe a 60 character limit would work? From the looks of it someone with a 101 characters on their description has stretched the block, while someone with 53 characters looks fine.
Would there be a way to add a link to the rest of the cut off description in the gallery, some thing like three periods that when clicked takes you there? I imagine that would be hard considering it's random...
By formatting of links I mean if someone posts [url=http://url]text[/url]
it shows the raw code instead of the actual link.
-
Ah, you mean BBC...
Edit the block, and use this codes instead:
global $modSettings, $context, $user_info, $scripturl, $sc, $ID_MEMBER, $db_prefix, $txt;
$GD_Installed = function_exists('imagecreate');
$dbresult = db_query("
SELECT p.ID_PICTURE
FROM {$db_prefix}gallery_pic as p
ORDER BY ID_PICTURE DESC
LIMIT 1", __FILE__, __LINE__);
$max = mysql_fetch_assoc($dbresult);
$check = 0;
while($check == 0) {
$idpic = rand(1, $max['ID_PICTURE']);
$dbresult = db_query("
SELECT p.ID_PICTURE, p.commenttotal, p.filesize, p.views, p.thumbfilename, p.filename, p.height, p.width, p.title, p.ID_MEMBER,
m.memberName, m.realName, p.date, p.description
FROM {$db_prefix}gallery_pic as p
LEFT JOIN {$db_prefix}members AS m on ( p.ID_MEMBER = m.ID_MEMBER)
WHERE p.approved = 1 AND p.ID_PICTURE = $idpic
ORDER BY ID_PICTURE DESC", __FILE__, __LINE__);
$gallery_picture = array();
while($row = mysql_fetch_assoc($dbresult))
{
$gallery_picture = array (
'id' => $row['ID_PICTURE'],
'title' => $row['title'],
'desc' => $row['description'],
'views' => $row['views'],
'senderid' => $row['ID_MEMBER'],
'sendername' => $row['realName'],
'thumbfilename' => $row['thumbfilename'],
'filename' => $row['filename']
);
}
mysql_free_result($dbresult);
if(!empty($gallery_picture) || empty($max))
$check = 1;
}
if (!empty($gallery_picture)) {
echo '<div align="center">';
echo '
<a href="' . $scripturl . '?action=gallery;sa=view;id=' . $gallery_picture['id'] . '"><img ' . ($GD_Installed == true ? 'src="' . $modSettings['gallery_url'] . $gallery_picture['thumbfilename'] . '" ' : 'src="' . $modSettings['gallery_url'] . $gallery_picture['filename'] . '" height="78" width="120" ') . ' border="0" /></a>
<br />' . $gallery_picture['title'] . '<span class="smalltext"><br />
' . parse_bbc($gallery_picture['desc']) . '<br />
<b>' . $txt['sp-grpictureviews'] . '</b> ' . $gallery_picture['views'] . '<br />';
if ($gallery_picture['sendername'] != '')
echo '<b>' . $txt['sp-grpicturesender'] . '</b> <a href="' . $scripturl . '?action=profile;u=' . $gallery_picture['senderid'] . '">' . $gallery_picture['sendername'] . '</a><br />';
else
echo '<b>' . $txt['sp-grpicturesender'] . '</b> ' . $txt['sp-grpictureguest'] . '<br />';
echo '</span>';
echo '</div>';
}
else
echo 'There is no picture in the gallery.';
It will make those raws codes normal. But with this, limiting the chars could result with descriptions like this:
This is a description. <a href="http://gotosomewhere.net">Tes...
Normally it should be
This is a description. Testing
Word "Testing" is link to http://gotosomewhere.net.
If you want, we can remove those raw codes, but they won't be shown as link; just as a plain text without raw codes.
-
If you want, we can remove those raw codes, but they won't be shown as link; just as a plain text without raw codes.
This seems like a better idea, yes :) That way the tables won't get broken and there will be no BBC codes showing up either.
-
This will remove BBCodes and just show 150 chars:
global $modSettings, $context, $user_info, $scripturl, $sc, $ID_MEMBER, $db_prefix, $txt, $func;
$GD_Installed = function_exists('imagecreate');
$dbresult = db_query("
SELECT p.ID_PICTURE
FROM {$db_prefix}gallery_pic as p
ORDER BY ID_PICTURE DESC
LIMIT 1", __FILE__, __LINE__);
$max = mysql_fetch_assoc($dbresult);
$check = 0;
while($check == 0) {
$idpic = rand(1, $max['ID_PICTURE']);
$dbresult = db_query("
SELECT p.ID_PICTURE, p.commenttotal, p.filesize, p.views, p.thumbfilename, p.filename, p.height, p.width, p.title, p.ID_MEMBER,
m.memberName, m.realName, p.date, p.description
FROM {$db_prefix}gallery_pic as p
LEFT JOIN {$db_prefix}members AS m on ( p.ID_MEMBER = m.ID_MEMBER)
WHERE p.approved = 1 AND p.ID_PICTURE = $idpic
ORDER BY ID_PICTURE DESC", __FILE__, __LINE__);
$gallery_picture = array();
while($row = mysql_fetch_assoc($dbresult))
{
$row['description'] = strip_tags(strtr(parse_bbc($row['description'], 1), array('<br />' => '')));
if ($func['strlen']($row['description']) > 128)
$row['description'] = $func['substr']($row['description'], 0, 150) . '...';
$gallery_picture = array (
'id' => $row['ID_PICTURE'],
'title' => $row['title'],
'desc' => $row['description'],
'views' => $row['views'],
'senderid' => $row['ID_MEMBER'],
'sendername' => $row['realName'],
'thumbfilename' => $row['thumbfilename'],
'filename' => $row['filename']
);
}
mysql_free_result($dbresult);
if(!empty($gallery_picture) || empty($max))
$check = 1;
}
if (!empty($gallery_picture)) {
echo '<div align="center">';
echo '
<a href="' . $scripturl . '?action=gallery;sa=view;id=' . $gallery_picture['id'] . '"><img ' . ($GD_Installed == true ? 'src="' . $modSettings['gallery_url'] . $gallery_picture['thumbfilename'] . '" ' : 'src="' . $modSettings['gallery_url'] . $gallery_picture['filename'] . '" height="78" width="120" ') . ' border="0" /></a>
<br />' . $gallery_picture['title'] . '<span class="smalltext"><br />
' . $gallery_picture['desc'] . '<br />
<b>' . $txt['sp-grpictureviews'] . '</b> ' . $gallery_picture['views'] . '<br />';
if ($gallery_picture['sendername'] != '')
echo '<b>' . $txt['sp-grpicturesender'] . '</b> <a href="' . $scripturl . '?action=profile;u=' . $gallery_picture['senderid'] . '">' . $gallery_picture['sendername'] . '</a><br />';
else
echo '<b>' . $txt['sp-grpicturesender'] . '</b> ' . $txt['sp-grpictureguest'] . '<br />';
echo '</span>';
echo '</div>';
}
else
echo 'There is no picture in the gallery.';
If you want more or less, just change the number 150 in the codes.
-
Thanks Sinan, that's perfect :) Was making the "..." after the character limit cut off link to the rest of the description possible too? If not I'm very happy anyway!
-
Try this:
global $modSettings, $context, $user_info, $scripturl, $sc, $ID_MEMBER, $db_prefix, $txt, $func;
$GD_Installed = function_exists('imagecreate');
$dbresult = db_query("
SELECT p.ID_PICTURE
FROM {$db_prefix}gallery_pic as p
ORDER BY ID_PICTURE DESC
LIMIT 1", __FILE__, __LINE__);
$max = mysql_fetch_assoc($dbresult);
$check = 0;
while($check == 0) {
$idpic = rand(1, $max['ID_PICTURE']);
$dbresult = db_query("
SELECT p.ID_PICTURE, p.commenttotal, p.filesize, p.views, p.thumbfilename, p.filename, p.height, p.width, p.title, p.ID_MEMBER,
m.memberName, m.realName, p.date, p.description
FROM {$db_prefix}gallery_pic as p
LEFT JOIN {$db_prefix}members AS m on ( p.ID_MEMBER = m.ID_MEMBER)
WHERE p.approved = 1 AND p.ID_PICTURE = $idpic
ORDER BY ID_PICTURE DESC", __FILE__, __LINE__);
$gallery_picture = array();
while($row = mysql_fetch_assoc($dbresult))
{
$row['description'] = strip_tags(strtr(parse_bbc($row['description'], 1), array('<br />' => '')));
if ($func['strlen']($row['description']) > 128)
$row['description'] = $func['substr']($row['description'], 0, 150) . '<a href="' . $scripturl . '?action=gallery;sa=view;id=' . $row['ID_PICTURE'] . '">...</a>';
$gallery_picture = array (
'id' => $row['ID_PICTURE'],
'title' => $row['title'],
'desc' => $row['description'],
'views' => $row['views'],
'senderid' => $row['ID_MEMBER'],
'sendername' => $row['realName'],
'thumbfilename' => $row['thumbfilename'],
'filename' => $row['filename']
);
}
mysql_free_result($dbresult);
if(!empty($gallery_picture) || empty($max))
$check = 1;
}
if (!empty($gallery_picture)) {
echo '<div align="center">';
echo '
<a href="' . $scripturl . '?action=gallery;sa=view;id=' . $gallery_picture['id'] . '"><img ' . ($GD_Installed == true ? 'src="' . $modSettings['gallery_url'] . $gallery_picture['thumbfilename'] . '" ' : 'src="' . $modSettings['gallery_url'] . $gallery_picture['filename'] . '" height="78" width="120" ') . ' border="0" /></a>
<br />' . $gallery_picture['title'] . '<span class="smalltext"><br />
' . $gallery_picture['desc'] . '<br />
<b>' . $txt['sp-grpictureviews'] . '</b> ' . $gallery_picture['views'] . '<br />';
if ($gallery_picture['sendername'] != '')
echo '<b>' . $txt['sp-grpicturesender'] . '</b> <a href="' . $scripturl . '?action=profile;u=' . $gallery_picture['senderid'] . '">' . $gallery_picture['sendername'] . '</a><br />';
else
echo '<b>' . $txt['sp-grpicturesender'] . '</b> ' . $txt['sp-grpictureguest'] . '<br />';
echo '</span>';
echo '</div>';
}
else
echo 'There is no picture in the gallery.';
Hope I got what you mean. :)
-
Exactly what I was after, thanks Sinan. It looks great in action 8)
-
You're welcome. ;)
-
This is weird, it was working perfectly a second ago and now it's allowing descriptions bigger than 60 characters to show again, I did change the 150 to 60, did I break anything? :D
global $modSettings, $context, $user_info, $scripturl, $sc, $ID_MEMBER, $db_prefix, $txt, $func;
$GD_Installed = function_exists('imagecreate');
$dbresult = db_query("
SELECT p.ID_PICTURE
FROM {$db_prefix}gallery_pic as p
ORDER BY ID_PICTURE DESC
LIMIT 1", __FILE__, __LINE__);
$max = mysql_fetch_assoc($dbresult);
$check = 0;
while($check == 0) {
$idpic = rand(1, $max['ID_PICTURE']);
$dbresult = db_query("
SELECT p.ID_PICTURE, p.commenttotal, p.filesize, p.views, p.thumbfilename, p.filename, p.height, p.width, p.title, p.ID_MEMBER,
m.memberName, m.realName, p.date, p.description
FROM {$db_prefix}gallery_pic as p
LEFT JOIN {$db_prefix}members AS m on ( p.ID_MEMBER = m.ID_MEMBER)
WHERE p.approved = 1 AND p.ID_PICTURE = $idpic
ORDER BY ID_PICTURE DESC", __FILE__, __LINE__);
$gallery_picture = array();
while($row = mysql_fetch_assoc($dbresult))
{
$row['description'] = strip_tags(strtr(parse_bbc($row['description'], 1), array('<br />' => '')));
if ($func['strlen']($row['description']) > 128)
$row['description'] = $func['substr']($row['description'], 0, 60) . '<a href="' . $scripturl . '?action=gallery;sa=view;id=' . $row['ID_PICTURE'] . '">...</a>';
$gallery_picture = array (
'id' => $row['ID_PICTURE'],
'title' => $row['title'],
'desc' => $row['description'],
'views' => $row['views'],
'senderid' => $row['ID_MEMBER'],
'sendername' => $row['realName'],
'thumbfilename' => $row['thumbfilename'],
'filename' => $row['filename']
);
}
mysql_free_result($dbresult);
if(!empty($gallery_picture) || empty($max))
$check = 1;
}
if (!empty($gallery_picture)) {
echo '<div align="center">';
echo '
<a href="' . $scripturl . '?action=gallery;sa=view;id=' . $gallery_picture['id'] . '"><img ' . ($GD_Installed == true ? 'src="' . $modSettings['gallery_url'] . $gallery_picture['thumbfilename'] . '" ' : 'src="' . $modSettings['gallery_url'] . $gallery_picture['filename'] . '" height="78" width="120" ') . ' border="0" /></a>
<br />' . $gallery_picture['title'] . '<span class="smalltext"><br />
' . $gallery_picture['desc'] . '<br />
<b>' . $txt['sp-grpictureviews'] . '</b> ' . $gallery_picture['views'] . '<br />';
if ($gallery_picture['sendername'] != '')
echo '<b>' . $txt['sp-grpicturesender'] . '</b> <a href="' . $scripturl . '?action=profile;u=' . $gallery_picture['senderid'] . '">' . $gallery_picture['sendername'] . '</a><br />';
else
echo '<b>' . $txt['sp-grpicturesender'] . '</b> ' . $txt['sp-grpictureguest'] . '<br />';
echo '</span>';
echo '</div>';
}
else
echo 'There is no picture in the gallery.';
-
Sorry, missed a point. Change 128 in the codes as 60.
-
Perfect! Thanks again, sorry I took up so much time there :-[
-
great work sinan is possible have code for smf 2?
-
Very soon...
-
I'm using this code (really cool, thank you!) and I just thought of the following additions to this block:
- the number of comments for the displayed picture
- total number of pictures in the gallery
Something like:
--
[THUMBNAIL]
Title
Description
Views: 120
Author: sAmpLe
Comments: 3
Total pictures in Gallery: 44
--
Is there an easy way of implementing these changes?