How to make gallery photos even in PHP - php

Here is my code for a gallery on the website that I am making
<h3>Gallery</h3>
<?php
include"includes/connection.php";
$sql = "select * from kategorije";
$res = mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($res))
{
?>
<h4 class="text-center"><?php echo ucwords($row['naziv']); ?></h4>
<?php
$id = $row['sifra'];
$sql1 = "select * from rad where kategorija = $id";
$res1 = mysqli_query($con, $sql1);
$n = mysqli_num_rows($res1);
if($n>0)
{
while($row1 = mysqli_fetch_assoc($res1))
{
$pid = $row1['sifra'];
?>
<div class="col-md-2 col_1">
<img src="uploads/<?php echo $row1['datoteka']; ?>" class="img-responsive" alt=""/>
</div>
<?php
}
}else{
?>
<div class="col-md-2 col_1">
<h6>No Images</h6>
</div>
<?php
}
?>
<div class="clearfix"></div>
<?php
}
?>
I need to make that all the photos in gallery have same height and width when they are uploaded, but I don't know how. I tried to insert this code, but it just doesn't work:
echo"<img src='$dir_path$files[$i]'style='width:150px;height:200px;'>
Also I tried in CSS but it won't work either.

Since you just want to display them in another size, you can achieve this by the way you tried, but with the correct syntax. I hope its this line of code where you want the resized image:
<img src="uploads/<?php echo `$row1['datoteka']; ?>" class="img-responsive" height="200" width="150" alt=""/>`
Let me know it it worked for you. It this wasen't the line you want to do it, keep in mind you simply have to follow the syntax:
<img src="http://url.com/pic.png" width="150" height="200">
Thats all. :)

Related

display images from db

i have a table
tbl_image
--------------
imgID(int)
imgName(varchar)
image(blob)
here is code to display image :
<?php
$query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)){
$imgName = $row['imgName'];
echo '<div class="col-sm-3 gallery-grids-left">
<div class="gallery-grid">
<a class="example-image-link" href="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"
data-lightbox="example-set"
data-title='.$imgName.'>
<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"/></a></div></div>';
}
?>
but I'm really not like to use echo ''; so I changed to
<?php
$query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)){
$imgName = $row['imgName'];
?>
<div class="col-sm-3 gallery-grids-left">
<div class="gallery-grid">
<a class="example-image-link" data-lightbox="example-set"
href="<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"; '?>"
data-title="<?php echo imgName;?>">
<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"; '?>
</a>
</div>
</div>
<?php }
?>
It just display only image , no title no "image block" like this image demo
Plz help me to show my mistake?and how to fix it.
Many thanks,
HTML error in your code: wrong binding of PHP scripting
<?php
$query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)){
$imgName = $row['imgName'];
?>
<div class="col-sm-3 gallery-grids-left">
<div class="gallery-grid">
<a class="example-image-link" data-lightbox="example-set"
href="data:image/jpeg;base64,<?php echo base64_encode($row['image'] )?>"
data-title="<?php echo $imgName;?>">
<img src="data:image/jpeg;base64,<?php echo base64_encode($row['image']) ?> />
</a>
</div>
</div>
<?php }
?>
You have missed $ for variable imgName
Try below code:
<?php
$query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)){
$imgName = $row['imgName'];
?>
<div class="col-sm-3 gallery-grids-left">
<div class="gallery-grid">
<a class="example-image-link" data-lightbox="example-set"
href="<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"; '?>"
data-title="<?php echo $imgName;?>">
<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"; '?>
</a>
</div>
</div>
<?php }
?>
First of all, as #Suyog pointed out, you missed $ on imgName variable in data-title. Second, if you are simplifying the code to use html as much as possible - do that and remove the extra stuff. Have something like this:
<?php
$query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)){
$imgName = $row['imgName'];
?>
<div class="col-sm-3 gallery-grids-left">
<div class="gallery-grid">
<a class="example-image-link" data-lightbox="example-set"
href='<img src="data:image/jpeg;base64,<?php echo base64_encode($row['image'] );?>"/>'
data-title="<?php echo $imgName;?>">
<img src="data:image/jpeg;base64,<?php echo base64_encode($row['image'] ); ?>">
</a>
</div>
</div>
<?php } ?>
Other than that, I would strongly advise against storing binary data in the database. This is not what the database is for. Store images are files and store filenames in the database. It'll make your life easier.

fetching data from php Admin for display

I have a total 4 book of abc. I showing that author 1 book in book detail page. As well as i want to display remaining 3 books of author in below other books by this writer section. But in this section showing 1 book of that author. Please help me what can i do?
Book detail code:
<?php
if(isset($_GET['pid']) && !empty($_GET['pid'])){
$id = $_GET['pid'];
$result = $conn->query("SELECT * FROM bookrecord WHERE id ='$id' and status='a'");
if($result->num_rows > 0){
$imgData = $result->fetch_assoc();
extract($imgData);
?>
<div class="preview-pic tab-content">
<div class="tab-pane active" id="pic-1">
<img src="admin_pannel/book_images/<?php echo $imgData['file']; ?>" class="img-responsive" alt=""/>
</div>
</div>
<?php
}
}
?>
OTHER BOOKS BY THIS WRITER code:
<?php
if(isset($_GET['auth_name']) && !empty($_GET['auth_name'])){
$auth_name = $_GET['auth_name'];
$result = $conn->query("SELECT * FROM bookrecord WHERE author_name ='$auth_name' and status='a'");
if($result->num_rows > 0){
$row = $result->fetch_assoc();
extract($row);
?>
<div class="col-md-2">
<img src="admin_pannel/book_images/<?php echo $row['file']; ?>" class="img-responsive center-block">
<h4 class="text-center"><?php echo $row['book_title']; ?></h4>
<h5 class="text-center"><?php echo $row['author_name']; ?></h5>
</div>
<?php
}
}
?>
This is my (book-detail.php) page and i come from this link:
<a class="btnpoem" href="book-detail.php?pid=<?php echo htmlentities($row['id']);?>&auth_name=<?php echo htmlentities($row['author_name']);?>">Download</a>
You need to apply while() in your second code:-
<?php
if(isset($_GET['auth_name']) && !empty($_GET['auth_name'])){
$auth_name = $_GET['auth_name'];
$result = $conn->query("SELECT * FROM bookrecord WHERE author_name ='$auth_name' and status='a'");
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
extract($row);
?>
<div class="col-md-2">
<img src="admin_pannel/book_images/<?php echo $row['file']; ?>" class="img-responsive center-block">
<h4 class="text-center"><?php echo $row['book_title']; ?></h4>
<h5 class="text-center"><?php echo $row['author_name']; ?></h5>
</div>
<?php
}
}
}
?>
Note:- Your queries are Wide-Open for SQL-INJECTION.
So try to use prepared statements.
Reference:-
PHP: mysqli::prepare - Manual
PHP: PDO::prepare - Manual
Just change your code from this code and in this code you have to change your sql query a lil bit to get rows except first one because you have got first row(Image), and instead of while loop you can also use foreach
<?php
if(isset($_GET['auth_name']) && !empty($_GET['auth_name'])){
$auth_name = $_GET['auth_name'];
$result = $conn->query("SELECT * FROM bookrecord WHERE author_name ='$auth_name' and status='a'" and WHERE ID NOT IN (SELECT MIN(ID) FROM your_table GROUP BY USER_ID));
if($result->num_rows > 0){
foreach ($result as $key){
?>
<div class="col-md-2">
<img src="admin_pannel/book_images/<?php echo $key['file']; ?>" class="img-responsive center-block">
<h4 class="text-center"><?php echo $key['book_title']; ?></h4>
<h5 class="text-center"><?php echo $key['author_name']; ?></h5>
</div>
<?php
}
}
}
?>`

