Code Snippet: Display news items, anywhere

hr_d

#77
Hello,

I install and tested the "display news items modul" without any problems. It will be nice to show also the short-text of the news.

Thanks to Christian for his good work.

Regards Hermann




doc

@YeShakka
I am thinking to implement a feature which let you control how the news will be displayed.
Possible options:
- display as list (only news header and read more link)
- display as table (allow choice of columns to be used)

In addition I want some feedback first, before moving that snippet to the completed section.

Regards Christian

mikejd

kweitzel - I tried adding the ;s but it didn't work. i think they were in the right place. It stopped all the page after the code block from loading.

Thanks anyway.
Mike

YeShakka

Thank you for that. Nice contribution.

But shouldn't this go into "completed code snippets"?

doc

#73
Hello,

Matthias (ruebenwurzel) asked me, if I could create a snippet module of the display news items anywhere code posted in that thread. Well here my first version.

Installation
- download the attached zip file to your computer
- log into the backend of WB and install the module as usual

Using the snippet function
Once the module is installed, it can be invoked either from the index.php file of your template or from any code module section.
<?php display_news_items(); ?>

For a more customised output, you can pass over serveral parameters to the function explained below.

<?php display_news_items($group_id$max_news_items$max_news_length
$header_text$readmore_text$no_news_text); ?>



Optional parameters (default values stated as well):
group_id... group from which you want to read news from (default:= 0 for all groups)
max_news_items... max. number of news shown (default:= 10, values>10 will be lowerd to 10)
max_news_length... max. length of the short news text shown (default:=150)
header_text...  heading text shown (default:="Latest news")
readmore_text...  text displayed for readmore link (default:=read more)
no_news_text...  text shown if no news available (default:="no news avaialbe yet...")

Have fun.
Regards Christian

Removed file. Any news is now available in the section Additional Functions on addons repository

kweitzel

I think you just forgot 2 closing semicolons behind the brackets: "};" but I might be wrong there since I am no real coder.

cheers

Klaus

mikejd

I have been trying to modify this code to use with the events module, inserted into a Code section.

The code I'm using is :

global $database;
$query = "SELECT date,event_desc FROM ".TABLE_PREFIX."2006mod_event_dates ORDER BY date DESC LIMIT 0, 5;";

$error = mysql_error();
if (!$result = mysql_query($query)) {
print "$error";
exit;
}

while($data = mysql_fetch_object($result)){
$title = $data->event_desc;

echo '<p><a href="">'.$title.'</a></p>';
}

and I get an error message :

Parse error: parse error, unexpected T_VARIABLE, expecting ',' or ';' in /home/fhlinux190/d/deveron-arts.com/user/htdocs/wb/modules/code/view.php(30) : eval()'d code on line 13

Is this code OK? or how can I change it to work?

Regards,
Mike Davies

kweitzel

Have a look at the template sourcecode. There you can see, how it is implemented. You then take the script part and paste it into the template you want to use it ...

In the Andreas02 Template the code comes in like this here:


<div id="sidelink">
</div>
</p>
<-- Login Box start -->
<form name="login" action="http://w007be2b.dd15712.kasserver.com/account/login.php" method="post" class="login_table">
<h2>Login</h2>
Username:
<input type="text" name="username" style="text-transform: lowercase;" />

Password:
<input type="password" name="password" />
<input type="submit" name="submit" value="Login" style="margin-top: 3px; text-transform: uppercase;" />
<a href="http://w007be2b.dd15712.kasserver.com/account/forgot.php">Forgot Details?</a>
</form>
<-- Login Box End -->

<--News Box start -->

<?php
// Specify the Group(id) you want to read the news from or 0 for all groups
$group 0;       

// Specify number of newsitems                       
$limit=10;

// Specify read more text
$readmore "Read more";       

// Specify news text
$news "Latest News";       

// Query for obtaining stuff from a group
$query "SELECT * FROM ".TABLE_PREFIX."mod_news_posts WHERE group_id = $group and active = 1 ORDER BY position DESC LIMIT 0, 10;";

// Query for all groups ;)
if ($group<) {
  
$query "SELECT * FROM ".TABLE_PREFIX."mod_news_posts WHERE active = 1 ORDER BY position DESC LIMIT 0, 10;";
}

