Show Articles with it's Category instead of a separate list

4 posts by 2 authors in: Forums > CMS Builder
Last Post: September 27, 2012   (RSS)

Re: [ScottL] Show Articles with it's Category instead of a separate list

Hi,

So you have a section of articles and each one has has a category which it gets from a categories section? Are these linked using the list field type as described in this article?

http://www.interactivetools.com/kb/article.php?Populate-a-list-field-from-another-section-15

If this is how you have your system set up then you should be able to get a articles category by doing something like this:

<?php
//I'm assuming that $light_aircraft_projectsRecords contains the articles array and has a categories field
foreach ($light_aircraft_projectsRecords as $record): ?>

<span class="style3"><?php echo $record['title'] ?></span> – <a href="<?php echo $record['_link'] ?>">
Category: <?php echo $record['fieldGoesHere:labels']; ?>
<em>Read more</em></a><br/>

</p>
<div align="left">
<?php endforeach ?>


You'll need to replace fieldGoesHere with the name of the field that contains the category list.

Let me know if this doesn't work.

Thanks
Greg Thomas







PHP Programmer - interactivetools.com

Re: [greg] Show Articles with it's Category instead of a separate list

Sorry. That's not working. Yes, I have categories set up and each article will be assigned to a category. Categories are linked with list field type.

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

require_once "/home/content/41/5724841/html/cmsAdmin/lib/viewer_functions.php";

list($piston_categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'piston_categories',
));

list($light_aircraft_projectsRecords, $light_aircraft_projectsMetaData) = getRecords(array(
'tableName' => 'light_aircraft_projects',
));

?>




I don't see how your suggestion will sort the categories like this:

Category 1
story 1 link
story 2 link
etc.

Catgeory 2
story 1 link
story 2 link
etc.

Category 3
story 1 link
story 2 link
etc.

Thanks.

Re: [ScottL] Show Articles with it's Category instead of a separate list

Hi Scott,

Sorry, I misunderstood the question. The easiest way to create a list like that is to use the array_groupBy function to sort the articles by there category number (I'm assuming that each article only has one category). And then cycle through both arrays like this:

<?php

list($piston_categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'piston_categories',
));

list($light_aircraft_projectsRecords, $light_aircraft_projectsMetaData) = getRecords(array(
'tableName' => 'light_aircraft_projects',
));

$light_aircraft_projectsRecords = array_groupBy($light_aircraft_projectsRecords, 'categoryNumGoesHere',TRUE);

?>

<ul>
<?php
foreach($piston_categoriesRecords as $key => $categoryRecord){ ?>
<li><a href="?category=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></a></li>
<?php if(is_array($light_aircraft_projectsRecords[$categoryRecord['num']])){ ?>
<ul>
<?php foreach($light_aircraft_projectsRecords[$categoryRecord['num']] as $key => $row){ ?>
<li><?php echo $row['titleofArticleGoesInHere']; ?></li>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
</ul>


This is only an example. So you will have to adapt the code with the correct variables. But I've tested the method locally so it should work. You'll have to replace categoryNumGoesHere with the name of the list field that contains the articles category.

Let me know if you need any more help.

Thanks!
Greg Thomas







PHP Programmer - interactivetools.com