Jump to content

Announcements on a side box on website


RPS

Recommended Posts

This script is for pages that WHMCS is not integrated into. It pulls the announcement information from the database.

 

I was going to try to use the XML file that is generated, instead of opening up a DB connection every time someone visits the page (DB connection just uses up a LOT more resources), however PHP4 is horrible with XML/RSS.

 

Since our website is built completely around PHP and WHMCS is installed outside of the main directories, we decided it would be easier to just write a custom script to display the announcements in one of our side boxes.

 

This script will limit the announcement post length (it will insert ... to show that it was truncated). It will also allow you to limit the number of announcements to display, as well as display them in the correct order of appearance (newer announcements appear at the top).

 

1) Change the path to whmcs/configuration.php

2) Change the number of announcements you wish to display (currently set to 2)

3) Change the HTML inside the PHP to integrate into your website.

 

<?
$x=1; // requested edit
// truncate the text to only 65 chars, add ... to the end
function truncate($text) {
$text = substr($text,0,60);
$text = substr($text,0,strrpos($text,' '));
$text = $text."...";
return $text;
}

// format the date to mm/dd/yyyy
function formatdate($date) {
   $year = substr ($date, 0, 4);
   $month = substr ($date, 5, 2);
   $day = substr ($date, 8, 2);
return $month.'/'.$day.'/'.$year;
}

// limit the number of announcements to return
$limit = 2;


require '/home/user/public_html/whmcs/configuration.php';
$link = mysql_connect($db_host,$db_username,$db_password);
mysql_select_db($db_name);

$result = mysql_query("SELECT * FROM tblannouncements WHERE published='on' ORDER BY id DESC LIMIT ".$limit);
while($data = mysql_fetch_array ($result))
{
$id = $data['id'];
$date = $data['date'];
$announcement = truncate($data['announcement']);
$date = formatdate($date);
echo'<p><span>'.$date.'</span> - '.$announcement.' <a href="/whmcs/announcements.php?id='.$id.'"><img src="/images/more.gif" alt="more info" /></a></p>';
echo'<hr />';
$x=false;
}
if(!$x) echo '<p>News & announcements: <a href="/whmcs/announcements.php">view more</a></p>';
if($x) echo '<p>There are no new announcements.</p>';
?>

 

This is tested and working on 3.5.1, however, unless some major database changes are made, it should work fine for future versions of WHMCS (should even work for previous versions).

Link to comment
Share on other sites

I have to double post since it's too late to edit my post (stupid 15 minute rule..)

 

 

Also, in case anyone wants to display the title of the announcement as well, you're going to have to insert a line into the little script there.

 

Add after line 30:

$title = $data['title'];

 

Then of course to use it, you'll just use '.$title.' where you want the title of the announcement to output.

Link to comment
Share on other sites

I'm getting ready to do something like this, and decided to search the whmcs forums to see what others have done. I just wanted to point out a couple items for consideration:

 

The OP indicates the code is for pages that are NOT generated by WHMCS. If you are using WHMCS to generate all pages of your site, you can reuse the whmcs database connection and replace this:

 

require '/home/user/public_html/whmcs/configuration.php';
$link = mysql_connect($db_host,$db_username,$db_password);
mysql_select_db($db_name);

 

with this:

 

require("dbconnect.php");
require("includes/functions.php");

 

This code:

 

$x=false;

 

is within the while loop, meaning it is run for each iteration of the loop (wasted cpu cycles), and also means the lines which evaluate $x:

 

if(!$x) echo '<p>News & announcements: <a href="/whmcs/announcements.php">view more</a></p>';
if($x) echo '<p>There are no new announcements.</p>';

 

will never vary, because $x will ALWAYS be false by the time you get to those lines of code.

 

I would suggest this as an alternative:

 

