php zencart - having problems with attributes array

The basic problem is, when the form is submitted it should also include the product, the attribute and the model. The product and model work fine, but the attribute always just says "Array."

There are two places where $product['attributes'] shows up in this foreach loop. The first section is what prints the product, attribute and model on the page that also has the form. So I know the attributes are getting to the page.

The part later is where the problem occurs. I can't figure out why the same thing later in the code returns only the word "Array." This souldn't be that hard, it does print out the attribute on the page. This is really making me feel stoopid but I just don't see what's wrong.

Can anyone help me on this? I'm really stumped...


Here's the code:
 <!-- Loop through all products /-->
<?php
  foreach ($productArray as $product) {
?>
     <tr class="<?php echo $product['rowClass']; ?>">
       <td class="cartQuantity">
<?php
  if ($product['flagShowFixedQuantity']) {
    echo $product['showFixedQuantityAmount'] . '<br /><span class="alert bold">' . $product['flagStockCheck'] . '</span><br /><br />' . $product['showMinUnits'];
  } else {
    echo $product['quantityField'] . '<br /><span class="alert bold">' . $product['flagStockCheck'] . '</span><br /><br />' . $product['showMinUnits'];
  }
?>
       </td>
       <td class="cartQuantityUpdate">
<?php
  if ($product['buttonUpdate'] == '') {
    echo '' ;
  } else {
    echo $product['buttonUpdate'];
  }
?>
       </td>
       <td class="cartProductDisplay">
<a href="<?php echo $product['linkProductsName']; ?>"><span id="cartImage" class="back"><?php echo $product['productsImage']; ?></span><span id="cartProdTitle"><?php echo $product['productsName'] . '<span class="alert bold">' . $product['flagStockCheck'] . '</span>'; ?></span></a>
<br class="clearBoth" />

<?php
  echo $product['attributeHiddenField'];
  if (isset($product['attributes']) && is_array($product['attributes'])) {
  echo '<div class="cartAttribsList">';
  echo '<ul>';
    reset($product['attributes']);
    foreach ($product['attributes'] as $option => $value) {
?>

<li><?php echo $value['products_options_name'] . TEXT_OPTION_DIVIDER . nl2br($value['products_options_values_name']); ?></li>

<?php
    }
  echo '</ul>';
  echo '</div>';
  }
?>
       </td>
       <td class="cartUnitDisplay"><?php /*?><?php echo $product['productsPriceEach']; ?><?php */?></td>
       <td class="cartTotalDisplay"><?php /*?><?php echo $product['productsPrice']; ?><?php */?></td>
       <td class="cartRemoveItemDisplay">
<?php
  if ($product['buttonDelete']) {
?>
           <a href="<?php echo zen_href_link(FILENAME_SHOPPING_CART, 'action=remove_product&product_id=' . $product['id']); ?>"><?php echo zen_image($template->get_template_dir(ICON_IMAGE_TRASH, DIR_WS_TEMPLATE, $current_page_base,'images/icons'). '/' . ICON_IMAGE_TRASH, ICON_TRASH_ALT); ?></a>
<?php
  }
  if ($product['checkBoxDelete'] ) {
    echo zen_draw_checkbox_field('cart_delete[]', $product['id']);
  }
?>
</td>
     </tr>

<?php

//Begin Adding of New features
//$productsimage = $product['productsImage'];
$productsname = $product['productsName'];
$attributes = $value;

$products_options_name = $value['products_options_name'];

$arr_product_list[] = "<strong>Product Name:</strong> $productsname <br />";
$arr_product_list[] .= "<strong>Attributes:</strong> $attributes <br />";
$arr_product_list[] .= "<strong>Products Options Name:</strong> $products_options_name <br />";
$arr_product_list[] .= "---------------------------------------------------------------";

//End  Adding of New features

  } // end foreach ($productArray as $product)
?>
       <!-- Finished loop through all products /-->
LVL 7
MHenryAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HackneyCabCommented:
Well, I know nothing about Zen Cart, but this question has dropped down the list without any answers, so I'll have a go.

If you're seeing 'Array' unexpectedly then I recommend you investigate the variable using var_dump($variable_name) which will spit out a nicely spaced description of the variable. For the following array variable:

$array_variable = array(
      'a string',
      array('string within a nested array', 'and another'),
      34,
      new DateTimeZone('UTC')
);

you would get the following from var_dump($array_variable):

array(4) {
  [0]=>
  string(8) "a string"
  [1]=>
  array(2) {
    [0]=>
    string(28) "string within a nested array"
    [1]=>
    string(11) "and another"
  }
  [2]=>
  int(34)
  [3]=>
  object(DateTimeZone)#1 (0) {
  }
}

Once you've seen the internals of an array, it's much easier to work out what's going on and correct your code to work with that structure.

(By the way, if you want to be able to read the output of var_dump in an HTML page, wrap it in '<pre>' and '</pre>' so that the spacing is correctly rendered on the page. Also, if you use the xdebug extension for PHP, it will do that automatically, and it adds syntax colouring to the output to make it easier to read.)
MHenryAuthor Commented:
HackneyCab,

Thanks for taking a look at this. I've been stuck on it for a couple weeks so I appreciate any help I can get.

I'm not sure which variable I should be looking at. Would it be $product['attributes']? Would that work in your example?


