Listing Blog Categories (in various ways)

10 posts by 2 authors in: Forums > CMS Builder
Last Post: March 19, 2013   (RSS)

By dwellingproductions - March 18, 2013 - edited: March 19, 2013

Hello!  I have a few follow-up questions to some things we worked on last year (as detailed on this post): http://www.interactivetools.com/forum/forum-posts.php?postNum=2215008#post2215008

For reference, you can see the page I'm working on here:  http://www.liviaglobal.com/health_articles_list.php

I have a "blog" with a "category" multi-section called "health_articles_categories" and a content multi-section called "health_articles_content".  I am successfully pulling the "categories" into the "content" CMS Admin editor.  I am also successfully displaying all USED categories in the site's sidebar (while not displaying those that are not selected in the content section.  Now, I'm stuck on a couple of items.

1. Display category name in page headline after "Health Articles :: "   -   I basically need a way to know what category is currently being filtered and then display that after the two colons on the page headline to remind people of what they are looking at.  So, if someone clicks "Probiotics" in the categories list in the sidebar, I want the resulting, filtered list page to display the headline "Health Articles :: Probiotics".  Of course, I only want to display this here if the list is being filtered, so it needs to be conditional so that on the normal list view (showing all "Health Articles", the double colon and this category name would not appear.)

2. Highlight active category  -  I have a CSS class that I want to add to the active category (in the sidebar on the left of the site).  So, for instance, under "Categories" in the sidebar (where all categories containing associated content are listed), if they are filtering the list of "Health Articles" to only display those about "Digestive Health,"  I want to add my highlighting CSS class to only that category, while still listing the rest.

3. Display only "Posted In" categories under article preview  -  On this same list page, under each article (where it says "Posted In") I need a link to each category that the article is marked with (separated by commas).  Right now, as you will see, I am correctly displaying each category the article is associated with.  However, the link applies to all of them collectively.  I'm not sure how to break them out of the array and give each a link to their respective category filter, (For example, clicking "Pets" would link to: health_articles_list.php?category=2  -  While, clicking "Probiotics" would link to: health_articles_list.php?category=1)

