1

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"><a href="<?php echo html_encode(getImageLinkURL());?>" title="<?php echo getBareImageTitle();?>"><?php printImageThumb(getAnnotatedImageTitle()); ?></a></div>

     $str .= '<a href="<?php echo html_encode(getImageLinkURL());?>" title="<?php echo getBareImageTitle();?>"><?php printImageThumb(getAnnotatedImageTitle()); ?></a>;'
<?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.

1
  • I've updated your question title for accuracy. There's no need to beg for help, that's what we're here for! Commented Mar 20, 2011 at 19:58

4 Answers 4

5

The concatenation is not inside the <?php tags. For readability, you should use sprintf:

<?php $str  = ''; ?>
<?php while (next_image()): ?>
    <div class="imagethumb"><a href="<?php echo html_encode(getImageLinkURL());?>" title="<?php echo getBareImageTitle();?>"><?php printImageThumb(getAnnotatedImageTitle()); ?></a></div>

     <?php $str .= sprintf('<a href="%s" title="%s">%s</a>', 
                           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('<a href="%s" title="%s">%s</a>', 
                               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

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for readability. I get confused when writing PHP (when I'm forced not to use Python)...
@DarkUFO: You're welcome :) Oh btw. I just noticed it: <textarea> elements have no type or size attribute. First, they contain always text and second, to set the width or height of it, you have to use the attributes cols and rows. type and size only apply to to <input> elements.
0

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 .= '<a href="'.html_encode(getImageLinkURL()).'" title="'.getBareImageTitle().'">'.printImageThumb(getAnnotatedImageTitle()).'</a>'; ?>

Comments

0

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 .= '<a href="<?php echo html_encode(getImageLinkURL());?>" title="<?php echo getBareImageTitle();?>"><?php printImageThumb(getAnnotatedImageTitle()); ?></a>' ?>

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.

Comments

0

The $str variable is not enclose in <?php ?> tags.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.