Echo mix of HTML and PHP giving an error - php

I'm trying to echo this long statement, tried looking at other answers here, nothing is fixing it.
Error:
Parse error: parse error, expecting `','' or `';''
Errors parsing -
.
<?php
$videoEmbed = get_post_meta( get_the_id(), 'ctslider_videoembedcode', true );
$postThumb = the_post_thumbnail();
echo '<div onclick="thevid=document.getElementById('thevideo'); thevid.style.display='block'; this.style.display='none'"><img src="'.echo $postThumb.'" style="cursor:pointer" /></div><div id="thevideo" style="display:none">"'.echo $videoEmbed.'"</div>';?>

You have to escape your '
So it gives:
echo '<div onclick="thevid=document.getElementById(\'thevideo\'); thevid.style.display=\'block\'; this.style.display=\'none\'"><img src="'.$postThumb.'" style="cursor:pointer" /></div><div id="thevideo" style="display:none">"'.$videoEmbed.'"</div>';
And you don't need echo inside the first echo

Do you realy need to echo a div?
Can't you do that :
<div onclick="thevid=document.getElementById('thevideo'); thevid.style.display='block'; this.style.display='none'">
<img src="<?php echo $postThumb ?>" style="cursor:pointer" />
</div>
<div id="thevideo" style="display:none">
<?php echo $videoEmbed ?>
</div>

Can you try this,
echo '<div onclick="thevid=document.getElementById(\'thevideo\'); thevid.style.display=\'block\'; this.style.display=\'none\'">
<img src="'.$postThumb.'" style="cursor:pointer" />
</div>
<div id="thevideo" style="display:none">"'.$videoEmbed.'"</div>';

You have to add <?php ?> around any php commands you are typing. try <?php echo "whatever you want here"?>

Related

