PDA

View Full Version : CMS Widget/Forum Block Same Code Different Problems


akvaryumforum
01-23-2010, 07:04 PM
<?php
include "includes/config.php";
echo "<div>";
$con = mysql_connect($config['MasterServer']['servername'], $config['MasterServer']['username'], $config['MasterServer']['password']) or die(mysql_error());
mysql_select_db($config['Database']['dbname'], $con) or die(mysql_error());
$selectthread = mysql_query("SELECT * FROM `thread` WHERE `forumid`='171' ORDER BY RAND() LIMIT 0,1") or die(mysql_error());
$address = "http://www.akvaryumforum.com/forum";
while ($row=mysql_fetch_array($selectthread)) {
$attc = $row['firstpostid'] ;
$selectattachment = mysql_query("SELECT * FROM `attachment` WHERE `contentid`='{$attc}'");
while ($att=mysql_fetch_array($selectattachment)) {
echo "<tr><td><img src='{$address}/attachment.php?attachmentid={$att['attachmentid']}&stc=1&thumb=1&d=1262998375' /><br /><a href=\"{$address}/showthread.php?t={$row['threadid']}\" target=\"_self\">{$row['title']}</a>";
}
}
echo "</div>";
?>

I use this code to show random thread and the attachment image in the first post of thread. It work fine in a seperate php file. But when i use this code in the Widget it shows the content over the header, when i try to use it in a Forum Block Forumhome says no database selected and don't show anything else.

How can i do this script work?

Lynne
01-23-2010, 08:19 PM
Have you tried looking at some of the example widgets and blocks that people have put in the mods forum here? Go see their syntax (you shouldn't be using mysql_connect). And, you should not be using echo, but instead outputing to a variable or using return, depending on if you are writing a block or widget.

akvaryumforum
01-23-2010, 11:35 PM
So for widget it must be like this:
ob_start();
require_once('./includes/functions_user.php');
require_once('./includes/functions_bigthree.php');
// Get Latest Sticky Threads
$selectthread = vB::$db->query_read("SELECT * FROM `thread` WHERE `forumid`='171' ORDER BY RAND() LIMIT 0,1");
$output_bits = '';
while($row = vB::$db->fetch_array($selectthread))
{
$attc = $row[firstpostid] ;
$selectattachment =vB::$db->query_read("SELECT * FROM `attachment` WHERE `contentid`='{$attc}'");
while ($att=vB::$db->fetch_array($selectattachment)) {
$output_bits .= "<img src='attachment.php?attachmentid=".$att[attachmentid]."&stc=1&thumb=1&d=1262998375' /><br /><a href='showthread.php?t=".$row[threadid]." target="_self"'>'.$row[title].'</a>";
}
}
$output = $output_bits;
ob_end_clean();