Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 625 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Magento: How do I separate the display of multiple attributes?

#1
I am using this code to display (on the frontend) all assigned attributes of a product in Magento:

<?php if ($_product->getData('metal')): ?>
<?php echo nl2br($_product->getResource()->getAttribute('metal')->getFrontend()->getValue($_product )) ?>
<?php endif; ?>

I need to separate each item with a bullet point `•` and a break `<br/>`. How would I edit this code to reflect those changes?
Thanks in advance.

_Sam
Reply

#2
Assuming that the above code is "working"... make this change:

<?php echo "•".nl2br($_product->getResource()->getAttribute('metal')->getFrontend()->getValue($_product ))."<br/>" ?>

the `.` is a string concatenation operator.

Or are you asking how to do it if the above is the HTML of a listing of products? Then this should work...


<?php
$somehtml=nl2br($_product->getResource()->getAttribute('metal')->getFrontend()->getValue($_product ));
$somehtml=explode("<br />",$somehtml); // This ASSumes nl2br inserts <br /> between each line and this
// would lead to the behaviour you want.
// see:

[To see links please register here]

$somehtml=implode("<br/>\n•",$somehtml);
$somehtml="•".$somehtml."<br/>\n";
echo $somehtml;
unset($somehtml);
?>

`explode` makes an array from a string by chopping up the string via the `"<br />"`. `implode` then rejoins the the string by putting `<br/>\n•` between each element. And finally, I add the bullet point to the front and br at the end. When imploding, you still must consider the "outside" of the string.
Reply

#3
Solved.
It is easy: copy attributes.phtml from base if you do not have one in you default package.

Put in app/design/frontend/default/default/template/catalog/product/view/ folder

Change line 46 from:

<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>

to show separate product attributes:

<td class="data"><?php echo $_helper->productAttribute($_product, str_replace(",", "<br />", $_data['value']), $_data['code']) ?></td>


Reply

#4
If you want more control over the output (maybe you want to style the individual value), use explode();

<?php $attr = explode("," , $_helper->productAttribute($_product, $_data['value'], $_data['code']));
echo "<ul>";
?>
<?php foreach ($attr as $attrValue) {
echo "<li>" . $attrValue . "</li>";
}
echo "</ul>";
?>


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through