// No changes needed here, maybe for output!

global $database;

$result $database->query($query);
$total $result->numRows();      // how many items are there?

if ($total>1) {                   // at least 2 needed for 2 news items
  
$data $result->fetchRow() ;   // get left item
  
$data2 $result->fetchRow() ;  // get right item
  
echo '<span style="font-size: 14px; color: #fff; display: block; font-weight: bold; border-bottom: 1px solid;">'.$news.'</span>';
  echo 
'<p style="border-bottom: 1px solid #000; font-weight: bold; font-size: 12px; color: #fff;">'.$data['title'].'</p><p style="font-size: 12px; text-align: justify; color: #fff;">'.$data['content_short'].'<br /><a href="'.WB_URL.PAGES_DIRECTORY.$data['link'].PAGE_EXTENSION.'">'.$readmore.'</a></p>';
  echo 
'<p style="border-bottom: 1px solid #000; font-weight: bold; font-size: 12px; color: #fff;">'.$data2['title'].'</p><p style="font-size: 12px; text-align: justify; color: #fff;">'.$data2['content_short'].'<br /><a href="'.WB_URL.PAGES_DIRECTORY.$data2['link'].PAGE_EXTENSION.'">'.$readmore.'</a></p>';
}
else {
  echo 
'<span style="font-size: 14px; display: block; font-weight: bold; border-bottom: 1px solid;">No News yet ...</span>';
}
?>

<-- News Box ends -->
</div>


Then you just change the styles to what you need (all the styles used in this script are direct formatting).

cheers

Klaus

beeswax

I've downloaded MetalSlabs and installed it, and the News sections work how I want them to. I've gone into page settings and written down the settings as they appear in MetalSlabs so that I can change the template to Andreas02 and make sure the page settings are the same (the only difference being that instead of "Left Column", Andreas02 has "Sidebar"), and that's when it all falls apart for me. I really am at a loss as to what to do next :|

kweitzel

have a look at the templates "Gray Snake" and "Metal Slabs" (the links are temporary links to the add-on pages! They will change!)


They have included, what you want to achieve.

cheers

Klaus

beeswax

Happy New Year.
I've pasted the code into a Code page, and it works, but what I'd like to do is to have the News "Headlines" in the sidebar, and when they click on "Read More" the detailed news opens in the Main page. I've looked at modify_settings.php (if that's where I should be looking) in the News module but I'm afraid that I can't make too much sense of it.
At the moment I'm working on the site "locally" so I can't give anyone a preview.
thanks, in advance

kweitzel

the wiki can be found here (link goes to the english page): http://projects.websitebaker.org/websitebaker2/wiki/Docs-EN

To get this code going, create a page of type "code" and paste the code into this page. You will need to have a page of type "news", with some news-articles, otherwise this code has nothing to display.

cheers

Klaus

beeswax

I'm really new to WB, and I've tried to follow this thread and implement it, but so far with no luck.
Earlier in this thread sowmone refers to the template Wiki, where is it? I don't know how to insert code into a template (I tried, but didn't get very far), will I find this on the Wiki?
When it comes to code(ing) I'm very, very, very noob.
beeswax

Bramus

#64
Some one made me this piece of code also some time ago. Maybe it is usefull for anyone here. I have translated some things into dutch, but to edit it, that should be real easy. You can enter the max of news items to show and from what group or just all the news groups.

As i said IT IS NOT MADE BY ME! I respect this work and just want to share it with you. Some one posted it already some time for me, can't remember if it was by mail or here.

Example --> www.cavaliershelden.nl


// Specify the Group(id) you want to read the news from or 0 for all groups
$group = 2;       

// Specify number of newsitems                       
$limit=4;

// Specify read more text
$readmore = "Lees Verder";       

// Specify Older news text
$oldernews = "Oude items";       

// Query for obtaining stuff from a group
$query = "SELECT * FROM ".TABLE_PREFIX."mod_news_posts WHERE group_id = $group and active = 1 ORDER BY position DESC LIMIT 0, 10;";

// Query for all groups ;)
if ($group<1 ) {
  $query = "SELECT * FROM ".TABLE_PREFIX."mod_news_posts WHERE active = 1 ORDER BY position DESC LIMIT 0, 10;";
}

// No changes needed here, maybe for output!

