variables in img src - php

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>
';

Related

How can I make this line work, and what's the science behind these quotation marks?

I really need to get the following line working and apart from that, would love to read an article or anything of that form to finally know how to use my quotation marks properly.
echo '<div class="video"><iframe width="540" height="330" src="'//www.youtube.com/embed/ . $row['link']"/>'</frame>;
Should be good :
echo '<div class="video"><iframe width="540" height="330" src="//youtube.com/embed/'. $row['link'] .'"></iframe></div>';
But read comments, your error was pretty easy to find with highlights syntax.
The quotes are not placed properly.
Try this:
<?php
$link='//www.youtube.com/embed/'. $row['link'];
?>
<div class="video">
<iframe width="540" height="330" src="<?php echo '//www.youtube.com/embed/'. $row['link']; ?>" /></iframe>
</div>

php variable inside echo 'html code'

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.

Php echo " and '

Can someone help me with this code, I know what I'm doing wrong but I don't know how to repair it.
echo "<div onclick='showplayer('".$usersList["username"]."')' id='playerLookupNameResult' style='color:".$color.";'>Some text</div>
I need to use " after onclick= but it is already used after echo.
you can escape " like this \".
echo "<div onclick=\"'showplayer('".$usersList["username"]."')'\" id='playerLookupNameResult' style='color:".$color.";'>Some text</div>

Echoing javascript with events and function calls with parameters in PHP

I want to echo the following in PHP:
echo "<td><p style='visibility: hidden' id='joindata$rowindex'>$joindata</p><a style='visibility: visible;' onclick='toggleDisplay('joindata$rowindex'); toggleDisplay('showjoindata$rowindex')' id='showjoindata$rowindex'>Show</a></td>";
But it is not echoing due to the nested ' ' Any Ideas how I can echo HTML elements which have events which call javascript functions which have parameters in PHP would be much appreciated, thanks :)
Just use double-quotes to delimit your attribute values, and escape them in your PHP string:
echo "<td><p style=\"visibility: hidden\" id=\"joindata$rowindex\">$joindata</p><a style=\"visibility: visible;\" onclick=\"toggleDisplay('joindata$rowindex'); toggleDisplay('showjoindata$rowindex')\" id=\"showjoindata$rowindex\">Show</a></td>";
Or, you could separate behaviour from content and just add the event handlers dynamically.
You have to add some double quotes, Try this:
echo "<td><p style=\"visibility: hidden\" id=\"joindata".$rowindex."\">$joindata</p><a style=\"visibility: visible;\" onclick=\"toggleDisplay('joindata".$rowindex."'); toggleDisplay('showjoindata".$rowindex."')\" id=\"showjoindata".$rowindex."\">Show</a></td>";
PHP have best performance and scalability when you separate the views from the data. However in this case you could do:
<td>
<p style="visibility: hidden" id="joindata<?php echo $rowindex; ?>">
<?php echo $joindata; ?>
</p>
<a style="visibility:visible" onclick="toggleDisplay('joindata<?php echo $rowindex; ?>'); toggleDisplay('showjoindata<?php echo $rowindex; ?>')" id="showjoindata<?php echo $rowindex; ?>">Show</a>
</td>
Also take a look on the jQuery docs to avoid hard-coding the javascript functions. Your app will run faster.

where is wrong in my php code?

'.{$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>

Categories