Note: I am currently only displaying two articles per page in order to test the pagination.  Of course, later there will be 10-20 per page.  :-)  Also, I've uploaded some files so that you can see my current code.  I'll also paste snippets of what I have in a post below.  (I'm making liberal use of php includes statements, so the uploaded files include a couple of the included files too.)  :-)

Thanks, in advance, for your assistance!

- Jeremy

---------------------------

Dwelling Productions

www.dwellingproductions.com

By dwellingproductions - March 18, 2013 - edited: March 19, 2013

Here are some code samples I've been playing around with:

1. I don't have any code samples the first item because I couldn't even get close to getting this to work.  However, I'm pretty sure there is a way to detect the category number in the URL to make this work.  But, I can't remember how to do that.

2. Again, my attempts at coding this were unsuccessful.  However, I was wondering if the sample here (http://www.interactivetools.com/forum/forum-posts.php?postNum=2229484#post2229484) is what I need.  I'm just not sure how exactly to impliment it.

//Get the category items  list($mainNav, $selectedNav) = getCategories(array(     'tableName'            => 'menu_items',     'loadUploads'          => true,    'liAttributesCallback' => 'liStyle'  ));  //If the category is selected, add a class of selected.  function liStyle($category) {    //Check if the user must be logged in to see the page.    if ($category['num'] == $_REQUEST['category']) { return ' class="selected" '; }    return '';  }

3. For this one, the code I'm currently using (which lists the correct category names, but blankets them all with the same link) is:

<!-- CATEGORY MODULE -->
<?php
  // load records from 'health_articles_content'
  list($health_articles_contentRecords, $health_articles_contentMetaData) = getRecords(array(
    'tableName'   => 'health_articles_content',
    'loadUploads' => false,
    'allowSearch' => false,
  ));
?>
        
        <span>Posted in <a href="health_articles_list.php?category=<?php echo $category['num'];?>"><?php echo join(', ', $record['category:labels']); ?></a>
</span>
        </div>
      </article>
    <?php endforeach ?>
<!-- /end CATEGORY MODULE -->

Of course, there may be much better ways to do these things.  So, I'm open to any and all suggestions.

Thanks again!

- Jeremy

---------------------------

Dwelling Productions

www.dwellingproductions.com

Still haven't had any luck with these.  The first item is most important at this point.  I'm just not sure how to display the currently filtered category list.

Hopefully it's something relatively straight-forward.  :-)

- Jeremy

---------------------------

Dwelling Productions

www.dwellingproductions.com

OK, well I've managed to have success on item #2, thanks to Greg's post here: 

In case anyone is interested, here is the WORKING code for highlighting an active category (when needing to check it against the category number in the URL) while also only displaying categories containing content.

<?php
        $where = ""; 
         
        if (@$_REQUEST['category']) { 
          $where = "category LIKE '%\t".intval(@$_REQUEST['category'])."\t%'"; 
        } 
         
        list($health_articles_categoriesRecords, $health_articles_categoriesMetaData) = getRecords(array( 
          'tableName'    =>    'health_articles_categories', 
          'allowSearch'  =>     false, 
          //'where'        =>     $where, 
        ));
?>

<h3>Categories</h3>
<nav class="left_menu">
  <ul>
    <?php foreach ($health_articles_categoriesRecords as $category): ?>
    <?php if (!mysql_select("health_articles_content", "category LIKE '%\t".$category['num']."\t%'")) { continue; } ?>
    <li <?php echo ($_SERVER['REQUEST_URI'] == '/health_articles_list.php?category='.$category['num'])? 'class="current"' : ''; ?> ><a href="health_articles_list.php?category=<?php echo $category['num'];?>"><?php echo $category['category'];?></a>
    </li>
    <?php endforeach ?>
  </ul>
</nav>

---------------------------

Dwelling Productions

www.dwellingproductions.com

So, now if anyone can help me wrap my head around items #1 and #3, I would really appreciate it.  (Especially item #1 - This one is absolutely essential for me.  I think it should be as easy as referencing the correct variable, but I'm not sure what to use.)

- Jeremy

---------------------------

Dwelling Productions

www.dwellingproductions.com

Hi Jeremy,

I think you're close to solving the category listing problem, I think you need to create an array of num and label values for each record, and then list each link:

<?php  $linkArray = array_combine($record['category:values'], $record['category:labels']);  ?>
    <span>Posted in 
      <?php foreach($linkArray as $num => $title): ?>
        <a href="health_articles_list.php?category=<?php echo $num;?>"><?php echo $title; ?>,</a>
      <?php endforeach; ?>
    </span>

This code does assume that a record can belong to multiple categories. So the array_combine function creates an array where the key is the category num values, and the category labels are the values. Then the foreach loop cycles through each item and creates the links. 

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Greg!

Thank you so much for your reply.  We're really close on #1.  

It's working perfectly on pages that are specifically calling the category in the URL:  http://www.liviaglobal.com/health_articles_list.php?category=1  (Note the large headline: "Health Articles :: Probiotics")

However, for some reason, it's returning an "undefined variable" error on the main list page: http://www.liviaglobal.com/health_articles_list.php

I used the header code exactly as you listed.  My body code is slightly different, but it still seems like it should work:

<h1 class="page_title">Health Articles :: <?php if ($selectedCategoryData['category']): ?><?php echo $selectedCategoryData['category']; ?><?php else: ?><?php endif ?></h1>

Any ideas?  :-)

- Jeremy

---------------------------

Dwelling Productions

www.dwellingproductions.com

Hi Jeremy, 

You're getting that error because the $selectedCategoryData array only exists when a category is selected, I'd get around this problem by suppressing the error with an @ symbol in your if statement:

<h1 class="page_title">Health Articles :: <?php if (@$selectedCategoryData['category']): ?><?php echo $selectedCategoryData['category']; ?><?php else: ?><?php endif ?></h1>

This will stop any error messages appearing that are related to $selectedCategoryData['category'] not exsisting, and the if statement will still work correctly. 

Cheers!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Oh man!!! You are totally my hero!  :-)

That did it!  All three items are working perfectly now.  I can't thank you enough, Greg.  If you're up for it, I'm going to have two more questions related to this blog (but not the categories).  :-)

I so appreciate your assistance.

- Jeremy

---------------------------

Dwelling Productions

www.dwellingproductions.com