Magento: How do I separate the display of multiple attributes? - php

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

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>";
?>

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: http://php.net/manual/en/function.nl2br.php
$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.

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>

Related

Adding a Prefix to a Value to make a link

I'm returning a tracking number from a database and would like to make it a clickable link.
<td><?php echo $row['tracking_number']; ?></td>
PREFIX is "http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums="
http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums=TRACKINGNUMBER
Use below the line for this requirement
echo "http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums="
. $row['tracking_number'];
$link='http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums='
$link.=$row['tracking_number'];
Building on the comment by marv255:
<td>
<?php
$link_href = 'http://wwwapps.ups.com/WebTracking/track?'
. http_build_query(['track' => 'yes', 'trackNums' => $$row['tracking_number']]);
echo '' . $row['tracking_number'] . '';
?>
</td>
If $row['tracking_number'] is a string no further alteration is needed, but if it is an array of tracking numbers then $tracking_numbers = implode(',', $row['tracking_number']); could be inserted and $tracking_numbers would replace the usages of $row['tracking_number']);.

creating a space before a variable including htmlencode str_repeat

I have a variable given to me by my cms
I want to add one space before that but have tried many things lol and all dont work, below is what I have tried so far.
<?php echo str_repeat('', 1) htmlencode('$postcode_coveringsRecord['postcode']) ?>
<?php echo htmlencode('&nbsp$postcode_coveringsRecord['postcode']) ?>
<?php echo htmlencode('. $postcode_coveringsRecord['postcode'].) ?>
<?php echo '&nbsp''htmlencode('$postcode_coveringsRecord['postcode'])' ?>
<?php echo htmlencode('&nbsp''$postcode_coveringsRecord['postcode']) ?>
How can I minipulate
where as the variable gives me one blank space prior to the variable content.
cheers for any input
emma
These ones should work
echo ' '.htmlencode($postcode_coveringsRecord['postcode']);
or
echo ' '.htmlencode($postcode_coveringsRecord['postcode']);

Echoing PHP within HTML

I am currently trying to create a shopping cart for my website and I have images of products stored in a database and I want to include them within <img src> . By putting $get_row[imagesrc] within the src. I need to know the correct way to add it to the below code as I dont fully understand the ' and . tags
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/>'.$get_row['imagesrc'].
'<br/>£'.number_format($get_row['price'],2).'Add</p>';
This should achieve what you're looking for:
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'" /><br/>£'.number_format($get_row['price'],2).'Add</p>';
The ' character defines a string literal when it is wrapped around a series of characters.
The . character is used for concatenating strings for output or storage.
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'"><br/>£'.number_format($get_row['price'],2).'Add</p>';
. concatenates two strings, and ' is wrapped around a string.
so
echo 'Hello '.'World'; // Shows Hello World
I'd split yours up to make it easier to read:
echo '<p>';
echo $get_row['name'].'<br/>';
echo $get_row['description'].'<br/>';
echo '<img src="'.$get_row['imagesrc'].'" /><br/>';
echo '£'.number_format($get_row['price'],2);
echo 'Add';
echo '</p>';
But it all looks OK.
echo '<p>'.$get_row['name'].'<br/>
<img src="'.$get_row['imagesrc'].'" alt="'.$get_row['name'].'"><br/>
<br/>£'.number_format($get_row['price'],2).'
Add</p>';`
echo '<img src="'.$get_row['imagesrc'].'">';
Try that.
A specific answer has been given:
echo '<img src="'.$get_row['imagesrc'].'">';
Nonetheless, it's worth adding that you should:
You should escape output - with htmlspecialchars() or otherwise.
echo '<img src="' . htmlspecialchars($get_row['imagesrc']) . '">';
Read the documentation on PHP Strings.
Check out this way of including PHP in your HTML. It's much easier to read and maintain. The last line in the paragraph is your image tag.
<p>
<?php echo $get_row['name']; ?><br/>
<?php echo $get_row['description']; ?><br/>
<?php echo $get_row['imagesrc']; ?><br/>
£<?php echo number_format($get_row['price'],2); ?>
Add
<img src="<?php echo $get_row['imagesrc']; ?>" />
</p>

What's the meaning of this syntax ?> <?php

<?php
for($i=0;$i<=100;$i++)
{
?> // why this
<tr>
<td> <?php echo $i; ?></td>
<td><?php echo "tên sách $i"; ?></td>
<td><?php echo "noi dung sach $i";?></td>
</tr>
<?php
}
?>
So that is the scenario I'm looking to understand. Thanks
The <?php opens a php script sequence so it is saying that inside this is php code. ?> closes that sequence and says that I am not longer using php code.
In your case the php opens up and starts a for loop. Inside of the for loop a table is made but it is done using html not php. Then in each table piece, php is being used to echo (write something to the screen) some content into the table. Then finally at the end the php for loop must be finished with a closed bracket. I hope that makes sense.
The meaning of that syntax, is essentially telling the server to stop processing PHP. What follows in the page, is HTML code. That is <?php tells the server process this as PHP script and the ?> says stop processing PHP script.
Normally, you'll not see HTML outputted in this manner, but instead using PHP's echo to write the HTML
<?php
for($i=0;$i<=100;$i++)
{
echo "
<tr>
<td>{$i}</td>
<td>\"tên sách {$i}\"</td>
<td>\"noi dung sach {$i}\"</td>
</tr>
";
}
?>
?> is the end tag of PHP much like </td> is an end tag of a table cell.
The PHP parser lets you enable and disable parsing by including the PHP start and end tags <?php and ?> in your document. These two samples have the same output:
One
-------------------
echo '<td>' . $foo . '</td>';
Two
-------------------
?>
<td><?php echo $foo; ?></td>
<?php
As far as which is easier to read, well, that's a matter of preference I suppose.
See also: the shortcut tag <?= for echoing a value.
You can come in and out of php execution any time you like. If text is not between php tags, it will be output rather than executed.
<?php // start executing
if($test){ ?> <!-- start a php if statement -->
<p>This will only be output if the php test is true</p>
<?php } ?> <!-- don't forget to close the if statement -->
<p>This will always be output</p>
PHP allows you to min PHP code and HTML markup in the same file, so it needs a way to tell them apart.
If you don't enclose your PHP code between <?php and ?>, it won't be processed, and will be output as HTML.
You should read a minimum of documentation before start a project (and ask basic questions).
First part (l.1 to 4) is PHP code, here you start a for loop.
Second part (l.5 to 9) is HTML code, which is include in the loop.
Third and last part is here to close the loop.
You can do the same with :
<?php
for($i=0;$i<=100;$i++)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>tên sách".$i."</td>";
echo "<td>noi dung sach".$i."</td>";
echo "</tr>";
}
?>
This is a way to embed the HTML code without having to use the php print or echo function. using
?>
Another way to get at the same result would be this:
";
echo " $i ";
echo " tên sách $i ";
echo " noi dung sach $i ";
echo "";
}
?>

PHP Symfony Convert Propel Choice to String

I am using a propel form to allow a user to create an account, the form has two parts; the entering and then the preview.
On my preview page I declare the values of the form as usual and these are provided by the previous form
public function configure() {
//Preview page no fields are displayed anyway xD
$this->useFields(array('email', 'user_gender_id', 'search_gender_id', 'content', 'age', 'location'));
But instead of outputting the fields I am trying to render the values on the page:
<?php echo $form->renderHiddenFields(); ?>
<?php foreach($form as $field): ?>
<?php if(!$field->isHidden()): ?>
<tr>
<th><?php echo $field->renderLabel() ?></th>
<td><?php echo $field->getValue(); ?></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
<?php else: ?>
<?php echo $form; ?>
<?php endif; ?>
Unfortunately for my sfWidgetFormPropelChoice / sfWidgetFormChoice fields it just outputs the chosen ID rather than a string representation of it.
Is there a proper way in Symfony to output the text representation of a widget's value? Or do I have to hack something together? (any ideas?)
Many thanks,
Pez,
Try:
$obj = $form->getObject();
to have the values of your object. Then:
echo $obj[$field->getName()];
this can work in most cases. But, if it won't, you'll have to write all the preview fields by hand and display them using the $obj above.
I got around this problem by doing the following:
<td><?php if(method_exists($field->getWidget(), "getChoices")) {
$choices = $field->getWidget()->getChoices();
echo $choices[$field->getValue()];
} else
echo $field->getValue();
?></td>
I hope this is of use to someone as it has been driving me crazy!

Categories