Create list of categories from upload fields' info field

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

By benedict - March 17, 2013

Hi guys,

I am just populating an XML file for a gallery using data from CMSB.

Everything is working fine in terms of showing all the listings from a single viewer, and when clicked showing an enlarged version (My Gallery is the section name, 'images' is the upload field that contains all the images). My problem is this.

The gallery groups images that have the same category name (I am using 'info2' for each image). At the top of the XML page, it annnounces the category names, but I don;t know how to do this dynamically as I only want to list the category once, not 20 times.

Here is my code, with the categories hard-coded - is there some kind of loop I can do, that goes through all the info2 fields it finds and then spits out each category name once in a list?

<?php echo('<?xml version="1.0" encoding="utf-8"?>'); ?>
<?php 
  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('/home/stockw/public_html/','','../','../../','../../../');
  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 'my_gallery'
  list($my_galleryRecords, $my_galleryMetaData) = getRecords(array(
    'tableName'   => 'my_gallery',
    'where'       => '', // load first record
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $my_galleryRecord = @$my_galleryRecords[0]; // get first record
  if (!$my_galleryRecord) { dieWith404("Record not found!"); } // show error message if no record found

?>
<portfolio>
<!--Here is where I need to create my list of categories-->
    <categories>
        <category id="doncaster">Doncaster</category>
        <category id="bhs">Box Hill South</category>
        <category id="hawthorn">Hawthorn East</category>
        <category id="aspendale">Aspendale</category>
        <category id="templestowe">Templestowe</category>
        <category id="doncaster2">Doncaster</category>
    </categories>
    <items>
    <?php foreach ($my_galleryRecord['images'] as $index => $upload): ?>
            <item>
                <thumbnail><?php echo $upload['thumbUrlPath'] ?></thumbnail>
                <preview><?php echo $upload['urlPath'] ?></preview>
                <category><?php echo $upload['info2'] ?></category>
                <description><?php echo $upload['info3'] ?></description>
            </item>
        <?php endforeach ?>
    </items>
</portfolio>

By benedict - March 20, 2013

Anybody have any idea on this?

By gregThomas - March 21, 2013

Hi,

Sorry for the delay in reply, I think this is the easiest way to loop through your categories:

<?php echo('<?xml version="1.0" encoding="utf-8"?>'); ?> 
<?php  
  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('/home/stockw/public_html/','','../','../../','../../../');
  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 'my_gallery'
  list($my_galleryRecords, $my_galleryMetaData) = getRecords(array(
    'tableName'   => 'my_gallery',
    'where'       => '', // load first record
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $my_galleryRecord = @$my_galleryRecords[0]; // get first record
  if (!$my_galleryRecord) { dieWith404("Record not found!"); } // show error message if no record found

  //Create the array that will store the images
  $categoryArray = array();

  //Cycle through the images.
  foreach($my_galleryRecord['images'] as $image){
    //If the description isn't already in the category array
    if(!in_array($image['info2'], $categoryArray)){
     //Add the description to the category array
      $categoryArray[] = $image['info2'];
    }
  } 

  //Sort the items into alphabetical order
  sort($categoryArray);

?>

<portfolio>
<!--Here is where I need to create my list of categories-->
    <categories>
        <?php foreach($categoryArray as $category): ?>
          <category id="<?php echo strtolower($category); ?>"><?php echo $category; ?></category>
        <?php endforeach; ?>
    </categories>
    <items>
    <?php foreach ($my_galleryRecord['images'] as $index => $upload): ?>
            <item>
                <thumbnail><?php echo $upload['thumbUrlPath'] ?></thumbnail>
                <preview><?php echo $upload['urlPath'] ?></preview>
                <category><?php echo $upload['info2'] ?></category>
                <description><?php echo $upload['info3'] ?></description>
            </item>
        <?php endforeach ?>
    </items>
</portfolio>

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

So the code cycles through each image array and stores the info2 field into a category array if its value isn't already stored in there. 

Then the code loops through the category array to create the categories list.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com