Hi
I am writing a php code and and am supposed to display a large number of images on the webpage and hence I am using mysql data base to store the coordinates and taking them using SELECT and then trying to position them.
But I am not able to parse the cordinate as variable.
echo "img style=\"position :absolute; top:123px;left:123px\" border=\"0\" src=\"throbber.gif\" alt=\"Image\"/>";
in this place I want to do:
echo "img style=\"position :absolute; top:$variable1 px;left:variable2 px\" border=\"0\" src=\"throbber.gif\" alt=\"Image\"/>";
Please help me with this..
Try the following:
echo '<img style="position:absolute;top:'.$posX.'px;left'.$posY.':px;border:none;" src="throbber.gif" alt="Image" />
Note that you need to start the <img>-tag with a <. I moved the border-attribute to CSS (styles do not belong in markup) and used single quotes (') instead of double quotes ("), to prevent clashes inside the string (escaping sequences are rather ugly to read). Besides that, i used string concatenation (operator is .) to add the variables to the string.
Since there shouldn't be a space between the variable and the "px" you could use the concatenation operator "." for doing this.
It works like this:
$a = "hello";
$b = 10;
echo $a . $b; // Output: hello10
So
echo '<img style="position: absolute; top: ' . $variable1 . 'px; left: ' . $variable2 . 'px;" border="0" src="throbber.gif" alt="Image">';
should work.
You can use curly brackets around the variables:
echo "<img style=\"position: absolute; top: {$variable1}px; left: {$variable2}px\" border=\"0\" src=\"throbber.gif\" alt=\"Image\"/>";
Related
I am currently working on a Webproject for my school which is build with HTML, PHP and SQL Databases for dynamic content. Until now everything works out pretty good but I have reached a point where I have to echo something out which contains many Characters like '' and "" which pretty much makes it impossible to use PHP echo with those starting tags ('' and ""). Is there any other way to start a PHP echo ?
if ($rows[$number]['kulturschule'] == 1) {
echo '<div class="tp-caption tp-resizeme hover-scale"
data-x="center"
data-y="center"
data-voffset="[290, 290, 250, 210]"
data-hoffset="0"
data-frames='[{"delay":1000,"speed":2000,"frame":"0","from":"sX:0.9;sY:0.9;opacity:0;fb:20px;","to":"o:1;fb:0;","ease":"Power3.easeInOut"},{"delay":"wait","speed":500,"frame":"999","to":"sX:0.9;sY:0.9;opacity:0;fb:20px;","ease":"Power3.easeInOut"}]'
style="z-index: 20; max-width: auto; max-height: auto; white-space: nowrap;"><img src="img/logo/kulturschule.jpg"> ';
This is a perfect situation to use HEREDOC:
// put all the html in a variable:
$html = <<<EOT
<div class="tp-caption tp-resizeme hover-scale"
data-x="center"
data-y="center"
data-voffset="[290, 290, 250, 210]"
data-hoffset="0"
data-frames='[{"delay":1000,"speed":2000,"frame":"0","from":"sX:0.9;sY:0.9;opacity:0;fb:20px;","to":"o:1;fb:0;","ease":"Power3.easeInOut"},{"delay":"wait","speed":500,"frame":"999","to":"sX:0.9;sY:0.9;opacity:0;fb:20px;","ease":"Power3.easeInOut"}]'
style="z-index: 20; max-width: auto; max-height: auto; white-space: nowrap;"><img src="img/logo/kulturschule.jpg">
EOT;
// note, that EOT; has to be at the very start of the line.
// then:
echo $html;
I have reached a point where I have to echo something out which contains many Characters like '' and "" which pretty much makes it impossible to use PHP echo with those starting tags
Then you should escape those characters inside. cf. http://php.net/string
When you look for an alternative, you can use HEREDOC/NOWDOC syntax (see link above).
Yes you have the possiblity to use this format:
$text = <<<EOT
Place your text between the EOT. It's
the delimiter that ends the text
of your multiline string.
$var
EOT;
If you want to use raw strings use this format:
$var = "foo";
$text = <<<'EOT'
My $var
EOT;
This will ignore the $var and print it as-is
Note:
You can not indent the EOT;
Another solution is to go way old-school and use php like it was used 10 years ago:
<?php
if ($rows[$number]['kulturschule'] == 1) {
?>
<div class="tp-caption tp-resizeme hover-scale"
data-x="center"
data-y="center"
data-voffset="[290, 290, 250, 210]"
data-hoffset="0"
data-frames='[{"delay":1000,"speed":2000,"frame":"0","from":"sX:0.9;sY:0.9;opacity:0;fb:20px;","to":"o:1;fb:0;","ease":"Power3.easeInOut"},{"delay":"wait","speed":500,"frame":"999","to":"sX:0.9;sY:0.9;opacity:0;fb:20px;","ease":"Power3.easeInOut"}]'
style="z-index: 20; max-width: auto; max-height: auto; white-space: nowrap;"><img src="img/logo/kulturschule.jpg">
<?php } ?>
You can use the following way to escape quotes inside echo :
if ($rows[$number]['kulturschule'] == 1) {
echo "<div class='tp-caption tp-resizeme hover-scale'
data-x='center'
data-y='center'
data-voffset='[290, 290, 250, 210]'
data-hoffset='0'
data-frames='[{\"delay\":1000,\"speed\":2000,\"frame\":\"0\",\"from\":\"sX:0.9;sY:0.9;opacity:0;fb:20px;\",\"to\":\"o:1;fb:0;\",\"ease\":\"Power3.easeInOut\"},{\"delay\":\"wait\",\"speed\":500,\"frame\":\"999\",\"to\":\"sX:0.9;sY:0.9;opacity:0;fb:20px;\",\"ease\":\"Power3.easeInOut\"}]";
You can escape charachters:
\' single quote
\" double quote
So I've created a CMS that takes text input. This is how the data is used when I grab it from the database.
echo "<img src=" . $row['image_url'] . " alt=" . $row['caption'] . ">";
Now the problem is, whenever there's a comma or a semi colon, php treats it as part of the code and the page ends up either not rendering well or completely breaking with errors like
Parse error: syntax error, unexpected '=' in
I've tried using htmlspecialcase() when posting the data to the MySQL database but it didn't fix the problem.
EDIT: The main problem is with the alt part not the src part.
When you're using double quotes (") to create a string literal and in that string literal you want to use double quotes ("), then you may escape those double quotes (") to form a valid string.
echo "<img src=\"" . $row['image_url'] . "\" alt=\"" . $row['caption'] . "\">";
You can try like that:
<img src="<?php echo $row['image_url'];?>" alt="<?php echo $row['caption'];?> ">
I have a problem with my page. I am displaying images using PHP Loop statement. Now I want to assign these images with different id's. Example first loop the first image displayed will have an id="img1", next loop and second image has id="img2". Numbers on the id changes based on the loop iteration variable, while "img" is constant. Here's my code:
for ($i=1;$i<=6;$i++){
echo ("<img src='gangjeong.png' width='113' id='img'.$i>");
}
but it's not working. Any help would be much appreciated.
UPDATE: I got it to work now, thanks for the answers. The working code is:
for ($i=1;$i<=6;$i++){
echo ("<img src='gangjeong.png' width='113' id='img$i'>");
}
You can't use unescaped single quotes if you start your string by single quotes.
These are the possibilities you have:
Using double quotes inside single quotes:
for ($i=0;$i<6;$i++){
echo ('<img src="gangjeong.png" width="113" id="img' . ($i+1) . '" />');
}
Using escaped single quotes inside single quotes (ugly):
for ($i=0;$i<6;$i++){
echo ('<img src=\'gangjeong.png\' width=\'113\' id=\'img' . ($i+1) . '\' />');
}
Using double quotes for starting/ending the string:
for ($i=0;$i<6;$i++){
echo ("<img src='gangjeong.png' width='113' id='img" . ($i+1) . "' />");
}
Using escaped double quotes inside double quotes (ugly):
for ($i=0;$i<6;$i++){
echo ("<img src=\"gangjeong.png\" width=\"113\" id=\"img" . ($i+1) . "\" />");
}
Quotes inside quotes are a no no without proper escaping. Also you have a missing > for the img tag. This should do:
for ($i=1;$i<=6;$i++){
echo "<img src='gangjeong.png' width='113' id='img".$i."'>";
}
Where's your escape characters? Your quotes need escaping, or put your HTML part in double quotes and PHP strings in single, or vice versa. Just make sure you're not confusing start and end quotes.
your syntax is not correct, use:
for ($i=1;$i<=6;$i++){
echo '<img src="gangjeong.png" width="113" id="img'.$i.'" alt="">';
}
Use:
for ($i=1;$i<=6;$i++){
echo "<img src=\"gangjeong.png\" width=\"113\" id=\"img$i\">";
}
Hey guys, I have the following code:
foreach($collection as $img)
{
$image_id = $img['imageid'];
$thumbwidget = wp_get_attachment_image_src($image_id, 'full');
$gallery .= '<a class="fav-image-a" href="http://www.bangstyle.com/haircut-detail/?uid='.$uid.'&img_id='.$image_id.'&ucolid='.$user_id.'&catid='.$col_id.'&theater">';
$gallery .= '<img src="';
$gallery .= thumbGen($thumbwidget[0],259,320,'valing=top');
$gallery .= '">';
$gallery .= '</a>';
}
I think I may have the wrong order of escaping. The rendered variable is not staying within the img src when rendered. I assume it has to do with my escaping somewhere.
The live url can be seen at http://bangstyle.com/test-widget/
You can see what's happening. The rendered elements are on top.
Why the extra quotes inside? What you are producing is this:
<img src="'THUMBWIDGETURL_IS_INSERTED_HERE'">
What you probably want is this:
<img src="THUMBWIDGETURL_IS_INSERTED_HERE">
To do that just remove the extra \':
$gallery .= '<img src="'.$thumbwidgeturl.'">';
Rules to be aware of:
In PHP, both single quotes and double quotes can be used to produce string literals.
Each should be used in a pair and that pair constitutes one string literal. So, in your example you have two string literals and a variable being combined (concatenated) with the dot (.) operator.
Inside single quotes, single quotes need to be escaped, and inside double quotes, double quotes need to be escape. The other type of quotes in each can be used freely without escaping.
Strings inside single quotes are taken as they are, while strings inside double quotes are interpreted for variables.
More information in the PHP docs on Strings.
How about this:
$gallery .= "<img src=\"" . $thumbwidgeturl . "\">";
or even:
$gallery .= '<img src="' . $thumbwidgeturl . '">';
I was provided with a set of data that represents URL links. Such as: "doug'sfood.jpg".
.
I keep these strings in an array, and then select them randomly to display inserting them into an
<img src="doug'sfood.jpg"></img>
What Chrome is putting out is:
<img src="doug'sfood.jpg"></img>
I tried replacing the quotes with a php escaped (\') apostrophe, but this just ended the quote prematurely.
Can someone help me? Thanks.
I think Chrome automatically escapes characters that are not correctly escaped.
Always use:
<img src="quote'quote.jpg" alt="" />
Instead of:
<img src="quote'quote.jpg" alt="" />
Certain characters should always be escaped in HTML, for example:
' -> '
& -> & or &
Check the htmlspecialchars() and urlencode() functions, example:
$string = "quote'quote.jpg";
echo htmlspecialchars($string, ENT_QUOTES);
// quote'quote.jpg
echo urlencode($string);
// quote%27quote.jpg
Anyway, when printing out the filename tags, use urlencode() rather than relying on HTML escapes or browser behaviour:
foreach ($img as $href) {
print '<img src="' . urlencode($href) . '" />';
}
This will become doug%27sfood.jpg in your example (AKA the correct way to do it). Which hopefully can be located by your webserver.