Link not sending to the right page - php

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.

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>";

Echoing image from MySQL Database PHP

I have been trying to echo images from my database using PHP. I have tried various solutions on the forum with no success. I am just getting a blank image as an output.
while($rows=mysqli_fetch_assoc($getquery))
{
$id=$rows['id'];
$LastName=$rows['LastName'];
$FirstName=$rows['FirstName'];
$Year=$rows['Year'];
$Press=$rows['Press'];
$Description=$rows['Description'];
$Title=$rows['Title'];
$image=$rows['image'];
echo '<div class = "paragraph">' . $LastName . '<br/>'. $FirstName . '<br/>' . $Year . '<br/>' . $Press . '<br/>' . $Title . '<br/>'. $Description . "<img src='image/".$row['image']."' />" . '</div>' ;
}
You're echoing the image as $row['image'], but the only prior references to the image are $rows['image'] (note the s) and $image. Update the echo statement to use either of these and not $row['image'].
Edit: This will not fully fix your issue. As noted in a comment to your question, you will need to do some finagling to actually display the image as demonstrated here.
while($rows=mysqli_fetch_assoc($getquery)){
$id=$rows['id'];
/*
assuming the `image` directory is relative to the root
and not the directory from which the script runs then
a leading slash might be the issue.
*/
echo "
<div id='{$id}' class='paragraph'>
{$rows['LastName']}<br/>
{$rows['FirstName']}<br/>
{$rows['Year']}<br/>
{$rows['Press']}<br/>
{$rows['Title']}<br/>
{$rows['Description']}
<!--<img src='/image/{$row['image']}' />-->
<img src='data:image/jpeg;base64, {$rows['image']}' alt='' title='' />
</div>";
}
If you store the mimetype of the image when you save it to the db then you would substitute the proper mime/content type in above ~ ie: image/png, image/gif etc so that might then become something like:
<img src='data:{$rows['mimetype']};base64, {$rows['image']}' alt='' title='' />

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.

php string not working properly

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]}";

Unable to display image from database using PHP

Screenshot of the code:
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
require ('../../mysqli_oop_connect.php'); //Connect to the database.
$errors = array(); // Initialize an error array.
// Check for an isbn:
if (empty($_GET['isbn'])) {
$errors[] = 'You forgot to enter the ISBN-13.';
} else {
$isbn = $mysqli->real_escape_string(trim($_GET['isbn']));
}
if (empty($errors)) {
//Make the query:
$q = "SELECT * FROM books WHERE isbn=$isbn";
$r = $mysqli->query($q);
$num = $r->num_rows;
if ($num == 0) { // If it fails to run:
echo "<br /><br /><p>This book is currently not listed in our database. Would you like to add it?</p>\n";
//HERE IS WHERE IT NEEDS TO REDIRECT TO A PAGE THAT ALLOWS THE CONTENT TO BE ADDED TO THE DB LIKE REGISTER.PHP DOES.
} else { //If it suceeds then run the query:
echo '<h1>Is this your book?</h1>';
while ($row = $r->fetch_object()) { // POST DATA FROM DB.
echo '<img src="$row->image" height="100" width="100">' . '<br />' . $row->name . '<br />' . $row->isbn ;
The rest of the code is just closing the DB connection.
I don't know what that icon means or where the issue is in the code. The DB is setup with three columns in a table: image, isbn, and name. The goal is to get the book to display the data being retrieved from the database. It is displaying all three items from the database based on the query, except the image is something else. The HTML is recognizing that there is an image being retrieved because when I just do $row->image it freaks out and posts random nonsense, as it should when there is an image being displayed like that. Any help is greatly appreciated.
echo '<img src="'.$row->image.'" height="100" width="100"><br />' . $row->name . '<br />' . $row->isbn ;
Try this way, and better you print your image path and check if it does exist or does not exist, and yes you can also inspect image element and check path of the image.
Its not clear in the question whether there is image link saved in the db or image data in base64. Answer is given assuming db has image link.
I'd guess from your comments about 'freaks out and posts random nonsense' that your image is stored in your database, rather than the path to your image. If so, you can't simply echo the image data in your <img> tag. The src attribute requires a URL, which you can create in one of two ways: create a script that serves the image with appropriate headers and use the URL of that script as your src attribute; or, create a data URI and include the data in your page. The latter is probably the simplest, but could increase the load time of your pages. Here's how:
// base64 encode image data and prepend the header
$imgURL = "data:image/png;base64,".base64_encode($row->image);
echo '<img src="'.$imgURL.'" height="100" width="100">' .
'<br />' . $row->name . '<br />' . $row->isbn ;
Note - you'll need to change the dataURL header to send the correct image type: image/jpg, image/png, etc.
You cannot have string substitution with single quotes. It will simply place the text $row->image in the echo:
echo '<img src="$row->image" height="100" width="100">' . '<br />' . $row->name . '<br />' . $row->isbn ;
Try this instead:
echo "<img src='$row->image' height='100' width='100'>" . '<br />' . $row->name . '<br />' . $row->isbn ;
Or this:
echo '<img src="' . $row->image . '" height="100" width="100">' . '<br />' . $row->name . '<br />' . $row->isbn ;
Personally, I prefer using sprintf like this:
echo sprintf('<img src="%s" height="100" width="100">', $row->image) . '<br />' . $row->name . '<br />' . $row->isbn ;
But on top of that, to make your debugging life even easier, I recommend formatting your PHP code to be easier to read like this:
echo sprintf('<img src="%s" height="100" width="100">', $row->image)
. '<br />'
. $row->name
. '<br />'
. $row->isbn
;
The cleaner your code is from the beginning, the easier it is to spot flaws & bugs when they arise.

Categories