Code Snippet: Display news items, anywhere

pcwacht

all the " are addslashed, ie, there are too many \ (backslashes)
<img height=\"73\" hspace=\"5\" should be
<img height="73" hspace="5" in html

John

babsy

hi and thanks... ill try this tomorrow when i´ll get back to work

but why does the sorce look like that?? and you said "Doesn't look all to good to me"
can i change it, so it looks more "normal"?

pcwacht

Did you look at your source of that page?


<a href="http://www.kjolurin.com/pages//posts/rrrrrrrrrrrrrrrrrrrr146.php">rrrrrrrrrrrrrrrrrrrr</a></td></tr><tr><td></td><td><p><img height=\"73\" hspace=\"5\" src=\"/media/Image/bowla.jpg\" width=\"142\" align=\"left\" border=\"1\" /><img height=\"1\" src=\"/\"//\"/media/Image/bowla.jpg//\"/\"\" width=\"1\" border=\"0\" /><br /><img style=\"\"WIDTH: \" height=\"1\" src=\"/\"/media/Image/bowla.jpg/\"\" width=\"1\" border=\"0\" />

Doesn't look all to good to me

The images should come from the /media/.news directory! Not from /media/Image ....

I searched the source of your page for /media/.news nothing found!


In your posted code, this bit:

if(file_exists(WB_PATH.MEDIA_DIRECTORY.'/.news/image'.$group_id.'.jpg')) {
  $picture = WB_URL.MEDIA_DIRECTORY.'/.news/noimage.jpg';
} else {
  $picture = '';
}

If the picture exists, set picture to noimage.jpg or to nothing?
Maybe change to following:

if(file_exists(WB_PATH.MEDIA_DIRECTORY.'/.news/image'.$group_id.'.jpg')) {
  $picture = WB_URL.MEDIA_DIRECTORY.'/.news/image'.$group_id.'.jpg';
} else {
  $picture = '';
}

So if the picture exists it will get the url to the picture.

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


John

babsy

hi, and sorry

i forgot, i had set, the page to "hidden" now you should see it right!

www.kjolurin.com

pcwacht

I see pictures on that page, looks like a bowling ally?

John

babsy

Hi everyone, i´m still trying to get this picture to work... but i just can´t find the picture folder anywhere - does any off you know the path to the pictures in "short text" in the news, my code for now looks like this:

$group = 0; // Specify the Group(id) you want to read the news from
global $database;
$query = "SELECT post_id,title,short,group_id,link FROM ".TABLE_PREFIX."mod_news_posts WHERE group_id = $group ORDER BY position DESC LIMIT 0, 10;";
/*
This limits the results to max 5, so if you want it to be 10, make it LIMIT 0, 10. The first number defines the starting point, and the second the max/end of the results */

$error = mysql_error();
if (!$result = mysql_query($query)) {
print "$error";
exit;
}
while($data = mysql_fetch_object($result)){
$title = $data->title;
$short= $data->short;
$id = $data->post_id;
$link = $data->link;
$group_id = $data->group_id;
if(file_exists(WB_PATH.MEDIA_DIRECTORY.'/.news/image'.$group_id.'.jpg')) {
  $picture = WB_URL.MEDIA_DIRECTORY.'/.news/noimage.jpg';
} else {
  $picture = '';
}


echo '<br />';
echo '<table width=100%>';
echo '<tr><td bgcolor=#e7e7e7><a href="'.WB_URL.'/pages/'.$link.PAGE_EXTENSION.'">'.$title.'</a></td></tr>';
echo '<tr><td>'.$picture.'</td><td>'.$short.'</td></tr>';
echo '</table>';
}

But it just leaves an empty hole.... try take a look at this!
http://www.kjolurin.com

pcwacht

@babsy,

This snippet only will show news posts
So if your posts are divided among those four pages testing for the pageid is the obvious thing to do.

global $database;
$get_page = 1; // change to your page_id you need

$query = "SELECT post_id, title,group_id,link FROM ".TABLE_PREFIX."mod_news_posts WHERE page_id=$get_page 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 '<p><a href="'.WB_URL.$link.PAGE_EXTENSION.'">'.$title.'</a></p>';
}

The fields you can choose from are:
post_id, section_id, page_id, group_id, active, position,
title, link, content_short, content_long, commenting, posted_when, posted_by

phpmyadmin shows you that in a jiffy ;)

Happy baking,
John


babsy

Hi i haven´t got the image working yet, but i´m still trying to work on that!

for now i have used the snippet, with group=0

but now a have on page (sport) with four subpages (football, hockey, tennis, else)
How can i make the snippet work so it just shows these four subpages... how can i find what group they are, i can just find page_id, and thats different for every page???

