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.
Related
I'm trying to put a read more button and of course excerpt.
this is my base code to call text <?php the_content();?>
I add something for read more but it doesn't work.
<?php if($excerpt = $desc->the_content):?>
<?php
$num_word = 15;
$excerpt = strip_shortcodes($excerpt);
echo '<p>' . wp_trim_words($excerpt,15,'...') . '</p>';
echo '<p class="property-content">' . wp_trim_words($excerpt,25,'...') . '</p>';
?>
<?php endif;?>
Problem is solved.
<?php if ($excerpt = the_excerpt()):
$num_word = 5;
$excerpt = strip_shortcodes($excerpt);
echo '<p>' . wp_trim_words($excerpt, $num_word, '...') . '</p>';
?>
<?php endif; ?>
When I run the following file I get the database data i.e it prints it out on the website so I know my connections are good.
<html>
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo $row["name"], $row["image"];
}
?>
</div>
</html>
However when I try and format the results like below
<html>
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo <div id = "bookbar">
<img src= "$row['image']" alt = "image">
<p> $row['name'] </p>
</div>
}
?>
</div>
</html>
it doesn't work. Can anyone help me fix the code?
Maybe try this your code didn't close/open the php tags properly also don't echo like that
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<div id = "bookbar">
<img src= "<?php echo $row['image'] ?>" alt = "image">
<p><?php echo $row['name']; ?></p>
</div>
<?php
}
}
$conn->close();
?>
If you want to echo something out
Its better to close on open the php tags like so
PHP goes here
?>
HTML goes here
<?php
PHP goes here
And if you want to echo something inside the HTML just do this
<span> <?php echo "something" ?> </span>
much easier and makes the code easier to read.
change your echo statement to -
echo '<div id = "bookbar"><img src= "' . $row['image'] . '" alt = "image"><p>'. $row['name'] .'</p>'
Your issue is a syntax problem - you can't use echo like that, it has to echo a string variable. You should be seeing an error message about it.
You could keep the echo statement and put all the HTML inside a string, and concatenate (or interpolate) the PHP data into it. But IMO the easiest thing here in terms of readability and maintenance is to step out of the PHP tags, print the HTML, embed some PHP tags in it for the variables, and then step back in again to continue with the code. It makes the HTML far easier to understand:
?>
<div id="bookbar">
<img src="<?php echo $row['image'] ?>" alt="image">
<p><?php echo $row['name'] ?></p>
</div>
<?php
When you are in php mode you should echo strings as php variables wrapped with single quotes:
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo '<div id = "bookbar">';
echo '<img src="' . $row['image'] . '" alt = "image">';
echo '<p>' . $row['name'] . '</p>';
echo '</div>';
}
This might be easy, but i could not get solution for it as is novice and learning on the php
I want to put the output into a variable called $list so that it becomes
<?php $list .= '<h3> <?php echo $this->escape($item->n_make); ?> <?php echo $item->n_model; ?>
</h3>
<p> <?php echo $item->n_month; ?> <?php echo $item->n_year; ?> </p>
<p> <?php echo $item->n_short_description; ?> </p>
<hr/>'
?>
But the syntax is incorrect as its like php within php
How to get value of $list capturing in details
and can then use in explode function
<?php
$listings = explode("<hr/>", $list);
$numberOfListings = count($listings);
---- conditions
echo $listings;
?>
It's just simple concatenation:
$list .= '<h3> ' . $this->escape($item->n_make) . $item->n_model . '</h3>
<p> ' . $item->n_month . ' ' . $item->n_year . '</p>
<p> ' . $item->n_short_description . ' </p>
<hr/>';
I have this:
<?php echo $this->htmlLink($this->viewer()->getHref(), $this->itemPhoto($this->viewer(), 'thumb.icon')); ?>
That generates an HTML code like:
<a href="http://www.domain.com/john">
<img src="http://www.domain.com/thumb_0205.jpg" alt="" class="thumb_icon item_photo_user thumb_icon">
</a>
Now, what I am trying to do, is to add:
<?php echo $this->viewer()->getTitle(); ?> //This will generate the member's name, like "John Doe"
to the code above, to generate an HTML code like:
<a href="http://www.domain.com/john">
<img src="http://www.domain.com/thumb_0205.jpg" alt="" class="thumb_icon item_photo_user thumb_icon">
<span>John Doe</span>
</a>
Anyway I can do that?
Thanks
This ought to work:
<?php echo $this->htmlLink(
$this->viewer()->getHref(),
$this->itemPhoto($this->viewer(), 'thumb.icon') . '<span>' . $this->viewer()->getTitle() . '</span>'
); ?>
Guessing, this should work:
<?php echo $this->htmlLink($this->viewer()->getHref(), $this->itemPhoto($this->viewer(), 'thumb.icon').'<span>'. $this->viewer()->getTitle().'</span>'); ?>
Just append the extra string to the second argument of htmlLink.
HtmlLink($href, $text, $title = "", array $attribs = array());
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