Here's the thing, the above code renders the product, model and attribute on the page already and they all work. I think this is the code that does it:

<?php
  echo $product['attributeHiddenField'];
  if (isset($product['attributes']) && is_array($product['attributes'])) {
  echo '<div class="cartAttribsList">';
  echo '<ul>';
    reset($product['attributes']);
    foreach ($product['attributes'] as $option => $value) {
?>



a little later in the code is where the product, model and attribute are setup to email:

//Begin Adding of New features
//$productsimage = $product['productsImage'];
$productsname = $product['productsName'];
$attributes = $value;

$products_options_name = $value['products_options_name'];

$arr_product_list[] = "<strong>Product Name:</strong> $productsname <br />";
$arr_product_list[] .= "<strong>Attributes:</strong> $attributes <br />";
$arr_product_list[] .= "<strong>Products Options Name:</strong> $products_options_name <br />";
$arr_product_list[] .= "---------------------------------------------------------------";

And only the attributes isn't working. I was wondering if it has to do with that reset in the first snippet. I've never seen that one before.

Thanks,
MH
HackneyCabCommented:
Ah, I see. So your output says "Attributes: Array"?

Try changing your code to the following, to see if you get a list of attribute name, attribute value pairs:

$arr_product_list[] = "<strong>Product Name:</strong> $productsname <br />";
$arr_product_list[] .= "<strong>Attributes:</strong>";
foreach ($product['attributes'] as $option => $value) {
    echo $value['products_options_name'] . TEXT_OPTION_DIVIDER . nl2br($value['products_options_values_name']).", ";
}
$arr_product_list[] .= <br />";
$arr_product_list[] .= "<strong>Products Options Name:</strong> $products_options_name <br />";
$arr_product_list[] .= "---------------------------------------------------------------";

You will end up with a hanging comma at the end of the attribute list, but so long as you see the attributes as needed, we can finesse that later.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

MHenryAuthor Commented:
Yes, exactly. The output just says Attributes: Array.

The bad news is, the code you supplied is generating

500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.


MHenryAuthor Commented:
Not that I know much of what's going on, but this
echo $value['products_options_name'] . TEXT_OPTION_DIVIDER . nl2br($value['products_options_values_name']).", ";

looks like it's related to the third line down in your code:
$arr_product_list[] .= "<strong>Products Options Name:</strong> $products_options_name <br />";

the Products option name is working, it's the array that's hosed.

Best,
MH
HackneyCabCommented:
Sorry, I did intend to change echo to a variable append (in fact, I thought I had), but I meant this:

$arr_product_list[] = "<strong>Product Name:</strong> $productsname <br />";
$att_list = '';
foreach ($product['attributes'] as $option => $value) {
    $att_list .= $value['products_options_name'] . TEXT_OPTION_DIVIDER . nl2br($value['products_options_values_name']).", ";
}
$arr_product_list[] .= "<strong>Attributes:</strong>".$att_list;
$arr_product_list[] .= <br />";
$arr_product_list[] .= "<strong>Products Options Name:</strong> $products_options_name <br />";
$arr_product_list[] .= "---------------------------------------------------------------";

Try that. I can't see anything in there which would cause a server error, but if you do get a 500 code, check your Apache error log file.
MHenryAuthor Commented:
That still throws the 500 error.

I think the problem is the attributes and the product options are two different things. The product option is what generates the Model #. The attributes is what further identifies the product.

By the way, what's Apache? ;) This is on a windows server. Which is prolly about half the problem...
MHenryAuthor Commented:
The only error in my log file is:
[25-Apr-2010 13:12:06] PHP Warning:  PHP Startup: Unable to load dynamic library 'C:\Program Files\PHP\ext\php_mssql.dll' - The specified module could not be found.
 in Unknown on line 0
HackneyCabCommented:
Apache is a web server (most PHP developers use it, so I wrongly presumed it was the software you were running).

Which web server are you using? You need to find the error log for the web server and find out any details you can about the 500 "internal server error" code that it's generating.

I really can't think of anything in the PHP code we're discussing that would cause the web server to choke like this.
MHenryAuthor Commented:
I was just kidding about the Apache thing. I know what Apache is.

This is on a windows server.

And the only error I get is what I pasted above:
[25-Apr-2010 13:12:06] PHP Warning:  PHP Startup: Unable to load dynamic library 'C:\Program Files\PHP\ext\php_mssql.dll' - The specified module could not be found.
 in Unknown on line 0

MHenryAuthor Commented:
Maybe I'm pasting your code in the wrong area?

I'm pasting it over this:
$products_options_name = $value['products_options_name'];

$arr_product_list[] = "<strong>Product Name:</strong> $productsname <br />";
$arr_product_list[] .= "<strong>Attributes:</strong> $attributes <br />";
$arr_product_list[] .= "<strong>Products Options Name:</strong> $products_options_name <br />";
$arr_product_list[] .= "---------------------------------------------------------------";


is that right?
MHenryAuthor Commented:
Hey, got it working!

All I had to do was replace

$attributes = $$product['sttributes'];

with

$attributes = $value['products_options_values_name'];

I'm giving you the points though. Without your help it never would have occured to me that the attributes was actually products_options_values_name.

Thanks so much for your help!

Best,
MH

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.