How to use TAPIr to output additional CCK fields for products

Posts: 41
Joined: 09/07/2007

Hello,

I have created several fields with CCK that I have added to the add new product page. I now have about 40 additional fields that contain information about my products. Each product only uses about 10 of those fields, so there are several that are blank depending on the product. I would like to create a "Specifications" table on the product page that will show all of the variables that have a value assigned to them for that product.
Here is an example of the layout with the "Specifications" table.
http://www.xikar.com/redesign/Final_Site/Xi1_individual_product_page.pdf

Is it possible to use TAPIr to create this table? Does is have the ability to only show the fields that have a value in them? Is there a better way to do this?

Here is an example of an array that contains the data:

[group_general] => Array
                (
                    [field_style] => Array
                        (
                            [#access] => 1
                            [#value] => <div class="field field-type-text field-field-style"><div class="field-label">Style:&nbsp;</div><div class="field-items"><div class="field-item">Titanium</div></div></div>
                            [#weight] => -1
                            [#printed] => 1
                        )

                    [field_body_material] => Array
                        (
                            [#access] => 1
                            [#value] => <div class="field field-type-text field-field-body-material"><div class="field-label">Body Material:&nbsp;</div><div class="field-items"><div class="field-item">Aluminum</div></div></div>
                            [#weight] => 0
                            [#printed] => 1
                        )

                    [#title] =>
                    [field_body_finish] => Array
                        (
                            [#access] => 1
                            [#value] => <div class="field field-type-text field-field-body-finish"><div class="field-label">Body Finish:&nbsp;</div><div class="field-items"><div class="field-item">Bead Blast</div></div></div>

                            [#weight] => 0
                            [#printed] => 1
                        )

                    [#attributes] => Array
                        (
                            [class] => fieldgroup group-general
                        )

                    [#weight] => 2
                    [#description] =>
                    [field_accent_material] => Array
                        (
                            [#access] => 1
                            [#value] =>
                            [#weight] => 1
                            [#printed] => 1
                        )

                    [field_accent_finish] => Array
                        (
                            [#access] => 1
                            [#value] =>
                            [#weight] => 1
                            [#printed] => 1
                        )

                    [field_blade_finish] => Array
                        (
                            [#access] => 1
                            [#value] => <div class="field field-type-text field-field-blade-finish"><div class="field-label">Blade Finish:&nbsp;</div><div class="field-items"><div class="field-item">Brushed</div></div></div>
                            [#weight] => 3
                            [#printed] => 1
                        )

                    [field_blade_material] => Array
                        (
                            [#access] => 1
                            [#value] => <div class="field field-type-text field-field-blade-material"><div class="field-label">Blade Material:&nbsp;</div><div class="field-items"><div class="field-item">440, HR57 Stainless</div></div></div>
                            [#weight] => 3
                            [#printed] => 1
                        )

                    [field_handle_material] => Array
                        (
                            [#access] => 1
                            [#value] => <div class="field field-type-text field-field-handle-material"><div class="field-label">Handle Material:&nbsp;</div><div class="field-items"><div class="field-item">Aluminum</div></div></div>
                            [#weight] => 4
                            [#printed] => 1
                        )

                    [field_handle_finish] => Array
                        (
                            [#access] => 1
                            [#value] => <div class="field field-type-text field-field-handle-finish"><div class="field-label">Handle Finish:&nbsp;</div><div class="field-items"><div class="field-item">Bead Blast</div></div></div>

                            [#weight] => 4
                            [#printed] => 1
                        )

                    [field_accessories] => Array
                        (
                            [#access] => 1
                            [#value] =>
                            [#weight] => 5
                            [#printed] => 1
                        )

                    [field_max_ring_gauge] => Array
                        (
                            [#access] => 1
                            [#value] => <div class="field field-type-number-integer field-field-max-ring-gauge"><div class="field-label">Max Ring Gauge:&nbsp;</div><div class="field-items"><div class="field-item">58Ring Gauge</div></div></div>
                            [#weight] => 5
                            [#printed] => 1
                        )

                    [field_lock_style] => Array
                        (
                            [#access] => 1
                            [#value] => <div class="field field-type-text field-field-lock-style"><div class="field-label">Lock Style:&nbsp;</div><div class="field-items"><div class="field-item">Spring Action Sliding Button</div></div></div>
                            [#weight] => 5
                            [#printed] => 1
                        )

                    [field_features] => Array
                        (
                            [#access] => 1
                            [#value] =>
                            [#weight] => 5
                            [#printed] => 1
                        )

                    [field_sheath_material] => Array
                        (
                            [#access] => 1
                            [#value] =>
                            [#weight] => 7
                            [#printed] => 1
                        )

                    [field_leather_type] => Array
                        (
                            [#access] => 1
                            [#value] =>
                            [#weight] => 7
                            [#printed] => 1
                        )

                    [field_purity] => Array
                        (
                            [#access] => 1
                            [#value] =>
                            [#weight] => 8
                            [#printed] => 1
                        )

                    [field_volume] => Array
                        (
                            [#access] => 1
                            [#value] =>
                            [#weight] => 8
                            [#printed] => 1
                        )

                    [field_personalize_logo] => Array
                        (
                            [#access] => 1
                            [#value] => <div class="field field-type-text field-field-personalize-logo"><div class="field-label">Personalize Logo:&nbsp;</div><div class="field-items"><div class="field-item">Laser Engraving</div></div></div>
                            [#weight] => 9
                            [#printed] => 1
                        )

                    [field_us_patent_number] => Array
                        (
                            [#access] => 1
                            [#value] => <div class="field field-type-text field-field-us-patent-number"><div class="field-label">US Patent Number:&nbsp;</div><div class="field-items"><div class="field-item">5937523</div></div></div>

                            [#weight] => 10
                            [#printed] => 1
                        )
                    [#printed] => 1
                )

How do I go about assigning this array to a TAPIr table and then only outputting the fields that have a value?

Thanks for any help you can give me.

Posts: 41
Joined: 09/07/2007

I figured out how to access the elements of the array that i need with this:

<?php
foreach(array_keys($node->content['group_general']) as $key){
    print
$node->content['group_general'][$key]['#value'];
     }
if(isset(
$node->content['group_cutter_specific'])){
     foreach(
array_keys($node->content['group_cutter_specific']) as $key) {
     print
$node->content['group_cutter_specific'][$key]['#value'];
     }
}
if(isset(
$node->content['group_lighter_specific'])){
     foreach(
array_keys($node->content['group_lighter_specific']) as $key) {
     print
$node->content['group_lighter_specific'][$key]['#value'];
     }
}
if(isset(
$node->content['group_punch_specific'])){
     foreach(
array_keys($node->content['group_punch_specific']) as $key) {
     print
$node->content['group_punch_specific'][$key]['#value'];
     }
}
if(isset(
$node->content['group_knife_specific'])){
     foreach(
array_keys($node->content['group_knife_specific']) as $key) {
     print
$node->content['group_knife_specific'][$key]['#value'];
     }
}
if(isset(
$node->content['group_humidification_specific'])){
     foreach(
array_keys($node->content['group_humidification_specific']) as $key) {
     print
$node->content['group_humidification_specific'][$key]['#value'];
     }
}
if(isset(
$node->content['group_cases_and_humidors'])){
     foreach(
array_keys($node->content['group_cases_and_humidors']) as $key) {
     print
$node->content['group_cases_and_humidors'][$key]['#value'];
     }
}
if(isset(
$node->content['group_appearl'])){
     foreach(
array_keys($node->content['group_appearl']) as $key) {
     print
$node->content['group_appearl'][$key]['#value'];
     }
}
?>

I'm trying to simplify it into this:

<?php

   
foreach(array_keys($node->content) as $key){
        if(isset(
$node->content[$key])){
            foreach(
array_keys($node->content[$key]) as $specifications){
                print
$node->content[$key][$specifications]['#value'];
            }
        }
    }
?>

For some reason I get an error with the simplified version.
* warning: array_keys() [function.array-keys]: The first argument should be an array in C:\WAMP Server\wamp\www\themes\xikar\node-product.tpl.php on line 29.
* warning: Invalid argument supplied for foreach() in C:\WAMP Server\wamp\www\themes\xikar\node-product.tpl.php on line 29.
* warning: array_keys() [function.array-keys]: The first argument should be an array in C:\WAMP Server\wamp\www\themes\xikar\node-product.tpl.php on line 29.
* warning: Invalid argument supplied for foreach() in C:\WAMP Server\wamp\www\themes\xikar\node-product.tpl.php on line 29.

Any idea why the simplified version causes these errors? It still prints out, but I get the above errors.

I'm also getting some random characters in the printed code like the "<", "<3" and "2" in bold below.

Style: TitaniumBody Material: AluminumBody Finish: Bead Blast2Blade Finish: BrushedBlade Material: 440, HR57 StainlessHandle Material: AluminumHandle Finish: Bead BlastMax Ring Gauge: 58Ring GaugeLock Style: Spring Action Sliding ButtonPersonalize Logo: Laser EngravingUS Patent Number: 5937523<3Cutter Type: Double Guillotine<

Posts: 2244
Joined: 08/07/2007
AdministratoreLiTe!

$node->content contains all the stuff that is displayed when the node is viewed, not just the CCK fields. Some things might not be arrays like your code expects, so that's why you get the errors.

Put

<?php
 
print '<pre>';
 
print_r($node->content);
  print
'</pre>';
?>

in your template file to get a good look at what you're dealing with.

From your examples, you might be able to test the keys of the content array to get the parts you want:

<?php
// Use element_children() to ignore keys like '#weight'.
foreach (element_children($node->content) as $key){
  if (
substr($key, 0, 6) == 'group_'){
    foreach (
element_children($node->content[$key]) as $specifications){
     
// Work, work...
   
}
  }
}
?>

Posts: 41
Joined: 09/07/2007

Thanks Lyle, that worked perfect! I started debugging by added echo $key; everywhere and realized I needed to only access the keys that had "group_" in them. However, I still couldn't find the right function to check if 'group_' was in the key.

It's hard to theme things when your a PHP noob, but I'm learning fast.