<?php if($this->item->params->get('itemExtraFields') && count($this->item->extra_fields)): ?>
<!-- Item extra fields -->
<div class="itemExtraFields">
<h3><?php echo JText::_('Additional Info'); ?></h3>
<ul>
<?php foreach ($this->item->extra_fields as $key=>$extraField):?>
<?php $user =& JFactory::getUser(); ?>
<strong><?php if($extraField->name == "Price" && $user->get('guest') ==1) { ?></strong>
<?php else: ?>
<li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsLabel"><?php echo $extraField->name; ?>:</span>
<span class="itemExtraFieldsValue"><?php echo ($extraField->type=='date')?JHTML::_('date', $extraField->value, JText::_('K2_DATE_FORMAT_LC')):$extraField->value; ?></span>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<div class="clr"></div>
</div>
<?php endif; ?>
What seems to be the problem here? It's giving back an error of Parse error: syntax error, unexpected T_ELSE in.
This is for K2 Extrafield Visibility
You're mixing the if() : and if() { syntaxes. There's one if() { around the middle when everything else uses :.
Debugging tip: The error is very often in one line before the line that the error message points to.
You're mixing up different ways of using if-else statements. In the middle of that code, you open an if statement with a {, and then use else:. You also never close that if.
Change this -
<?php if($extraField->name == "Price" && $user->get('guest') ==1) { ?>
to this -
<?php if($extraField->name == "Price" && $user->get('guest') ==1): ?>
Related
I am trying to make a basic todolist but i want to display a list with task undone at the top and the done at the bottom.
Every time i try i have a blank page
<h1 class="header">To do.</h1>
<?php if (!empty($items)): ?>
<ul class="items">
<?php foreach ($items as $item): ?>
<?php if ( $item['done'] == 0 ) {
echo '<li>';
echo '<span class=\"item'?><?php echo $item['done'] ? ' done' : '' ?>"> <?php echo $item['name']; c</span>
<?php if (!$item['done']): ?>
Mark as done
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p> No tasks</p>
I have a table items with a field Done (0,1)
I have tryed to put if condition in the foreach but it failed.
Thanks for the help
At the end of your line which begins with echo '<span class=\"item'?> you set a PHP open tag but without closing it and then open another one on the next line:
<?php echo $item['name']; c</span>
<?php if (!$item['done']): ?>
It looks like you meant to close the PHP tag on the previous line with ?>.
As a side suggestion, if you use a good editor or IDE it will show you errors on front of you as you type, saves a lot of time.
Viewing your PHP error logs is a must as a developer, as it tells you of problems as you work, and this would have shown a fatal error and the line number etc.
A white page is also a sign that you have a fatal error as PHP crashes before it will output any content to the screen.
When i remove the if
The code is working
<?php if (!empty($items)): ?>
<ul class="items">
<?php foreach ($items as $item): ?>
<li>
<span class="item<?php echo $item['done'] ? ' done' : '' ?>"> <?php echo $item['name']; ?> </span>
<?php if (!$item['done']): ?>
Mark as done
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p> You have no task</p>
I'm using a PHP if else statement to ignore a custom field 'thingstodo' if it's empty. I have got the following code to work but it's putting unwanted line-breaks after every paragraph tag in my content. Where am I going wrong?
<?php if (get_field('gettingthere')): ?>
<div class="inprofile3col">
<h2>Getting there</h2>
<span class="content">
<?php echo get_post_meta($post->ID, 'gettingthere', true); ?>
</span>
<?php endif; ?>
<!-- ends Getting there -->
<!-- Things to do begins -->
<?php if (get_field('thingstodo')): ?>
<h2>Things to do</h2>
<?php endif; ?>
<span class="content">
<?php
$value = get_field('thingstodo');
if ($value) {
echo $value;
} else {
echo '';
}
?>
</span>
</div>
Here
<span class="content">
<?php
$value = get_field('thingstodo');
if ($value ) {echo $value ;} else {echo '';}
?>
</span>
you still have an output if the field is empty: the empty span-tag:
<span class="content">
echo '';
</span>
You should remove line breaks from thingstodo and avoid empty html tags. Here is the updated mark up:
<?php if( get_field('gettingthere') ): ?>
<div class="inprofile3col">
<h2>Getting there</h2>
<span class="content">
<?php echo get_post_meta( $post->ID, 'gettingthere', true ) ; ?>
</span>
<?php endif; ?>
<!-- ends Getting there -->
<!-- Things to do begins -->
<?php if( get_field('thingstodo') && !empty(trim(get_field('thingstodo')))) { ?>
<h2>Things to do</h2>
<span class="content">
<?php
$value = preg_replace('/\s\s+/',' ',trim(get_field('thingstodo')));
if ($value) {echo $value ;} else {echo '';}
?>
</span>
<?php } ?>
</div>
I would like to use a PHP echo as a condition inside a PHP if statement.
The aim is to have the list of blog articles written by John Doe, displayed on his biography page.
It worked when I directly wrote the author's name in the if condition:
<!-- current page: biography page -->
<div id="list_of_articles_by_John_Doe">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == 'John Doe'): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
But I would like to automate the process, for each writer's biography page to have their own list of articles.
I tried to have as a condition the author of the current biography page ($page):
<!-- current page: biography page -->
<div id="automatic_list_of_articles">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
but it makes another issue: it does not work because inside the foreach statement, $page->author() (condition in the if statement) does not echo the author once, but one time for each page('magazine')->children() as $article.
The condition if($article->author() == $page->author()) does not work in this case, as $page->author() is not strictly the writer's name.
I'm wondering how to call $page->author() to echo the writer's name only once, when inside the foreach statement.
What could be an option is to save all author within an array
// if article->author() isn't within the array
$authors[] == $article->author();
After that you could go as the following:
<?php foreach($authors as $author){ ?>
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
<?php } ?>
That should work, even if you must do 2 foreachs
<?php if( $article->author() == $page->author() ) { ?>
<p><?php echo $article->title(); ?></p>
<?php } ?>
should work, but you can also try
<?php
if( $article->author() == $page->author() ) {
echo "\n<p>", $article->title(), "</p>\n";
}
?>
which to me looks "cleaner"; but you'd have to have a look for missing whitespaces
I suggest trying to set it equal too a variable and then using that variable in the if statement.
<?php foreach(page('magazine')->children() as $article): ?>
<?php $condition = $page->author()?>
<?php if($article->author() == $condition ?>'): ?>
echo "\n<p>", $article->title(), "</p>\n";
<?php endif ?>
<?php endforeach ?>
I am not sure if my syntax is correct but i think it is something along them lines.
You cannot use echo in condition because it is special language construct that sends given contents to the output stream and it returns no value.
Are you sure you shouldn't have this?
<div id="automatic_list_of_articles">
<?php $page = page('magazine'); ?>
<?php foreach($page->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
I have reconstructed an approximation of what looks to be your data, and you can see it working at the link below. It correctly echo's multiple article titles.
Working example:
http://ideone.com/jvLVhF
In this example you can see the PHP as above works correctly, and it is likely a data issue (ie. you should perhaps be using $page and not calling a function in the foreach statement).
Here's my php code:
<?php if($user->uid == '1'){ ?>
<h3 class="info">Upcoming Games</h3> <!--MY CHANGE -from Athletics Events -->
<?php } ?>
<?php else { ?> <h3 class="info">Athletic Events</h3> <?php }?>
Why do I get this error? I have all the brackets I need, don't I?
The } and else { can't be broken apart with PHP tags the way that you have it. Think of it as if you were trying to do:
<?php
if($some_condition) {
//do something
}
echo ' ';
else {
//something else
}
This would give you a parse error. Because you are closing the PHP tags, and then effectively outputting whitespace, then reopening, your code is behaving similarly to this. The same also applies if you were to be doing <?php }?><?php else {?> as well, only it would behave like you were doing echo ''; in between.
A cleaner and less error prone way is to use :
<?php if($user->uid == '1'): ?>
<h3 class="info">Upcoming Games</h3> <!--MY CHANGE -from Athletics Events -->
<?php else: ?>
<h3 class="info">Athletic Events</h3>
<?php endif; ?>
Try it like this
<?php if($user->uid == '1'){ ?>
<h3 class="info">Upcoming Games</h3> <!--MY CHANGE -from Athletics Events -->
<?php } else { ?> <h3 class="info">Athletic Events</h3> <?php }?>
Rewrite like this and let's see
<?php if($user->uid == '1'){ ?>
<h3 class="info">Upcoming Games</h3> <!--MY CHANGE -from Athletics Events -->
<?php
} else {
?>
<h3 class="info">Athletic Events</h3>
<?php
}
?>
this also worked
<?php if($user == '1'){ ?>
<h3 class="info">Upcoming Games</h3> <!--MY CHANGE -from Athletics Events -->
<?php } else ?>
<?php { ?> <h3 class="info">Athletic Events</h3> <?php }?>
that was a replica of your code but the else is move up. So it is obvious else should not be at the start
I created an unordered list with the following html:
<ul id="infoBox">
<li class="facebook"><?php the_title(); ?> on Facebook</li>
<li class="twitter">Follow <?php the_title(); ?> on Twitter</li>
<li class="youtube">Watch <?php the_title(); ?> on Youtube</li>
</ul>
<?php echo get_field('value'); ?> is grabbing a string from the backend of my site. Sometimes, I do not have a string to display, so I want to create a conditional statement in jquery and/or php that basically says: If there is no field to get (if the field is left empty on the backend), do not display the list item at all. For instance, if a band doesn't have a youtube page, do not display the list item with a class of 'youtube' at all.
Any idea how I would go about this?
<ul id="infoBox">
<?php $response = get_field('facebook_page');
if(!empty($response)): ?><li class="facebook"><?php the_title(); ?> on Facebook</li><?php endif; ?>
<?php $response = get_field('twitter_page');
if(!empty($response)): ?><li class="twitter">Follow <?php the_title(); ?> on Twitter</li><?php endif; ?>
<?php $response = get_field('youtube_page');
if(!empty($response)): ?><li class="youtube">Watch <?php the_title(); ?> on Youtube</li><?php endif; ?>
</ul>
Would this work for you?
if(get_field('value') == '' || is_null(get_field('value'))
echo 'no value';
else
echo 'there is a value';
I hope understand your question and could help.
<ul id='infoBox'>
<?php
$name = the_title();
$potentialItems = array('facebook' => "$name on facebook",
'youtube' => "Watch $name on youtube",
'twitter' =? "Follow $name on twitter");
foreach($potentialItems as $k=>$v)
{
$gf = get_field($k.'_page');
if($gf)
{
echo "<li class='$k'><a href='$gf'>$v</a></li>";
}
}
?>
</ul>