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)

By ScottL - September 26, 2012 - edited: September 26, 2012

I have an editor setup to select a Category related to the Article. When displayed, it shows a list of categories, then Articles. Click on a category and it shows only that category. Is there a way to show Articles with it's category? (see attached).

Here's what I have for code:
<table width="70%" border="0" cellspacing="0" cellpadding="10">
<tr>
<th scope="col"> <ul>
<div align="left"><span class="articles">
<?php foreach ($piston_categoriesRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>

<?php if ($categoryRecord['_isSelected']): ?>
<b>
<?php endif ?>
<a href="?category=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php if ($categoryRecord['_isSelected']): ?>
</b>
<?php endif ?>

<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach ?>
</span> </div>
</ul> </th>

</tr>
</table>
<table width="85%" border="0" class="articles2" align="center" cellpadding="0" cellspacing="0">
<tr>
<th scope="col"><p align="left">
<?php foreach ($light_aircraft_projectsRecords as $record): ?>
<span class="style3"><?php echo $record['title'] ?></span> – <a href="<?php echo $record['_link'] ?>"><em>Read more</em></a><br/>

</p>
<div align="left">
<?php endforeach ?>
</div>
<a href="light-aircraft-projects.php"></a></th>
</tr>
</table>
<br>
<a href="light-aircraft-projects.php"><u>Show All Recent Piston Projects</u></a>

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: [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