Split Multiple Options in Single Attribute in Magento - php

I have multiple options in an attributes in Magento and when I call the attribute, all of the options show in a string and I would like to show them on separate lines (p tags or li's).
<?php echo $this->getChildHtml('spamodel') ?>
The above is my code, I think i need to use explode but I'm a newbie at php.
Thanks for any help.

<?php $spamodel = $this->getChildHtml('spamodel');
$spamodel_explode = explode(",",$spamodel);
echo $spamodel_explode[0];
?>
Here you can go with this code. you can get single the names with $spamodel_explode[0] and if you want to get all the names than you can use forloop

Here You can able to get the answer
<?php
$variable = $this->getChildHtml('spamodel')
$variable_exp = explode(",",$variable)
foreach($variable_exp as $var){
echo $var;
}
?>
In above coding you can explode based on your requirement.Just replace the ',' tag and get the results based on your requirement.

Find the below link for more details.
http://www.brandammo.co.uk/output-magento-custom-attributes-to-front-end-from-multi-select-dropdowns/

Related

preg_replace of php code

I am writing an application that will look at a single record, obtain values from about 12 flags (0 or 1), look up those flags against a status table (in MySQL) and return a variable called $status_message which is in that table.
In this table I need to have hyperlinks (working fine) but also echo some variables, i.e.
You have no bids for {{$row->_item_name}}
or
View this item now by clicking here
Now I need item name and the other example to be translated into <?php echo $row->_item_name; ?>
I have tried a preg_replace with the following:
<?php
$find = array('/{{/', '/}}/');
$replace = array('<?php echo ', ' ?>');
echo preg_replace($find, $replace, $status_message);
?>
but this is not working.
Can anyone advise how I can get the desired result and 'echo' the variable in the MySQL field?
Had a brainwave. Much simpler,
instead of $row->_item_name I just put {{itemname}} in the string. I then use the following code:
<?php
$message_buyer = str_replace('{{itemname}}', $row->_item_name , $message_buyer);
echo $message_buyer;
?>
so no need to have <?php calls in the string at all.

ACF plugin, displaying as list instead of comma separated

A plugin for wordpress called Advanced Custom Fields uses a
<?php the_field('field_name'); ?
function to display information that I have determined inside WP admin in posts.
However, it outputs it as a comma separated horizontal list (e.g. cow, milk, farm).
I want to output it like this, in an unordered list:
cow
milk
farm
How would I go by doing this?
add css class to your php call, when you creating list, make list and ul, then as cotent call fields..
For more please share your htm/php file, and css also...
In your php file, save the field in an array, and use foreach to create an li foreach string.
What kind of 'Field type' do u use ?
<?php
$arr = get_the_field('field_name');
$str = explode (",", $arr);
echo '<ul>';
foreach($str as $s){
echo '<li>'.$s.'</li>';
}
echo '</ul>';
?>
OR use this code
https://www.advancedcustomfields.com/resources/get_fields/

How to use php variable inside another one

I'm trying to find the category ID of a post and then use it in the short code below. To find the category id I'm using <?php the_category_ID(); ?>, but i'm not sure how to use the output from category id and replace it in include_categories=. i.e. i want to replace number 4 with the current category ID i got from <?php the_category_ID(); ?>.
<?php echo do_shortcode('[include_categories="4"]'); ?>
i tired doing the following, but it didn't work. Any ideas will be appreciated.
<?php echo do_shortcode('[include_categories="<?php the_category_ID(); ?>"]'); ?>
I know i can't use php inside another php code, but i'm not sure how to place the category id between the quotation.
Thanks.
A couple of things: (1) you can't have nested <?php ?> tags, (2) the_category_ID() has been deprecated since WordPress 0.71. You'll want to use get_the_category() instead.
The correct solution would be:
<?php echo do_shortcode('[include_categories="' . get_the_category() . '"]'); ?>
The dots . "concatenate" the string with the returned values of get_the_category(). You can read more about string operators in the PHP docs.
Try with -
<?php echo do_shortcode('[include_categories="'.the_category_ID().'"]'); ?>
Use this to add php verial in short code:
<?php $category=the_category_ID();
echo do_shortcode("[include_categories=$category]"); ?>

separating the output of tags

I'm trying to seperate each output individually. Is there any possible way to do this ?
<?php echo stripslashes($row['meta_keys'] = str_replace(',',' ',$row['meta_keys'])) ?>
I want to link each one to a specific thing, but it all comes out as 1 a link
I did <?php echo stripslashes($row['meta_keys'] = str_replace(',',' ',$row['meta_keys'])) ?> but again, it all came out as one link. Any simple way to do this?

Putting Something Inside a Link with PHP

So I'm basically calling and returning an entire row from a mysql table using a while loop (which is working), but I'm trying to use the data that I call inside an html link, but I can't seem to get it to work.
Ideally, eventually it will just be a list of links with each person's individual name. I can return the list fine, but I can't seem to return the list with a link.
Here is my code that I feel should be working :(
<?php
require 'db/connect.php';
$result = $con->query('SELECT distinct name FROM mytable');
while($rows = $result->fetch_assoc())
{
echo ''$rows['name']'' , "</br>";
}
?>
Any help would be greatly appreciated!
Issue might be with your string concatenation. Try following code block
echo ''.$rows['name'].'';
echo ''. $rows['name']. '' , "</br>";
You just need to use . to concatenate strings together.
try this
echo ''.$rows['name'].'' , "</br>";
Should work just fine. Basically it's '.$row['name'].'
when concatenating strings with variables you have to use dot(.) like echo "string".$var; it will be invalid to write echo "string"$var; in your example you have ignored this point.

Categories