I am trying to generate a specific link and accompanying html depednant on the existance of a file. The code I am using to do so:
if(file_exists('../images/'. $pk . '.jpg'))
{
$imageSrc = "../images/". $pk . ".jpg";
$imagehtml = htmlentities(json_encode("<img src=\"".$imageSrc."\" >"));
$screenshotLink = "<p>View Screenshot";
}
else {
$screenshotLink = '';
}
This results in the following, useless html:
View Screenshot
I don't understand this, because the above is essentialy the same code as:
$html = htmlentities(json_encode($ARTICLE_DESC));
$imagehtml = htmlentities(json_encode("<img src='".$imageSrc."' >"));
echo "<a href='#' onclick=\"makewindows(" . $imagehtml . "); return false;\">
<img src='".$imageSrc."' width='".$imageSize["width"]."' height='".$imageSize["height"]."'></a>
<p>Click for full description </p>";
which produces the following html which works fine:
<a href="#" onclick='makewindows("<img src=\"..\/images\/160329461329.jpg\" >"); return false;'>
<img src="../images/160329461329.jpg" width="199" height="300"></a>
I know it has something to do with quotes, but I am not sure what exactly.
Try this:
$imagehtml = htmlspecialchars(json_encode("<img src=\"".$imageSrc."\" >"), ENT_QUOTES);
$screenshotLink = "<p>View Screenshot";
$imagehtml = htmlspecialchars(json_encode('<img src="'.$imageSrc.'" >'), ENT_QUOTES);
$screenshotLink = '<p>View Screenshot';
Why not use ticks?
Lookup the ENT_NOQUOTES parameter in the php manual
And htmlspecialchars() != htmlentities() btw.
Related
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 3 years ago.
I'm essentially converting this php code into a string assigned to a var so I can return it's value within a function:
<?php if ($add_cta=='yes' ){?>
<a class="button" href="<?php echo strip_tags(trim($a_href)); ?>">
<?php echo strip_tags(trim($a_title)); ?>
</a>
<?php } ?>
I have converted the above into the following:
$html = '
($add_cta == "Yes" ? .
' < a class = "button" href = "'.strip_tags(trim($a_href)).
'" > '.strip_tags(trim($a_title)).
' < /a>'. : "")
';
return $html;
But getting unexpected '.' errors on the line ($add_cta == "Yes" ? .'
But that is required to concat the string and php, right? where am I going wrong
You have to correct the usage of your single quotes. Especially the first and the last single quotes are not necessary. PHP does not execute any code inside the single quotes. You can use double quotes, but this will only print variables and makes things more complicated in combination with HTML. The following code uses the correct single quotes:
$html = ($add_cta == "Yes" ? .
'<a class="button" href="'.strip_tags(trim($a_href)).'">'.
strip_tags(trim($a_title)).
'</a>'. : '');
return $html;
Or just use an if statement:
$html = '';
if ($add_cta == "Yes")
{
$href = strip_tags(trim($a_href));
$title = strip_tags(trim($a_title));
$html .= ' <a class="button" href="'.$href.'">'.$title.'</a>';
}
return $html;
try this. You have done some concatenation mistakes which I have fixed
$a_href = "stackoverflow.com";
$a_title = 'Anything';
$html =
($add_cta == "Yes" ?
' < a class = "button" href = "'.strip_tags(trim($a_href)) .
'" > '.strip_tags(trim($a_title)) .
' < /a>' : "")
;
echo $html;
I think the most simple/readable way to do this is by using a separate template which returns the rendered link.
link-template.php
<?php
return '' . strip_tags(trim($a_title)) . '';
The method/function you want to use this template on:
return $add_cta === 'Yes' ? include 'link-template.php' : '';
What you should consider would be to define $a_href and $a_title before including the template
I have an webpage where I want to have another html page displayed. I used iframes. The page gets to know what to load is via the get procedure. But there is an fault in this coding I think...
<iframe src="
<?
$file = ($_GET['ti'])
if ($title = '')
echo "information.html";
else echo "$file";
?>
"></iframe>
The url the page would recieve looks like this:
http://www.website.com/reference.html?ti=unlimited.html
http://www.w3schools.com/php/php_if_else.asp
It's your if / else syntax and over all php code. It's not very well written.
<?php
$file = $_GET['ti'];
if ($title = '') {
echo "information.html";
} else {
echo "$file";
}
?>
Need semicolon:
$file = ($_GET['ti']);
Use empty(), like this:
<iframe src="
<?
$file = ($_GET['ti'])
if (empty($title))
echo "information.html";
else echo $file;
?>
"></iframe>
<?php
$possible = array("information.html", "home.html", "test.html");
$file = isset($_GET['ti']) &&
in_array($_GET['ti'], $possible)? $_GET['ti'] : "information.html";
?>
<iframe src="<?php echo $file;?>"></iframe>
I am trying to make a variable which contains some HTML tags, this isn't working like I want it to. I was hoping someone could tell what I am doing wrong here.
My Code:
$foto = "put picture here";
$naam = 'Sieraad1';
$prijs = '20,00';
$artikel = '<img src="'$foto'"><h4>'$naam'</h4><h6>€'$prijs'</h6>';
echo '<table><tr><td>'.htmlspecialchars(stripslashes($artikel)).'</td><td>'.htmlspecialchars(stripslashes($artikel)).'</td><td>'.htmlspecialchars(stripslashes($artikel)).'</td><td>'.htmlspecialchars(stripslashes($artikel)).'</td></tr>';
echo '<table><tr><td>'.htmlspecialchars(stripslashes($artikel)).'</td><td>'.htmlspecialchars(stripslashes($artikel)).'</td><td>'.htmlspecialchars(stripslashes($artikel)).'</td><td>'.htmlspecialchars(stripslashes($artikel)).'</td></tr>';
echo '</table>';
$artikel = '<img src="'$foto'"><h4>'$naam'</h4><h8>€'$prijs'</h8>';
You are missing . here between the variables for concatenation.
Using htmlspecialchars on html code will convert < to <, > to > and " to ". So it will obviously break your code. Run the htmlspecialchars on the inner contents instead:
$artikel = '<img src="' . htmlspecialchars($foto) . '"><h4>' . htmlspecialchars($naam) . '</h4><h8>€' . htmlspecialchars($prijs) . '</h8>';
echo '<table><tr><td>'.$artikel.'</td>...';
var concatenation is missing for variable $artikel. replace user code line with below:
$artikel = '<img src="'.$foto.'"><h4>'.$naam.'</h4><h8>€'.$prijs.'</h8>';
easy to use
<?php
echo <<< END
<table class="head"><tr>
<td class='head'>$name</td>
<td>$fname</td>
</tr></table>
END;
?>
or
<?php
echo "<table class='head'><tr>
<td class='head2'>$name</td>
<td class='head3'>$fname</td>
</tr></table>";
?>
try
$artikel = "<img src=\"'$foto'\"><h4>'$naam'</h4><h8>€'$prijs'</h8>";
or
$artikel = '<img src="' . $foto . '"><h4>' . $naam . '</h4><h8>€' . $prijs .'</h8>';
then just echo the $artikel - you don't need the htmlspecialchars
I am having a problem converting a div to a php variable. This works great as a div but I need to implement this into my php script to show this if the user is viewing another uses page this will display.
//Check to see if you are tracking this member.
$sqlFollow = mysql_query("SELECT * FROM follow WHERE follower_id= " .$id. " and myMember = " .$viewerID . " LIMIT 1");
$numTrack = mysql_num_rows($sqlFollow);
if ($numTrack < 1) {
$divValue = "Track This Person";
$onclick = "trackMember";
}
else {
$divValue = "Don't Track Person";
$onclick = "donttrackMember";
};
$display_tracking_option = '<div class="track_btn_div" id="addremoveTrack"><span class="follow_b">'.$divValue.'</span></div>';
Saving this in PHP as a value I am having trouble with the " vs '.
$display_tracking_option = '<div class="track_btn_div" id="addremoveTrack"><span class="follow_b">"'.$divValue.'"</span></div>';
You are adding the stuff to a variable using php so you don't need to echo anything:
$display_tracking_option = '<div class="track_btn_div" id="addremoveTrack"><span class="follow_b">'.$divValue.'</span></div>';
Simply concatenate the string.
Alternatively you could use sprintf():
$theString = '<div class="track_btn_div" id="addremoveTrack"><span class="follow_b">%s</span></div>'
$display_tracking_option = sprintf($theString, $onclick, $id, $viewerID, $divValue);
Another thing: why do you have inline stuff?
You probably want this:
$display_tracking_option = '<div class="track_btn_div" id="addremoveTrack"><span class="follow_b">'.$divValue.'</span></div>';
I am trying to .load a script called 'refreshImages.php'. Inside that script is a while loop pulling from the database. I have got it to load a single echo function but it wont load anything inside the while loop I have on the script... this is what the php file has...
<?php
include 'includes/config.php';
$pimages = mysql_query("SELECT * FROM property_images WHERE pid='$pid'");
//Cant Post Images So Leaving The Echo Content Out//
while($img = mysql_fetch_array($pimages)){
$image = $img['image'];
$image_alt = $img['image_alt'];
echo "<li>$img</li>";
}?>
I am using .load('refreshImages.php') on the page I need it to show up on.
Any explanation I am not seeing?
Your $img is an array, not a string. You will get output like <li>Array</li>, if you have stuff coming from the database. Is that what you mean? Or are you getting an empty result?
If empty - what does your mysql_num_rows tell you when ran against the result resource?
try changing this:
echo "<li>$img</li>";
to
echo "<li><img src=\"{$image}\" alt=\"{$image_alt}\" /></li>";
You may not be getting any results from the database. Try using this code which will display a message if there is something wrong with your sql query.
<?php
include 'includes/config.php';
$pimages = mysql_query("SELECT * FROM property_images WHERE pid=" . $pid );
if (mysql_num_rows($pimages) > 0) { // checks to see if you are getting results from db
while($img = mysql_fetch_array($pimages)){
$image = $img['image'];
$image_alt = $img['image_alt'];
echo '<li><a class="thumb" href="{$image}"><img src="{$image}" width="50px" height="50px" alt="{$image_alt}"></a></li>';
}
} else {
echo "no results returned from database";
} // end of mysql_num_rows check
?>
You might be better off concatenating all the images and then echo-ing it out rather than echo-ing each one e.g
$htmlOutput = '';
while($img = mysql_fetch_array($pimages)){
$image = $img['image'];
$image_alt = $img['image_alt'];
$htmlOutput .= "<li><img src=\"{$image}\" alt=\"{$image_alt}\" /></li>";
}
echo $htmlOutput ;