My drop down div isn't visible

I am working on a online shopping website.
This is a div that will display the item and the image should be also used from database.
Help me!
This is the code by which I am trying to fetch the data from mysql database created on phpmyadmin and construct this div.
The problem is that I can't see any output
<?php
include "connect_database.php";
$sql="SELECT * FROM product";
$result = mysqli_query($connection, $sql);
$rows=mysqli_fetch_assoc($result);
while ($rows=mysqli_fetch_assoc($result)) {
echo "<div class='item'>
<img class='image' src='".$rows["image"]."'><br/>
<span>".$rows["product_name"]."</span><hr/>
<span>".$rows["model_no"]."</span><hr/>
<span>Rs ".$rows["amount"]."</span>
</div>
";
}
?>
You call mysqli_fetch_assoc first before the while loop. That call reads the first row, but the result is not used. After that, the while loop kicks in and processes and outputs the rest of the rows. If you have only one row, you won't see anything.
So you can try this, with that line removed.
Note that I renamed $rows to $row. This isn't necessary, but since the variable will contain data of only one row, I think this name is better. It will help you understand the code better if you need to change it later.
<?php
include "connect_database.php";
$sql="SELECT * FROM product";
$result = mysqli_query($connection, $sql);
while ($row=mysqli_fetch_assoc($result)) {
echo "<div class='item'>
<img class='image' src='".$row["image"]."'><br/>
<span>".$row["product_name"]."</span><hr/>
<span>".$row["model_no"]."</span><hr/>
<span>Rs ".$row["amount"]."</span>
</div>
";
}
?>
echo '<div class="item">
<img class="image" src="'.$rows['image'].'"><br/>
<span>"'.$rows["product_name"].'"</span><hr/> <span>"'.$rows['model_no'].'"</span><hr/>
<span>Rs "'.$rows['amount'].'"</span>
</div>
';
<?php
include "connect_database.php";
$sql="SELECT * FROM product";
$result = mysqli_query($connection, $sql);
while ($rows=mysqli_fetch_array($result,MYSQLI_BOTH)) {
echo "<div class='item'>
<img class='image' src="<?php echo $rows['image']; ?>"><br/>
<span><?php echo $rows['product_name']; ?></span><hr/>
<span><?php echo $rows['model_no']; ?></span><hr/>
<span>Rs<?php echo $rows['amount']; ?></span>
</div>
";
}
?>

