Category Pages

11 posts by 4 authors in: Forums > CMS Builder
Last Post: April 14, 2014   (RSS)

By meg - March 10, 2014

I created a category menu that's connected to Blog Entries (multi-listing) by a list record. I want figure out a way to automate Category specific pages within this blog.

I know how to create pages that could take only from a certain category (manually), but is there a way to create one page that automates this? I essentially want my client to keep adding categories as needed and I don't want to keep having to create this category pages manually.

Any thoughts would be much appreciate!

Seriously love your product!

Hi Meg,

I'd just like to clarify that I've understood the problem.

So there are two multi record sections, one for the categories, and one for the blog entries. When a user creates a blog record, they can select a category from a list field. Are you looking for a way to add a new category while creating a blog record, to save the user having to go the categories section first to add their new categories.

Is that correct?

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Mohaukla - April 4, 2014

Hi Greg

Thought I might be able to ask a question under this subject. 

I created a list called 'projects' for my portfolio page. I created a list of 'project_categories' and a list of 'core_category'. I added these 2 as lists inside the 'projects' list. The project_categories is multiple choice drop down and the core_category is a single choice. On the page you can sort the list of projects showing by type of project (controlled by the core_category). All this works fine. When you goto the projects detail page everything is working except the part where I call on the project_categories that were chosen. Right now it is listing each of the choices for each time a  choice is made. So if the I create a project and choose three different categories it will just list out all categories 3 times. See it on this page: http://www.justritedesign.com/jrd7/single_portfolio.php?Diamond-Builders-Inc-3

Im sure its how I am creating the foreach loop. 

<?php foreach ($projectsRecord['category:values'] as $index => $all_categories): ?>
   <a href="#" title="<?php echo join(', ', $projectsRecord['category:labels']); ?>"><?php echo join(', ', $projectsRecord['category:labels']); ?></a>, 
<?php endforeach ?>

Thanks for your help on this,

Michael

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

By Chris - April 7, 2014

Hi meg,

I think you want to add Blog Lists to Category Detail pages. Start out by using the Code Generator to create a Detail page for your Category section:

// load record from 'category'
list($categoryRecords, $categoryMetaData) = getRecords(array(
  'tableName'   => 'category',
  'where'       => whereRecordNumberInUrl(0),
  'loadUploads' => true,
  'allowSearch' => false,
  'limit'       => '1',
));
$categoryRecord = @$categoryRecords[0]; // get first record
if (!$categoryRecord) { dieWith404("Record not found!"); } // show error message if no record found

You said you already know how to create hard-coded Category-specific Blog List pages, all you need to do is insert the record "num" from the $categoryRecord into the where clause when you're loading Bog records. Let's say this is how you'd load Blog records for Category 123 manually:

// load records from 'blog'
$where = "category LIKE '%\t123\t%'";
list($blogRecords, $blogMetaData) = getRecords(array(
  'tableName'   => 'blog',
  'loadUploads' => true,
  'allowSearch' => false,
  'where'       => $where,
));

...you'll just need to change that to:

// load records from 'blog'
$where = "category LIKE '%\t" . mysql_escape($categoryRecord['num']) . "\t%'";
list($blogRecords, $blogMetaData) = getRecords(array(
  'tableName'   => 'blog',
  'loadUploads' => true,
  'allowSearch' => false,
  'where'       => $where,
));

Put that all together and you get:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  
  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load record from 'category'
list($categoryRecords, $categoryMetaData) = getRecords(array(
  'tableName'   => 'category',
  'where'       => whereRecordNumberInUrl(0),
  'loadUploads' => true,
  'allowSearch' => false,
  'limit'       => '1',
));
$categoryRecord = @$categoryRecords[0]; // get first record
if (!$categoryRecord) { dieWith404("Record not found!"); } // show error message if no record found

// load records from 'blog'
$where = "category LIKE '%\t" . mysql_escape($categoryRecord['num']) . "\t%'";
list($blogRecords, $blogMetaData) = getRecords(array(
  'tableName'   => 'blog',
  'loadUploads' => true,
  'allowSearch' => false,
  'where'       => $where,
));

?>

<h1>Category</h1>
<?php showme($categoryRecord) ?>

<h2>Blogs in this Category</h2>
<?php foreach ($blogRecords as $blog): ?>
  <?php showme($blog) ?>
  <hr>
<?php endforeach ?>

I hope this helps! Please let me know if you have any questions.

All the best,
Chris

By Chris - April 7, 2014

Hi Michael,

I think you want to process both the :values and the :labels pseudo fields. The simplest way to do this is by using a counter variable and accessing the array elements by index. For example, your pseudo fields may look like this:

$record['category:labels'] = array( 0 => 'Apple Juice', 1 => 'Pear Juice' );
$record['category:values'] = array( 0 => 17, 1 => 32 );

You can loop over them like this:

