php string not working properly - php

My code in php is
echo $preference[0]."</br>";
echo '<p class="filter_entity">'.$preference[0].'<img src="' . base_url() . 'images/close-icon.png" onClick="filter_close()"></p>';
html out put is
Business
<img onclick="filter_close()" src="http://localhost/AEC/images/close-icon.png">
I want to put $preference[0] in filter_close() of my php code ... dont know how to do it .

Something like this?
$param = $preference[0];
echo '<p class="filter_entity">'.$preference[0].'<img src="' . base_url() . 'images/close-icon.png" onClick="filter_close(\''.echo $param.'\')"></p>';
Just append the variable as a parameter of the js function

http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
echo "Some array item: {$array[0]}";

Related

How to make a PHP echo into a link

I'm trying to get this so when I click the image, the image then shows in a new tab so it's able to be saved
I've tried adding the a href tags inside and around.
Code:
echo "<img src=" . random_image('css/avatars') . " />";
This is what you want:
echo '<img src="' . random_image('css/avatars') . '" />';
You should try
echo "<a target='_blank' href='".random_image('css/avatars')."'>"."<img src=" . random_image('css/avatars') . " /></a>";
I think this can help you
The problem is that you are not properly concatenating your string with what you get from random_image function.
You can try following code, here I am assuming that your random_image function returns complete path to your image
function random_image($path)
{
// returning dummmy image
return "https://images.unsplash.com/photo-1535498730771-e735b998cd64?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=623ed05afcc5a3fa1e444ffca95854dc&w=1000&q=80";
}
echo "<a href='".random_image('css/avatars')."' target='_blank'> <img src='".random_image('css/avatars')."' /></a>";

Adding php inside image src not working?

I know this has already been put out on here but I've looked and mine still won't work and I don't know why someone please give me a hand.
echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50); . '"/>';
Remove the semicolon you have after resize
echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50) . '"/>';
Try this
echo "<img src= '".Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50)."'/>";
Please change
echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50); . '"/>';
this line to
echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50) . '"/>';
Because you had put one extra semi column ; after the resize(50,50)
Please change your code and try. Definitely work .

Php echo url inside an echo

I am aware that I cannot use an echo inside an echo. My php code is:
echo '<img src="img/image1.jpg"
I want to use php variable as the source. Somethink like:
echo '<img src="php-code"
Using .(dot) you can concatenate php variable in echo statement.
echo '<img src="'.$src.'" />';
You have four options:
$url = '...';
//1
echo '<img src="' . $url . '">';
//2
echo "<img src='{$url}'>"; //Note that you have to use double quotes
//3
echo '<img src="';
echo $url;
echo '">';
//4
echo '<img src="', $url, '">'; //I would not recommend this one though
just skip first echo and write the html with an echo in the middle
<img src="<?=$src?>">
All of these works
echo '<img src="', $url, '">'; # This works by sending 3 different parameters to echo
echo '<img src="' . $url . '">'; # This works by concatenating 3 strings before echoing
echo '<img src="'; echo $url; echo '">'; # This works by echoing 3 strings in turn
echo "<img src=\"$url\">"; # All of these works by inserting
echo "<img src=\"${url}\">"; # the value of $url in the string
echo "<img src=\"{$url}\">"; # before echoing. " needs to be escaped.
# And finally, this (called HEREDOC) does the same thing as above
# only without ", so that sign is not needed to be escaped
echo <<<FOO
<img src="$url">
<img src="{$url}">
<img src="${url}">
FOO;
There are many solutions.
I prefer this one: <?php echo '<img src="'.$url.'">'; ?>
$url stands for the Image-Url, I guess you know ist.
You can do it also:
For example: <img src="<?php echo $url; ?>">
But I like the first method, it's a simple way to put out strings.

Link not sending to the right page

I have this code:
if ($topic['user']==$_SESSION['display'])
{
echo '<div class="bottomright"><img src="../assets/icons/Comments-edit.png" /><img src="../assets/icons/Lock.png" /><img src="../assets/icons/Trash.png" /></div>';
}
When I hove over the image the link shows as:
?id=%3C?php%20echo%20$topic_id;%20?%3E&part=3
Rather than:
?id=3&part=3
Why is this not working?
I tried this:
if ($topic['user']==$_SESSION['display'])
{
echo '<div class="bottomright"><img src="../assets/icons/Comments-edit.png" /><img src="../assets/icons/Lock.png" /><img src="../assets/icons/Trash.png" /></div>';
}
Now I get:
?id=$topic_id&part=3
Your code is already within PHP tags, you don't have to add an additional <?php to the link's parameters.
echo '<div class="bottomright"><a href="'. $topic_id .'&part=5"...
Simply terminate your string and concatenate the relevant variables into the string.
Here is a very simplified example:
<?php
$user_name = "Anthony";
echo "Hello ". $user_name ."! How are you?";
//----------^ terminating the string
?>
This will result in:
Hello Anthony! How are you?
Here is the relevant documentation dealing with string concatenation.
You are echoing a php statement, it won't be interpreted, but displayed as it is. Instead, you should concatenate the string, like this:
if ($topic['user']==$_SESSION['display'])
{
echo '<div class="bottomright"><a href="?id='
. $topic_id
. '&part=5"><img src="../assets/icons/Comments-edit.png" /></a><a href="?id='
. $topic_id
. '&part=6"><img src="../assets/icons/Lock.png" /></a><a href="?id='
. $topic_id
. '&part=7"><img src="../assets/icons/Trash.png" /></a></div>';
}
You don't need to "echo" within a PHP statement. Just concatenate the string (here id) as shown here:
if ($topic['user']==$_SESSION['display']){ echo '<div class="bottomright"><img src="../assets/icons/Comments-edit.png" /><img src="../assets/icons/Lock.png" /><img src="../assets/icons/Trash.png" /></div>'; }
Also, you should use "& amp;" (without that space, don't know how to format that here) instead of "&" in your links.

How to add a variable to this?

I am using the following snippet. How do I add q=$k in the <a href url>?
<?php
echo '<a id=d href="'.$_SERVER['PHP_SELF'].'?pg='.($s+10).'">Next</a>';
?>
<?php
echo '<a id=d href="'.$_SERVER['PHP_SELF'].'?pg='.($s+10).'&q='.$k.'">Next</a>';
?>
Something like that?
echo '<a id=d href="' . $_SERVER['PHP_SELF'] . '?pg=' . ($s+10) . '&q=' . $k . '">Next</a>';
You can do this:
echo "<a id='d' href='" . html_entities("$_SERVER[PHP_SELF]?pg="
. urlencode($s + 10) . "&q=" . urlencode($k)) . "'>Next</a>";
I added suitable escaping for the URL parameter and HTML output: URL encoding for the URL GET parameters, and HTML entity replacement for the entire URL string.

Categories