Endif statement won't go through [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
Why's the endif; not working? Can't figure it out. I apologize beforehand because It's probably a stupid typo or something but as I said I can't figure it out.
Parse error: syntax error, unexpected 'endif' (T_ENDIF) in /Applications/XAMPP/xamppfiles/htdocs/jqueryphp/views/index.tmpl.php on line 28
<?php include "_partials/header.php"; ?>
<h1>Search actors by last name</h1>
<form action="index.php" method="post">
<select name="q" id="q">
<?php
$alphabet = str_split("abcdefghijklmnopqrstuvxyz");
foreach ( $alphabet as $letter ) {
echo "<option value='$letter'>$letter</option>";
}
?>
</select>
<button type="submit" name="button">
Go!
</button>
</form>
<?php if ( isset($actors) ) ?>
<ul class="actors_list">
<?php foreach( $actors as $a ) {
echo "<li>{$a->first_name} {$a->last_name}</li>";
}
?>
</ul>
<?php endif; ?>
<?php include "_partials/footer.php"; ?>

if you want to use the endif; syntax, instead of a { you must use a : in the if
The syntax is
if () :
else :
endif;
You can also use
foreach():
endforeach;
So corrected code is
<?php if ( isset($actors) ) : ?>
<ul class="actors_list">
<?php foreach( $actors as $a ) :
echo "<li>{$a->first_name} {$a->last_name}</li>";
endforeach;
?>
</ul>
<?php endif; ?>

Try This Please you did not put ":" to end of IF
<?php if ( isset($actors) ){ ?>
<ul class="actors_list">
<?php foreach( $actors as $a ) {
echo "<li>{$a->first_name} {$a->last_name}</li>";
}
?>
</ul>
<?php } ?>
or
<?php if ( isset($actors) ): ?>
<ul class="actors_list">
<?php foreach( $actors as $a ) :
echo "<li>{$a->first_name} {$a->last_name}</li>";
endforeach;
?>
</ul>
<?php endif; ?>

Related

"Unexpected { on line …": Can't find the cause [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
I’m going mad over this – I can’t find the reason for the "unexpected {"-error being thrown right after the last "else" (7th line from below). Does anyone see something that I don’t?
<?php
$i = 0;
foreach($bgs as $bg) { ?>
<?php $i++; ?>
<div class="item <?php if($i == '1') echo "active"; ?> img-responsive" style="background-image: url('/new/images/<?=$bg['b_url']?>')" >
<div class="metabox">
<?php if(($bg['b_weight']) != '1000') { ?>
<h1><?=$bg['w_titel']?></h1>
<p><?=$bg['w_info']?> // <?=$bg['w_jahr']?> // <?=$bg['w_ort']?><?=$bg['w_function']?></p>
<?php if (isset($_GET['w']) && (is_numeric($_GET['w']))) { ?>
<?=$bg['w_desc']?>
<?php } else { ?>
<p>More</p>
<?php } ?>
<?php } else { ?>
<h1><?=$bg['w_titel']?></h1>
<p><?=$bg['w_info']?></p>
<?php } ?>
</div>
</div>
<?php } ?>
This is very hard to read, you have my sympathies. Have you thought about using an alternative syntax for if
try using
<?php if (condition) :?> <?php do something; ?>
<?php else: do something; ?>
<?php endif; ?>
This was odd: Right before said last else statement was an invisible item (have no idea what it could have been?) – when I deleted the space between the preceding } and else, and inserted a new blank space, the code parsed fine.
Everything looks right. Try to use following syntax it will be more readable.
foreach($bgs as $bg) :
$i++;
if(1 != '1000') :
echo 3;
if ( 1 != 2) :
echo 4;
else:
echo 1;
endif;
else:
echo 2;
endif;
endforeach;
Also try to replace:
<?php if($i == '1') echo "active"; ?>
to:
<?= $i == '1' ? "active" : ''; ?>

