php display <img src /> in Joomla based on link in Database column - php

I am trying to display an image linked to a user that is logged in based on a Joomla site.
The PHP code I am using is as follows:
<?php
$user = JFactory::getUser();
$username = $user->username;
echo '<img src="$user->picture" />'
;
?>
During my testing and troubleshooting, I tested without the image source tag and I received the call back from my database table labeled Images/LOGO.jpg which displayed as a string on my front end.
when I use the Image source again and replace $user->picture with the same string from the DB column, the image returns 100%.
By leaving the code as per above the page loads successfully but no image is returned.
Thanks,

The Bug is quite obvious....echo '<img src="$user->picture" />'Did you notice anything unusual in the Snippet above?
Here is a Hint: Try this: <?php $var="test"; echo '$var'; ?> What did you get?
Sure you got it all back as is: VERBATIM - $var instead of the expected test. So here's the Catch: Any variable within Single Quotes in PHP would NOT expand/evaluate to a Value...You'd just get the Variable back: Verbatim
Rather do it like so:
<?php
$user = JFactory::getUser();
$username = $user->username;
// NOTICE THE DOUBLE QUOTES SURROUNDING THE "<img... />" TAG
// THOUGH NOT SO IMPORTANT, BUT NOTICE ALSO THE BRACES AROUND
// {$user->picture}
echo "<img src='{$user->picture}' />";
By the way; in Joomla, you could get the User-Avatar like so:
<?php
// USE jimport TO PULL IN THE PROFILE PICTURE CLASS
jimport('profilepicture.profilepicture');
// GET THE USER OBJECT LIKE YOU ALREADY DID
$user = JFactory::getUser();
// CREATE A NEW INSTANCE OF THE ProfilePicture CLASS PASSING IT THE USER ID
$userPix = new ProfilePicture($user->get('id'));
// ECHO OUT THE ENTIRE HTML FOR THE USER PROFILE PICTURE
echo $userPix->toHTML();
Cheers & Good-Luck, Mate! 💪☝️✌️

single quote does not print the var value, change it to
"<img src=\"$user->picture\" />"

Related

Why is this code not displaying my photo on the page?

if my query is working fine by fetch the data from the database.
The variables are working fine as well. The name for the image in the database is stored in the post_banner column. please note that the original file is stored in the server folder only the image name is sent to the database.
if the variable is like this:
$featured_post_image = $post['post_banner'];
<?php echo "<img class='img_ft' src='content/newsimages/'".$featured_post_image."' alt='$featured_post_image'> "; ?>
This code is not display the image from the sever folder. What is wrong?
Maybe it's just a problem with single and double quotes.
your line
<?php echo "<img class='img_ft' src='content/newsimages/'".$featured_post_image."' alt='$featured_post_image'> "; ?>
Should be
<?php echo "<img class='img_ft' src='content/newsimages/".$featured_post_image."' alt='$featured_post_image'> "; ?>
Please note the absence of a quote after /newsimages/

insert content into Template through Variable

I want to use my index.php page as my template for all my other pages. So I'm printing it out with the code below.
echo file_get_contents("index.php");
I've added this piece of code into the template (index.php) where i want to display the contents. of whichever page im on.
<?php
echo $index_content;
?>
So when I use
echo file_get_contents("index.php");
to get my page template, on for example users.php. In the users.php file I want to use the code below
$index_content = echo "string";
to then print out my page contents where I added this variable
<?php
echo $index_content;
?>
My problem is when I say $index_contents = echo ("string");
it's not printing anything out. onto my template. or it prints the stuff out but at the end or the beginning of the template. not where i've inserted my variable. Why wont it echo out my stuff where I've inserted my variable.
file_get_contents() give you the source of your file.
If I get you right you want to use include instead. Also don't echo in a variable but assign the value and echo it in the template.
users.php:
$content = 'what ever';
include 'template.php';
other.php:
$content = 'other page';
include 'template.php';
template.php:
echo $content
If you call users.php output will be "what ever". If you call other.php output will be "other page".
You are storing the return value of "echo" in $index_content, which is empty.
Just omit the echo when assigning the string to the variable.
The other problem is, with file_get_contents you don't evaluate the php expression where you echo out the $index_content.
Instead, you should use include('index.php') in users.php, and set the variable $index_contents before that.

Putting Something Inside a Link with PHP

