statmentI am currently working on a project. I am having issues with making a variable ++ in the loop while nested in if statements. I really do not understand why it's doing what it's doing. Here is my code.
<?php $i=0;
while ($loop->have_posts() ) : $loop->the_post(); ?>
<?php if(get_field('featured_game') && valid_release(get_field('release_date'))):?>
<div class="item <?php if($i == 0){echo 'active';}?> row carousel-<?=$i;?>">
// do generated html
<style>
.carousel-<?=$i;?>{
// do generated css
}
</style>
</div>
<?php $i++;?>
<?php endif?>
<?php endwhile;?>
</div>
My results have
carousel-
these spots all having returned 0 despite the loop running. meaning that incrment is not happening.
if I remove the if statement the loop works, as expected. The if statement are just to verify criteria to be true. I do can place the ++ outside of the if statement I get the same results.
Could some please explain what I am missing here? This seems like such a basic problem. I really do not understand why $i will not increment when does meet the criteria and posts the correct info. Thanks in advance to anyone who takes the effort.
I ran this code:
<?php $i = 0;
while ($i < 10) : ?>
<?php if (true): ?>
<?php if ($i == 0) {
echo 'active';
} ?>
<?= $i; ?>
<?php $i++; ?>
<?php endif ?>
<?php endwhile; ?>
and the result was:
active 0 1 2 3 4 5 6 7 8 9
so either
get_field('featured_game')
or
valid_release(get_field('release_date'))
must be false.
Try
var_dump(get_field('featured_game'));
var_dump(valid_release(get_field('release_date')));
inside the loop.
Turns out, it was increment properly all along.
My issue turns out to be that bootstraps carousel cycles the the content within the the parent div not the actual div it seems. If I disable it, or look directly at the source, everything is what it is supposed to be.
Related
I am trying to set a condition for php- mysql pagination so that it can change the current page "li" to "li class="active" " to mark the current selected page. I am new to php and searching for such pagination tutorial but no luck.
I have done so far what is working but not able to mark selected page. Here $id is for detecting the current page id. How can I set if condition ( or other) so that I can mark the current page item in the pagination? Thousands thanks for helping me.
<ul class="pagination">
<?php if($id > 1) {?> <li>Previous</li><?php }?>
<?php
for($i=1;$i <= $page;$i++){
?>
<?php
if ($id>1)
{ ?>
<li class="active"><?php echo $i;?></li>
<?php }
?>
<!-- <li><?php echo $i;?></li> -->
<?php
}
?>
<?php if($id!=$page)
//3!=4
{?>
<li>Next</li>
<?php }?>
</ul>
You could change your for loop from
<?php
for($i=1;$i <= $page;$i++){
?>
<?php
if ($id>1)
{ ?>
<li class="active"><?php echo $i;?></li>
<?php }
?>
<!-- <li><?php echo $i;?></li> -->
<?php
}
?>
to:
<?php
for($i=1;$i <= $page;$i++){
$class=($i==$id)? ' class="active"' : '';
echo '<li'.$class.'>'.$i.'</li>';
}
?>
If I've understood your code properly, $page represents total pages and $id represents the current page, this will set the current page number as the active class and leave the other pages without the class
Assuming that $id is the current page number, and that $page is the total number of pages, you need to highlight only one by doing the following in your loop:
if($i==$id) // highlight
else // don’t highlight
Your main errors are that you didn’t test whether $i==$id, and you didn’t have an alternative unhighlighted version.
I really think that you should simplify your code by separating your logic from the HTML. It becomes very unreadable otherwise, and very hard to manage.
I have taken the liberty of doing just that. This way you can see where the logic does the hard work, an you only need to print the results in the HTML.
<?php
$id=3; // test value
$page=20; // test value
$li=array(); // temporary array for convenience
$template='<li%s>%s</li>';
if($id>1) $li[]=sprintf($template,'',$id-1,'Previous');
for($i=1;$i<=$page;$i++) {
if($i==$id) $li[]=sprintf($template,' class="active"',$i,$i); // Current
else $li[]=sprintf($template,'',$i,$i);
}
if($id<$page) $li[]=sprintf($template,'',$id+1,'Next');
$li=implode('',$li); // convert to string for printing
?>
<ul class="pagination">
<?php print $li; ?>
</ul>
You will also see two techniques to make things easier:
I use an array as a temporary container. It is easier to add items this way and then to implode it into a string afterwards
sprintf makes it easier to define a template string, so you can manage the HTML component more easily, and fill in the gaps when the time comes.
My count loop currently works but it is not working correctly. I need every second item to go into the right column and the rest stay in the left. So 1, 3, 5 etc in the column called split-left and 2, 4, 6 to go into the column called split-right
<!-- SPLIT EFFECT PAGE BUILDER -->
<div class="page-builder">
<?php if( have_rows('split_effect_page_builder') ): ?>
<div class="split-left">
<?php $i = 1; ?>
<?php while ( have_rows('split_effect_page_builder') ) : the_row(); ?>
<?php get_template_part('template-parts/page', 'builder'); ?>
<?php
if($i % 2 == 0){
echo '</div><div class="split-right">';
$i = 0;
}
$i++;
?>
<?php endwhile; ?>
</div>
<?php else : ?>
<?php // no layouts found ?>
<?php endif; ?>
</div>
<!-- END SPLIT EFFECT PAGE BUILDER -->
Seems you just need to reset your counter whenever it reaches 2..
<?php
if($i == 2){
echo '</div><div class="split-right">';
$i =0; // set back to zero...
}
$i++; // and now it's 1 again so next iteration would be left aligned
?>
PS.. not sure why you close a PHP tag only to immediately re-open it.. That won't break anything, but probably a good habit to get rid of.
I am new to php and I am trying to create a simple bit of code that prints a "last" class at the start of the last element in a "while" loop. There are only two items in the loop (blog excerpts) hence why I have tried below with the if ($i == 1)... Thanks for any help.
Here is my code so far - which only returns the p
<?php
$i = 0;
if($i == 1) {
echo '<p class="last">';
}
else {
echo '<p>';
}
?>
EDIT:
Thanks for the help so far. Greatly appreciated - I have provided a bit more information below (I posted late at night, so I realise I haven't been all that clear).
This is the full piece of code I am trying to write. It is pulling blog excerpts from Wordpress - currently limited to 2 blog articles.
<?php
$posts = new WP_Query('showposts=2');
while ( $posts->have_posts() ) : $posts->the_post();
?>
<p><?php echo the_title(); ?><br/>
<?php echo substr(get_the_excerpt(), 0,200); ?>... Read More</p>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I am wanting to add the class "last" to the p at the start of line 5 - for the last blog except only.
Thanks again.
Nick's answer says almost all that needs to be said.
The only thing I might add is a slight variation to save duplication particularly if the the contents of your paragraph tags is more complicated.
This might be better done with the following tweak on Nick's code:
<style>
#contents p:last-child {
PUT CONTENTS OF CLASS HERE
}
</style>
<body>
<div id="#contents">
<?php
$numLoops = 2;
$ctext=""
for($i=0; $i<$numLoops; $i++) {
$info="whatever";
if($i == (numLoops-1)) {
$ctext=' class="last"';
}
echo "<p${ctext}>${info}</p>\n";
}
?>
</div>
</body>
Cheers
So you have two paragraphs, and you want to apply the class "last" to the last one? Sounds like this is better handled with CSS
<style>
#contents p:last-child {
PUT CONTENTS OF CLASS HERE
}
</style>
<body>
<div id="#contents">
<p> first info</p>
<p> last info </p>
</div>
</body>
OR if you want to learn about loops
<?php
$numLoops = 2;
for($i=0; $i<$numLoops; $i++) {
if($i == (numLoops-1)) {
echo '<p class="last">';
} else {
echo '<p>';
}
}
What we are doing here with the for loop is initially setting the variable $i=0; then setting a test that says keep looping as long as the variable is less than the number of loops we want do do. Next we are setting what to do each loop, in this case we increment our variable by one each time.
First loop
i=0, we see that 0 is < 2 so we continue
Second loop
We execute the $i++ so we increment $i by 1 from 0 to $i=1,
we test and see $i=1 is still less than 2 so we continue.
Third attempted loop
We increment $i by 1 and get $i=2.
We test to see if this is less than 2, but it is NOT so we do not execute code in the loop
The main issue is that you don't have a loop in your code, and if you did you aren't incrementing your variable $i
I am trying to detect each 4th post to insert extra code in my layout in wordpress using modulus method but I just cant get it.
Here is a short example of mine:
<?php if (have_posts()) : ?>
<?php $count=0;?>
<?php while (have_posts()) : the_post(); ?>
<div class="column">
<!--content-->
</div>
<?php
if ($count % 4 == 0){
echo '<div class="clear"></div>';
}
$count++;
?>
<?php endwhile; ?>
<?php endif; ?>
all that is inside the while loop. What am I doing wrong? Thank you.
You need to start your counter at 1, as you are increasing it at the end of the loop:
<?php $count=1;?>
Either that, or you increase it at the start of the loop / before the check:
<?php
$count++;
if ($count % 4 == 0){
echo '<div class="clear"></div>';
}
?>
When it comes to things like this, I always increment one on the if statement before calling modulo like so:
if(($count+1)%4 == 0)
This way it's easy for me to make a mental note that the statement naturally reads "if the current count is the 4th one then do:"
Hokay, so. Here's my embedded template, chilling:
<? $i=0; ?>
{exp:channel:entries
channel="products"
dynamic="no"
entry_id="{embed:ids}"
}
<? $i++; ?>
{exp:playa:parents
field_id="25"
limit="1"
}
<!-- product -->
{if no_parents}
<? $i--; ?>
{/if}
{/exp:playa:parents}
{if no_results}
No results!
{/if}
{/exp:channel:entries}
<? if ($i === 0 ) { echo 'No products found!'; } ?>
The logic I had for this $i variable was to get an accurate reading of whether any results have been output. "Result" in this sense refers to what gets output by Playa.
The exp_channel_entries's no_results test only gets triggered if {embed:ids} is empty or the embedded ids don't correspond to entries in the channel. If the entries method returns entries but none of the results have a parent entry, nothing gets output -- and I needed a way to determine this, and I thought "Hmm, PHP should be able to do this, right?"
The desired outcome is that 'No products found!' gets output when $i = 0 but for some reason, $i is always 0 regardless of what entries get spit out.
Oh, and before you ask: YES, PHP is indeed enabled. Example: Below, {embed:ids} = 41|78|79|80|81|87|106. When set to OUTPUT, the PHP tags just get printed in the source:
<? $i=0; ?>
<? $i++; ?>
<!-- product -->
<? $i++; ?>
<!-- product -->
<? $i++; ?>
<!-- product -->
<? $i++; ?>
<!-- product -->
<? $i++; ?>
<? $i--; ?>
<? $i++; ?>
<!-- product -->
<? if ($i === 0 ) { echo 'No products found!'; } ?>
If I switch PHP parsing to INPUT the tags get processed, but $i = 0 every time.
I added an echo $i; after $i=0, $i++, and $i--. With PHP set to OUTPUT, as before, the statements just get output in the page source. With PHP set to INPUT, I get this string of values: 0 1 1 1 1 1 01
So my questions to you, StackOverflow community, is:
1) Why does PHP in OUTPUT mode just output the PHP tags without processing them?
2) How can I keep count of the number of product parents being output?
I couldn't tell you why your PHP isn't being parsed when switched to output (never seen that before), but I do think there's a simpler way to do this:
{exp:query sql="SELECT child_entry_id FROM exp_playa_relationships WHERE parent_field_id = 25 AND child_entry_id IN({embed:ids})"}
{exp:playa:parents field_id="25" entry_id="{child_entry_id}" limit="1"}
<!-- product -->
{/exp:playa:parents}
{if no_results}<p>No products found!</p>{/if}
{/exp:query}
This query will only return IDs of entries which do indeed have parents. The only thing you'll have to do is change your passed ids embed from using pipes to commas.
You could also try this:
{exp:channel:entries channel="products" entry_id="0{exp:query sql="SELECT parent_entry_id FROM exp_playa_relationships WHERE parent_field_id = 25 AND child_entry_id IN({embed:ids})"}|{parent_entry_id}{/exp:query}" dynamic="no"}
<!-- product -->
{if no_results}<p>No products found!</p>{/if}
{/exp:channel:entries}
I realize that this thread is old but I'm posting just in case someone else has similar problems and can't follow Derek's awesome solution due to architectural issues.
My issues were solved with -
Adding full php start tags (with php)
Remove all braces and going with the alternative syntax
foreach(items as item):
...
endforeach;
Changing permissions on the file to 755.
Changing the group owner of the file to the default web user
Deleting all template manager entries (if that doesn't work, just edit the file on template manager itself)
Hope this helps.