Why isn't this simple IF statement not working? - php

I can't get this script to work for nothing. I tried many different ways to make this work but it's not working.
<?php
if (!empty ($image->alttext )) : ?>
<div class="thumbtitle"><?php echo $image->alttext ?></div>
<?php endif; ?>
Any help would be appreciated!
It always shows true that there is alttext when there isn't sometimes.

Your question is vague but try the following
<?php
if (strlen(trim($image->alttext)) > 0) : ?>
<div class="thumbtitle"><?php echo $image->alttext ?></div>
<?php endif; ?>
As said your string is probably not empty but with whitespaces.
use var_dump() or strlen() to find out. With trim you remove the whitespaces.

The value in $image->alttext is probably not blank, maybe there are white spaces in it.
You can trim it first before testing.
$imgText = trim($image->alttext);
if (!empty ($imgText)) : ?>
<div class="thumbtitle"><?php echo $image->alttext ?></div>
<?php endif; ?>
Note: use var_dump($image->alttext) to see that it has the value you are expecting.

Related

How to Wrap A Condition Inside Another Condition in PHP

Can anyone please help me wrap the condition inside another condition in php.
I have this code #1 that I want to be inside code #2.
Here's code #1
<?php if( get_field('highlights') ): ?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php endif; ?>
Here's code #2
<?php if(strstr($_SERVER['HTTP_REFERER'],'www.example.com'))
{
echo '**CODE #1 should be placed here**';
}
?>
Sorry, I don't haev any knowledge in PHP.
Wrapping code 1 inside code 2
After several trial and error, here's what I have to make it work. Please correct me if there's something wrong or to improve.
<?php if (strstr($_SERVER['HTTP_REFERER'], 'www.google.com')){ ?>
<?php if( get_field('highlights') ):?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php endif; ?>
<?php } ?>
This should work in theory. Though I'm unsure what the highlights field is for.
<?php
if(strstr($_SERVER['HTTP_REFERER'],'www.example.com')){
if( get_field('highlights') ){ ?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php
}
}
?>
You may want to replace <?php the_field('highlights'); ?> with something like <?=highlight ?> and iterate through the highlights in a loop. That depends on the situation though.

If statement to hide a <Div>

I'm pulling both the 'book_series' and 'series_link' from custom fields within Wordpress.
The link shows up correctly when a name and URL are included but it shows "Books Series:' on blank pages (that don't include links or series). How can I hide the content when left blank?
Here is what I have so far:
<?php if (('books_series') == true) : ?>
<div class="series"><b>Book Series: </b><?php the_field( 'books_series' ); ?></div>
<?php else: ?>
<div class="series" hidden></div>
<?php endif; ?>
Thank you.
Maybe,
if (('books_series') == true)
Should be:
if (the_field('books_series') == true)
This is what I came up with and it seems to work, although I'm not sure how functional it is overall - `
<?php if ($series_link) { ?>
<div class="series"><b>Book Series: </b><?php the_field( 'books_series' ); ?></div>
<?php } ?>`
Thank you everyone for your help and suggestions.

PHP echo as condition inside PHP if statement

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).

Cleaner code when using alternative syntax and associative arrays.

Im writing a foreach loop for my view using the alternative syntax, to get a piece of html like this
<h3>My Post</h3>
Ive used the line below, but it seems awfully clunky with all the concatenation.
<?php
foreach ($index_posts as $post):
?>
<h3><?= "" . $post['title'] . ""; ?></h3>
<?php
endforeach;
?>
Ive also tried writing it like this :
<?= "<a href='post.php/?id=$post['id']'>$post['title']</a>"; ?>
But it shows errors when written in sublime text 2 around the ['id'] and ['title'] any ideas why this is, as they are single quotes ?
Is there another way i could write this that is cleaner ?
You could just do:
<?php foreach ($index_posts as $post): ?>
<h3>
<a href="post.php?id=<?php echo $post['id']; ?>">
<?php echo $post['title']; ?>
</a>
</h3>
<?php endforeach; ?>
The reason Sublime is showing an error when you do this
<?= "<a href='post.php/?id=$post['id']'>$post['title']</a>"; ?>
is due to the fact that it's a syntax error as you can't have complex variables (like array values with strings for keys) interpolated into a string directly like this. You need to wrap them in {}
<?= "<a href='post.php/?id={$post['id']}'>{$post['title']}</a>"; ?>
Alternatively, you could use:
foreach ($posts as $post) {
printf('%s', $post['id'], $post['title']);
}
<?php foreach ($index_posts as $post): ?>
<h3>
<a href='post.php/?id=<?=$post['id']?>'>
<?=$post['title']?>
</a>
</h3>
<?php endforeach; ?>
Keeping it to HTML whenever you can; I much prefer to not 'echo' out HTML unless needed, although this question really quite about preference. Afterall, PHP is an templating language!
Gotta help a fellow sam...
<?php echo "<h3>{$post['title']}</h3>"; ?>

if statement not echoing out html

For some reason I can't get my if-statement to echo out the HTML.
<?php
$my_description = meta('description');
if (!empty($my_description)): echo '<p class="description">'.$my_description.'</p>';
echo '<br>'; ?>
<?php endif; ?>
It only outputs the text, nothing else.
meta('description') is from a plugin in Wordpress that should output the text I placed in the backend. The above code ONLY outputs the following: Lorem Ipsum...
Update: I would like it to output:
<p class="description">Lorem Ipsum...</p>
After a discussion in chat, we discovered that the meta() function was not returning the value we expect. The correct function is get_post_meta
<?php
$my_description = get_post_meta(get_the_ID(), 'description', 1);
if (!empty($my_description)):
?>
<p class="description"><?php echo $my_description; ?></p>
<br>
<?php endif; ?>
Ugh... sooo ugly, just do it like this:
<?php
echo !empty($my_description) ? '<p class="description">'.$my_description.'</p>' : '<br />';
?>
Use the ternary operator, don't use that messy if formatting...

Categories