global $database;

$result = $database->query($query);
$total = $result->numRows();       // how many items are there?

if ($total>1) {                                // at least 2 needed for 2 news items
  $data = $result->fetchRow() ;    // get left item
  $data2 = $result->fetchRow() ;  // get right item
  echo '<table width=100% border=0>';
  echo '<tr><td width=50%><b>'.$data['title'].'</b></td><td></td></tr>';
  echo '<tr><td>'.$data['content_short'].'</td><td></td></tr>';
  echo '<tr><td><a href="'.WB_URL.PAGES_DIRECTORY.$data['link'].PAGE_EXTENSION.'">'.$readmore.'</a></td><td></td></tr>';
  echo '<tr><td></td><td></td></tr>';
  echo '<tr><td></td><td></td></tr>';
  echo '<tr><td></td><td></td></tr>';
  echo '<tr><td></td><td></td></tr>';
  echo '<tr><td></td><td></td></tr>';
  echo '<tr><td></td><td width=50%><b>'.$data2['title'].'</b></td></tr>';
  echo '<tr><td></td><td>'.$data2['content_short'].'</td></tr>';
  echo '<tr><td></td><td><a href="'.WB_URL.PAGES_DIRECTORY.$data2['link'].PAGE_EXTENSION.'">'.$readmore.'</a></td></tr>';
  echo '</table>';
}

// Next show the rest
if ($total>2) {                                // are there any left?
echo '<br><br><table><tr><td colspan=2><b>'.$oldernews.'</b></td></tr>';
echo '<tr><td>Datum</td><td>Onderwerp</td></tr>';
while ($data = $result->fetchRow()) {
  echo '<tr><td>'.strftime( "%d-%m-%Y",$data['posted_when']).'</td>';
  echo '<td><a href="'.WB_URL.PAGES_DIRECTORY.$data['link'].PAGE_EXTENSION.'">'.$data['title'].'</a></td></tr>';
}
echo '</table>';
}


This can be used in a CODE section on a page.
BRAMUS Internet Services

babsy

#63
thanks..... i will... :-)

UPDATE:... works like a charm now!  :-D so thx again!!

but i could´nt find the exact image path in the template tutorial!??
but i just downoaded another template and took it from there, (don´t know why i didn´t think of that right away  :| )

so if anyone else (well a newbie i guess  :-D) is looking, this was it:

<td><img src="<?php echo TEMPLATE_DIR; ?>/top1_2.jpg" alt="" height="30" width="700" border="0" /></td>

so thanks again...
now everythink is working.. so far so good..

actually i would love to build some template for wb, now i think i can start on that  :wink:

kweitzel

but that is the template ... cause the URL:

Quotehttp://klaknav.sudurras.com/pages/templates/round/top1_2.jpg

can not work.

Rework your template (have a look at the tutorial in the WIKI for the template path).

cheers

Klaus

babsy

thx... so far so good!!

but, i think i have another problems, but its not in the code i think!

because.... check it out http://klaknav.sudurras.com

now the code works... and the news.... exept when i press on read more...

the top header of my template goes away  :?

really weird!... 

kweitzel

I just tested the exact script, works fine here.

Have a look into my Templates "Snakeskin", Metalslabs" and "Molten Lava", they have this functionality implemented.

Maybe as a test, install them and try if they do work, just to make shure, that it's not a server issue.

Also try with a simple testpost (simple name like "test") to see if this works.

cheers

Klaus

babsy

#59
hi... i know this is a long time, since this subject was in use!

but i just can´t get it to work right!

i use this code.. in my template:

__________________________________
<?php
global $database;
$query "SELECT post_id,title,group_id,link FROM ".TABLE_PREFIX."mod_news_posts ORDER BY position DESC LIMIT 0, 5;";

$error mysql_error();
if (!
$result mysql_query($query)) {
print 
"$error";
exit;
}

while(
$data mysql_fetch_object($result)){
$title $data->title;
$id $data->post_id;
$link $data->link;
echo 
'<font class="nyttlink"><a href="'.WB_URL.$link.PAGE_EXTENSION.'">'.$title.'</font><br /><br /></a>';
}
?>

____________________________________

it works.. fine, until... when i press on the news... displayed.. it just says this:

