Adding a second category...

10 posts by 2 authors in: Forums > CMS Builder
Last Post: September 11, 2014   (RSS)

By osga - August 27, 2014

We're trying to have the same articles headline be found in different spots... i.e.:

Writer "tom" publishes a Football article... we would like its headline to show up under "Toms" Category AND under the "Football" Category.

I placed a Category2Num dropdown in the Article form to select where else we would like the Article to show up, but i am missing something and its (obviously) not working.

What did I miss?

By gregThomas - August 28, 2014

Hi Osga,

Could you give me a few more details on how it isn't working? Are you not able to select a second category when creating the record? Or is the record not appearing correctly on the category list on your website?

Thanks,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By osga - August 29, 2014

We got the 2nd Cat dropdown showing up in the article form and it lists all the diff cats we use, but when we select a second cat from the dropdown, the article doesnt show up under the second categories page or the headline feeds of the second cat

so

if we used "Toms Arts" as the first cat and "Football" as the second cat...the art shows up under "Tom" but not under "Football"

By gregThomas - August 29, 2014

Thanks for clarifying. Would it be possible to post the code you're using to display art from the second category? Are you using a single select, or can the user select multiple second level categories for the art to show up under when they're creating the record in CMSB?

Greg Thomas







PHP Programmer - interactivetools.com

By osga - September 3, 2014

hi Greg!

we are using the standard generated headlines code (attached below) with the cat # on it, as such

http://www.osga.com/tab_box_headlines_usregs.php?categoryNum=61

The second cat is selected with its own dropdown so i guess it would be called a "double select(?)" in the article form.

.

.

.

.

.

.

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('/var/www/html/osga.com/','','../','../../','../../../');
  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 records
  list($articlesRecords, $articlesMetaData) = getRecords(array(
    'tableName'   => 'articles',
    'limit'       => '8',
  ));

?>

<style type="text/css">