pcwacht

Yes it is possible, rather easy,

take out the test on $group in the query

or take a look at this one:
http://forum.websitebaker.org/index.php/topic,1851.msg11398.html#msg11398

or try this one:

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 '<p><a href="'.WB_URL.$link.PAGE_EXTENSION.'">'.$title.'</a></p>';
}





restonian

I noticed that a few posts back there was a post including code that didn't have a group id defined inside it, i tried this myself, and it displayed nothing. Not a sausage.

Is it possible to have the latest news stories globally over every category?

If so what do i alter?

pcwacht

Doesn't seem to be in the database.


module/news/view.php reveals:

if(file_exists(WB_PATH.MEDIA_DIRECTORY.'/.news/image'.$group_id.'.jpg')) {
  $groups[$group_id]['image'] = WB_URL.MEDIA_DIRECTORY.'/.news/image'.$group_id.'.jpg';
} else {
  $groups[$group_id]['image'] = '';
}


So something like this might work (mind you , I had no time to test it!!)

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

global $database;

$query = "SELECT post_id,title,short,group_id,link FROM ".TABLE_PREFIX."mod_news_posts WHERE group_id = $group ORDER BY position DESC LIMIT 0, 10;";

/*
This limits the results to max 5, so if you want it to be 10, make it LIMIT 0, 10. The first number defines the starting point, and the second the max/end of the results */

$error = mysql_error();
if (!$result = mysql_query($query)) {
print "$error";
exit;
}
while($data = mysql_fetch_object($result)){
$title = $data->title;
$short= $data->short;
$id = $data->post_id;
$link = $data->link;
$group_id = $data->group_id;
if(file_exists(WB_PATH.MEDIA_DIRECTORY.'/.news/image'.$group_id.'.jpg')) {
  $picture = WB_URL.MEDIA_DIRECTORY.'/.news/image'.$group_id.'.jpg';
} else {
  $picture = '';
}

echo '<br />';
echo '<table width=100%>';
echo '<tr><td colspan=2 bgcolor=#e7e7e7><a href="'.WB_URL.'/pages/'.$link.PAGE_EXTENSION.'">'.$title.'</a></td></tr>';
echo '<tr><td>'.$picture.'</td><td>'.$short.'</td></tr>';
echo '</table>';
}


You could change the  $picture = ''; to a NoImage picture with:
  $picture = WB_URL.MEDIA_DIRECTORY.'/.news/noimage.jpg';

Happy baking,
John

babsy

hi all!

i tryed to change the code a bit, and so far, so good!

BUT... i want it to show the pictures that are shown along the "short text too", but i cant fint the id, in the database, does anyone know where the pictures goes in the database.

This i my code for now:

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

global $database;

$query = "SELECT post_id,title,short,group_id,link FROM ".TABLE_PREFIX."mod_news_posts WHERE group_id = $group ORDER BY position DESC LIMIT 0, 10;";

/*
This limits the results to max 5, so if you want it to be 10, make it LIMIT 0, 10. The first number defines the starting point, and the second the max/end of the results */

$error = mysql_error();
if (!$result = mysql_query($query)) {
print "$error";
exit;
}
while($data = mysql_fetch_object($result)){
$title = $data->title;
$short= $data->short;
$id = $data->post_id;
$link = $data->link;
echo '<br />';
echo '<table width=100%>';
echo '<tr><td bgcolor=#e7e7e7><a href="'.WB_URL.'/pages/'.$link.PAGE_EXTENSION.'">'.$title.'</a></td></tr>';
echo '<tr><td>'.$short.'</td></tr>';
echo '</table>';
}



Plesase any help wanted?
i know where to put it in the code,just can´t find it in the mysql database???

pcwacht

#39
Fetch the stuff you need from the database
You need content_short (look at table mod_news_post) for all the fields

The <ul><li> ... </li></ul>, just add it to the output:

Something like this:
global $database;

$query = "SELECT post_id,title,group_id,content_short, page_id,link FROM ".TABLE_PREFIX."mod_news_posts WHERE page_id = $page_id ORDER BY post_id DESC LIMIT 0, 5;";

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

echo "Last 5 news posts:"."<br>";
echo "<ul>"; // start of the loop

while($data = mysql_fetch_object($result)){
$title = $data->title;
$page = $data->page_id;
$id = $data->post_id;
$link = $data->link;
$short = $data->short_content;

echo '<li><a href="'.WB_URL.'/pages/'.$link.PAGE_EXTENSION.'">'.$title.'</a>'.$short.'</li>'; // the loop

}
echo "</ul>"; // end of the loop

Didn't test it!

John

ictcafe

