DB entries: which ones are displayed on the website? which ones are old versions?

Permalink Browser Info Environment
Hi Community,

And thanks Jordan for this great Designer Content add-on!

I use it to allow an easy addition/edition/sorting on a NEWS page and everything works well: my client adds/edits/sorts news on that page.

Now I need to promote these news on the homepage so I access the DB, select the appropriate table and print the news titles using the following code:

$rs = $db->query('SELECT * FROM btDCNews');
while($row = $rs->fetchRow()) {
   echo $row['field_2_textbox_text'].'<br />';
}


But I end up having ALL the news titles printed, instead of just the "current" ones (the ones published on the website). Here I can see ALL the titles of ALL the previous editing steps.

My question is: how to know which entry to choose? which entry is the one displayed on the website?

Could anyone point me in the right direction?
(Please don't advise me to get a NEWS add-on, the example above is just and EXAMPLE!)
Thanks!

Type: Discussion
Status: New
stephanebeck
View Replies: View Best Answer
jordanlev replied on at Permalink Best Answer Reply
jordanlev
Hi Stéphane,
The answer is a bit more complicated than you are probably expecting.

I do not believe that using Designer Content is the ideal approach for your News section. Instead, I would make a page type for news items, and each news item is an individual page under a single top-level "index" page. On the index page, you put a Page List block that displays the news item pages in reverse-chronological order. You will probably want to create a custom template for this page list block on the top-level news index page (or use the free "Page List Teasers" addon from the marketplace to show excerpts of each news item).

Then, on your home page, you would do something similar -- add a page list block that shows recent pages of the "news item" page type, with a custom template to display the HTML how you want it.

If you are concerned that adding an entire new page for each news item is too much work, you can use the "Composer" feature to provide a dashboard interface for adding new items more easily.

This is the "Concrete5 way" to do things -- everything revolves around individual pages. Doing things this way makes development much easier.

All that being said, if you still want to use your Designer Content blocks, you are going to want to use the C5 API to retrieve the blocks on the page -- do not make direct database queries. The problem with database queries is that the block table stores a new record for every version of the block, so every time the block on a page is edited, a new record is added to that table. You will wind up with tons of duplicates this way, and it will be a very complicated query that joins a LOT of different tables to extract out exactly what you're looking for.

To do this with the C5 API instead, you need to bring up the NEWS page, then loop through its blocks and pull out the content from there. Something like this should do the trick:
$newsPage = Page::getByPath('/news');
$pageBlocks = $newsPage->getBlocks('Main');
foreach ($pageBlocks as $pb) {
  if ($pb->btHandle == 'your_custom_block_handle') {
    $bi = $pb->getInstance();
    echo $bi->field_1_wysiwyg_content;
  }
}

You will need to change some things in the above code, for example setting the proper page path, area name, and custom blocktype handle. Also the name of the field that has the content you want to get out of the block. Post back if you need help understanding how to tweak the code.

Good luck!

-Jordan
stephanebeck replied on at Permalink Reply
stephanebeck
Your code snippet works perfectly, that was exactly what I was looking for, thanks Jordan.

At first I considered using the "one news = one page" approach (which is the most common one for sure) but on this website, each news won't be longer that 2 lines, which almost results in an empty page if you display one news per page.

Which leads me to another question (not sure it belongs here though) :

Is there a way to display/include several pages of the sitemap (like these very short news that would be sub pages) into just ONE page (that would be the main "news" page) to achieve the same goal (displaying the news below each other)?

Thanks, good karma!
jordanlev replied on at Permalink Reply
jordanlev
Glad the code worked for you, and I understand that separate blocks are probably best for just 1 or 2 lines in each news item.

Unfortunately I don't understand what you mean by your followup question -- I think what you're asking about is the Page List block, but it sounds like you don't want to use that approach, so I'm a bit confused. Can you try to explain in more detail?
stephanebeck replied on at Permalink Reply
stephanebeck
Let's say I have a set of 4 short pages under a parent page in the sitemap. I'd like the parent page to display the CONTENT of its four children pages (like the PHP include() function could display the content of other .php files). Hope I made it clearer :)
jordanlev replied on at Permalink Reply
jordanlev
Try the Page List Teasers addon in the marketplace:
http://www.concrete5.org/marketplace/addons/page-list-teasers...

It is a custom template for the page list block that shows the actual page content instead of just descriptions. I think this is what you want.
stephanebeck replied on at Permalink Reply
stephanebeck
Great! That add-on will contain the PHP code I was looking for. Thanks again.

concrete5 Environment Information

Browser User-Agent String

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You have not specified a license for this support ticket. You must have a valid license assigned to a support ticket to request a refund.