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: [greg] Show Articles with it's Category instead of a separate list

By ScottL - September 27, 2012

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

By gregThomas - September 27, 2012

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