I just want the last value to be printed, but it is printing all of them:
<?php if (get_field('share_sentiment')):?>
<?php while (has_sub_field('share_sentiment')):?>
<?php if (get_sub_field('share_medium')):?>
<?php $kid = 0; ?>
<?php while (has_sub_field('share_medium')):?>
<?php
$negative += get_sub_field('medium_negative');
$positive += get_sub_field('medium_positive');
$totalMinus = ($positive - $negative) / ($positive + $negative);
$rounded = round($totalMinus, 3);
print_r($rounded);
?>
<?php endwhile;?>
<?php endif;?>
<?php endwhile;?>
<?php endif;?>
Put print_r($rounded); after the loop. And $rounded = round($totalMinus, 3); too, I guess.
Try to use
echo $variable;
instead of print_r since the language used is php?
Related
trying to embed some html inside a do statement but PHPStorm flags it as a syntax error. Here's what I tried:
<?php do : ?>
<?php while ($time !== $foo); ?>
I had a look at the syntax in the documentation for php and couldn't see an example of it being done. Is there any way I can achieve it so it looks something like;
<?php do : ?>
<tr><td><p>some words</p></td></tr>
<?php while ($time !== $foo); ?>
Your do... while syntax is invalid.
From the PHP manual do-while page :
There is just one syntax for do-while loops:
<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
?>
Applied to your code, the syntax would be :
<?php do { ?>
<tr><td><p>some words</p></td></tr>
<?php } while ($time !== $foo); ?>
You can also use a simple while loop :
<?php while ($time !== $foo) { ?>
<tr><td><p>some words</p></td></tr>
<?php } ?>
Or
<?php while ($time !== $foo) : ?>
<tr><td><p>some words</p></td></tr>
<?php endwhile; ?>
you'd better "collect" everything and print it at once. Keep in mind to set the "stop the loop" condition sometime in the loop itself to avoid an infinite loop.
<?php
$html_out='';
do {
$html_out.='<tr><td><p>some words</p></td></tr>';
} while ($time !== $foo);
echo $html_out;
?>
There is no alternate syntax for do-while but you can do this
<?php while($time !== $foo): ?>
<tr><td><p>some words</p></td></tr>
<?php endwhile; ?>
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 got below code
<?php foreach ( $resultsb as $optionb ) : ?>
<option value="<?php echo $optionb->bID; ?>"><?php echo $optionb->book;?></option>
<?php endforeach; ?>
i know how the foreach is working here but i didn't get the : after the php statement what does the : do after the foreach statement
<?php foreach($resultsb as $optionb) : ?>
...
<?php endforeach; ?>
is nothing else then:
<?php foreach($resultsb as $optionb) { ?>
...
<?php } ?>
IMO it enhances the readability in files with a lot of html/xml etc.
Try like This
$a = array(1, 2, 3, 17);
foreach ($a as $v) {
echo "Current value of \$a: $v.\n";
}
Your code is true. You must check your array. Error not related to ":". It related to your $resultsb.
A begginers question. I have this little code:
<?php
$content = 'hello there, hello!';
echo substr_count("$content","hello");
?>
How could i replace 'hello there, hello!' part with another php enclosure, like
<?php the_content(); ?>
for example. What is the correct way of doing this? Thanks!
<?php
echo substr_count(the_content(),"hello");
?>
This:
<?php
$a = 10;
?>
<?php
$b = 20;
?>
<?php
echo $a + $b;
?>
--output:--
30
is equivalent to:
<?php
$a = 10;
$b = 20;
echo $a + $b;
?>
If the_content does not return a meaningful value but rather echos things out, use output buffering:
ob_start();
the_content();
$captured_content = ob_get_clean();
echo substr_count($captured_content, "hello");
$content = the_content();
echo substr_count($content,"hello");
or
echo substr_count(the_content(),"hello");
It is really the basics, try to look at some tutorials ;)
I get some links from feed with this code on simplepie :
if ($check) :
foreach ($feed->get_items(0,3) as $item):
$links = $item->get_permalink();
echo $links;
endforeach; endif;
thats result me:
http://link1....
http://link2....
http://link3....
i want to put each link in separate variable like :
$links1 = 'http://link1....';
$links1 = 'http://link2....';
$links1 = 'http://link3....';
thanks , mori
$i = 0;
foreach($feed->get_items(0,3) as $item)) {
${'link' . ++$i} = $item->get_permalink();
}
Maybe what you want is array variable?
try this:
if ($check) :
foreach ($feed->get_items(0,3) as $item):
$links[] = $item->get_permalink();
endforeach; endif;
if ($check) :
$i=0;
foreach ($feed->get_items(0,3) as $item):
$links.$i = $item->get_permalink();
echo $links.$i;
$i++;
endforeach; endif;
I think it may works...