[an error occurred while processing this directive] The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page  about the error. [an error occurred while processing this directive]

anyone know why  :?

gokcer

I've modified this code for my needs. You can specify the columns, last post limit, "active or not" from that pages "news settings" in the administration panel of wb without changing anything in the code. You must only specify the page_id of news page. This code is available with a "modified a lot" version of wb :). Some of the variables may not be available with standart (not much modified) versions. But maybe you can make available by modifying or deleting some codes. For example my link creation codes are different so I use only ".$link." in the "href"s. But the standart link code is ".WB_URL.'/pages/'.$link.PAGE_EXTENSION."

Place this code in your template file where you want to display the latest news:





<table width="100%" height="0%" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
<?php 
define
('yenisatir''</tr><tr>'); // yenisatir means new row
global $database;
$get_page 4// insert page_id here

$query_settings $database->query("SELECT posts_per_page FROM ".TABLE_PREFIX."mod_news_settings WHERE page_id=$get_page");
$fetch_settings $query_settings->fetchRow();
$latest $fetch_settings['posts_per_page']; // this calls the standard wb's post per page setting. So you can change latest news quantity by only changing your post per page setting. So, the quantities of post per page and latest news will be same.

$query "SELECT ".TABLE_PREFIX."mod_news_posts.post_id,".TABLE_PREFIX."mod_news_posts.short,".TABLE_PREFIX."mod_news_posts.title,".TABLE_PREFIX."mod_news_posts.link,".TABLE_PREFIX."mod_news_settings.num_cols FROM ".TABLE_PREFIX."mod_news_posts,".TABLE_PREFIX."mod_news_settings WHERE ".TABLE_PREFIX."mod_news_posts.page_id=$get_page AND ".TABLE_PREFIX."mod_news_posts.active=1 AND ".TABLE_PREFIX."mod_news_settings.page_id=$get_page ORDER BY position DESC LIMIT 0, $latest;";
// If you want to make different the quantities of post per page and latest news, replace "$latest" with a number above to specify the latest news quantity

$error mysql_error();
if (!
$result mysql_query($query)) {
print 
"$error";
exit;
}

