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 19, 2013

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

By dwellingproductions - March 19, 2013

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

By dwellingproductions - March 19, 2013

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

By gregThomas - March 19, 2013

Hi Jeremy, 

I think I've found a solution to point 1. First you need to get the category records data if a category has been selected, I would do this using the mysql_get function:

  //get category data if category is selected.
  if(@$_REQUEST['category']){
    $selectedCategoryData = mysql_get('health_articles_categories', intval($_REQUEST['category']));
  }

This code would go with your getRecord requests near the top of the page. mysql_get requires two variables, the table you want to retrieve the record from, and the num value for that record. It then returns an array of data similar to getRecords but without the meta data (eg, uploads).

Then I would display the title like this:

<title>
  <?php echo htmlencode($master_infoRecord['meta_title_prefix']) ?>
  <?php if(@$selectedCategoryData['num']): ?>
   :: <?php echo $selectedCategoryData['category']; ?>
  <?php endif; ?> 
</title>

So if an array of category data exists, then display the title (I've had a look at your code, and it looks as if the category name field is called category).

I'm looking into point 3 now.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gregThomas - March 19, 2013

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

By dwellingproductions - March 19, 2013

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

By gregThomas - March 19, 2013

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

By dwellingproductions - March 19, 2013

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