<?php $arrayLength = count($record['category:values']); // count either labels or values -- both should be the same ?>
<?php for ( $i = 0; $i < $arrayLength; $i++ ): ?>
  <?php $label = $record['category:labels'][$i]; ?>
  <?php $value = $record['category:values'][$i]; ?>
  
  <a href="category.php?<?php echo $value ?>"><?php echo htmlencode($label) ?></a>
  
<?php endfor; ?>

Does that help?

All the best,
Chris

By Mohaukla - April 8, 2014 - edited: April 9, 2014

It's still not working. It has an error message now. 

It is picking up that there are 5 out of 10 categories assigned to the project (http://www.justritedesign.com/jrd7/single_portfolio.php?Ideal-Reefer-LLC-1) because it is giving me the error 5 times. Before it was also doing the same except that it listed all 10 categories 5 times.

The error is with an undefined index:

Notice: Undefined index: category:values in C:\Webspace\resadmin\mmoyers\justritedesign.com\www\jrd7\single_portfolio.php on line 299 Notice: Undefined index: category:values in 

So my code looks like this at the moment:

<?php foreach ($projectsRecord['category:values'] as $index => $category): ?>
                                <?php $arrayLength = count($record['category:values']); // count either labels or values -- both should be the same ?>
                                <?php for ( $i = 0; $i < $arrayLength; $i++ ): ?>
                                <?php $label = $record['category:labels'][$i]; ?>
                                <?php $value = $record['category:values'][$i]; ?>
  
                                <a href="category.php?<?php echo $value ?>"><?php echo htmlencode($label) ?></a>
  
                                <?php endfor; ?>
<?php endforeach ?>

Im not sure that this is what you intended.

Let me know what you think,

Michael

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

By Chris - April 10, 2014

Hi Michael,

You'll need to change all three instances of $record to $projectsRecord.

You'll also need to remove the foreach and endforeach lines. The for loop will loop over all the labels/values, there is no need for another loop.

Does that help?

All the best,
Chris

By Mohaukla - April 10, 2014

Thanks Chris

That worked great.

My last question for this page that I am working on is I need to show related projects at the bottom. I have a loop that shows the first 4 projects but I would like to filter this with those that have the same 'core_category' (pulled from another list in the data base with a single choice). But I don't want the current project that is showing to be in that loop. The question is do I use a WHERE clause in the head or an IF statement in the loop or a combination of the two? And what would it look like?

Head Code:

  list($projectsRecords, $projectsMetaData) = getRecords(array(
    'tableName'   => 'projects',
    'where'       => whereRecordNumberInUrl(0),
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $projectsRecord = @$projectsRecords[0]; // get first record
  if (!$projectsRecord) { dieWith404("Record not found!"); } // show error message if no record found 
  
  list($projectsRecords, $projectsMetaData) = getRecords(array(
    'tableName'   => 'projects',
    'loadUploads' => true,
    'allowSearch' => false,
    'limit' => '4',
  ));

Loop Code:

                    <div class="row">
                    <?php foreach ($projectsRecords as $record): ?>
                     <div class="col-sm-3 item"> 
                            <?php foreach ($record['full_image_gallery'] as $index => $upload): ?><a href="<?php echo $upload['urlPath'] ?>" class="swipebox" title="<?php echo htmlencode($record['title']) ?>"><?php endforeach ?>
                                <figure class="figure-hover">
                                    <?php foreach ($record['main_image'] as $index => $upload): ?><img src="<?php echo $upload['urlPath'] ?>" alt="<?php echo htmlencode($record['title']) ?>" /><?php endforeach ?>
                                    <div><span class="icon-hover icon-hover-zoom"></span></div>
                                </figure>
                            </a>
                            <div class="btn-readmore">
                                <a href="<?php echo $record['_link'] ?>" title="<?php echo htmlencode($record['title']) ?>"><?php echo htmlencode($record['title']) ?></a>
                            </div>
                        </div>
                    <?php endforeach ?>    
                    </div>

As always thanks for your advice!

Michael

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

By Mohaukla - April 11, 2014

I added the code as you have it but it does not show anything related. 

In my list of projects currently I have: (only 4 right now)

Diamond Builders Inc - Web Design / Redesign

2Tinker LOGO - Logo Design

Ideal Reefer LLC - Web Design / Redesign

Montevideo Family Dentistry - Web Design / Redesign

The page is one of the projects that shares the Web Design / Redesign core_category.
http://www.justritedesign.com/jrd7/single_portfolio.php?Diamond-Builders-Inc-3

It is showing this error. 

Notice: Undefined variable: projectRecord in C:\Webspace\resadmin\mmoyers\justritedesign.com\www\jrd7\single_portfolio.php on line 22 Notice: Undefined variable: projectRecord in C:\Webspace\resadmin\mmoyers\justritedesign.com\www\jrd7\single_portfolio.php on line 22

Line 22 is the $where line.

    $where = mysql_escapef("core_category = ? AND num != ?", $projectRecord['core_category'], $projectRecord['num']);

I can not see the error. Maybe you can.

Thanks 

Michael

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"