Displying images stored in database - php

I am doing gallery and I need some help. I have uploaded images to database through website - they are stored in MySQL (names) and in folder called images. What I want is to display (miniatures) them in line and on click enlarge them.
What my code does is displaying miniatures and links them to nothing :/ ...
This is my code:
<?php
$hostname_connect= "localhost";
$database_connect= "gallery";
$username_connect= "root";
$password_connect= "root";
$connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
#mysql_select_db($database_connect) or die (mysql_error());
$query_image = "SELECT * FROM gallery_images";
$result = mysql_query($query_image);
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
?>
<a href = "javascript:void(0)" onclick = "document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'">
<?php
echo '<img alt="gallery" src="images/'.$row["image"].'" class="pic-resize" alt=""></a>';
}
while($row = mysql_fetch_array($result))
{
?>
<div id="light" class="white_content">
<?php
echo '<img alt="gallery" src="images/'.$row["image"].'" class="" alt=""></a>';
?>
Close
</div>
<?php
}
}
else
{
echo 'File name not found in database';
}
?>

You are using this in a loop:
<div id="light" class="white_content">
So you will have multiple elements with the same ID and that is not allowed.
Then you try to access them like:
document.getElementById('light')
Which will give you the first element it finds and not the actual one you want to enlarge (unless it is the first...).
Personally I would use the standard lightbox solution, link your thumbnail to your big image (instead of javascript:void(0)) and use the href attribute of your link to set the source for the big image using javascript when the thumbnail gets clicked.
Edit: An example for the html to get you started:
<?php
while($row = mysql_fetch_array($result))
{
?>
<a href="<?php echo 'images/'.$row["image"]; ?>" onclick="return showImage(this);">
<?php
echo '<img alt="gallery" src="images/'.$row["image"].'" class="pic-resize" alt=""></a>';
}
?>
<div id="light"><img src='' alt=''></div>
Now you just have to write the showImage() function in javascript that will do the actual work:
get the href attribute of the clicked link;
set the source of the image in #light to that value;
show the #light element.

Related

this is my php code to view multiple images from database.but i can only see one image in front end.please help me

<?php
$servername = "*****";
$username = "****";
$password = "";
$dbname = "****";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$product_id = intval($_GET['product_id']);
// make sure its only an id (SQL Incjection problems)
$sql = "SELECT * FROM product_gallery WHERE pid=$product_id";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_array($result)) {
echo"<div class='tg-productbox'>";
echo" <div id='tg-thumblider' class='tg-thumblider tg-productgalleryslider owl-carousel'>";
echo" <figure class='item'><img src='$row[product_image]' alt='image description'></figure>";
echo" </div>";
echo" </div>";
echo" </div>";
};
} else {
echo "0 results";
}
$conn->close();
?>
This is my php code to view multiple images from database.but I can only see one image in front end.please help me..there is a main image and a slider to view to multiple images. It is similar to how we see product images in a shopping site
this is my php code to view multiple images from database.but i can only see one image in front end.please help me..there is a main image and a slider to view to multiple images.its is similar to how we see product images in a shopping site
There are some other mistakes like one extra closing div in loop and semicolon }; which not necessary.
Try something like below if you are getting multiple records it will definitely work.
while ($row = mysqli_fetch_array($result)) {
extract($row);
?>
<div class='tg-productbox'>
<div id='tg-thumblider' class='tg-thumblider tg-productgalleryslider owl-carousel'>
<figure class='item'>
<img src="<?php echo $product_image; ?>" alt="image description">
</figure>
</div>
</div>
<?php
}
Changes
Remove id='tg-thumblider' from echo "<div id='tg-thumblider' class='tg-thumblider. ID must need to be unique. According to the code provided, In while loop, every <div> will have same ID, which is wrong. So, either remove that ID or give some unique ID to each div.
Remove extra echo "</div>";
Remove ; from closing of while loop.
Code:
<?php
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_array($result)) {
echo "<div class='tg-productbox'>";
echo "<div class='tg-thumblider tg-productgalleryslider owl-carousel'>";
echo "<figure class='item'><img src='".$row['product_image']."' alt='image description'></figure>";
echo "</div>";
echo "</div>";
}
}?>

How to display multiples images from MySql

I would like to know how to display multiples images from MySQL in Html.
I have two files:
photogallery.php where I display the image and gallery.php where I have the php code.
This works but I can only display 1 image and I can't see all images!
Here is the code for photogallery.php where I display the photo:
<div align='left'>
<img src='gallery.php' height='95' width='95'/>
</div>
and here is the code for gallery.php:
session_start();
$host = "localhost";
$username = "root";
$password = "";
$db_name = "photos";
$tbl_name="gallery";
mysql_connect("$host","$username","$password")or die ("error22");
mysql_select_db("$db_name") or die("error2");
$ussername=$_SESSION['username'];
$query= mysql_query("SELECT * FROM $tbl_name where username='$ussername'");
while($row= mysql_fetch_assoc($query)){
$imageData=$row["image"];
//header("content-type:image/jpeg");
echo $imageData;
}
Thank you!
If you have image data stored on the database with a function like get_file_contents :
while($row= mysql_fetch_assoc($query)){
$imageData=$row["image"];
echo "<div align='left'>";
echo " <img src='data:image/jpeg;base64,"
. base64_encode($imageData) . "' height='95' width='95'/>";
echo "</div>";
}
if we assume that $row["image"] is path to image, then you should change your code like below:
...
while($row= mysql_fetch_assoc($query)){
$imageData=$row["image"];
?>
<div align='left'>
<img src='<?php echo $imageData;?>' height='95' width='95'/>
</div>
<?php
}
...
I don't execute my code, I want show you only idea

