'.{$row['MemberName']}.'';?>
";?>
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\xampp\htdocs\home - Copy\membercopy.php on line 141
I really don't know where it went wrong. Please help,
<?php
echo '<label onclick="window.open('profilephp.php?member=$row['MemberID']','mywindow')">'{$row['MemberName']}.'</label>';
?>
If you look at that line, you'll see you have your single quoted string with single quotes inside it. Also, you're trying to use variables inside a single quoted string, which doesn't work. You want to change this to:
echo "<label onclick=\"window.open('profilephp.php?member={$row['MemberID']}','mywindow')\">'{$row['MemberName']}.'</label>";
Notice I've double quoted your string and then escaped, with a backslash, any double quotes inside the quoted string.
I also added {} around the first complex variable in the string, since it will give you an error without it.
The error are the non-escaped single quotation marks and the bracets. You write this:
<?php echo '<label onclick="window.open('profilephp.php?member=$row['MemberID']','mywindow')">'.{$row['MemberName']}.'</label>';?>
but is has to look like this:
<?php echo '<label onclick="window.open(\'profilephp.php?member='.$row['MemberID'].'\',\'mywindow\')">'.$row['MemberName'].'</label>';?>
I hope that is what you needed.
This fixes most of the problems with your code (and it's even readable!):
<td style="text-align: center; background-color: #FFFFFF;">
<label onclick="window.open('profilephp.php?member=<?php=$row['MemberID']?>','mywindow')">
<?php=$row['MemberName']?>
</label>
<br />
<img src="<?php=$row['MemberImg']?>" width="100" height="100" alt="" />
</td>
Related
I am trying to register a WordPress shortcode.
I have a PHP return statement that includes HTML. Inside the HTML, I have a javascript onclick function. Visual Studio Code is throwing a syntax error which is, unexpected 'frame' (T_STRING), expecting ',' or ';'
I have read other articles on Stack Overflow and I have tried escaping single quotes that are inside the string but I could be inaccurately escaping. Below is some raw code without any escaping.
I understand the code below may not be pretty, but all help is appreciated nonetheless.
<?php
function torque_hello_world_shortcode() {
return '<div class="description pagecontent simple"></div>
<a onclick="document.getElementById('frame').style.display = document.getElementById('frame').style.display == 'none' ? 'block' : 'none'; return false;"><img class="someclass" src="#" alt="alt text" style="width:60px;height:60px;cursor:pointer !important;">
<p class="something" style="cursor:pointer !important;">text! <span class="otherclass" style="cursor:pointer !important;">more text</span></p></a>
<div class="description pagecontent simple"></div>';
}
add_shortcode( 'helloworld', 'torque_hello_world_shortcode' );
You're mixing single and double quotes in your return statement (on line 2 of the string, you basically close the string and follow it with the word "frame", without using concatenation or even the $ variable sign).
If you open a string with single quotes, a second single quote will close the string. If you need to use single quotes within your string, you need to escape it, with a backslash: echo 'Arnold once said: "I\'ll be back"';
I added backslashes to your code:
<?php
function torque_hello_world_shortcode() {
return '<div class="description pagecontent simple"></div>
<a onclick="document.getElementById(\'frame\').style.display =
document.getElementById(\'frame\').style.display == \'none\' ? \'block\' :
\'none\'; return false;"><img class="someclass" src="#" alt="alt text"
style="width:60px;height:60px;cursor:pointer !important;">
<p class="something" style="cursor:pointer !important;">text! <span
class="otherclass" style="cursor:pointer !important;">more text</span></p></a>
<div class="description pagecontent simple"></div>';
}
add_shortcode( 'helloworld', 'torque_hello_world_shortcode' );
I have php file index.php
In this file to use html code I am doing:
index.php
echo '
<div>
<a class="fragment" href="">
<div>';
In href I want to put value of some php variable i.e. $url How could be done?
is this correct way?
<a class="fragment" href="<?php echo $url; ?>">
You concatenate the string by ending it and starting it again:
echo '
<div>
<a class="fragment" href="' . $url . '">
<div>';
Though I personally prefer to stop the PHP tags and start them again (if I have a lot of HTML) as my IDE won't syntax highlight the HTML as it's a string:
?>
<div>
<a class="fragment" href="<?php echo $url; ?>">link</a>
</div>
<?php
Since you are printing several lines of HTML, I would suggest using a heredoc as such:
echo <<<HTML
<div>
<a class="fragment" href="$url">
<div>
HTML;
HTML can be anything as long as you use the same tag both in the beginning and the end. The end tag must however be on its own line without any spaces or tabs. With that said, specifically HTML also has the benefit that some editors (e.g. VIM) recognise it and apply HTML syntax colouring on the text instead of merely coluring it like a regular string.
If you want to use arrays or similar you can escape the variable with {} as such:
echo <<<HTML
<div>{$someArray[1]}</div>
HTML;
if you are echoing php directly into html i like to do this
<div><?=$variable?></div>
much shorter than writing the whole thing out (php echo blah blah)
if you are writing html in php directly then there several options
$var = '<div>'.$variable.'</div>'; // concatenate the string
$var = "<div>$variable</div>"; // let php parse it for you. requires double quotes
$var = "<div>{$variable}</div>"; // separate variable with curly braces, also requires double quotes
Do it like
<?php
$url='http://www.stackoverflow.com';
echo "<div><a class='fragment' href='$url' /></div>";
If you want to maintain the echo statement you can do either
echo '<a class="fragment" href="'.$url.'"><div>';
or
echo "<a class=\"fragment\" href=\"$url\">";
The first is better for performances and IMHO is more readable as well.
If you want to input/output large blocks of HTML with embedded variables you can simplify the process by using Heredocs:
echo <<<_EOI_
<div>
<a class="fragment" href="$url">
<div>
_EOI_;
You don't have to worry about escaping quotes, constant concatenation, or that ugly dropping in and out of <?php echo $var; ?> that people do.
I need to set a div's background colour using PHP. Here's what I'm doing at the moment:
<div class="box" style="background-color:"<?php echo $permacolour; ?>"">
However, this isn't working. What am I doing wrong?
Thanks
<div class="box" style="background-color:<?=$permacolour?>">
quotes do not belong to PHP syntax. PHP tags are <?, <?php and ?> only
style="background-color:"<?php echo $permacolour; ?>""
You have two sets of quotes here. Try this:
style="background-color:<?php echo $permacolour; ?>"
To be near your original post, here's the correct way :
<div class="box" style="background-color: <?php echo $permacolour; ?>">
The problem is that you were writing the background color surrounded by double quotes. The result would have been like
<div class="box" style="background-color:"red"">
instead of
<div class="box" style="background-color: red">
So just remove the double quotes between the value you are good to go.
Of course, this suppose you defined $permacolour or you made sure the value of $permacolour is sanitized and filtered if inputed by a user.
The answer of #your-common-sense is based on the shorthand syntax that is not always activated on hosted servers.
<div class="box" style="background-color:<?php echo $permacolour; ?>;">
remove the double quotes
style="<?php echo $permacolour ; ?>"
or without quotes at all would be easier for some versions in html
style=<?php echo $permacolor ;?>
this is going to an easy one i think but for the life of me i cant get it to work.
i have a variables $id i want to put it in a img src that i have echo (eg. "uploaded/$id.jpg")
i have tried lots of way and looked all over the net and cant get it to work
echo "
<td>
<img src="uploaded'.$id'.jpg">
</td>
";
this is what the echo looks like if anyone can tell me why it is not work would be a big help
It's because you're mixing your double quotes with single quotes and you forgot the . concat operator after $id. Try this:
echo '
<td>
<img src="uploaded/'.$id.'.jpg">
</td>
';
No one stated the obvious:
Separate HTML and PHP, don't echo HTML:
<td>
<img src="uploaded/<?php echo $id; ?>.jpg">
</td>
You are mixing single and double quotes. Try this:
echo "<td>
<img src='uploaded/{$id}.jpg' />
</td>";
You're using double quotes inside a double quoted string.
echo "<td>
<img src='uploaded{$id}.jpg'>
</td>
";
Your mixing of quote styles is the problem. This should work.
or
echo "<td>
<img src=\"uploaded{$id}.jpg\">
</td>
";
if you're worried about valid html... 100 ways to skin a cat.
Try this:
echo "<td>
<img src=\"uploaded/{$id}.jpg\">
</td>";
EDIT: I think you were missing a / in the image path. Is "uploaded" a directory?
You were mixing single and double quotes. Double quotes allow you to embed variable in them like this... "Hello, $name" but if you tried "name$firstBlue", PHP would assume you were trying to insert the value of $firstBlue. You'd have to use "name{$first}Blue" for that to work. Always placing variables in {curly braces} is a good habit, because it prevents silly mistakes.
why it is not working.
echo "//double quotes begins here
<td>
<img src="/*it will end here later portion will not be printed*/ uploaded'.$id'.jpg">
</td>
";
you can use
echo '<td>
<img src="uploaded/'.$id.'.jpg">
</td>
';
i try to make a while{ part in php, to read out a mysql db. Works perfectly, only with the output i have problems:
echo "<div id=\"content\"><ul class=\"pageitem\"><li class=\"store\"><span class=\"image\" style=\"background-image: url('<?php echo \". $zeile['image'] . \"; ?>')\"></span><span class=\"name\"><?php echo \". $zeile['title'] . \"; ?></span><span class=\"arrow\"></span></li></ul>";
Error is:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\xampplite\htdocs\harti\products.php on line 40
Sry i'm really new with php, i think this is a simple mistake but cannot find it.
thx for help
Don't use <?php again...
echo "<div id=\"content\"><ul class=\"pageitem\"><li class=\"store\"><span class=\"image\" style=\"background-image: url('". $zeile['image'] . "')\"></span><span class=\"name\">". $zeile['title'] . "</span><span class=\"arrow\"></span></li></ul></div>";
Also, you forgot to close the DIV.
Instead, you could do something like this (outside <?php and ?>):
<div id="content">
<ul class="pageitem">
<li class="store">
<a href="index.html">
<span class="image" style="background-image: url('<?php echo $zeile['image']; ?>')"></span>
<span class="name"><?php echo $zeile['title']; ?></span>
<span class="arrow"></span>
</a>
</li>
</ul>
</div>
Read up on how strings work in PHP:
http://php.net/manual/en/language.types.string.php
If you use double quotes you can do this:
echo "<b>{$myarray[$index]}</b>";
Or you can do this:
echo 'blah';
Note how I used single quotes to avoid having to escape the double quotes. However, with a single quote, I cannot embed the variable within the quote.
Try this
echo "<div id='content'><ul class='pageitem'><li class='store'><a href='index.html'><span class='image' style='background-image: url(" . $zeile['image'] . ")'></span><span class='name'>" . $zeile['title'] . "</span><span class='arrow'></span></a></li></ul></div>";
Do not use
try this:
echo "<div id=\'content\'><ul class=\'pageitem\'><li class=\'store\'><a href=\'index.html\'><span class=\'image\' style=\'background-image: url(".$zeile['image'].")></span><span class=\'name\'>".$zeile['title']."</span><span class=arrow></span></a></li></ul></div>";
Check Single quotes also.
And even I agree with "Parkyprg", use html content outside php.
you may replace each " with "+"\""+".
It works in all scripting languages.