Using a link to connect two pages in database

I have a page with a search bar that gets images of people in alphabetical order from a database. I also have a page with Next and Previous buttons that allows the user to browse through the database of images using Next and Previous buttons. I'm trying figure out a way to make an image a link so that the user can search through images, click the image, and it takes them to the same image on the Next and Previous page.
This is my code that allows me to search and returns, lastname, firstname, and a picture:
<div class="clearfix"></div>
<?php
if (isset($_GET['LastName'])) {
$ln = $_GET['LastName'];
}
include 'connection.php';
$query = "SELECT * FROM residents WHERE LastName like '$ln%' ";
$result = mysql_query($query);
while($person = mysql_fetch_array($result)) { ?>
<div class="media col-sm-4">
<a class="pull-left" href="Browse.php">
<img class="media-object" src="upload/<?php echo $person['Picture'];?>" width="100"
height="100"/>
</a>
<div class="media-body">
<h4 class="media-heading"><?php echo $person['LastName'] . ", " .
$person['FirstName']; ?></h4>
</div>
The page I'm trying to connect to is "Browse.php" but as you browse through the images the URL changes by increasing..."Browse.php?page=1"..."Browse.php?page=2" and so on. Is there an easy way to connect an image with the corresponding Browse.php page? I've tried several things and any help would be much appreciated!
If you have id column in residents table. You can do something like this.
<div class="clearfix"></div>
<?php
if (isset($_GET['page'])) {
$page_id = $_GET['page'];
}
include 'connection.php';
$query = "SELECT * FROM residents WHERE ID = $page_id";
$result = mysql_query($query);
while($person = mysql_fetch_array($result)) { ?>
<div class="media col-sm-4">
<a class="pull-left" href="Browse.php?page=<?php echo $page_id; ?>">
<img class="media-object" src="upload/<?php echo $person['Picture'];?>" width="100" height="100"/>
</a>
<div class="media-body">
<h4 class="media-heading"><?php echo $person['LastName'] . ", " . $person['FirstName']; ?></h4>
</div>
</div>
<?php } ?>

Setting width and height in image

I'm currently coding a slide that uses images that are stored in a database.
I would like to know how can i set width and height for the image. Here's my code:
<div id="my-slide">
<?php
$resultado = mysqli_query($con, "SELECT * FROM slides ORDER BY id DESC LIMIT 3");
while($fila = mysqli_fetch_array($resultado))
{
?>
<div data-lazy-background="images/<?php echo $fila["slideimg"]; ?>">
<h5><?php echo $fila["cap"]; ?></h5>
</div>
<?php
}
?>
</div>
Set Height and Width between your image Div like
<div width="100px" height="100px" data-lazy-background="images/<?php echo $fila["slideimg"]; ?>">

Categories