unable to retrieve data from mysql to my webpage

I want to create an automatic cms in my website using php and mysql
i.e. I would just feed data to mysql table and it will generate result.
so my code is:
<!DOCTYPE html>
<html>
<head>
<?php
include 'head.php';
include 'conct.php';
?>
<title>GIZMOMANIACS|DOWNLOADS</title>
</head>
<body>
<div id="container" style="width:100%;height:100%;clear:both">
<div id="header" style="background-color:#FFFFFF;">
<div class="head" style="clear:both">
<a href= "http://gizmomaniacs.site88.net">
<img src="/GM%203D.gif" width= "200" height="100" alt='gizmomaniacs logo'></a></div>
<h1 style="margin-bottom:0; float:right"><font id="gmfont">GIZMOMANIACS</font></h1>
</div>
<div id="menu" style="clear:both;background-color:#0762AE">
<?php include 'head.html'; ?></div> <div class="content" >
<?php include 'search.php'; ?>
<ul>
<?php
$sql = "SELECT * from downloads";
$result = mysql_query($sql);
if($result==false){
$view = "error";
$error = "Could not retrieve values";
}
else {
$dload = $GET_['downloadname'];
$imagelink = $GET_['imagelink'];
$title = $GET_['title'];
while ($row = mysql_fetch_array($result))
{
echo "<li><a class =\"alldld\" href=\"$dload\" title=\"$title\"><img class=\"downloads\" src =\"$imagelink\"/>$title</a><br></li>";
}
}
?>
</ul>
</div>
<div class="social">
<?php
include 'social.php';
?></div>
</div>
</body>
</html>
everything is gong well but three thing are not happening they are:
a href atrribute not retreiving from db
a title atrribute not retreiving from db
img src atrribute not retreiving from db
in the actual source it is showing
<a class ="alldld" href="" title=""><img class="downloads" src =""/></a><br></li>
<li><a class ="alldld" href="" title=""><img class="downloads" src =""/></a>
the src href title attribute are blank
so what to do?
change this part of code
$dload = $GET_['downloadname'];
$imagelink = $GET_['imagelink'];
$title = $GET_['title'];
while ($row = mysql_fetch_array($result))
{
echo "<li><a class =\"alldld\" href=\"$dload\" title=\"$title\"><img class=\"downloads\" src =\"$imagelink\"/>$title</a><br></li>";
}
to
while ($row = mysql_fetch_array($result))
{
$dload = $row['downloadname'];
$imagelink = $row['imagelink'];
$title = $row['title'];
echo "<li><a class =\"alldld\" href=\"$dload\" title=\"$title\"><img class=\"downloads\" src =\"$imagelink\"/>$title</a><br></li>";
}
While retrieving from DB you need to use the array in which the values are fetched and not the $_GET array. also there is no such thing as $GET_.
Important Note: Stop using mysql_ function and start using mysqli_* functions or PDO.For more info see here
You're outputting the rows like this:
$dload = $GET_['downloadname'];
$imagelink = $GET_['imagelink'];
$title = $GET_['title'];
while ($row = mysql_fetch_array($result))
{
echo "<li><a class =\"alldld\" href=\"$dload\" title=\"$title\"><img class=\"downloads\" src =\"$imagelink\"/>$title</a><br></li>";
}
}
There's a couple of issues with that. First of all, if you wanted to retrieve it from the query string, you'd want to use $_GET, not $GET_. Second, you don't want to retrieve it from the query string with $_GET; you want to retrieve it from $row. Thirdly, you need to put it inside the while loop. Once you've fixed that, it should work.
$_GET is the array containing values passed in the URL.
<?php
// Let's take the following URL
// http://localhost/index.php?name=John&age=20
echo $_GET["name"]; // Will display 'John'
echo $_GET["age"]; // Will display '20'
?>
When you select something from the database, the variable $row contains each line of the results that you get using the function mysql_fetch_array. Try this to display your data :
<?php
// ...
while ($row = mysql_fetch_array($result)) {
$dload = $row["downloadname"];
$imagelink = $row["imagelink"];
$title = $row["title"];
echo "<li><a class =\"alldld\" href=\"$dload\" title=\"$title\"><img class=\"downloads\" src =\"$imagelink\"/>$title</a><br></li>";
}
?>

retrieve image from mysql database to array php

