Adding Dynamic link with Spaces in variable - php

i am trying to create a dynamic link. $path="uploads/" and $fileName="Data Communication and Networking.pdf" which is retrieved from database. But the link gets created with href="upload/Data" ignoring the " Communication and Networking.pdf" part. How to add $fileName with spaces between the content.
$message=$row["message"];
$fileName=$row["filename"];
$date=$row["date"];
echo "<tr>";
echo "<td>".$Serial."</td>";
echo "<td>".$message."</td>";
echo "<td>Download</td>";
echo "<td>".$date."</td>";
echo "</tr>";
$Serial++;;

href value must consist in quotes. Place single quotes around your value.change your href as below:
echo "<td><a href='".$path.$fileName."'>Download</a></td>";

You can replace any space with %20
$CompletePath = str_replace(" ", "%20", $path . $fileName);
echo "<td>Download</td>";
//uploads/Data%20Communication%20and%20Networking.pdf is a valid URL

$path="/path";
$filename="/file with space.name";
echo "<td>Download</td>";
Will output:
`<td><a href=/path/file with space.name>Download</a></td>`
So you need to put the path between the quotes like this:
$path="/path";
$filename="/file with space.name";
echo "<td><a href='".$path.$fileName."'>Download</a></td>";

Related

How to print a variable in href with php?

I'm trying to pass an id in a link href and I need it to be printed in the URL, my code is:
echo " $ville_nom ";
But the id is not print in the url, could you help me with the syntax ?
You have mistake in your usage of echo and your concat was wrong.
The corrected code :
echo "<a href='ville.php?id='".$ville_id."'> $ville_nom ";
When you used the double quote it's not necessary to concat variables to show their content. Be careful: it can be dangerous sometimes.
echo "<a href='ville.php?id=$ville_id'> $ville_nom ";
Try to use this:
echo "<a href='ville.php?id=" . $ville_id . "'>"

Escape the double quotes in css inline style in php echo

I am sorry if this is duplicated but I could not find an answer to this situation. I am trying to escape the double quotes from the inline style in the PHP code.
<?php if (isset($ioTitle)){
echo "<div style='background-color: echo $params->get('colorbgt'); ;' class=\"ioTitleBox\"><h3 class=\"ioTitle\">";
echo $params->get("ioTitle"); echo "</h3></div>";
}
You can't put echo inside a string and expect it to be executed. You need to use concatenation.
echo "<div style='background-color: " . $params->get('colorbgt') . ";' class=\"ioTitleBox\"><h3 class=\"ioTitle\">";
You don't need to escape anything in the style for this. Also, you can use single quotes around the classes, to avoid those escapes.
echo "<div style='background-color: " . $params->get('colorbgt') . ";' class='ioTitleBox'><h3 class='ioTitle'>";
<?php if (isset($ioTitle)){
echo "<div style='background-color:". $params->get('colorbgt').";' class=\"ioTitleBox\"><h3 class=\"ioTitle\">";
echo $params->get("ioTitle"); echo "</h3></div>";
}
Is this how you want it?
You are echo-ing from inside an echo, you just need to concatenate that part into the existing echo statement.

space is neglected when passed a string by URL

I am using the following code to display a hyperlink inside a table:
echo "<td><a href=http://www.smstoneta.com/show.php?opcode=TCP Y".">".
$row['num_y']."</a></td>";
The hyperlink is displayed successfully but when I click on the hyperlink, the URL is
www.smstoneta.com/show.php?opcode=TCP
instead of
www.smstoneta.com/show.php?opcode=TCP Y
Why am I not getting the full URL?
Use urlencode()
$opCode = urlencode('TCP Y');
echo "<td>".$row['num_y']."</td>";
You need URL Encode spaces to make them working in links.
Here is manual for PHP function urlencode
$safe_url = urlencode('http://www.smstoneta.com/show.php?opcode=TCP Y');
echo "<td>" .$row['num_y']. "</td>";
BTW, more readable (no concatenation needed) version to echo such strings is:
echo "<td><a href='{$safe_url}'>{$row['num_y']}</a></td>";

Echoing through PHP an HTML image path which is dynamic

I am trying to echo a dynamic image path in PHP. I have this code that works
<?php
//this code works
$image = '<img src="img/newlogo.jpg">';
echo($image);
?>
//gives me an image
And this code that doesn't
<?php
//this code doesn't
$lineofstring='newlogo.jpg';
$image = '<img src="img/$lineofstring">';
echo($image);
?>
$lineofsting is actually going to be an image path which is stored in a mysql database where one row is filled with, for example: pictureabc.jpg, second row is picturexyz.jpg etc.
I am trying to pass on the searched imagepath name onto $lineofstring, which is then echo'd but no picture comes out. Where am i going wrong?
To interpolate variables in PHP, you need to use double quote marks ", e.g.
$image = "<img src=\"img/$lineofstring\">";
In this case, you need to escape the inner ".
You can also concatenate strings with .:
$image = '<img src="img/' . $lineofstring . '">';
if it's easier.
Your $lineofstring variable is in a string using single quotes. Single quotes tell PHP to use the string literally so your variable is not being recognized as a variable. Change it to double quotes or use concatenation to accomplish your goals.
$image = "<img src=\"img/$lineofstring\">";
Or:
$image = '"<img src="img'.$lineofstring.'">';
$profilepicture="images/profiles/".$myid.".gif";
$noprofilepicture="images/profiles/no_avatar.gif";
echo "<IMG SRC='";
if (file_exists($profilepicture)) {
echo "".$profilepicture."";
} else {
echo "".$noprofilepicture."";
}
echo "'>";

PHP convert javascript when echo

I have following PHP echo statement:
echo "<td><a href='delete-news.php?deleteID=".$id." onclick='return confirm('Really delete?');'>Delete</a></td>";
which is convert to html as:
<td class="last-td nth-8"><a delete?');'="" confirm('really="" return="" href="delete-news.php?deleteID=5 onclick=">Delete</a></td>
As you can see something has gone wrong?
What is the problem? I have already tried swaping " " for single ' '.
Your have double single quotes in the onclick statement, try confirm(\'Really delete?\') instead.
You have forgot ' after href. Use it like
Double quotes:
echo "<td>Delete</td>";
Single quotes:
echo '<td>Delete</td>';
You have href not closed. Also, your 'Really delete' cause trouble too. Try this
echo "<td><a href='delete-news.php?deleteID=$id' onclick='return confirm(\"Really delete?\");'>Delete</a></td>";

Categories