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) {
....
Related
I am getting only the last element value, like in my case i am trying to get value of $dwnld_name
but don't know where i am missing, so only getting download name for the last record
<?php
global $cat_id;
$dwnld_sql = "SELECT * FROM wp_dm_downloads";
$dwnld_qry = mysql_query($dwnld_sql);
while($dwnld_row = mysql_fetch_array($dwnld_qry)){
echo $link = $dwnld_row['link'];
echo $dwnld_name = $dwnld_row['name'];
$cat_id = $dwnld_row['category'];
}
?>
<?php
$sql = "SELECT * FROM wp_dm_category";
$myquery = mysql_query($sql);
echo '<ul>';
while($row = mysql_fetch_array($myquery)){
$x = $row['name'];
echo $x_id = $row['id'];
?>
<li><a href="#"><?php echo $x; ?>
<?php if($x_id == $cat_id) { ?>
<ul>
<li><?php echo $dwnld_name; ?></li>
</ul>
<?php } ?>
</a></li>
<?php echo "<br/>";
}
echo '</ul>';
?>
It is because, you are taking the values from loop in strings.
Everytime loop runs, it overwrites the values by recent values.
You can create an array and append values to that and loop through that array using foreach
Corrected Code:
<?php
global $cat_id;
$dwnld_sql = "SELECT * FROM wp_dm_downloads";
$dwnld_qry = mysql_query($dwnld_sql);
$downloads = array();
while($dwnld_row = mysql_fetch_array($dwnld_qry)){
$downloads['link'] = $dwnld_row['link'];
$downloads['dwnld_name'] = $dwnld_row['name'];
$downloads['cat_id'] = $dwnld_row['category'];
}
?>
Now, loop through $downloads array.
Note: Do not use mysql_ functions are they are deprecated and will be removed in future versions of PHP.
You need to echo $dwnld_name in the loop, or save names into array.
With this solution you overwrite $dwnld_name variable in each loop and as a result after the loop you have the last record.
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 want to run two seperate foreach loops on the one page, but only write my SQL once. For example see the below code, only the ul with the id of first runs, the second ul is empty.
<?php
$mylist = 'SELECT * FROM myTable';
$showlist = $pdo->prepare($mylist);
$showlist->execute();
?>
<ul id="first">
<?php foreach ($showlist as $rowid):;?>
<li><?php echo $rowid['id'] ?></li>
<?php endforeach; ?>
</ul>
<ul id="second">
<?php foreach ($showlist as $rowname):;?>
<li><?php echo $rowname['name'] ?></li>
<?php endforeach; ?>
</ul>
I thought renaming the as would allow me to use it again but this doesn't seem to be the case? What is the best approach here?
try:
<?php
$mylist = 'SELECT * FROM myTable';
$showlist = $pdo->prepare($mylist);
$showlist->execute();
$result = $showlist->fetchAll();
foreach ($result as $rowid) {
// do stuff
}
foreach ($result as $rowname) {
// do stuff
}
You'd need to stack your elements in an extra array first:
$mylist = 'SELECT * FROM myTable';
$showlist = $pdo->prepare($mylist);
$showlist->execute();
$rows = array();
foreach($showlist as $row) {
array_push($rows, $row);
}
echo "<ul>";
foreach($rows as $row) {
echo "<li>".$row['id']."</li>";
}
echo "</ul><ul>";
foreach($rows as $row) {
echo "<li>".$row['name']."</li>";
}
echo "</ul>";
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How can i group php foreach?i'v already tried 5 days,but still can't get it work
i don't know php foreach how it works, but I'm learning it,
Thanks to everyone's advice
original php:
<?php if(isset($this->leading) && count($this->leading)): ?>
<?php foreach($this->leading as $key=>$item): ?>
<?php
$this->item=$item;
echo $this->loadTemplate('item');
?>
<?php endforeach; ?>
<?php endif; ?>
<?php if(isset($this->primary) && count($this->primary)): ?>
<?php foreach($this->primary as $key=>$item): ?>
<?php
$this->item=$item;
echo $this->loadTemplate('item');
?>
<?php endforeach; ?>
<?php endif; ?>
<?php if(isset($this->secondary) && count($this->secondary)): ?>
<?php foreach($this->secondary as $key=>$item): ?>
<?php
$this->item=$item;
echo $this->loadTemplate('item');
?>
<?php endforeach; ?>
<?php endif; ?>
i tried
<?php if(isset($this->leading) && count($this->leading)) && (isset($this->primary) && count($this->primary)) && (isset($this->secondary) && count($this->secondary)): ?>
<!-- Leading items -->
<?php foreach (array($this->leading, $this->primary, $this->secondary) as $key=>$item) ($this->leading as $key=>$item): ?>
<?php
// Load category_item.php by default
$this->item=$item;
echo $this->loadTemplate('item');
?>
<?php endforeach; ?>
<?php endif; ?>
but not work
As always, your assistance is appreciated!
Thanks!every one:)
Thanks,Steven!
do you mean something like this..?
<?php
$arr = array();
$arr[] = $this->leading;
$arr[] = $this->primary;
$arr[] = $this->secondary;
foreach($arr as $v) {
if(is_array($v) && (count($v) > 0)) {
foreach($v as $item) {
$this->item=$item;
echo $this->loadTemplate('item');
}
}
}
?>
Define a function like:
function foobar ($tmp) {
if(isset($tmp) && count($tmp)) {
foreach ($tmp as $key => $item) {
// Load category_item.php by default
$this->item = $item;
echo $this->loadTemplate('item');
}
}
}
and call it with your data:
foobar($this->leading);
foobar($this->primary);
foobar($this->secondary);
The simplest way to handle this is to first build a list containing all the items you need, and then use just one foreach to cycle through that list.
I'd also recommend some consistancy in whether the three variables (leading, primary, secondary) are defined. If you can guarantee that they're set and are arrays - even if they're empty arrays, it can make your code as simple as this:
<?php
$items = array_merge($this->leading, $this->primary, $this->secondary);
foreach ($items as $Item) {
$this->item = $item;
$this->loadTemplate('item');
}
?>
If you really don't know whether the variables are set, and you can't refactor your code elsewhere to change this, you could do this instead:
<?php
$items = array();
if (isset($this->leading)) $items = array_merge($items, $this->leading);
if (isset($this->primary)) $items = array_merge($items, $this->primary);
if (isset($this->secondary)) $items = array_merge($items, $this->secondary);
foreach ($items as $Item) {
$this->item = $item;
$this->loadTemplate('item');
}
?>
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.