how to display image in html by retrieving it from mysql database.
i have the following code but only text is being displayed in place of image.
How to display the image instead of the text of image.
Code:
while ($row = mysql_fetch_assoc($result))
{
//echo "<div>";
//echo "<div class=\"slide-image\">";
print $row['Image'];
//echo "</div>";
echo "<div class=\"slide-text\">";
echo $row['Head'];
echo $row['Description'];
//echo "</div>";
//echo "</div>";
}
It displays the resulting JPEG as text, which as you might imagine is very difficult to interpret.
You have to set the Content-Type header with PHP. If it is JPEG it will be image/jpeg. You can set it with:
header('Content-Type: image/jpeg');
Be sure to set that header before outputting any data.
update
But in your file as you are outputting image data and text data from same document you cant set its header type as multiple types.
one option would be
while ($row = mysql_fetch_assoc($result))
{
echo "<div>";
echo "<div class=\"slide-image\">";
echo "<img src='image_render.php?id=".$row['some_key_for_record']."'";
//print $row['Image']; we are moving it to an external page
echo "</div>";
echo "<div class=\"slide-text\">";
echo $row['Head'];
echo $row['Description'];
echo "</div>";
echo "</div>";
}
and in image_render.php
//code to pull data from db according to $_GET['id'];
header('Content-Type: image/jpeg');
print $row['Image'];
//no text data to be output from here
If you are stroing the image source(full path) in your MYSQL table then you can able to print the html img tag inside your php like a html div. You can do it by this way,
while ($row = mysql_fetch_assoc($result))
{
//echo "<div>";
//echo "<div class=\"slide-image\">";
echo '<img src='".$row['Image']."' width="100" height="100" />';
//echo "</div>";
echo "<div class=\"slide-text\">";
echo $row['Head'];
echo $row['Description'];
//echo "</div>";
//echo "</div>";
}
Note :
If you want only the image to be displayed in your web page then you can do it with header('Content-Type: image/jpeg');
Related
I'm making a form (form.php) for user to insert data, after user finish inserting data, form action will jump to formGenerateXLS.php (using POST method) for generate user data to XSL file. In the bottom of formGenerateXLS.php I put a javascript code to jump to dashboard (home.php) but it fail. The Excel file successfully generate but javascript code not working. How to work with that?
if(isset($_POST['issue_time'])){
$product_desc = $_POST['product'];
$filename_excel = date("Ymd_")."$product_desc";
header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=$filename_excel.xls");
echo "<table border='1'>";
echo "<tr>";
echo "<th colspan='3' bgcolor='#1bf3b3'>New Ticket Request</th>";
echo "</tr>";
echo "<tr>";
echo "<td>Name</td>";
echo "<td></td>";
echo "<td>User</td>";
echo "</tr>";
echo "</table>"
}
echo "<script>window.location='home.php'</script>";
Because echo does not execute the command, but simply writes it to the file.
In order to reposition a php file, use the header () function, for example header ('location: home.php');.
I'm trying to retrieve all images from a folder in the server to my web page through PHP.
At local server, they're displaying fine but at main server, images are not displaying and showing error 404 not found
even though the images are there.
Please help me to get rid of this. The images are in Big Rock server.
Here is my code:
$filename = $_POST['file'];
echo '<table class="table1" border="0" width="100%" >';
echo "<tr>";
echo "<td class='td1'>";
$images = glob($_SERVER["DOCUMENT_ROOT"].
"/admin/gallery/".$filename.
"/*.*");
$count = 0;
foreach($images as $image) {
echo '<a href="'.$image.
'" target="_blank"><img src="'.$image.
'"class="img-rounded mySlides" width="200" height="150"/></a><br />';
if ($count < 3) {
$count++;
echo "</td>";
echo "<td class='td1'>";
} else {
echo "</tr>";
$count = 0;
echo "<tr>";
echo "<td class='td1'>";
}
}
The HTTP 404 Not Found Error means that the webpage you were trying to reach could not be found on the server. It is a Client-side Error which means that either the page has been removed or moved and the URL was not changed accordingly, or that you typed in the URL incorrectly. Give extensions in giving url.
There is no issue with the permission on the file. The error is due to mismatch of the Relative Path of the image. 404 means the resource doesn't exist.
You can check my answer here [can't find correct relative paths of CSS background-image
The problem is glob($_SERVER["DOCUMENT_ROOT"]."/admin/gallery/".$filename."/.").$_SERVER["DOCUMENT_ROOT"] will take path as home/xxxx/public_html/admin/gallery.which cannot open in the browser.The image will open only by the path like this "http://example.com/folder/img.jpg".so i have cropped the path till admin folder like home/xxx/public_html/admin/gallery to admin/gallery and in the img src i have given src="http://example.com/".$image as shown in the code
$filename=$_POST['file'];
echo '<table class="table1" border="0" width="100%" >';
echo "<tr>";
echo "<td class='td1'>";
$curdir=getcwd();
$images = glob($curdir."/admin/gallery/".$filename."/*.*");
$count=0;
foreach($images as $image)
{
$dir="http://screccs.com".str_replace($curdir,"",$image);
echo "<a href='".$dir."' target='_blank'>"; echo "<img src='".$dir."'class='img-rounded' width='200' height='150'/></a><br />";
if($count<3)
{
$count++;
echo "</td>";
echo "<td class='td1'>";
}
else
{
echo "</tr>";
$count=0;
echo "<tr>";
echo "<td class='td1'>";
}
}
echo '</tr>';
echo '</table>';
}
I believe issue is related to folder permissions. Give proper read permission and it will work.
I have stored images path in database and images in project folder. Now i am trying to view all IMAGES with their PRODUCTS in my HTML template. I have written the following code and its given me an empty images box in the output and when i open that image box in the new tab, it opens the following URL.
http://localhost/cms/1
Kindly tell me the way of referring images in 'img src ' tag.
include 'connect.php';
$image_query = "select * from product_images";
$image_query_run = mysql_query($image_query);
$image_query_fetch = mysql_fetch_array($image_query_run);
if (!$query=mysql_query ("select product_id,name from products")) {
echo mysql_error();
} else {
while ($query_array = mysql_fetch_array($query)) {
echo '</br><pre>';
$product_id = $query_array['product_id'];
echo "<a href='single_product.php?product_id=$product_id' >";
print_r ($query_array['name']);
echo "</a>";
echo "<img src=". $image_query_fetch
['images'] ." alt=\"\" />";
echo '</pre>';
}
}
} else {
echo "You need to Log in to visit this Page";
}
Add your local image path before source
example
echo "<img src='http://localhost/cms/1/". $image_query_fetch['images'] .'" alt=\"\" />";
*Use PHP *
<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
You can use the HTML base tag to set a base URL that relative URLs will be resolved from.
See -> http://www.w3schools.com/tags/tag_base.asp
Please Help, nothing i add after the ?> works, i tried to put the code in a echo, but its not working would someone please put together a JSFiddle for me or point me in the right direction, i am fairly new with PHP, Thanks for the help
<?php
$filepath = 'http://www.godsgypsychristianchurch.net/music.json';
$content = file_get_contents($filepath);
$json = json_decode($content, true);
foreach ($json['rows'] as $row)
{
if ($_GET['album'] == $row[doc]['album'])
{
echo "<title>{$row[doc]['album']}</title>";
echo "<table align=\"center\" border=\"0\"><tr><td valign=\"top\" width=\"330\">";
echo "<img src=\"{$row['doc']['artwork']}\" alt=\"my image \" width=\"250\" /><br /><br />";
echo "<div class=\"albuminfo\" id=\"albuminfo\">";
print ' Download entire album.<p>';
echo "<font color=\"#fff\">Album: {$row[doc]['album']}</font><br />";
echo "<font color=\"#fff\">Church: {$row[doc]['church']}</font><br />";
echo "<font color=\"#fff\">Description: {$row[doc]['des']}</font><P><br /><P>";
echo "Tweet<br><br>";
print '<div id="like-button"></div>';
echo "<td valign=\"top\">";
echo "<div class=\"playlist\" id=\"playlist\">";
echo "<ol>";
$songCount = 0;
foreach ($row['doc']['tracks'] as $song) {
++$songCount;
$songUrl = $row['doc']['baseurl'] . urldecode($song['url']);
echo "<li>{$song['name']}<div id=\"download\">Download</li>";
}
echo "</ol>";
echo "<br><div id=\"player\"><audio preload></audio></div>";
echo "</div>";
echo "<P>";
echo "<small>To download a single MP3 at a time:</br><b>Windows OS:</b> hold the ALT button on the keyboard and click the Download button<br><b>Mac OSX:</b> hold the OPTION button on the keyboard and click the Download button<P><BR><b>Controls:</b><br>Play/Pause = spacebar</br>Next track = Right arrow<br>Previous track = Left arrow";
echo '</tr></td></table>';
}
}
exit;
?>
<!----NOTTING SHOWING UP---->
<!-- begin htmlcommentbox.com -->
<div id="HCB_comment_box">HTML Comment Box is loading comments...</div>
<script type="text/javascript" language="javascript" id="hcb"> /*<!--*/ if(!window.hcb_user){hcb_user={};} (function(){s=document.createElement("script");s.setAttribute("type","text/javascript");s.setAttribute("src", "http://www.htmlcommentbox.com/jread?page="+escape((window.hcb_user && hcb_user.PAGE)||(""+window.location)).replace("+","%2B")+"&opts=0&num=10");if (typeof s!="undefined") document.getElementsByTagName("head")[0].appendChild(s);})(); /*-->*/ </script>
<!-- end htmlcommentbox.com -->
Because you are using exit, which terminates the script. Get rid of that and it will continue to output the HTML underneath.
http://php.net/manual/en/function.exit.php
Omit the exit.
Exit will end Php processing.
With php exit the parser stops, and hands back all the content gathered until that point to the web server. Simply delete the exit; row.
I have a PHP script to echo the contents of a Mysql table.
I'm using it as a small CMS for a static page.
What I want to know is how can I go about displaying an Image in my PHP script.
I have a form that submits the Date, Title, Message, and Image.
I'm using the Image field to insert the URL of an image.
Whats the correct way of displaying the image on a page.
Below is my code:
<?php
$con = mysql_connect("localhost","name","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databaseName", $con);
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo $row['Date'];
echo "<br />";
echo $row['Title'];
echo "<br />";
echo $row['Message'];
echo "<br />";
echo $row['Image'];
echo "<br />";
echo "<br />";
echo "<br />";
}
mysql_close($con);
?>
This is the outcome:
17th Feb
Title
Here is my Message
http://vickybeeching.com/blog/wp-content/uploads/2011/09/canthearyou.jpeg
18th Feb
Title
Here is my Message
http://vickybeeching.com/blog/wp-content/uploads/2011/09/canthearyou.jpeg
but I want the page to display the image not the URL.
I'm guessing just using the <img></img> tags, but I'm not sure where in the PHP.
You can write code in image tag instead of echo directly.
Try this code.
echo "<img src='".$row['Image']."'/>";
Try this code, but give image folder path perfectly
<?php
$imagepath = "../uploads/";
echo "<img src='".$imagepath.$row['Image']. "' alt='' height='200' width='200' /> ";
?>
According to need , you can change height and width of the image. but clarity differs.
Thanks
echo $row['Image'];
replace like this
echo '<img src="'.$row['Image'].'" alt="" />';