adding image src from mysql database [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
My question is why doesn't the image appear on my php page, i have image url in database under 'imageurl'. I want to echo url to image src and this will show the picture from the folder. The error is Parse error: syntax error, unexpected '<' in .... on line 95. I will put the line 95 in the sample code too.
<html>
<body>
<?php while($row = mysql_fetch_assoc($query)): ?>
<div class="NewsItem">
<div>
<h1><?php echo $row['txt']; ?></a> <br>
<span>posted on <?php echo $row['added']; ?></span><span> by <?php echo $row['adder'];?></span>
</div>
<div class="imgdiv"><?php if(($row['imageurl'] == 'NULL') or ($row['imageurl']== '')){
//No images
}
else{
Line 95---- <img src="<?php echo $row['imageurl'];?>" width='300' height='300'/>
}
?></div>
<br>
<div>
<form action="comment.php"method="post" name="addComment"><input type="text" placeholder="kommentaar" name="commenttext" maxlenght="100"><input type="submit" name="addComment" value="Lisa kommentaar">
<input id="prodId3" name="prodId3" type="hidden" value="<?php echo $row['id']; ?>">
</form>
</div>
</div>
<?php
endwhile;
?>
You forget about printing the result after your elseif clause. Moreover, you should use alternative syntax in PHP template, it will make things easier to read/maintain (You did it for while/endwhile).
<?php while($row = mysql_fetch_assoc($query)): ?>
<div class="NewsItem">
<div>
<h1><?php echo $row['txt']; ?></a> <br>
<span>posted on <?php echo $row['added']; ?></span><span> by <?php echo $row['adder'];?></span>
</div>
<div class="imgdiv">
<?php if(($row['imageurl'] == 'NULL') or ($row['imageurl']== '')): ?>
No image
<?php else: ?>
<img src="<?php echo $row['imageurl'];?>" width='300' height='300'/>
<?php endif; ?>
</div>
<br>
<div>
<form action="comment.php"method="post" name="addComment"><input type="text" placeholder="kommentaar" name="commenttext" maxlenght="100"><input type="submit" name="addComment" value="Lisa kommentaar">
<input id="prodId3" name="prodId3" type="hidden" value="<?php echo $row['id']; ?>">
</form>
</div>
</div>
<?php endwhile; ?>
Need to maintain php tags open and close at line #95
So,
Change your else part like this:
else{ ?>
<img src="<?php echo $row['imageurl'];?>" width='300' height='300'/>
<?php }
In your code there is missmatch of opening and closing php tag, also put full URL of image with site URL
Try the following code
else{ ?>
<img src="<?php echo "your site url".$row['imageurl'];?>" width='300' height='300'/>
<?php }

Html link <a> not wrapping <div> element properly

I am currently writing a php website where I encountered a problem where the html link will not wrap div element properly. I have not found an answer to my problem yet, so I appreciate if someone can help me.
This is the php code I had written:
ECHO '<a target="_blank" href="' . $row["link"] . '">';
ECHO '<div class="header-box">';
ECHO '<div class="detail">';
ECHO '<p class="title">' . $row["title"] . '</p>';
$user_data = get_user_data($row["owner_id"]);
ECHO '<p class="author">This article is shared by <a class="user" href="./member.php?action=profile&id='.$user_data["id"].'">' . $user_data["firstname"] . " " . $user_data["lastname"] . '</a> on '.date("d F Y", strtotime($row["posted"])).'.</p>';
ECHO '</div>';
ECHO '<div class="good">';
ECHO '<p id="number">'.$row["good"].'</p>';
ECHO '<p id="icon"><i class="fa fa-thumbs-o-up"></i>Useful</p>';
ECHO '</div>';
ECHO '<div class="bad">';
ECHO '<p id="number">'.$row["bad"].'</p>';
ECHO '<p id="icon"><i class="fa fa-thumbs-o-down"></i>Not Useful</p>';
ECHO '</div>';
ECHO '</div>';
ECHO '</a>';
However, the outcome of the php code in browser are as followed:
<div class="header-box">
<a target="_blank" href="http://designinstruct.com/tools-basics/5-easy-steps-optimize-your-images-photoshop/"></a>
<div class="detail">
<a target="_blank" href="http://designinstruct.com/tools-basics/5-easy-steps-optimize-your-images-photoshop/">
<p class="title">5 Easy Steps to Optimize Your Images in Photoshop</p>
</a>
<p class="author"><a target="_blank" href="http://designinstruct.com/tools-basics/5-easy-steps-optimize-your-images-photoshop/">This article is shared by </a><a class="user" href="./member.php?action=profile&id=2">demoname demo</a> on 01 January 1970.</p>
</div>
<div class="good"><p id="number">1</p><p id="icon"><i class="fa fa-thumbs-o-up"></i>Useful</p></div>
<div class="bad"><p id="number">2333</p><p id="icon"><i class="fa fa-thumbs-o-down"></i>Not Useful</p>
</div></div>
I am getting a little bit frustrated trying to fix the problem because I can't figure out what is going wrong? The other part of code written with html link wrapping the div are working fine. Can anyone help me out? Thanks.
So the question was solved by Rick Hitchcock for pointing out my nesting issue.
So the answer is not nesting an tag within another tag.
After seeing the refactored code, I realized you have a link inside another link. The browser doesn't know what to do with this, so it closes them both down as small as they can.
Try using html inside your .php file, and just using tags where you have to use php. For Example:
close the php with a ?>, then open it each time you need it by wrapping your code in
<?php //php code goes here ?>
Here's your code refactored:
<a target="_blank" href="<?php echo $row["link"] ?>">
<div class="header-box">
<div class="detail">
<p class="title"><?php echo $row["title"] ?></p>
<?php $user_data = get_user_data($row["owner_id"]); ?>
<p class="author">This article is shared by <a class="user" href="./member.php?action=profile&id=<?php echo $user_data["id"] ?>"><?php echo $user_data["firstname"] ?> <?php echo $user_data["lastname"] ?></a> on <?php echo date("d F Y", strtotime($row["posted"])) ?></p>
</div>
<div class="good">
<p id="number"><?php echo $row["good"] ?></p>
<p id="icon"><i class="fa fa-thumbs-o-up"></i>Useful</p>
</div>
<div class="bad">
<p id="number"><?php echo $row["bad"] ?></p>
<p id="icon"><i class="fa fa-thumbs-o-down"></i>Not Useful</p>
</div>
</div>
</a>
Why are you using pure PHP for this? Its not necessary?

How to Add Full External URL prefix in PHP

I have written a PHP on a page displaying info from an XML feed to another website. On the feed I have a product image that we would like to display, but I need to add the external website address as a prefix in order for this to work.
Code I have on the feed is:
$xml = simplexml_load_file('feed/myfeed.xml');
foreach ($xml->item as $item) {
$short_description = substr($item->descshort, 0, 200); ?>
<div class="row">
<div class="lhs col-md-9">
<h3><?php echo $item->name ?></h3>
<p class="pri">£<?php echo $item->regprice ?></p>
<p id="shown">
<?php echo $short_description; ?> ... Read more</p>
</div>
<div class="rhs col-md-3"> //The following is the bit that I am struggling on
<img src="<?php echo 'http://mywebsite.com/myimagefolder/mysubimagefolder/' . $item->imgsm; ?>" alt="<?php echo $item->name ?>" height="150" />
</div>
</div>
But all this gives me is a warning message that I have just tried to access http://mywebsite.com/myimagefolder/mysubimagefolder/
So basically how could I add http://mywebsite.com/myimagefolder/mysubimagefolder/ on the beginning and the filename after it?
Its probably very simple, but not to me. Please could anyone help me?
Many thanks in advance.
I have just tried:
<img src="http://mywebsite.com/myfolder/mysubfolder/<?php echo $url, $item->imgsml ?>" alt="<?php echo $item->name ?>" height="150" />
And this seems to work throughout. And this seems to work throughout. Thanks #schenk, your answer was spot on.

PHP WordPress if else not working unexpected syntax

I have a PHP if else statement however it does not seem to work and I'm not sure why.
Here is my code
<?php
if (has_post_thumbnail()) {
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php
the_post_thumbnail('full', array('class' => 'img-responsive alignleft'));
?>
</a>
<?php
} else () {
?>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />';
<?php
}
?>
And here is the error that I get syntax error, unexpected ')'
I'm not sure where the unexpected ) is coming from.
I am basing my PHP on this structure, however editing it as I would like to be able to put mine into HTML without using echo
<?php if ( '' != get_the_post_thumbnail() ) {
// some code
}
else {
// some code
}
?>
Let's try with this:
<?php if (has_post_thumbnail()): ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail('full', array('class' => 'img-responsive alignleft')); ?>
</a>
<?php else: ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />';
<?php endif; ?>
I'm not sure why you wrote your code this way instead of concatenating it, but your if else statement has two parenthesis after else "()". You might want to get rid of these to fix your issue.
In PHP else is not a function, so you shouldn't call it like you did.
PHP's elseif () needs conditions,
try else instead:
<?php
if ( has_post_thumbnail() ) {
?>
<?php the_post_thumbnail( 'full', array( 'class' => 'img-responsive alignleft' ) ); ?>
<?php
} else { //instead of elseif () here, use else
?>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />
<?php
}
?>
Please also refer to the WordPress Coding Standards to make your code easier readable and maintainable. And try not to edit the original code in your question without marking the edits clearly, otherwise SO users won't be able to understand your problem/help you further.

Parse error: syntax error, unexpected '{' in if()

I'm getting Parse error: syntax error, unexpected '{'
Here is my code (used in a wordpress comment loop)
<?php
if (current_user_can('edit_comment',$comment->comment_ID)) { ?>
<a href="<?php echo get_edit_comment_link(); ?>" title="Edit Comment">
<img src="<?php echo get_template_directory_uri();?>/images/edit-link.png"/>
</a>
<?php } ?>
Try:
<?php if (current_user_can('edit_comment',$comment->comment_ID)) : ?>
<a href="<?php echo get_edit_comment_link(); ?>" title="Edit Comment">
<img src="<?php echo get_template_directory_uri();?>/images/edit-link.png"/>
</a>
<?php endif;?>
I'm a big fan of sticking to one language at a time. Context switches are confusing.
<?php
if (current_user_can('edit_comment',$comment->comment_ID)) {
printf( "<a href='%s' title='Edit Comment'><img src='%s/images/edit-link.php'/></a>",
get_edit_comment_link(),
get_template_directory_uri()
);
}
?>
Though this is personal preference, and I don't see anything technically wrong with the code in your question.
Try this
<?php
if (current_user_can('edit_comment',$comment->comment_ID)) {
echo "<a href=\"get_edit_comment_link()\" title=\"Edit Comment\">
<img src=\"get_template_directory_uri()/images/edit-link.png\"/>
</a>";
} ?>
also why not
<?php $a=get_edit_comment_link(); $b=get_template_directory_uri();
if (current_user_can('edit_comment',$comment->comment_ID)) {
echo "<a href=\"$a\" title=\"Edit Comment\">
<img src=\"$b/images/edit-link.png\"/>
</a>";
} ?>

Categories