$counter 0;
while(
$data mysql_fetch_object($result))
{
$title $data->title;
$short $data->short;
$id $data->post_id;
$link $data->link;
$num_cols $data->num_cols
echo stripslashes('<td class="haber" align="left" valign="top"><div style="padding-bottom: 1px; font-weight: bold;" ><b><a href="'.$link.'">'.$title.'</a></b></div>'.$short.'</td>'); // If you didn't modified your WB to work with "safe mode on" (in other words if you are using default WB), replace .link. with .WB_URL.'/pages/'.$link.PAGE_EXTENSION.
$counter $counter+1;
if($counter == $num_cols// I've added a column setting in my news module similiar to gallery module's column setting. I can specify my column quantity from news' settings and latest news gets the number from there. If you didn't add (in other words if you are using default WB) replace $num_cols with a number to specify a column quantity
{
echo yenisatir;
$counter 0;
}
}
?>

  </tr></table>





good luck...

artwebbo

Hi,

everything works perfect but I'm not a php coder and I have a little problem about how to impelement this...

I want to display news from mostly every news groups without one group on the other subpage.
How to put it in code?


thanks for helping,
Ewa


mangione

HI, i think you mean, that you wanna show the wysiwyg images also in the function where you get the last items and display it anywhere. I always had the problem, that the path from my images was wrong.

The plan was, to open the last newsitem in the "long" version and display the older news on the bottom. I used the following code:

--------- insert in a code page ----------

// Specify the Group(id) you want to read the news from or 0 for all groups
$group = 0;       

// Specify number of newsitems                       
$limit=10;

// Specify read more text
$readmore = "Weiterlesen";       

// Specify Older news text
$oldernews = "Older news";       

// Query for all groups ;)
if ($group<1 ) {
  $query = "SELECT * FROM ".TABLE_PREFIX."mod_news_posts WHERE active = 1 ORDER BY position DESC LIMIT 0, 10;";
}




// No changes needed here, maybe for output!

global $database;

$result = $database->query($query);
$total = $result->numRows();       // how many items are there?

if ($total>1) {                                // x
  $data = $result->fetchRow() ;    // get news item
$long=stripslashes($data['long']); //clear up the images path
  echo '<table width=100%>';
  echo '<tr><td width=100%><h1>'.$data['title'].'</h1></td></tr>';
echo '<tr><td>'.$long.'</td></tr>'; //view the long-version of the news
  echo '<tr><td></td><td></td></tr>';
  echo '</table>';
}

   // Specify the Group(id) you want to read the news from or 0 for all groups
$group = 0;       

// Specify number of newsitems                       
$limit=0;

// Specify read more text
$readmore = "Artikel lesen";       

// Specify Older news text
$oldernews = "weitere Einträge";       

// Query for obtaining stuff from a group
$query = "SELECT * FROM ".TABLE_PREFIX."mod_news_posts WHERE group_id = $group and active = 1 ORDER BY position DESC LIMIT 0, 10;";

// Query for all groups ;)
if ($group<1 ) {
  $query = "SELECT * FROM ".TABLE_PREFIX."mod_news_posts WHERE active = 1 ORDER BY position DESC LIMIT 1, 10;";
}

// No changes needed here, maybe for output!

global $database;

$result = $database->query($query);
$total = $result->numRows();       // how many items are there?

if ($total>1) {                                // at least 2 needed for 2 news items
  $data2 = $result->fetchRow() ;    // get left item
  $data3 = $result->fetchRow() ;    // get left item
  $data4 = $result->fetchRow() ;    // get left item
  $data5 = $result->fetchRow() ;    // get left item
  $data6 = $result->fetchRow() ;    // get left item
  $data7 = $result->fetchRow() ;    // get left item
  $data8 = $result->fetchRow() ;    // get left item
  $data9 = $result->fetchRow() ;    // get left item
  $data10 = $result->fetchRow() ;    // get left item
  echo '<table class="newsoverview" width=100%>';
echo '<tr><td width="100%"><b>weitere News</b></td></tr>';
  echo '<tr><td width=100%><li><a href='.WB_URL.PAGES_DIRECTORY.$data2['link'].PAGE_EXTENSION.'>'.$data2['title'].'</a></td></tr>';
  echo '<tr><td width=100%><li><a href='.WB_URL.PAGES_DIRECTORY.$data3['link'].PAGE_EXTENSION.'>'.$data3['title'].'</a></td></tr>';
  echo '<tr><td width=100%><li><a href="'.WB_URL.PAGES_DIRECTORY.$data4['link'].PAGE_EXTENSION.'">'.$data4['title'].'</a></td></tr>';
  echo '<tr><td width=100%><li><a href="'.WB_URL.PAGES_DIRECTORY.$data5['link'].PAGE_EXTENSION.'">'.$data5['title'].'</a></td></tr>';
  echo '<tr><td width=100%><li><a href="'.WB_URL.PAGES_DIRECTORY.$data6['link'].PAGE_EXTENSION.'">'.$data6['title'].'</a></td></tr>';
  echo '<tr><td width=100%><li><a href="'.WB_URL.PAGES_DIRECTORY.$data7['link'].PAGE_EXTENSION.'">'.$data7['title'].'</a></td></tr>';
  echo '<tr><td width=100%><li><a href="'.WB_URL.PAGES_DIRECTORY.$data8['link'].PAGE_EXTENSION.'">'.$data8['title'].'</a></td></tr>';
  echo '<tr><td width=100%><li><a href="'.WB_URL.PAGES_DIRECTORY.$data9['link'].PAGE_EXTENSION.'">'.$data9['title'].'</a></td></tr>';
  echo '<tr><td width=100%><li><a href="'.WB_URL.PAGES_DIRECTORY.$data10['link'].PAGE_EXTENSION.'">'.$data10['title'].'</a></td></tr>';
  echo '</table>';
}

babsy

hi again..

the group picture is´nt working, i can´t choose any picture, without getting an error!

but it is the wysiwig picture i wanted to show, but i thats impossible :|

but thanks for your help


pcwacht

This snippet will only show the group pictures, not the wysiyg added pictures.

babsy

hi John

please could you explain what you mean by this

"PS, The pictures for groups needs to be upoloaded first!
admin -> pages -> the page with news -> add groups (or modify group)-> image!!!"

When i have set pictures in, yet, i´ve just been putting them next to the text in both short and long text!