So I'm basically calling and returning an entire row from a mysql table using a while loop (which is working), but I'm trying to use the data that I call inside an html link, but I can't seem to get it to work.
Ideally, eventually it will just be a list of links with each person's individual name. I can return the list fine, but I can't seem to return the list with a link.
Here is my code that I feel should be working :(
<?php
require 'db/connect.php';
$result = $con->query('SELECT distinct name FROM mytable');
while($rows = $result->fetch_assoc())
{
echo ''$rows['name']'' , "</br>";
}
?>
Any help would be greatly appreciated!
Issue might be with your string concatenation. Try following code block
echo ''.$rows['name'].'';
echo ''. $rows['name']. '' , "</br>";
You just need to use . to concatenate strings together.
try this
echo ''.$rows['name'].'' , "</br>";
Should work just fine. Basically it's '.$row['name'].'
when concatenating strings with variables you have to use dot(.) like echo "string".$var; it will be invalid to write echo "string"$var; in your example you have ignored this point.

Concatenating HTML and PHP to make an image link

I'm trying to make an image into a link using PHP and HTML. The main idea is to grab user's images and screen names from Twitter, then make the image into a clickable link to their profile by building the URL and adding their screen name on the end. But I get a error message:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in C:\wamp\www\fyp\tweeter3.php on line 71.
This is line 71 (it's part of a foreach loop):
<?php echo "<img src = ".$userImage." class = ".$class.">"; ?>
There's a syntax error in there I just can't pinpoint.
These are my variables:
$userScreenName = $user -> screen_name;
$userImage = $user -> profile_image_url;
$class = "myImgClass";
$url = "https://twitter.com/".$userScreenName;
Can you spot the error?
You are missing a dot after $url and the HTML quotes to generate valid code:
<?php echo "<a href = '".$url."'><img src = '".$userImage."' class = '".$class."'></a>"; ?>
Without the quotes you get:
<a href = the url><img src = user image class = the class></a>
With the quotes:
<a href = 'the url'><img src = 'user image' class = 'the class'></a>
Missing . after $url:
<?php echo "<a href = ".$url"><img...
after $url you need to have a period.
Try this instead:
<?php
echo "<img src = \"".$userImage."\" class = \"".$class."\">";
?>
In my opinion, the simplest and most readable way to do is:
<?php echo "<a href = '$url'><img src = '$userImage' class = '$class'></a>"; ?>
There is only one long text, and no concatenation is used. It reduces the possibility to have an error caused by a missing double quote or missing dot. All PHP variables will be replaced automatically by their values.
You could also use printf to have all the variables outside of the string:
<?php printf('<img src = "%s" class = "%s">', $url, $userImage, $class); ?>
It is not a good practise to concatenate html string with the php variables. This leads to possible injection vector (XSS).
To avoid possible XSS (DOM OR STORED) please filter the variables as string. Specially if the value is from user input.
eg.
<?php echo "<a href = '".filter_var($url, FILTER_SANITIZE_STRING)."'

Changing Text in PHP

I haven't found anytihng in Google or the PHP manual, believe it or not. I would've thought there would be a string operation for something like this, maybe there is and I'm just uber blind today...
I have a php page, and when the button gets clicked, I would like to change a string of text on that page with something else.
So I was wondering if I could set the id="" attrib of the <p> to id="something" and then in my php code do something like this:
<?php
$something = "this will replace existing text in the something paragraph...";
?>
Can somebody please point me in the right direction? As the above did not work.
Thank you :)
UPDATE
I was able to get it working using the following sample:
Place this code above the <html> tag:
<?php
$existing = "default message here";
$something = "message displayed if form filled out.";
$ne = $_REQUEST["name"];
if ($ne == null) {
$output = $existing;
} else {
$output = $something;
}
?>
And place the following where ever your message is to be displayed:
<?php echo $output ?>
As far as I can get from your very fuzzy question, usually you don't need string manipulation if you have source data - you just substitute one data with another, this way:
<?php
$existing = "existing text";
$something = "this will replace existing text in the something paragraph...";
if (empty($_GET['button'])) {
$output = $existing;
} else {
$output = $something;
}
?>
<html>
<and stuff>
<p><?php echo $output ?></p>
</html>
but why not to ask a question bringing a real example of what you need? instead of foggy explanations in terms you aren't good with?
If you want to change the content of the paragraph without reloading the page you will need to use JavaScript. Give the paragraph an id.<p id='something'>Some text here</p> and then use innerHTML to replace it's contents. document.getElementById('something').innerHTML='Some new text'.
If you are reloading the page then you can use PHP. One way would be to put a marker in the HTML and then use str_replace() to insert the new text. eg <p><!-- marker --></p> in the HTML and $html_string = str_replace('<!-- marker -->', 'New Text', $html_string) assuming $html_string contains the HTML to output.
If you are looking for string manipulation and conversion you can simply use the str_replace function in php.
Please check this: str_replace()
If you're using a form (which I'm assuming you do) just check if the variable is set (check the $_POST array) and use a conditional statement. If the condition is false then display the default text, otherwise display something else.

Categories