$result = mysql_query("SELECT * FROM tblannouncements WHERE published='on' ORDER BY id DESC LIMIT ".$limit);
if (mysql_num_rows($result) == 0) {
echo '<p>There are no new announcements.</p>';
} else {
while($data = mysql_fetch_array ($result)) {
   $id = $data['id'];
   $date = $data['date'];
   $announcement = truncate($data['announcement']);
   $date = formatdate($date);
   echo'<p><span>'.$date.'</span> - '.$announcement.' <a href="/whmcs/announcements.php?id='.$id.'"><img src="/images/more.gif" alt="more info" /></a></p>';
   echo'<hr />';
}
echo '<p>News & announcements: <a href="/whmcs/announcements.php">view more</a></p>';
}?>

 

The only remaining question then is whether you should display the "view more" link if the number of total active announcements matches the $limit variable. You may not want to do this if it does, but the above code doesn't account for that. You would either have to issue a separate query to count the number of published articles, and compare that to $limit, or you can remove the LIMIT from the query, store mysql_num_rows() to a variable, and create a counter variable set to 0, which you incremement within the loop. Once the counter variable matches $limit, break the loop and compare the counter variable to the row count variable to determine whether or not you should display the view more link. Using a separate query means an additional trip to the database, but removing the LIMIT from the query means you'll be fetching the entire list of active announcements, so the former is probably the smarter approach.

 

I'll probably take one of these two approaches, and will post the code if anyone expresses an interest.

Link to comment
Share on other sites

Troy,

 

Thanks for the suggestions!

 

will never vary, because $x will ALWAYS be false by the time you get to those lines of code.

- It will only be set to false if there is data within the loop. If there is no data (thus no announcements are displayed), then it will display the no new announcements.

 

I remember when I tested this, it displayed correctly for when no announcements were displayed.

 

Either way, the logic for the implementation used in what you posted, takes up less time.

 

Thanks again Troy :-P

Link to comment
Share on other sites

Troy,

 

Thanks for the suggestions!

 

 

- It will only be set to false if there is data within the loop. If there is no data (thus no announcements are displayed), then it will display the no new announcements.

 

I remember when I tested this, it displayed correctly for when no announcements were displayed.

 

Either way, the logic for the implementation used in what you posted, takes up less time.

 

Thanks again Troy :-P

 

Okay, now I see how you were trying to use $x. However, you never set $x to true, so it's going to be false by default, or false by setting it false within the loop. I don't see where it would ever be true.

 

Logic-wise, I would have probably created a variable called something like $activeAnnouncements, set it false before the loop, and then set it true within the loop, then reverse the boolean expressions (true for view more link and not true for no new announcements text.)

Link to comment
Share on other sites

Troy,

 

You're right, I don't see $x being set to true anywhere.

 

When I tested this, I coded the $x=1, but for some reason it isn't in the code anymore. I clearly remember testing this against the zero announcement cases. Very odd.

 

Here's the update code for anyone using the code I posted in the earlier thread:

 

<?
$x=1;
// truncate the text to only 65 chars, add ... to the end
function truncate($text) {
   $text = substr($text,0,60);
   $text = substr($text,0,strrpos($text,' '));
   $text = $text."...";
   return $text;
}

// format the date to mm/dd/yyyy
function formatdate($date) {
   $year = substr ($date, 0, 4);
   $month = substr ($date, 5, 2);
   $day = substr ($date, 8, 2);
   return $month.'/'.$day.'/'.$year;
}

// limit the number of announcements to return
$limit = 2;


require '/home/user/public_html/whmcs/configuration.php';
$link = mysql_connect($db_host,$db_username,$db_password);
mysql_select_db($db_name);

$result = mysql_query("SELECT * FROM tblannouncements WHERE published='on' ORDER BY id DESC LIMIT ".$limit);
while($data = mysql_fetch_array ($result))
{
   $id = $data['id'];
   $date = $data['date'];
   $announcement = truncate($data['announcement']);
   $date = formatdate($date);
   echo'<p><span>'.$date.'</span> - '.$announcement.' <a href="/whmcs/announcements.php?id='.$id.'"><img src="/images/more.gif" alt="more info" /></a></p>';
   echo'<hr />';
   $x=false;
}
if(!$x) echo '<p>News & announcements: <a href="/whmcs/announcements.php">view more</a></p>';
if($x) echo '<p>There are no new announcements.</p>';
?> 

Link to comment
Share on other sites

  • 4 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated