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" : ''; ?>
Related
I am trying to understand a simple / better way of coding something like this php conditional statement.
<?php if (count($foo_bar) > 1) : ?>
<div class="myDiv1">
Hello!
</div>
<?php endif; ?>
<?php if (count($foo_bar) == 1) : ?>
<div class="myDiv2">
Goodbye!
</div>
<?php endif; ?>
Looking for example, and explanation as to why it may be better. Thanks!
Quite simply in this specific situation you dont need the second if as it can be accomplished with a simple if else
<?php if (count($foo_bar) > 1) : ?>
<div class="myDiv1">
Hello!
</div>
<?php else: ?>
<div class="myDiv2">
Goodbye!
</div>
<?php endif; ?>
<?php
$class = 'myDiv2';
$msg = 'GoodBye!';
if (count($foo_bar) > 1) {
$class = 'myDiv1';
$msg = 'Hello!';
}
?>
<div class="<?php echo $class; ?>">
<?php echo $msg; ?>
</div>
Try using an elseif like below. A bit more compact than the 2 statements.
<?php if (count($foo_bar) == 1) : ?>
<div class="myDiv2">
Goodbye!
</div>
<?php elseif(count($foo_bar) > 1): ?>
<div class="myDiv1">
Hello!
</div>
<?php endif; ?>
First, you should the count function only once.
Second, reduce the duplicated HTML blocks.
<?php
$countFooBar = count($foo_bar);
if ($countFooBar == 1){
$message = 'Goodbye! !';
$cssClass = 'class1';
}elseif( $countFooBar > 1){
$message = 'Hello!'
$cssClass = 'class2';
}
?>
<div class="<?php echo $cssClass ?>">
<?php echo $message; ?>
</div>
I'm brand new to PHP and don't have the first clue about what I'm doing. I found the way to embed HTML in PHP by looking around this site.
However, I can't make this work. My code is:
<?php do { ?>
<?php if ($row_rsMore['contentID'] = 35) : ?>
<li><h4><?php echo $row_rsMore['contentTitle']; ?></h4></li>
<?php elseif ($row_rsMore['contentID'] = 37) : ?>
<li><h4><?php echo $row_rsMore['contentTitle']; ?></h4></li>
<?php elseif ($row_rsMore['contentID'] = 38) : ?>
<li><h4><?php echo $row_rsMore['contentTitle']; ?></h4></li>
<?php else : ?>
<li><h4><?php echo $row_rsMore['contentTitle']; ?></h4></li>
<?php endif ?>
<?php } while ($row_rsMore = mysql_fetch_assoc($rsMore)); ?>
I've tried surrounding the number (35, 37 and 38) with single quotes, double quotes, brackets and variations on these themes.
What happens is that the 'contentTitle' for each of the statements displays correctly but each href shows the same link (that of the first 'if' ie: 'aboutus.php').
What have I got wrong?
try
if ($row_rsMore['contentID'] == 35) :
instead of single =
change all single = to ==
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; ?>
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
I think this is a simple problem. But, I don't know how to solve this problem. I have posted my codes below. Here, In this below code if else statement always shows No Sarees. I got the result from mysql table but it always shows with No Sarees text. I have checked this using var_dump($categories) it returns array(0){} . How can I solve this problem?
<?php if ($categories) { ?>
<!--BOF Refine Search Result-->
<div class="refine-search-result">
<?php if (count($categories) <= 5) { ?>
<?php foreach ($categories as $category) { ?>
<div class="refine-block">
<p><?php echo $category['name']; ?></p>
</div>
<?php } ?>
<?php } else { ?>
<?php for ($i = 0; $i < count($categories);) { ?>
<?php $j = $i + ceil(count($categories) / 4); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($categories[$i])) { ?>
<div class="refine-block">
<p><?php echo $categories[$i]['name']; ?></p>
</div>
<?php } ?>
<?php } ?>
<?php } ?>
<?php } ?>
<div class="clear"></div>
</div><!--EOF Refine Search Result-->
<?php } else {
?>
<div class="refine-search-result">
<div class="refine-block">
<p>No Sarees</p>
</div>
<div class="clear"></div>
</div>
<?php } ?>
If your $categories is empty array then in if($categories) your array is converted to boolean value. If it's empty array (and you say it is) then it's get converted to false.
Check this:
$categories = array(); //empty array
var_dump((boolean)$categories); //this will show what variable will look like after converting to boolean value
if ($categories){
echo '$categories IS NOT empty';
}else{
echo '$categories IS empty';
}