Am I missing an escape for one of these quotes? - php

I'm not entirely sure why this isn't working. But this piece of code does not work.
<?php
foreach ( $gallery_ids as $gallery ) {
echo '<div class="tgallery" rel="'.$gallery['gid'].'"><?php echo do_shortcode("[nggallery id='.$gallery['gid'].']"); ?></div>';
}
?>
I was guessing that maybe I'm putting the wrong quotes in the wrong place.
All the parts seperately work, as in :
I can display the 'gid' value with echo $gallery['gid']
I can make the div tags appear with the appropriate rel
I can, by itself, make <?php echo do_shortcode("[nggallery id=3]"); ?> work.
I just can't make the entire thing appear together.

You're mixing interpolated php and html, placing "<?php echo" inside what's already php.
<div class="tgallery" rel="<?php echo $gallery['gid'];?>">
<?php echo do_shortcode('[nggallery id="'.$gallery['gid'].'"]'); ?>
</div>

Why you put <?php ?> inside your echo ?
<?php
foreach ( $gallery_ids as $gallery )
{
echo '<div class="tgallery" rel="'.$gallery['gid'].'">'.do_shortcode('[nggallery id='.$gallery['gid'].']').'</div>';
}
?>

The issue
Pick either string concatenation or opening/closing PHP for HTML. You cannot combine both as you have done above.
echo '<div class="tgallery" rel="'.$gallery['gid'].'">
<?php echo do_shortcode("[nggallery id='.$gallery['gid'].']"); ?>
</div>';
The second line of code above does not belong inside a string as code in between the <?php ... ?> will not be parsed by PHP when it is contained in a string.
Solutions
Concatenation
I have fixed your code to use concatenation below:
foreach ( $gallery_ids as $gallery ) {
$shortcode = do_shortcode("[nggallery id={$gallery['gid']}]");
echo '<div class="tgallery" rel="' . $gallery['gid'] . '">' . $shortcode . '</div>';
}
Opening and closing PHP
This is how you would do it using PHP "templating":
<?php foreach($gallery_ids as $gallery ): ?>
<div class="tgallery" rel="<?php echo $gallery['gid']; ?>">
<?php echo do_shortcode("[nggallery id={$gallery['gid']}]"); ?>
</div>
<?php endforeach; ?>

You are already "in" php, so your opening tag is causing the problem:
<?php echo do_shortcode("[nggallery id='.$gallery['gid'].']"); ?>
It should be something like:
echo '<div class="tgallery" rel="'.$gallery['gid'].'">' . do_shortcode('[nggallery id='.$gallery['gid'].']') . '</div>';

<?php
foreach ( $gallery_ids as $gallery ) {
echo "<div class=\"tgallery\" rel=\"{$gallery["gid"]}\">". do_shortcode("[nggallery id=\"{$gallery["gid"]}\"]") ."</div>";
}
?>

try this....
<?php
foreach ( $gallery_ids as $gallery ) {
echo '<div class="tgallery" rel="'.$gallery['gid'].'">'.do_shortcode("[nggallery id=".$gallery['gid']."]").'</div>';
}
?>
you have inside a php statment
Try Notepad then it will colour code your php code, so you can clearly see what quotes etc you have wrong

Related

Print inside print php

How do i print INSIDE the defined tag?
When it ouputs this code, the_title() gets printed OUTSIDE (before) the h1 tag..
My code is:
<?php
if ( '' != get_the_post_thumbnail() ) {
print '<p>HEY</p>';
}
else {
print '<div class="page-header row full"><h1 class="page-title" itemprop="headline">', the_title() ,'</h1></div>';
}
?>
i have already tried:
<?php
if ( '' != get_the_post_thumbnail() ) {
print '<p>HEY</p>';
}
else {
print '<div class="page-header row full"><h1 class="page-title" itemprop="headline">'. the_title() .'</h1></div>';
}
?>
What am i doing wrong?
Thanks
You could do like below:
<?php if ( '' != get_the_post_thumbnail() ): ?>
<p>HEY</p>
<?php else: ?>
<div class="page-header row full"><h1 class="page-title" itemprop="headline"><?php the_title(); ?></h1></div>
<?php endif; ?>
Or use get_the_title()(which returns the title value) instead.
Try print "....".get_the_title().".....";
The methods "the_xxxx" print the value, the methods "get_the_xxx" returns the value.
try echo instead of print
echo - can output one or more strings
print - can only output one string, and returns always 1

