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

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

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