This php if else code on my website is not working [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
<?php if (get_the_author_meta('description')) { ?>
<?php
$author_ID = get_the_author_meta('ID');
$username = get_the_author_meta('display_name', $author_ID); ?>
<div class="mm-author-box">
<figure class="mm-author-box-avatar">
<?php echo get_avatar($author_ID, 70); ?>
</figure>
<?php if ($author_ID === 4) { ?>
<div class="mm-author-name">
<?php echo $username; ?>
</div>
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
</div>
<?php } ?>
<?php else { ?>
<?php if ($author_ID === 9) { ?>
<div class="mm-author-name">
<?php echo $username; ?>
</div>
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
</div>
<?php } ?>
<?php } ?>
It is a code to display author name and hyper link it.
From the first if statement if author has a description then go inside
Second if statement: if authors ID is 4 then execute the code below if not then
Else, there is one extra div inside the else statement which is for <div class="mm-author-box"> which is outside the if statement.
The problem is that when I put this code it breaks the page.. Only the header of the website loads and the content below it doesn't because i have placed this code in the php file which is a template for the page content.
I think there is some syntax problem coz I used the code without else statement and it worked.
You missed to close a curly bracket in the end. Add below line as a last line and try :
<?php } ?>
I don't know how strict that template engine is, but this could be the problem;
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
</div>
<?php } ?>
You're closing the div inside the if which was started outside the div. So , place that last </div> below the <?php } ?> and see if that helps
<?php if (get_the_author_meta('description')) {
$author_ID = get_the_author_meta('ID');
$username = get_the_author_meta('display_name', $author_ID); ?>
<div class="mm-author-box">
<figure class="mm-author-box-avatar">
<?php echo get_avatar($author_ID, 70); ?>
</figure>
<?php if ($author_ID === 4) { ?>
<div class="mm-author-name">
<?php echo $username; ?>
</div>
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
<?php } else if ($mh_author_ID === 9) { ?>
<div class="mm-author-name">
<?php echo $username; ?>
</div>
<div class="mm-author-bio">
<?php echo get_the_author_meta('description'); ?>
</div>
<?php } ?>
</div>
<?php } ?>
And as AnkiiG pointed out, you missed a closing tag, which I fixed without knowing while formatting my answer
Your if else structure is like this,
<?php if (get_the_author_meta('description')) { ?>
<?php if ($author_ID === 4) { ?>
<?php } ?>
<?php else { ?>
<?php if ($mh_author_ID === 9) { ?>
<?php } ?>
You're not closing the first if statement

if/else with foreach and if statement inside - Parse error: syntax error, unexpected '}'

I keep getting syntax errors on this piece of code.
<?php
$day = get_field_object('day_of_the_week');
$value = $day['value'];
$choices = $day['choices'];
if( $value ): ?>
<ul>
<?php foreach( $value as $v ): ?>
<li>
<?php if( $v != $page_title ) { ?>
<a class="btn" href="/venture-centre/activities-2/<?php echo $choices[ $v ]; ?>#<?php echo basename(get_permalink()); ?>">
<?php echo $choices[ $v ]; ?>
</a>
<?php } ?>
</li>
<?php endforeach; ?>
</ul>
<?php } else { ?>
<p>This activity is only available on <?php echo $page_title ?>.</p>
<?php } ?>
<?php endif; ?>
You're mixing standard and alternative syntax, which is not permitted:
if( $value ): ?> // start alternative syntax block
<?php } else { ?> // where did this get opened?
You have a closing curly brace without an opening one.
See <?php } ?> after the </a>
You have to remove this line or add <?php { ?> after the for each
Thanks for all the comments, I've made the suggested changes and realise that I needed an elseif in place of the else to complete what I was trying to achieve:
<?php
$day = get_field_object('day_of_the_week');
$value = $day['value'];
$choices = $day['choices'];
if( count($value) > 1 ) { ?>
<ul>
<?php foreach( $value as $v ) { ?>
<li>
<?php if( $v != $page_title ) { ?>
<a class="btn" href="/venture-centre/activities-2/<?php echo $choices[ $v ]; ?>#<?php echo basename(get_permalink()); ?>">
<?php echo $choices[ $v ]; ?>
</a>
</li>
<?php } } } elseif( count($value) == 1 ) { ?>
<p>This activity is only available on <?php echo $page_title; ?>.</p>
<?php } ?>

How to create 3 blocks with 4 elements each in php?

I have the following PHP code:
<div class="search-keywords-div">
<div class="search-keywords">
<?php if ( function_exists( 'ot_get_option' ) ) { $test_input=o t_get_option( 'word'.$i ); echo $i. '. '.$test_input. ''; } ?>
</div>
</div>
I need 3 blocks of 'search-keywords-div' including 4 'search-keywords'
I am wondering how I can do this so effecient as possible. The script uses $i to get the right word from my database.
- Block1 (search-keywords-div): word1(search-keywords),
word2(search-keywords), word3(search-keywords),
word4(search-keywords)
- Block2 (search-keywords-div): word5(search-keywords),
word6(search-keywords), word7(search-keywords),
word8(search-keywords)
- Block3 (search-keywords-div): word9(search-keywords),
word10(search-keywords), word11(search-keywords), word12
I got this as an answer:
<div class="search-keywords-body">
<?php for($i=0 ;$i<3;$i++){ ?>
<div class="search-keywords-div">
<?php if($a==0 ){ $c=4 ; } else if($a==1 ){ $c=8 ; } else{ $c=1 2; } ?>
<?php while($b <=$ c){ ?>
<div class="search-keywords">
<?php if ( function_exists( 'ot_get_option' ) ) { $test_input=ot_get_option( 'word'.$b); echo $b. '. '.$test_input. ''; } ?>
</div>
<?php $b++;?>
<?php } ?>
</div>
<?php $a++;?>
<?php } ?>
</div>
Is this a good way to do realize my goal?

Remove "," at end of foreach loop

I've created a loop to list a set of meta values. I've been able to apply a class to the last item in the list, but I'd like to remove the "," at the end of the last value. Any help would be much appreciated.
<?php $count = count($subcategory); $num = 0; ?>
<?php foreach ($subcategory as $subcategory): ?>
<p
<?php if($num == $count-1){ ?>
class="subcategory-item subcategory-last-item inline-block"
<?php } ?>
class="inline-block subcategory-item"> <?php echo $subcategory;?>,</p>
<?php $num++ ?>
<?php endforeach; ?>
I may be taking an incorrect route by worrying about adding a class to the last item. If I can remove the "," from the last item I'll be happy.
Here's a quick rewrite which may lead you to a solution:
<?php $count = count($subcategories); $num = 0; ?>
<?php $classes = 'inline-block subcategory-item'; ?>
<?php foreach ($subcategories as $subcategory): ?>
<p class="<?=$classes.($num==$count-1?' subcategory-last-item':'')?>">
<?php echo $subcategory;?>
<?php if ($num<$count-1): ?>
,
<?php endif; ?>
</p>
<?php $num++ ?>
<?php endforeach; ?>

Categories