Why can't I insert an image into php? - php

trying to insert an image into some php. This code pulls from the database thumbnail page to show an item's details. It works fine when it's text "see it in 3d view" but when I try to insert a premade image in that location instead (a button jpg, aka "img src="#"), I'm getting an error. How can I do this correctly? Still learning the ins and outs of php and html, they don't always play the way I expect them to. Thanks for any help.
echo ("<br><img src= \"");
echo ($thumbnail);
echo (" \"><br><br><a href = \"");
echo ($photo);
echo ("\"><b>See it in 360 view</b></a></div>");
echo ("<div id=\"info\"; style=\"width:45%\"><br><br><div class = \"date\">");
echo ($date);
echo ("</div><br>");
echo ("<div class = \"blurbs\">");
echo ($sub);
echo ("<br><br><br>");
echo ($desc);
echo ("<br><br>");
echo ($hist);
echo ("<br><br><br><b>Provenance:</b><br>");
echo ($prov);
echo ("<br><br><b>Construction Label:</b><br>");
echo ($labl);
echo ("<br><br><br><br><b>");
echo ($cNum);
echo ("</b>");

<img src="#"> would never work. src="#" is a shortcut for "current page". e.g. browsers will try to use the current page's URL as the source for the image, which means it'll be trying to load a bunch of HTML as if it was a jpg/gif/png image. Since html isn't any of those, it'll just be a flat-out "this image contains errors" error.
Whatever you're putting in $thumbnail needs to be a proper url, e.g.
<img src="kittens.jpg">
<img src="http://example.com/kittens.jpg">
<img src="data:image/jpeg;base64,<?php echp base64_encode(file_get_contents('kittens.jpg')); ?>">

I would start out with cleaning up your file and remove some of the unneeded overhead (I personally love to have my controllers (Which is generating the output for my view files)
What is the output of this PHP file and what did you expect it to be?
<br><img src="<?= $thumbnail ?>">
<br><br><b>See it in 360 view</b>
</div>
<div id="info" style="width:45%"><br><br><div class = "date">
<?= $date ?>
</div><br>
<div class="blurbs">
<?= $sub ?>
<br><br><br>
<?= $desc ?>
<br><br>
<?= $hist ?>
<br><br><br><b>Provenance:</b><br>
<?= $prov ?>
<br><br><b>Construction Label:</b><br>
<?= $labl ?>
<br><br><br><br><b>
<?= $cNum ?>
</b>
a note to this is that Short Open tag which is enabled by default from PHP 5.4)
You should also look into using div or p tags instead of all the line breaks (it makes it easier for you to make changes to later on)

Related

How to stop text from wrapping around image when using nl2br? overflow:hidden doesn't work here

I am designing a comment frame for a webpage I am working on. Upon using nl2br to ensure line-breaks aren't removed from stuff submitted from the the in the form, the text just wraps around the user image in the comment section (please see image). overflow:hidden doesn't work here. How to handle this?
A screenshot of the comment frame.
<div class="comment_section" style="overflow:hidden";>
<img src="<?php echo $user_obj->getProfilePic();?>" title="<?php echo $posted_by;?>" style="float:left;" height="30">
<b><?php echo $user_obj->getFirstAndLastName();?></b> <?php echo $time_message . "<br>" . nl2br($comment_body);?>
<hr>
</div>

PHP Code is printing text not typed code

I have created a homepage editor tool in a script I purchased. The function of this homepage editor is to allow me to create different sections and display them one on top of the other in the order they are created. Which in hopes will give me an effect of several blocks that stretch width of the screen.
All seems to work well except one piece. I input my html and php code into the field in the admin panel and it saves to the db as I wrote it. However, when I go to echo each section back to the homepage it just displays my php code as plain text and doesn't interpret it as php and do its function.
Here is code from the homepage.php that prints the results.
<?php
session_start();
require_once("inc/config.inc.php");
if (isset($_GET['ref']) && is_numeric($_GET['ref']))
{
$ref_id = (int)$_GET['ref'];
setReferal($ref_id);
header("Location: index.php");
exit();
}
/////////////// Page config ///////////////
function get_all_section($section_id='')
{
$sql="SELECT * FROM `cashbackengine_homepage` WHERE 1";
if($section_id!="")
{
$sql.=" AND section_id='".$section_id."'";
}
$sql.=" AND section_status=1";
$sql.=" ORDER BY section_order ASC";
//echo $sql;
$res=mysql_query($sql);
while($row=mysql_fetch_array($res))
{
$section_array[]=array(
'section_id' =>$row['section_id'],
'section_name' =>$row['section_name'],
'section_desc' =>$row['section_desc'],
'section_order' =>$row['section_order'],
'section_status' =>$row['section_status'],
'last_updated' =>$row['last_updated'],
);
}
return $section_array;
}
$get_all_section=get_all_section('');
/*$get_all_section2=get_all_section('2');
$get_all_section3=get_all_section('3');
$get_all_section4=get_all_section('4');
$get_all_section5=get_all_section('5');*/
for($i=0; $i<count($get_all_section);$i++)
{
//echo htmlspecialchars_decode($get_all_section[$i]['section_desc']);
//echo htmlspecialchars_decode(stripslashes(str_replace(" ","",(str_replace("<br />","\n",$get_all_section[$i]['section_desc'])))));
echo $get_all_section[$i]['section_desc'];
}
?>
I am certain the problem has to do with the echo at the end. But I am unsure how to use htmlspecialchars to make it work with php if it even will. Or if I have to put something weird in my saved section.
Here is one of my sections. Any help is greatly appreciated. Thank you.
<div style="height:260px; width:100%; background-color:#000; margin:0px; color:white;">
<div id="header">
<div id="logo"><img src="<?php echo SITE_URL; ?>images/logo.png" alt="<?php echo SITE_TITLE; ?>" title="<?php echo SITE_TITLE; ?>" border="0" /></div>
<div class="start_saving">
<div id="links">
<?php if (MULTILINGUAL == 1 && count($languages) > 0) { ?>
<div id="languages">
<?php foreach ($languages AS $language_code => $language) { ?>
<img src="<?php echo SITE_URL; ?>images/flags/<?php echo $language_code; ?>.png" alt="<?php echo $language; ?>" border="0" />
<?php } ?>
</div>
<?php } ?>
<div id="welcome">
<?php if (isLoggedIn()) { ?>
<?php echo CBE_WELCOME; ?>, <span class="member"><?php echo $_SESSION['FirstName']; ?></span><!-- | <?php echo CBE_ACCOUNT ?>--> | <?php echo CBE_BALANCE; ?>: <span class="mbalance"><?php echo GetUserBalance($_SESSION['userid']); ?></span> | <?php echo CBE_REFERRALS; ?>: <span class="referrals"><?php echo GetReferralsTotal($_SESSION['userid']); ?></span>
<?php }else{ ?>
<a class="signup" href="<?php echo SITE_URL; ?>signup.php"><?php echo CBE_SIGNUP; ?></a> <a class="login" href="<?php echo SITE_URL; ?>login.php"><?php echo CBE_LOGIN; ?></a>
<?php } ?>
</div>
</div></div>
</div>
It looks like you're getting these section contents pieces out of your database, and not from a file stored on your web server. Is that correct?
Assuming that's true, then my next question would be, who populates this data? Is this taken in any way from user input? The reason why I ask is because of my next suggestion, which may or may not be received well.
The reason why your PHP code isn't executing, is because it's being retrieved from the database and output as a string, not as code. So how do you execute code that's stored in a string, you ask? Well, the answer to that question is to use eval() on the string. But this is where you have to be really careful!!!!!!! If any part of that string could have possibly come from an untrusted source, then malicious PHP code could be executed, which could potentially give evildoers a way into your server, where they can find all the information in your database, server, etc. Make sure you know where your code is coming from before executing it!
You make a good point that it's HTML mixed with PHP. So I see two possible solutions...
This post suggests that you could do eval(' ?>'. $section .' <?php'); This makes sense, you're breaking out of PHP before you eval your string, and so requiring the included string to open its own PHP tags to write PHP code.
Another way I can think of would be to throw the contents into a temporary file, and then include() that file:
// get contents, store in $contents
$filename = tempnam(sys_get_temp_dir(), 'section');
file_put_contents($filename, $section);
include($filename);
unlink($filename);

retrieve image from folder in php codeigniter

imagedisplay.php(view)
<html>
<body>
<h3>Your file was successfully uploaded!</h3>
<?php print_r($upload_data); ?> </br>
<?php $str=base_url()."images/".$upload_data['file_name'] ?> </br>
<?php $str=str_replace('http://','',$str) ?>
<?php echo $str; ?>
<img src= '$str'/> </br>
For echo $str; I got the string i need to display th image
but when i pass it to img src.... i am not able to display it on the browswer
Is there any syntactical error or am i missing anything ...pls help?
Just a small syntax problem here.
Embed PHP echo command in the HTML code, like so:
<img src="<?php echo $str; ?>"/> </br>
or embed PHP echo short tags:
<img src="<?=$str?>"/> </br>
In other words: insert the PHP output at the positions, where you need it as HTML content.

issue in lightbox effect in php

I am using lightbox to show an image in php.
My current code:
<a class="sri" href="administrator/all_photo/mywork/
<?php
echo $res['path'];
?>
_1.jpg" rel="lightbox">
<img src="administrator/all_photo/mywork/
<?php
echo $res['path'];
?>
_1.jpg" class="photo_frame photo_size" >
<br />
<?php
echo $res['title'];
?>
</a>
CSS and JS for lightbox have already been included. In the above code, $res['title'] and $res['path'] are being fetched from database.
If I click on the image or the text which is showing as $res['title'], lightbox effect is showing. The image is within the lightbox. Now I need the $res['title'] value also be displayed when the lightbox is opened along with the image.
How do I do that?
Just use the title parameter in the <a> tag, as mentionned in the Lightbox Documentation:
Optional: Use the title attribute if you want to show a caption.
You must add the title:
<a class="sri" href="administrator/all_photo/mywork/
<?php
echo $res['path'];
?>
_1.jpg" rel="lightbox">
<img src="administrator/all_photo/mywork/
<?php
echo $res['path'];
?>
_1.jpg" title="
<?php
echo $res['title'];
?>
" class="photo_frame photo_size" >
</a>
You should be able to move all of your markup into a div. Add rel="lightbox" to that div, it should work. Although that depends on the lightbox library you are using.

Multiple JQuery hovercards on one page

I am using the jQuery Hovercard plugin (here) and trying to impliment it with user images pulled from Facebook with their php sdk, here is the code I am using to display the Hovercard:
<?php
$friends = $this->facebook->api('/me/friends');
foreach ($friends["data"] as $value) { ?>
<label id="demo-facebook" data-hovercard="<?php echo $value['id'] ?>">
<img src="https://graph.facebook.com/<?php echo $value["id"] ?>/picture" alt="<?php echo $value["name"] ?>" />
</label>
<?php }
?>
The problem I am having is that the Hovercard will only show for the first users image displayed, after that no Hovercard will show when the image is moused over ?
You should change the CSS selector "demo-facebook" from an id to a class. I haven't used HoverCard before, but that seems like it would be the problem.
An ID specifies one specific element of a page, whereas a class can be re-used on multiple elements.
Also, I think it would be easier to just echo the HTML instead of having a <?php echo $value; ?> everytime you have to insert a PHP value. You could just do echo "<img src='$value'>"; instead of <img src="<?php echo $value; ?>" />. Just seems like it would be easier that way.

Categories