How can I combine these two if/else statements for different results?

I currently have these two blocks of php that are pulling results:
<?php
$webtech = article_custom_field('web-tech');
if ( !empty($webtech) ) :
?>
<div class="tech-list">
<div class="tech-title">Technologies:</div>
<ul class="tech-ul"><?php echo $webtech; ?></ul>
</div>
<?php
endif;
?>
And
<?php
$url = article_custom_field('site-url');
elseif ( !empty($url) ) :
?>
<div class="site-url">Visit</div>
<?php
endif;
?>
I want to combine them to output one single block, like:
<div class="tech-list">
<div class="tech-title">Technologies:</div>
<ul class="tech-ul"><?php echo $webtech; ?></ul>
<div class="site-url">Visit</div>
</div>
It needs to meet the following:
If web-tech exists, output it. Don't output site-url if it doesn't exist.
If web-tech exists, output it. If site-url exists, output it.
If site-url exists, output it. Don't output web-tech if it doesn't exist.
The containing div should not be output at all if neither variable does not exist.
Am I missing an obvious way to do it? It seems trivial, but I can't get the if/else/elseif statements to line up.
You could store your outputs in a variable, like so:
<?php
$output = '';
$webtech = article_custom_field('web-tech');
if ( !empty($webtech) ) :
$output .= '<div class="tech-title">Technologies:</div>'
. '<ul class="tech-ul">' . $webtech . '</ul>';
endif;
$url = article_custom_field('site-url');
if(!empty($url)) :
$output .= '<div class="site-url">Visit</div>';
endif;
if($output != ''):
echo '<div class="tech-list">';
echo $output;
echo '</div>';
endif;
?>
This way, only when there is something set in your output variable, will it show anything.
Does this solve your problem?
It sounds like you need to check both variables before you output the container that they exist in.
<?php
$webtech = article_custom_field('web-tech');
$url = article_custom_field('site-url');
if ( !empty($webtech) || !empty($url))
{
?>
<div class="tech-list">
<?php
if ( !empty($webtech) )
{
?>
<div class="tech-title">Technologies:</div>
<ul class="tech-ul"><?php echo $webtech; ?></ul>
<?php
}
if ( !empty($url) )
{
?>
<div class="site-url">Visit</div>
<?php
}
?>
</div>
<?php
}
?>
if (a or b) then {
OpenTechTitle;
if (a) SetAtext;
if (b) SetBtext;
CloseTechTitle;
}

If statement to not show div / h2

