I've got this problem that I can't solve. Partly because I can't explain it with the right terms. I'm new to this so sorry for this clumsy question.
Below you can see an overview of my goal.
the following code is displaying all dropdown values but it should execute when i have the dropdown value is one
<?php
$product = Mage::getModel("catalog/product")->load($this->getProduct()->getId()); //product id
$i = 1;
foreach ($product->getOptions() as $o) {
$values = $o->getValues();
foreach ($values as $v) {
print_r($v->getTitle());
echo "<br/>";
}
$i++;
}
?>
Note : So for that i want to get dropdown size for custom options in product page.
And I am using Magento CE1.7.0.2
Any Ideas ?
<?php
$product = Mage::getModel("catalog/product")->load($this->getProduct()->getId()); //product id
$j = 0;
foreach ($product->getOptions() as $_option) {
$values = $_option->getValues();
foreach ($values as $v) {
$j++;
echo $v->getTitle(); // Displaying Dropdown values
echo "<br />";
}
}
echo $j;
?>
Here $j is displaying the size of dropdown(custom options).
I hope it will helpful for some one
Thanks :)
[23/08/13 10:50:21 AM] $i value will be size of drop down
[23/08/13 10:50:49 AM] and we can also use count function of magento when get all options
[23/08/13 10:51:13 AM] that will give exact number how much values in DD
you can get the value with attribute like
<?php
$_product = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
?>
<?php foreach($_attributes as $_attribute): ?>
<select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select">-->
<option><?php echo $this->__('Choose an Option...') ?></option>
</select>
<?php endforeach; ?>
hope this will sure help you.
Hello check below code may be help you
$product = Mage::getModel('catalog/product')->load($this->getProduct()->getId());
echo $optionsArr = count($product->getOptions());
Related
I'm using Wordpress and Custom Content Types Manager, how do I go about editing the code below to only grab the first image from the array below?
<?php
$array_of_images = get_custom_field('slide_images:to_array');
foreach ($array_of_images as $img_id) {
?>
<div><?php print CCTM::filter($img_id, 'to_image_tag'); ?> </div>
<?php } ?>
I tried adding in array_slice($array_of_images, 0, 1); but no luck so far. Thanks!
If all else fails you could do the same as what you have except add an $i value. It's kind of dumb but it would work if you can't get a normal method to work. This would be a last ditch effort sort of thing...
<?php
$array_of_images = get_custom_field('slide_images:to_array');
$i = 0;
foreach ($array_of_images as $img_id) { ?>
<div><?php print CCTM::filter($img_id, 'to_image_tag'); ?> </div>
<?php if($i == 0) break; } ?>
$key = array_keys($array_of_images);
$value = $array_of_images[$key[0]];
I have a section of php code where I need for the $_POST index to increment by one each time it goes through the "do while" loop. In other words, the first occurrence would be $_POST['tableserver1'], then the next one would be $_POST['tableserver2'], etc. This loops 6 times and then stops.
I have tried using variables as the index, trying to get them to increment. I found an example in the php manual http://php.net/manual/en/language.operators.increment.php which increments the number at the end of a string, but I'm not having any luck with getting it to work inside the $_POST index.
This section of code creates a set of 6 select lists that contain names from the database. I am trying to get the select lists to populate from the $_POST value if it is set, other wise it is zero.
Here is my code:
<?php
$x = 1;
do {
?>
<blockquote>
<p><?php echo $x . "."; ?>
<select name="tableserver<?php echo $x; ?>" id="tableserver<?php echo $x; ?>">
<option selected value="0" <?php
if (!(strcmp(0, '$tableserver.$x'))) {
echo "selected=\"selected\"";
}
?>>Select Server</option>
<?php
do {
if (strpos($row_getnamesRS['service'], '22') !== false) {
?>
<option value="<?php echo $row_getnamesRS['memberID'] ?>" <?php
if (!(strcmp($row_getnamesRS['memberID'], '$tableserver.$x'))) {
echo "selected=\"selected\"";
}
?>><?php
echo ucfirst(strtolower($row_getnamesRS['first_name']))
. " " . ucfirst(strtolower($row_getnamesRS['last_name']))
?></option>
<?php
}
} while ($row_getnamesRS = mysqli_fetch_assoc($getnamesRS));
$rows = mysqli_num_rows($getnamesRS);
if ($rows > 0) {
mysqli_data_seek($getnamesRS, 0);
$row_getnamesRS = mysqli_fetch_assoc($getnamesRS);
}
?>
</select>
</p>
</blockquote>
<?php
$x++;
} while ($x <= 6);
?>
$i=0;
do{
echo $_POST['someval'.$i];
}while(++$i < 6)
Perhaps like this? ...
$arr = [];
for ($i = 1; $i <= 6; $i++)
array_push($arr, $_POST["tableserver" . $i]);
$arr; // Contains 6 values (starting from $_POST["tableserver1"] to $_POST["tableserver6"])
It would be easier to post an an array
so instead of
name="tableserver<?php echo $x; ?>"
use
name="tableserver[]";
the you can just do
foreach($_POST['tableserver'] as $tableServer){....}
So i have a CMS which uses a foreach loop to generate the navigation which consists of individual list items inside a ul.
Basically what I want is to have my logo inserted in the center of these links, with an equal number of links on either side.
I've got my code to split up into two different columns of navigation with a gap, but I cant figure out where to place the logo div so it doesn't repeat more than once, current code also throws out some empty list items which I don't need.
<ul>
<?php $i = 0; foreach($items as $item): ?>
<li><a<?php ecco($item->isOpen(), ' class="active"') ?> href="<?php echo $item->url() ?>"><?php echo html($item->title());?></a></li>
<?php if (++$i % 3 === 0 && $i !== count($items)) echo "</li><li>"; endforeach ?>
</ul>
Thanks.
How about something like this?
UPDATE
Since your count doesn't work, try this:
<ul>
<?php
$j = 0;
$breakPoint = 0;
foreach($items as $item) {
$j++;
$breakPoint = $j;
}
$i = 0;
foreach($items as $item) { ?>
if ($i === $breakPoint) {
/* insert Logo Code */
} else { ?>
<li><a<?php ecco($item->isOpen(), ' class="active"') ?> href="<?php echo $item->url() ?>"><?php echo html($item->title());?></a></li>
<?php
}
$i++;
} ?>
</ul>
Haven't tested it. And it depends if you have a odd or even number of $item elements.
I'm trying to integrate a tracking pixel into the magento success page. For testing I build the following code and implemented it into line > 45 in success.phtml file within the template folder. Actually the variables are all empty. What's wrong?
<?php
<?php
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order');
$order->load($lastOrderId);
$skus = array();
$qtys = array();
$amounts = array();
foreach ($order->getAllItems() as $item){
$skus[$item->getProductId()] = $item->getSku();
$names[$item->getProductId()] = $item->getName();
$qtys[$item->getProductId()] = $item->getQtyOrdered() * 1;
$amounts[$item->getProductId()] = $item->getRowTotal() * 100;//or $item->getPrice()*$item->getQtyOrdered();//but this will ignore any applied coupons
}
$skuspipe = implode("|", $skus);
$namespipe = implode("|", $names);
$qtyspipe = implode("|", $qtys);
$amountspipe = implode("|", $amounts);
<!--
OrderID: <?php echo $orderID; ?>
skus: <?php print_r($skus); ?>
names: <?php print_r($names); ?>
qtys: <?php print_r($qtys); ?>
amounts: <?php print_r($amounts); ?>
skupipe: <?php echo $skupipe; ?>
namespipe: <?php echo $namespipe; ?>
qtyspipe: <?php echo $qtyspipe; ?>
amountspipe: <?php echo $amountspipe; ?>
-->
Thank you!
In Collections, Magento often only loads kind of a stub of data for each item.
You could load the whole objects by using
$item->load( $item->getId() );
on each iteration.
Also, try to debug output the collection first to see if there are any items found.
I have two foreach loops. The first grabs a load of questions from Wordpress, the second is supposed to grab the multiple answers. This is straight forward had it not involved some randomisation of the questions, which makes it confusing.
This is the two foreach loops without them being randomised.
<?php
$repeater = get_field('step_by_step_test');
foreach( $repeater as $repeater_row ){ ?>
<p><?php echo $repeater_row['question']; ?></p>
<?php $rows = $repeater_row['answer_options'];
foreach ($rows as $row){ ?>
<?php echo $row['answer']; ?><br />
<?php } ?>
<?php } ?>
This loops through each question and also grabs the multiple answers.
How can I incorporate it randomising the questions? This is my attempt, this works for getting a random set of questions but I'm getting an error for the answers part (invalid argument supplied for foreach).
<?php
$amount = get_field('select_number_of_questions');
$repeater = get_field('step_by_step_test');
$random_rows = array_rand( $repeater, $amount );
echo implode(', ', $random_rows);
foreach( $random_rows as $repeater_row ){ ?>
<p><?php echo $repeater[$repeater_row]['question']; ?></p>
<?php $rows = get_sub_field('answer_options');
foreach ($rows as $row){ ?>
<?php echo $row['answer']; ?><br />
<?php } ?>
<?php } ?>
I use this plugin for wordpress - http://www.advancedcustomfields.com/
First I'm going to rewrite your first code block to not look like chewed cud.
<?php
$repeater = get_field("step_by_step_test");
foreach($repeater as $repeater_row) {
echo "<p>".$repeater_row['question']."</p>";
$rows = $repeater_row['answer_options'];
foreach($rows as $row) {
echo $row['answer']."<br />";
}
}
?>
And now for the magic: Add shuffle($rows) immediately before the foreach($rows as $row) { line, and the answers will appear in random order.
EDIT in response to comments: Start your code like this:
$repeater = get_field("step_by_step_test");
shuffle($repeater);
$repeater_limit = array_slice($repeater,0,5);
foreach($repeater_limit as $repeater_row) {
....