By compiling this, display three image get my database and show it, when i click each image popup window show last image of my database.I want to know how to display the particular image, when i click first image,then popup window show first image and description, as well as same way second and third image. check to array loop in this code...
enter code here
<?php
$sql=mysql_query("select * from product_reg")or die(mysql_error());
while($row=mysql_fetch_array($sql))
{
$productname=$row['productname'];
$productid=$row['productid'];
$description=$row['description'];
$image=$row['image'];
$firstN = array();
$i=0;
$firstN = '<img src="'.$row ['image'].'">';
echo ' <a href="#" class="big-link" data-reveal-id="myModal" name="image" style="float:left;margin:100px 0 100px 100px;"> ' ; echo $productname;echo $firstN[$i];
$r=$firstN[$i];
echo '</a>';
$i++;
}
?>
<div id="myModal" class="reveal-modal">
<form>
<table>
<tr><td><?php echo $r; ?></td>
<td><h1>Reveal Modal Goodness</h1>
<p>This is a default modal in all its glory, but any of the styles here can easily be changed in the CSS.</p></td>
<a class="close-reveal-modal">×</a>
</div>
</body>
</html>
I guess you have the images stored in the database like BLOB data. If so you need to create a handler to retrieve those images and render them as image/[mime]...
So in short.
In your code you need to create a new file iz get_image.php in it you need to make a request to the server and retrieve the image so you can send it to the client.
In your code you need to change the image path to the handler path with some query parameters.
$firstN = '<img src="get_image.php?imageid='.$row ['productid'].'">';
There are a lot information how to render the image to the client from the internet.
may be you have to declare your $firstN = array(); and then incrementor $i=0; out of while loop and put in array like this:
$firstN[$i] = '<img src="'.$row['image'].'">';
below is the full code:
<?php
$sql=mysql_query("select * from product_reg")or die(mysql_error());
$firstN = array();
$i=0;
while($row=mysql_fetch_array($sql))
{
$productname=$row['productname'];
$productid=$row['productid'];
$description=$row['description'];
$image=$row['image'];
$firstN[$i] = '<img src="'.$row['image'].'">';
echo '<a href="#" class="big-link" data-reveal-id="myModal" name="image" style="float:left;margin:100px 0 100px 100px;"> ';
echo $productname;
echo $firstN[$i];
$r=$firstN[$i];
echo '</a>';
$i++;
}
?>
Updates:
You have a space here:
$firstN[$i] = '<img src="'.$row['image'].'">';
//-----------------------------^----here at this code block;
To get the last image , you need to modify your query to have SORT BY productid DESC
To display the images
echo "<a href='xxx.php?imgid=$image'><img src='yourimagesfolderpath/$image.jpg'> </a>";
to navigate in the images , you have to use JQUERY

How get url from database and show it in a iframe page

That's a simple script to adult content site I'd make with pieces of another codes. I am not a expertin PHP! I just can understand the basic.
<html>
<body>
<div align="center">
<div align="center" id="box">
<div align="center" id="carrosel">
<?php
$quantidade = 64;
$pagina = (isset($_GET['pagina'])) ? (int)$_GET['pagina'] : 1;
$inicio = ($quantidade * $pagina) - $quantidade;
$sql = "SELECT * FROM teen ORDER BY RAND() ASC LIMIT $inicio, $quantidade";
$qr = mysql_query($sql) or die(mysql_error());
while($ln = mysql_fetch_assoc($qr)){
?>
So, my problem is here. In that way, when you click the thumb, the browser will open that url that is out my site. the idea is to embed the video on a new iframe page. Someone can help me?
<a href="<?php echo $ln['url_video'];?>"target="_blank">
<img src="<?php echo $ln['thumb_video'];?>" /></a>
<?php
}
?>
<div align="center">
<?php
$sqlTotal = "SELECT thumb FROM teen";
$qrTotal = mysql_query($sqlTotal) or die(mysql_error());
$numTotal = mysql_num_rows($qrTotal);
$totalPagina= ceil($numTotal/$quantidade);
for($i = 1; $i <= $totalPagina; $i++){
if($i == $pagina)
echo $i;
else
echo "$i ";
}
?>
</div><br><br>
</body>
</html>
_blank : Opens the linked document in a new window or tab
_self : Opens the linked document in the same frame as it was clicked (this is default)
_parent : Opens the linked document in the parent frame
_top : Opens the linked document in the full body of the window
Or you can use iframe .or embed some players to play the video in the same area.
Use echo to print your links
while($ln = mysql_fetch_assoc($qr)){
echo '<a href='.$ln['url_video'].'target="_blank">',
'<img src='.$ln['thumb_video'].'/></a>';
}
And I found one more thing replace your echo "$i "; with this:
echo '$i';
And one more thing, use mysqli instead of mysql
You can also use window.open function, it will open the video in a new popup,
<a href="Javascript:;" onclick="window.open('<?php echo $ln['url_video'];?>',
'View Video')" ><img src="<?php echo $ln['thumb_video'];?>" /></a>
More details for window.open at http://www.w3schools.com/jsref/met_win_open.asp

Categories