This for each / if statement displays changes, and then right below it it displays the changes made.
I am trying to write an if statement that tells it not to show the h2 and the #change_box if there are no changes.
Help would be greatly appreciated.
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<? foreach ($audit['Events'] as $event):?>
<?if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?= $event['Field']?>
</span>:
<?= $event['Value'] ?>
<?=($event['Previous'])?>
<?endif?>
<?endforeach?>
</div>
<?php
if ($changes) { // You'll have to set this variable
//Echo all of your HTML
else {
//Echo the stuff you'd rather show if they didn't change anything
}
?>
To give you an idea of how I'd write your code
<?php
if ($changes) {
echo '<h2 class="changes">Changes:</h2>';
echo '<div id="change_box">';
foreach ($audit['Events'] as $event) {
if ($event['Type'] != 'Comment') {
echo $event['Field'] . </span> . $event['Value'] . $event['Previous'];
}
}
echo "</div>"
}
?>
You could wrap your code with an if like
if(count($audit['Events'])>0)
{
//Your code
}
Wny do you open and close php tags every time? Would be better to make everything in PHP and echo whenever you want to print HTML code.
<?php
echo "<h2 class=\"changes\"> Changes: </h2>";
echo "<div id=\"change_box\">";
foreach ($audit['Events'] as $event) {
if ( $event['Type'] != 'Comment') {
echo "<span class=\"field\">".$event['Field']."</span>";
echo $event['Value'];
echo "(".$event['Previous'] .")";
}
}
echo "</div>";
?>
please make space after each <? and make sure you enabled short tags
<?php if(is_array($audit['Events']) && $audit['Events']):?>
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<?php foreach ($audit['Events'] as $event):?>
<?php if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?php echo $event['Field'];?>
</span>:
<?php echo $event['Value']; ?>
<?php echo $event['Previous'];?>
<?php endif;?>
<?php endforeach;endif;?>
</div>

Conditional div class doesn't echo, only the variable within does

Is there any reason why this is happening due to the following code? All that gets displayed is the variable , i.e the image.
<?php $featured_image = the_post_thumbnail();?>
<?php if (is_page(7) || is_page(12))
echo '<div class="featured_image">' . $featured_image . '</div>'
?>
<?php
$featured_image = the_post_thumbnail();
if (is_page(7) || is_page(12)) {
echo '<div class="featured_image">' . $featured_image . '</div>';
}
?>
The function itself echos the img: http://codex.wordpress.org/Function_Reference/the_post_thumbnail
try:
<?php
if ( has_post_thumbnail() && (is_page(7) || is_page(12))) {
echo '<div class="featured_image">';
the_post_thumbnail();
echo '</div>';
}
?>
What you are seeing is the return value of the echo function.

Trouble concatenating strings in a loop

I'm try to make some changes to my ZenPhoto installation in order so that at the bottom of an album page I have a text area which displays the "embed" code of the generated images above, so that readers can simply copy it and post the html onto their own sites.
Here is the snippet of code.
<?php $str = ''; ?>
<?php while (next_image()): ?>
<div class="imagethumb"><?php printImageThumb(getAnnotatedImageTitle()); ?></div>
$str .= '<?php printImageThumb(getAnnotatedImageTitle()); ?>;'
<?php endwhile; ?>
<textarea type="text" size="50">
<?php echo $str; ?>
</textarea>
The code I've added is the $str stuff. I'm trying to loop through the images and create the html that is used in the first div in the while loop so that it puts it as text into the str string. This is concatenated for each image in the library and then the end str is posted into a simple text area for the user to copy.
I can't get the $str concatination working.
I'm very new to php and I can't quite get the syntax working.
Any help would be much appreciated.
The concatenation is not inside the <?php tags. For readability, you should use sprintf:
<?php $str = ''; ?>
<?php while (next_image()): ?>
<div class="imagethumb"><?php printImageThumb(getAnnotatedImageTitle()); ?></div>
<?php $str .= sprintf('%s',
html_encode(getImageLinkURL()),
getBareImageTitle(),
printImageThumb(getAnnotatedImageTitle()));
?>
<?php endwhile; ?>
But you are repeating things here (you are creating the link twice). You could restructure the code a bit to avoid that:
<?php
$images = array();
while (next_image()) {
$images[] = sprintf('%s',
html_encode(getImageLinkURL()),
getBareImageTitle(),
printImageThumb(getAnnotatedImageTitle()));
}
?>
<?php foreach($images as $image): ?>
<div class="imagethumb"><?php echo $image; ?></div>
<?php endforeach; ?>
<textarea>
<?php echo implode("", $images); ?>
</textarea>
Reference: implode
Your php code is starting and ending outside your php blocks with random php blocks in between.
<?php $str .= '<a href="';
$str .= html_encode(getImageLinkURL());
$str .= '" title="';
$str .= getBareImageTitle();
$str .= '">';
$str .= printImageThumb(getAnnotatedImageTitle());
$str .= '</a>';
?>
Alternatively:
<?php $str .= ''.printImageThumb(getAnnotatedImageTitle()).''; ?>
One issue is that ' and " strings behave differently in PHP. When you have:
echo '<?php echo $otherString; ?>';
PHP will print:
<?php echo $otherString; ?>
instead of the contents of $otherString.
I would rewrite this line:
<?php $str .= '<?php printImageThumb(getAnnotatedImageTitle()); ?>' ?>
to look more like this:
<?php $str .= '<a href="' . html_encode(getImageLinkURL()) . '" title="' . getBareImageTitle() . '" ' . printImageThumb(getAnnotatedImageTitle() . '</a>;'; ?>
Another issue is that you can't echo into a string and have it concatenate. Instead, you want to return a string value.
The $str variable is not enclose in <?php ?> tags.

Categories