I've tweaked a little the code, but two questions:
1. how get i the short description under the title
2. how do i get an unsorted list from all the items?

global $database;

$query = "SELECT post_id,title,group_id,page_id,link,short FROM ".TABLE_PREFIX."mod_news_posts WHERE page_id = $page_id ORDER BY post_id DESC LIMIT 0, 5;";

/*
This limits the results to max 5, so if you want it to be 10, make it LIMIT 0, 10. The first number defines the starting point, and the second the max/end of the results */

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

echo "Last 5 news posts:"."<br>";

while($data = mysql_fetch_object($result)){
$title = $data->title;
$page = $data->page_id;
$id = $data->post_id;
$link = $data->link;
$short = $data->short;

echo '* <a href="'.WB_URL.'/pages/'.$link.PAGE_EXTENSION.'">'.$title.'</a></br>';

}

babsy

Hi again.. and thanks...

this is the one that worked, thanks :-D


echo '<p><a href="'.WB_URL.'/pages/'.$link.PAGE_EXTENSION.'">'.$title.'</a></p>';

pcwacht

change: echo '<p><a href="'.WB_URL.$link.PAGE_EXTENSION.'">'.$title.'</a></p>';
to : echo '<p><a href="'.WB_URL.'/pages/'.$link.PAGE_EXTENSION.'">'.$title.'</a></p>';
or :echo '<p><a href="'.WB_URL.'pages/'.$link.PAGE_EXTENSION.'">'.$title.'</a></p>';

don't have time to test ;)



John


babsy

#35
hi agin.... thanks.... i set group to "0".... and it worked fine..... thank you......

now i just have to make it look like i want... thank you again.... if i want to change it, i should change in this same code, right?
i want it to show the "short" text under the title also.....

well... just 1. problem... it doesn´t find the pages.. it looks for a folder called post, put it inside the pages folder, but i don´t know where to set the /pages/ in the code?

pcwacht

did you check the group?????


Make sure the group is the same you have, or it won't show anything ;)

John

babsy

hi again,

i must be missing something, because when i put it in the code page... nothing happens, the page is just empty??
is there a special way to put it in the code?

pcwacht

This is the code you need: you can place it in your template or you can place it in a code section


$group = 2; // Specify the Group(id) you want to read the news from

global $database;

$query = "SELECT post_id,title,group_id,link FROM ".TABLE_PREFIX."mod_news_posts WHERE group_id = $group ORDER BY position DESC LIMIT 0, 5;";

/*
This limits the results to max 5, so if you want it to be 10, make it LIMIT 0, 10. The first number defines the starting point, and the second the max/end of the results */

$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 '<p><a href="'.WB_URL.$link.PAGE_EXTENSION.'">'.$title.'</a></p>';
}


John

babsy

#31
Hi all!

Now i tryed, to use, it, and i can get it to work, in the template (exept, when i press the link, it says, "the page can not be found"....
Then i tryed to use it on a page, as i want - but there i only get this message on the page

(title; $id = $data->post_id; $link = $data->link; echo '
'.$title.'

';}#125; ?> )


that what i can see on the page i put the code in....


this is the code i used:
<!-- NEWS READER -->
<?php
$group = 0; // Specify the Group&#40;id&#41; you want to read the news from

global $database;

$query = "SELECT post_id,title,group_id,link
        FROM ".TABLE_PREFIX."mod_news_posts
          WHERE group_id = $group
          ORDER BY posted_when DESC
        LIMIT 0, 5;"; // This limits the results to max 5, so if you want it to be 10, make it LIMIT 0, 10.
        // The first number defines the starting point, and the second the max/end of the results
$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 '<p><a href="'.WB_URL.$link.PAGE_EXTENSION.'">'.$title.'</a></p>';}#125;
?>
<!-- END NEWS READER -->



how do i set the code correct onto a page?..... please help, someone :? :|

pcwacht

if &#40;!$result = mysql_query&#40;$query&#41;&#41; &#123;


There are display codes in them, it is an old post, some leftovers from the forum migration.

Try the other one, posted by Craig.Rodway that one will work.


Happy Baking!
John

jonathanbb

Hi

I am trying to use this snippet in my template I get a Parse Error for this line

if &#40;!$result = mysql_query&#40;$query&#41;&#41; &#123;

and have no clue why.

To be truth full I dont have a clue what that line does either so hope someone out there can help me.

Thanks
Jonathan

fienieg

Just replace this
<a href="<?php echo WB_URL?><?php echo $link ?><?php echo PAGE_EXTENSION?>"><?php echo $title?></a>

With:

<a href="<?php echo WB_URL?>/pages/<?php echo $link ?><?php echo PAGE_EXTENSION?>"><?php echo $title?></a>