Category - How to display the top level items

6 posts by 2 authors in: Forums > CMS Builder
Last Post: January 10, 2013   (RSS)

By esupport - January 9, 2013

Hi, 

There are 3 levels on my category.

On the first category page.

It should display  top level items. 

Now the page is display the secand level list.

How can I display the top level?

Here is the page:

http://174.132.79.189/~quanmax/en/category-list.php

Header code:

<?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('/home/quanmax/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 detail record from 'category' 

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

// load list records from 'category' 
list($leftMenu, $categoryMetaData) = getCategories(array( 
'tableName' => 'category', 
'loadUploads' => true, 
'categoryFormat' => 'twolevel' 
)); 

list($rightMenu, $categoryMetaData) = getCategories(array( 
'tableName' => 'category', 
'loadUploads' => true, 
'rootCategoryNum' => $detailRecord['num'], 
'categoryFormat' => 'onelevel' 
));


?>

Body:

<?php foreach ($rightMenu as $listRecord): ?>

<table border="0" cellspacing="0" cellpadding="0" width="700">
<span 
id="news_list_lab">
<tbody>
<tr>
<td width="185" valign="top" class="content_s_000"><?php foreach ($listRecord['images'] as $index => $upload): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" 
alt="<?php echo htmlencode($productsRecord['title']) ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" hspace="20" /><br/>
<?php endforeach ?></td>
<td align="left" valign="top" class="content"><b><a <?php echo ($listRecord['depth']+1)*10; ?>px;" href="<?php if($listRecord['depth'] == '0'){ 
echo 'category-list.php?num='.$listRecord['num']; 
}elseif($listRecord['depth'] == '1'){ 
echo 'category-list.php?num='.$listRecord['num']; 
}elseif($listRecord['depth'] == '2'){ 
echo 'products-detail.php?num='.$listRecord['num']; 

?>" > <?php echo htmlencode($listRecord['name']) ?></a></b> <?php echo $listRecord['intro']; ?></td>
</tr>
<tr>
<td colspan="2" align="right" class="line_h_dot_black"><a style="margin-left:<?php echo ($listRecord['depth']+1)*10; ?>px;" href="<?php if($listRecord['depth'] == '0'){ 
echo 'category-list.php?num='.$listRecord['num']; 
}elseif($listRecord['depth'] == '1'){ 
echo 'category-list.php?num='.$listRecord['num']; 
}elseif($listRecord['depth'] == '2'){ 
echo 'products-detail.php?num='.$listRecord['num']; 

?>" ><img 
class="banner_line_no" hspace="0" alt="See More!" vspace="5" 
src="images/share/button_more.gif" width="41" 
height="11" /></a></td>
</tr>
</tbody>
</span>
</table>

<?php endforeach ?>

Thanks!

Jac

-Jax H.
Attachments:

category-list_001.php 42K

Hi Jac,

You could change your $rightMenu getRecords function so that it only gets the top level items like this:

list($rightMenu, $categoryMetaData) = getCategories(array(
'tableName' => 'category',
'loadUploads' => true,
'rootCategoryNum' => 0,
'categoryFormat' => 'onelevel'
));

I've set the rootCategoryNum to zero, so the function will start looking for items at the very top level.

The categoryFormat is set to onelevel, so it will only return the level you have selected.

You might be able to remove the $categoryRecords getRecords function, but I'm not sure if it is used anywhere else on your page. 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Gerg

One problem.

The top level items is fine on the first level page.

but when go to secand and thried level.

Top level items also display on other level.

Jac

-Jax H.

Hi Jac,

Sorry, I thought you wanted to create a completetly seperate page. If you want to display all of the parent categorys if no sub category is selected on the same page, I would do something like this:

/* 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('/home/quanmax/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."); }

// Get the last number from the URL, if there isn't one there set the number as zero. 

$mainCat =  intval(getLastNumberInUrl());

// load detail record from 'category' 

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

// load list records from 'category' 
list($leftMenu, $categoryMetaData) = getCategories(array( 
'tableName' => 'category', 
'loadUploads' => true, 
'categoryFormat' => 'twolevel' 
)); 

//Show either level one or two category pages depending on if a there is a number in the URL
list($rightMenu, $categoryMetaData) = getCategories(array( 
'tableName' => 'category', 
'loadUploads' => true, 
'rootCategoryNum' =>  $mainCat
'categoryFormat' => 'onelevel' 
));


So if there isn't a number in the URL, the rootCategoryNum will default to 0, and the page will only display top level items. You should be able to keep the same body code as you used last time.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Gerg

It's all good now!

Nice job, Thanks!

Jac

-Jax H.