I'm trying to pass on the parent url variables to the variables of the iframe url.
For example if the parent url is:
http://mywebsite.com/autos-zoeken?sessid=s838c7e5c3be0452fc38f4ffb6f307ed7&code=be3f&whsearch_key=6568
The iframe url needs to become:
http://anotherwebsite.com/s838c7e5c3be0452fc38f4ffb6f307ed7/be3f/stock/6568/
The code I'm using now is:
<?php
$val1 = $_GET[“sessid“];
$val2 = $_GET[“code“];
$val3 = $_GET[“whsearch_key“];
echo "<iframe src='http://anotherwebsite.com/' . $val1 . '/' . $val2 . '/stock/' . $val3 . '/' id='blockrandom' width='1000' height='1200'
scrolling='auto'
frameborder='0'
class='wrapper'>
Your browser doesn't support inline frames.</iframe>";
?>
The result on the website is:
http://anotherwebsite.com/' . . '/' . . '/stock/' . . '/' id='blockrandom1' etc
So the variables aren't being put in the right place in the iframe url
What am I doing wrong here?
this should work fine:
echo "<iframe src='http://anotherwebsite.com/$val1/$val2/stock/$val3/' id='blockrandom' width='1000' height='1200' scrolling='auto' frameborder='0' class='wrapper'> Your browser doesn't support inline frames.</iframe>";
the problem is with all your quote's inside your src, it thinks it needs to stop your src after http://anotherwebsite.com/ and you don't need to use "." inbetween because you used doubleqouote on start, you can just use variables inside doublequotes.
echo "<iframe src='http://anotherwebsite.com/$val1/$val2/stock/$val3/' id='blockrandom' width='1000' height='1200' scrolling='auto' frameborder='0' class='wrapper'>Your browser doesn't support inline frames.</iframe>"; ?>
you don't need to escape your variables
You're not actually concatenating multiple strings, you're just building a single string. Take a look at a simplified example:
"<iframe src='http://anotherwebsite.com/' . $val1"
That's just one string which happens to have a period between some spaces. In order to use the . concatenation operator you need to terminate the string first:
"<iframe src='http://anotherwebsite.com/'" . $val1
Related
I am newbie in php, but I need to modify some string in order to add a link with rel intro a visual composer shortcode, an animated icon.
The problem is that I have this variable, by default where i should add two variables only if insered.
Here is the originary code:
$svg_icon = '<div class="svg-icon-holder" data-size="'. $icon_size . '" data-animation-speed="'.$animation_speed_time.'" data-animation="'.$enable_animation.'" data-animation-delay="'.$animation_delay.'" data-color="'.strtolower($color) .'"><span>'. get_template_directory_uri() . '/css/fonts/svg/' . $image .'</span></div>';
The problem is that: I should insert a before the tag <div> into the code and a after the closing div BUT this only if variable &icon_link is set, if not, the <a> tag should not appear as I do not need to have a link to all icons.
Hope I have explained myself well, if not let me know!
Regards, Alin.
Do an if-statement, if $icon_link is defined and set, then add your <a> tag to your $svg_icon variable using a dot(.):
$svg_icon = '<div class="svg-icon-holder" data-size="'. $icon_size . '" data-animation-speed="'.$animation_speed_time.'" data-animation="'.$enable_animation.'" data-animation-delay="'.$animation_delay.'" data-color="'.strtolower($color) .'"><span>'. get_template_directory_uri() . '/css/fonts/svg/' . $image .'</span></div>';
if($icon_link)
{
$svg_icon = '<a href="'. $icon_link . '" rel="'. $icon_link_rel .'">'.
$svg_icon.'</a>';
}
Just use a if statment. You are manipulating string, so you can easily add words to an other string like that
if ($icon_link){
$beginning = "<a href='$icon_link' rel='$icon_link_rel'>";
$ending = "</a>"
}
else {
$beginning = "";
$ending = ""
}
$svg_icon = "$beginning<div class='svg-icon-holder' data-size='$icon_size' data-animation-speed='$animation_speed_time' data-animation='$enable_animation' data-animation-delay='$animation_delay' data-color='".strtolower($color)."'>
<span>".get_template_directory_uri()."/css/fonts/svg/$image</span></div>$ending";
Personally I prefer to format string using sprintf instead of pasting the string together using dots. You could put the $svg_icon string together using sprintf with only the <div> tag. After that just wrap an <a> tag around $svg_icon if $icon_link is defined:
$svg_icon_format = '<div class="svg-icon-holder" data-size="%d" data-animation-speed="%d" data-animation="%d" data-animation-delay="%d" data-color="%d"><span>%s</span></div>';
$values = array(..enter values here..);
$svg_icon = sprintf($svg_icon_format,$values);
if(isset($icon_link) && isset($icon_link_rel)) {
$svg_icon = sprintf('%s',$icon_link,$svg_icon,$icon_link_rel);
}
Disclaimer: This code is not tested. Please look at the sprintf documentation I linked and try writing the code yourself.
First, do not write everything on one line, so it fits on the screen here on SO (and maybe in your code too).
Second, I would use instring variables. In php you can use singlequotes, where your string gets used as-is, or you can use doublequotes, where you can use variables in it. like echo "Hey, my name is $name"; $name here would get exchanged by the value of the variable. If you need doublequotes in the string, you can escape them with a backslash like \"
Third, you can use inline if-else, folowing syntax: $str = boolean ? "str if true" : "str if false"
$svg_icon = isset($icon_link) ? "<a href=\"$icon_link\"
rel=\"$icon_link_rel\">" : '' . //add your opening <a> if needed
"<div class=\"svg-icon-holder\"
data-size=\"$icon_size\" data-animation-speed=\"$animation_speed_time\"
data-animation=\"$enable_animation\"
data-animation-delay=\"$animation_delay\"
data-color=\"strtolower($color) \"><span>"
. get_template_directory_uri()
. "/css/fonts/svg/ $image </span></div>"
. isset($icon_link) ? '<\a>' : ''; //add your closing <\a> if needed
I am having trouble with my PHP code. I've been changing everything for 6 hours and I still get Parse errors no matter what I do. This is the code:
$slider3 = '<img src="'templates/' . $this->template . '/images/slider/slider3.jpg'">' . '" alt="' . $sitename . '" />';
The only way I can figure to not get it to throw an error is by writing it this way:
$slider3 = '<img src="templates/" . $this->template . "/images/slider/slider3.jpg" . "/>"';
but I don't think that's right.
I want $slider3 = "templates/MYTEMPLATE/images/slider/slider3.jpg" then later I will echo $slider3;
I get so confused with all the single and double quotation marks. I think the first one is right - I look at it and study it and it looks right to me. But it throws a parse error.
$slider3 = '<img src="templates/'.$this->template.'/images/slider/slider3.jpg"/>';
should work.
Explanation:
'<img src="templates/'
is a single-quoted string, which happens to contain a double-quote (which is needed for the html src attribute, or any other html attribute value really)
.
(dot) is the string concatenation operator. It concatenates ("glues") the first string together with...
$this->template
which is presumably a string containing the name of the template (not clear from your code example). Note that if $this->template comes from user input, or an otherwise unvalidated source, it could be used for cross-site scripting, eg. if it contains "><script>alert("XSS!")<script>, javascript is executed in the browser!
.
another concatenation with...
'/images/slider/slider3.jpg"/>'
which is another single-quoted string which happens to contain a double-quote, ending the src attribute value.
Try this:
$slider3 = '<img src="templates/"' . $this->template . '"/images/slider/slider3.jpg"/>';
$template = "MYTEMPLATE";
$slider3 = '<img src="templates/'.$template.'/images/slider/slider3.jpg"/>';
echo $slider3;
Will echo - >
<img src="templates/MYTEMPLATE/images/slider/slider3.jpg"/>
Just write:
<?php
$templates = "var";
echo "<img src='templates/${templates}/images/slider/slider3.jpg'/>";
it will result in
<img src='templates/var/images/slider/slider3.jpg'/>
I’m trying to get the URL below to change the hostname of a server with Digital Oceans API
<a href=\"https://api.digitalocean.com/droplets/$serverid/rename/?client_id=$myClientID&api_key=$myDOApi&name=newHostName\" target=\"_blank\">
<font color=\"#CCFB5D\">Rename</font></a>
This link works perfectly if I manually type the new hostname in the code, but how would I pass a newHostName variable from a text box into the url with PHP as it is submitted?
This is your code as presented:
<a href=\"https://api.digitalocean.com/droplets/$serverid/rename/?client_id=$myClientID&api_key=$myDOApi&name=newHostName\" target=\"_blank\">
<font color=\"#CCFB5D\">Rename</font></a>
But then you say:
…but how would I pass a newHostName variable from a text box into the url with PHP as it is submitted?
Why not just make a URL like this and set $newHostName as a variable $newHostName?
https://api.digitalocean.com/droplets/$serverid/rename/?client_id=$myClientID&api_key=$myDOApi&name=$newHostName
But your code is lacking. Why are the double-quotes escaped like this \"? You have a PHP tag but where is the actual PHP? Is it already a part of a larger string that has double-quotes already in place?
Assuming good faith, why can’t your code—as presented—be like this instead?
echo '<a href="https://api.digitalocean.com/droplets/' . $serverid . '/rename/?client_id=' . $myClientID . '&api_key=' . $myDOApi . '&name=' . $newHostName . '" target="_blank">
<font color="#CCFB5D">Rename</font></a>';
Or perhaps use sprintf to make things more readable:
echo sprintf('<a href="https://api.digitalocean.com/droplets/%s/rename/?client_id=%s&api_key=%s&name=%s" target="_blank">', $serverid, $myClientID, $myDOApi, $newHostName)
. '<font color="#CCFB5D">Rename</font></a>'
;
I am trying to print the image whose location is saved in my database, I have stored the absolute location in the database and not the relative one , I browsed through a lot of question including this one
include a PHP result in img src tag
I tried all the options that were given to the respective asker of the question but I didn't get my output, rest everything is being displayed apart from the image, its showing no file found
Here's my code, any help will be appreciated
while($result=#mysql_fetch_array($resul,MYSQL_ASSOC)){
$image = $result['image'];
echo $result['company'] . " " . $result['model'] . "<br>" ;
echo '<img src="$image" height="50" width="50" />';
}
I know I am using mysql functions instead of mysqli but this code is not getting live ever.
As watcher said, PHP does not do variable interpolation within single-quoted strings.
The most important feature of double-quoted strings is the fact that variable names will be expanded.
Read more about strings from the PHP manual.
Therefore, when you view the HTML, you will literally see this:
<img src="$image" height="50" width="50" />
Your code should be:
while($result = mysql_fetch_array($resul,MYSQL_ASSOC)) {
$image = $result['image'];
echo $result['company'] . " " . $result['model'] . "<br>";
echo "<img src='$image' height='50' width='50'>";
}
Alternatively, interpolate the array value:
while($result = mysql_fetch_array($resul,MYSQL_ASSOC)) {
echo $result['company'] . " " . $result['model'] . "<br>";
echo "<img src='{$result['image']}' height='50' width='50'>";
}
If the filename contains spaces or other special characters, you may need to use rawurlencode(). In this case, you must concatenate the string since you are calling a function that returns a string value:
echo "<img src='" . rawurlencode($result['image']) . "' height='50' width='50'>";
PHP will not interpolate variables when you include them within single quotes. For more information, see the manual.
I'm trying to get the caption part of my slider to link to the respective article url on my wordpress site. I'm pretty sure I found the section in the plugin code I think needs to be edited, but when I try to do the following:
<?php
echo "<div id='" . $sl_caption . "' class='nivo-html-caption'>" . $sl_htmlcaption . onclick="location.href='$url';"</div>";
?>
I get "syntax error, unexpected T_STRING, expecting ',' or ';" showing up in my slider, and the rest of my site not working. I've tried many variations of what I've tried to do here, but I can't seem to find one that works.
Here's the original entire slider code if it's helpful: http://pastebin.com/4nKxXkSa
Your opening/closing of double-quotes is wrong : in PHP, strings must be enclosed in either quotes or double-quotes ; and if you want to put a double-quote in a double-quotes enclosed string, you have to escape it with a \.
For example, you need to open a quote or double-quote before onclick, as this is part of a string.
Also, your onclick should be inside the <div ...> tag, and not between <div> and </div>.
In the end, your PHP code would look a bit like this (I've set up hard-coded values for the variables, to help with testing) :
$sl_caption = 'ID';
$sl_htmlcaption = 'HTML';
$url = "URL";
echo "<div id='"
. $sl_caption
. "' class='nivo-html-caption' onclick=\"location.href='$url'\">"
. $sl_htmlcaption
. "</div>"
;
And you'd get the following HTML as output :
<div id='ID' class='nivo-html-caption' onclick="location.href='URL'" >HTML</div>
Your string quotes are not correctly placed. Try this
echo "<div id='" . $sl_caption . "' class='nivo-html-caption'>" . $sl_htmlcaption . "onclick=\"location.href='$url';\"</div>";
The problem is what it says - "onclick" isn't a quoted string in your example (T_STRING in PHP). Everything you join together (concatenate) in a string needs to be either a) a string in quotes (single or double), b) something that can be converted to a string or c) a variable/constant/function call.
If you didn't have that error then your current example would also have the "onclick" as the content of the tag, rather than an attribute on the tag. What I think you want is:
<?php
echo "<div id='" . $sl_caption . "' class='nivo-html-caption' onclick='location.href=\'" . $url . "\''>" . $sl_htmlcaption . "</div>";
?>
If you want standard HTML attributes then you'd normally use double-quotes, which would give you:
<?php
echo '<div id="' . $sl_caption . '" class="nivo-html-caption" onclick="location.href=' . $url . '">' . $sl_htmlcaption . '</div>';
?>
Also, is there any reason why you're using an onclick to set the current location rather than a normal <a href>?
the qoute isn't the only problem. The onclick is also out of the div element's properties. It must be (changes the doubles for singles to make it more clear):
echo '<div id="' . $sl_caption . '" class="nivo-html-caption" onclick="location.href=' . $url . ';">' . $sl_htmlcaption . '</div>';
Your quotes are the problem. It should be:
echo "<div id='" . $sl_caption . "' class='nivo-html-caption'>" . $sl_htmlcaption . "onclick=\"location.href='$url';\"</div>";
You needed double quotes to start the string again after the concatenation operator (.), but then you need to escape the double quotes inside this string wit a blackslash so the PHP interpreter won't think the string has ended too soon.
And as Pascal pointed out, the onclick attribute should actually be inside the stating div tag anyway:
echo "<div id='" . $sl_caption . "' class='nivo-html-caption'onclick=\"location.href='$url';\">" . $sl_htmlcaption . </div>";
Using interpolation may make things easier for you also. When using double quotes to delimit a string you can insert variables' values using curly brackets ({}) like this:
echo "<div id='{$sl_caption}' class='nivo-html-caption'onclick=\"location.href='{$url}';\">{$sl_htmlcaption}</div>";
This way you only have to open and close the string once (at the beginning and end).
Beware:
You cannot interpolate function calls directly. You can either concatenate like "<p>".strlen($x)."</p>" or store the result in a variable and the interpolate like before; "<p>{$result}</p>".
You'll still have to escape double quotes within the string though.