a:link {text-decoration:none; color: #000; font-weight: bold; font-size: small; font-family: Arial;}
a:visited {text-decoration:none;}
a:hover {text-decoration:underline;}
a:active {text-decoration:underline;


</style>




<table width="275">
<tr><td>
   
    <?php foreach ($articlesRecords as $record): ?>
      
     <a href="<?php echo $record['_link'] ?>" target="new"><?php echo $record['title'] ?></a><br/>
     
        <?php foreach ($record['images'] as $upload): ?>
          <?php if ($upload['hasThumbnail']): ?>
            <img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>

          <?php elseif ($upload['isImage']): ?>
            <img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" /><br/>


          <?php endif ?>
        <?php endforeach ?><br/>
      <!-- STEP2a: /Display Uploads -->


    <?php endforeach ?>

    <?php if (!$articlesRecords): ?>
      No records were found!<br/><br/>
    <?php endif ?>
  <!-- /STEP2: Display Records -->
</td></tr></table>

By gregThomas - September 3, 2014

Hi Osga,

I think I see the problem. Normally you'd be able to add the second variable you need to the URL like this:

http://www.osga.com/tab_box_headlines_usregs.php?categoryNum=61&Category2Num=61

But when you do this the variables are linked using and, eg:

`categoryNum`= '61' AND `Category2Num` = '61'

But in your example it should be one or the other. I think you need to use this code below to filter instead:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('/var/www/html/osga.com/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  $filterOn = array();
  $where    = "";

  if(@$_REQUEST['categoryNum']){
    $safeResult = intval($_REQUEST['categoryNum']);
    $filterOn[] = "`categoryNum` = '$safeResult'";
  }

  if($_REQUEST['Category2Num']){
    $safeResult = intval($_REQUEST['Category2Num']);
    $filterOn[] = "`Category2Num` = '$safeResult'";
  }

  //'Glue' our statement together 
  if($filterOn){
    $where = implode(" OR ", $filterOn);
  }

  // load records
  list($articlesRecords, $articlesMetaData) = getRecords(array(
    'tableName'   => 'articles',
    'limit'       => '8',
    'allowSearch' => false,
    'where'       => $where
  ));

?>
<html>
  <body>
    <style type="text/css">
      a:link {text-decoration:none; color: #000; font-weight: bold; font-size: small; font-family: Arial;}
      a:visited {text-decoration:none;}
      a:hover {text-decoration:underline;}
      a:active {text-decoration:underline; }
    </style>
    <table width="275">
      <tr>
        <td>
         <?php foreach ($articlesRecords as $record): ?>
           <a href="<?php echo $record['_link'] ?>" target="new"><?php echo $record['title'] ?></a><br/>
              <?php foreach ($record['images'] as $upload): ?>
                <?php if ($upload['hasThumbnail']): ?>
                  <img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>
               <?php elseif ($upload['isImage']): ?>
                  <img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" /><br/>
               <?php endif ?>
              <?php endforeach ?><br/>
            <!-- STEP2a: /Display Uploads -->
          <?php endforeach ?>
          <?php if (!$articlesRecords): ?>
            No records were found!<br/><br/>
          <?php endif ?>
        </td>
      </tr>
    </table>
  </body>
</html>

So I've disabled the allowSearch functionality, as it doesn't behave as you require in this case. Then I've written some code that will construct a MySQL where statement that will return results for either category.

Let me know if you have any questions.

Cheers,

Greg 

Greg Thomas







PHP Programmer - interactivetools.com

By osga - September 4, 2014

that code returned an error:
"Notice: Undefined index: Category2Num in /var/www/html/osga.com/tab_box_headlines_football.php on line 18"

By gregThomas - September 4, 2014

Hi osga,

This was happening because you'd need to pass Category2Num in the URL to get the previous code to work. 

But thinking about it, you'll always want to search for the same category num in both Category2Num and categoryNum, so the code can be simplified:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('/var/www/html/osga.com/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  $filterOn = array();
  $where    = "";

  if(@$_REQUEST['categoryNum']){
    $safeResult = intval($_REQUEST['categoryNum']);
    $where = "`Category2Num` = '$safeResult' OR `categoryNum` = '$safeResult'";
  }

  // load records
  list($articlesRecords, $articlesMetaData) = getRecords(array(
    'tableName'   => 'articles',
    'limit'       => '8',
    'allowSearch' => false,
    'where'       => $where
  ));

?>
<html>
  <body>
    <style type="text/css">
      a:link {text-decoration:none; color: #000; font-weight: bold; font-size: small; font-family: Arial;}
      a:visited {text-decoration:none;}
      a:hover {text-decoration:underline;}
      a:active {text-decoration:underline; }
    </style>
    <table width="275">
      <tr>
        <td>
         <?php foreach ($articlesRecords as $record): ?>
           <a href="<?php echo $record['_link'] ?>" target="new"><?php echo $record['title'] ?></a><br/>
              <?php foreach ($record['images'] as $upload): ?>
                <?php if ($upload['hasThumbnail']): ?>
                  <img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>
               <?php elseif ($upload['isImage']): ?>
                  <img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" /><br/>
               <?php endif ?>
              <?php endforeach ?><br/>
            <!-- STEP2a: /Display Uploads -->
          <?php endforeach ?>
          <?php if (!$articlesRecords): ?>
            No records were found!<br/><br/>
          <?php endif ?>
        </td>
      </tr>
    </table>
  </body>
</html>

This is example code, so you might have to make a few small changes to get it working.

With this code you just need to use the same URL structure as before:

http://www.osga.com/tab_box_headlines_usregs.php?categoryNum=61

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gregThomas - September 11, 2014

Hi,

The only thing you should need to change from the previous post is the name of the category 2 field:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('/var/www/html/osga.com/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  $filterOn = array();
  $where    = "";

  if(@$_REQUEST['categoryNum']){
    $safeResult = intval($_REQUEST['categoryNum']);
    $where = "`Category2Num` = '$safeResult' OR `categoryNum` = '$safeResult'";
  }

  // load records
  list($articlesRecords, $articlesMetaData) = getRecords(array(
    'tableName'   => 'articles',
    'limit'       => '8',
    'allowSearch' => false,
    'where'       => $where
  ));

?>
<html>
  <body>
    <style type="text/css">
      a:link {text-decoration:none; color: #000; font-weight: bold; font-size: small; font-family: Arial;}
      a:visited {text-decoration:none;}
      a:hover {text-decoration:underline;}
      a:active {text-decoration:underline; }
    </style>
    <table width="275">
      <tr>
        <td>
         <?php foreach ($articlesRecords as $record): ?>
           <a href="<?php echo $record['_link'] ?>" target="new"><?php echo $record['title'] ?></a><br/>
              <?php foreach ($record['images'] as $upload): ?>
                <?php if ($upload['hasThumbnail']): ?>
                  <img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>
               <?php elseif ($upload['isImage']): ?>
                  <img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" /><br/>
               <?php endif ?>
              <?php endforeach ?><br/>
            <!-- STEP2a: /Display Uploads -->
          <?php endforeach ?>
          <?php if (!$articlesRecords): ?>
            No records were found!<br/><br/>
          <?php endif ?>
        </td>
      </tr>
    </table>
  </body>

So the field you might need to change the name of is the second category field in the articles section (highlighted in red above), as I'm not sure if that's the correct field name or not. 

Other than that, the code should hopefully just work. If it doesn't, you can post any errors you get in here, and I can take a look into them.

Cheers!

Greg

Greg Thomas







PHP Programmer - interactivetools.com