Jump to content

Newsbox


Guest PsychoBT

Recommended Posts

Guest PsychoBT

I'm trying to build a very simple newsbox.

I've managed to fix the admin section for insertion into the database.

2011-10-19_162719.png

But I have problems with getting data on the website.

I have simply copied an existing box (siteDocs.inc.php & siteDocs.tpl) and has tried to change it for my needs.

But I don't get it to work!

Here's my code:

index.inc.php



require_once'includes'.CC_DS.'boxes'.CC_DS.'news.inc.php';

$body->assign('NEWS',$box_content);






news.inc.php




<?php

if (! defined('CC_INI_SET'))

	die("Access Denied");

	

## query database

$cache = new cache('boxes.news.master');

$results = $cache->readCache();



if (! $cache->cacheStatus)

{

	$results = $db->select("SELECT nyhet, datum FROM " . $glob['dbprefix'] . "CubeCart_news ORDER BY datum ASC");

	$cache->writeCache($results);

}



$box_content = new XTemplate('boxes' . CC_DS . 'news.tpl');

## Build attributes

if ($results)

{

	

	foreach ($results as $i => $result)

	{

		

		$result['nyhet'] = validHTML($result['nyhet']);

		

		$box_content->assign('NYHET', $result);

		$box_content->parse('news.a');

	}

}



$box_content->parse('news');

$box_content = $box_content->text('news');

?>





news.tpl


<!-- boxes/news.tpl -->

<!-- BEGIN: news -->

	<ul>

		<li>{NYHET}</li>

	</ul>

<!-- END: news -->

Link to comment
Share on other sites

Welcome PsychoBT! Glad to see you made it to the forums.

A couple of things I see...

1. In news.tpl, you have a news block, but no nested a block. Presumably the a block would actually contain the list item tag. So, surround the list item tags with <!-- BEGIN: a --> and <!-- END: a -->.

2. In news.inc.php, the foreach statement is using the $results array to extract a key/value pair per iteration. The variable $i is not defined anywhere. Perhaps you mean to cycle through the array without regard to the actual key. If that's the case, then use foreach($results as $result){.

3. But then, it seems that each element of the array $results is another array ($result), for which you are working on the value specified by the key 'nyhet'. Afterwards, you are assigning the $result array to the scalar placeholder {NYHET}. If you want to assign an array to a placeholder, the placeholder must be expressed as {NYHET.nyhet} and {NYHET.skribent}. And this might not work. Consider using NYHETER as the placeholder's group name.

Link to comment
Share on other sites

I'm not exactly sure how CC4's cache works, so I can't say if having added new records to a table will trigger the cache to dump what it has. Maybe by looking at another admin screen, you can learn if there is a standard statement that dumps the cache when the screen adds/edits a record.

In the meantime, try doing the Clear Cache command from the admin database maintenance screen. This supposes that the cache has the results from the first successful query (only one record) and the code will use that.

Link to comment
Share on other sites

Try this for your news.inc.php

<?php

if (! defined('CC_INI_SET'))

        die("Access Denied");

        

## query database

$cache = new cache('boxes.news');

$results = $cache->readCache();



if (! $cache->cacheStatus)

{

        $results = $db->select("SELECT nyhet, datum FROM " . $glob['dbprefix'] . "CubeCart_news ORDER BY datum ASC");

        $cache->writeCache($results);

}



$box_content = new XTemplate('boxes' . CC_DS . 'news.tpl');

## Build attributes

if ($results)

{

        

        foreach ($results as $result)

        {

                

                $result['nyhet'] = validHTML($result['nyhet']);

                

                $box_content->assign('NYHET', $result);

                $box_content->parse('news.li');

        }

}



$box_content->parse('news');

$box_content = $box_content->text('news');

?>


And this on your news.tpl file



<!-- BEGIN: news -->

        <ul>

<!-- BEGIN: li -->

                <li>{NYHET.nyhet}</li>

<!-- END: li -->

        </ul>

<!-- END: news -->

Lee

Link to comment
Share on other sites

Guest PsychoBT

That worked, thank you very much Lee!

And thanks again bsmither, it helped clearing the cache after changing Lee's changes.

Now I just have to figure out how I automatically clean the cache when I add or remove a post. Any idea? :)

Link to comment
Share on other sites

Guest PsychoBT

I added

	$cache = new cache();

	$cache->clearCache();



	if (file_exists(CC_ROOT_DIR.CC_DS.'includes'.CC_DS.'extra'.CC_DS.'admin_cat_cache.txt')) {

		unlink(CC_ROOT_DIR.CC_DS.'includes'.CC_DS.'extra'.CC_DS.'admin_cat_cache.txt');

	}

in my admin-news-adding-page and i worked :D

Thanks allot guys!

Link to comment
Share on other sites

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.

×
×
  • Create New...