I'd like to put images next to each other when they are selected from a while loop in PHP.
So, currently it looks like this http://prntscr.com/7tb42j
And I'd like it to put the images next to each other.
My foreach loop looks like this:
<div id='profile-album'>
<?php
$get_fotos = "SELECT * FROM fotos_profile WHERE username=:username LIMIT 4";
$stmt = $db->prepare($get_fotos);
$stmt->bindParam(':username', $username);
$stmt->execute();
foreach($stmt as $row)
{
$pic = $row['pic'];
$title_foto = $row['title'];
?>
<div id='album-foto-1'><img src="userdata/profile_fotos/<?php echo $pic; ?>" height='100px' width='206px' style='padding-right: 6px'/></div>
<?php } ?>
You'll just need to add display:inline-block to each div that contains a picture.
<div id='profile-album'>
<?php
$get_fotos = "SELECT * FROM fotos_profile WHERE username=:username LIMIT 4";
$stmt = $db->prepare($get_fotos);
$stmt->bindParam(':username', $username);
$stmt->execute();
foreach($stmt as $row)
{
$pic = $row['pic'];
$title_foto = $row['title'];
?>
<div id='album-foto-1' style="display:inline-block"><img src="userdata/profile_fotos/<?php echo $pic; ?>" height='100px' width='206px' style='padding-right: 6px'/></div>
<?php } ?>
Make sure on "album-foto" you have the css set for (see below why I use album-foto not album-foto-1):
.album-foto {
display:inline; // or inline-block depending how you want to be displayed
}
Also if you're displaying multiple images you should use a class not an ID for the images as duplication of the same ID is not good:
foreach($stmt as $row)
{
$id = $row["id"]; // or whatever your ID field is
$pic = $row['pic'];
$title_foto = $row['title'];
?>
<div class='album-foto' id='album-foto-<? echo $id; ?>'><img src="userdata/profile_fotos/<?php echo $pic; ?>" height='100px' width='206px' style='padding-right: 6px'/></div>
<?php } ?>
Related
i'm newbie on this.
I explain shortly what i need.
Obtained the category ID via $_GET i want to print how images i have for each product (for that category).
For example, i have 2 product and 2 images for each product.
Problem found: with this code i have printed only the images for the first product and not for the second. Why? Thanks
$id=17;
$prod = "SELECT pd_id FROM product WHERE cat_id = ?";
$stmt = $con->prepare($prod);
$stmt->bindParam(1, $id);
$stmt->execute();
$num = $stmt->rowCount();
if($num>0)
{
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$immagini = "SELECT pd_image FROM pd_images WHERE pd_id = ?";
$stmt = $con->prepare($immagini);
$stmt->bindParam(1, $pd_id);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
?>
<img src="<?php echo $home_url; ?>images/product/<?php echo $pd_image; ?>" class="img-fluid border-radius-0" alt="">
<?php
}
}
}else { ?>
<div class="col-lg-12 text-center">
<h6>There's no products for this category</h6>
<i class="rt-icon2-user"></i>
<div class="divider-10 d-none d-xl-block"></div>
</div>
<?php } ?>
I have the following code. How do I show the first image in the database with index of 0 for the large image display at end of the code? Right now it is showing the last image in the database.
<div id="imgWheel" class="treatmentContainer">
<?php
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$product = $row["product"];
$room = $row["room"];
$style = $row["style"];
$tags = $row["tags"];
$src = $row["url"];
$dataid = $row["id"];
$imgClass = "";
if (in_array($src, $favourites)) {
$imgClass = " favourite";
}
echo "<div class='treatment$imgClass' data-url='$src' data-product='$product' data-room='$room' data-style='$style' data-tags='$tags' data-number='$dataid' id='pic_$dataid' >";
echo "<img src='$src' crossorigin='anonymous'/>";
echo "</div>";
}
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $src ?>" />
</div>
Your result set is alreadyx ordered by id, so you need only a variable, to be filled once with the first imageurl
<div id="imgWheel" class="treatmentContainer">
<?php
$bigpictureurl = "";
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$product = $row["product"];
$room = $row["room"];
$style = $row["style"];
$tags = $row["tags"];
$src = $row["url"];
$dataid = $row["id"];
if (empty($bigpictureurl)) {
$bigpictureurl = $src ;
}
$imgClass = "";
if (in_array($src, $favourites)) {
$imgClass = " favourite";
}
echo "<div class='treatment$imgClass' data-url='$src' data-product='$product' data-room='$room' data-style='$style' data-tags='$tags' data-number='$dataid' id='pic_$dataid' >";
echo "<img src='$src' crossorigin='anonymous'/>";
echo "</div>";
}
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $bigpictureurl ?>" />
</div>
You just need to update your SQL query, just add LIMIT 1. This will limit the result just to 1 record and as you have ORDER id ASC, it will show the first record of the given user (as per you it is 0).
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id ASC LIMIT 1;";
A quick and dirty solution is to save your first image in some separate variables, for example like this:
$isFirst = true;
$firstImageSrc = "";
$result = ....;
while (...) {
// set your $product, $room etc here
if ($isFirst) {
$isFirst = false;
$firstImageSrc = $src;
}
}
echo ...
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $firstImageSrc ?>" />
</div>
A much more elegant solution would be to create an array with all your images, so that you can separate your php from your html. I will refactor your code below, and fix your first image problem as well:
<?php
$images = [];
$idx = 0;
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$images[$idx]["product"] = $row["product"];
$images[$idx]["room"] = $row["room"];
$images[$idx]["style"] = $row["style"];
$images[$idx]["tags"] = $row["tags"];
$images[$idx]["src"] = $row["url"];
$images[$idx]["dataid"] = $row["id"];
$images[$idx]["imgClass"] = "";
if (in_array($src, $favourites)) {
$images[$idx]["imgClass"] = " favourite";
}
$idx++;
}
?>
<div id="imgWheel" class="treatmentContainer">
<?php foreach ($images as $image) { ?>
<div class='treatment<?=$image["imgClass"]?>' data-url='<?=$image["src"]?>' data-product='<?=$image["product"]?>' data-room='<?=$image["room"]?>' data-style='<?=$image["style"]?>' data-tags='<?=$image["tags"]?>' data-number='<?=$image["dataid"]?>' id='pic_<?=$image["dataid"]?>' >
<img src='<?=$image["src"]?>' crossorigin='anonymous'/>
</div>
<?php } ?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?=$images[0]["src"]?>" />
</div>
Since you have all of that in your WHILE statement, I assume you want to echo all those records. And then at the end show the 1st pic. So for the "Large Image Display," give this a try:
<div id="display">
$query = "SELECT * FROM images WHERE user = 0;";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_ASSOC)
$src = $row["url"];
<img id="mainImage" src="<?php echo $src ?>" />
</div>
If you'd like less code, then save the value of $src inside your WHILE loop when user=0 into some other variable like $src2. And then your code simply becomes:
<img id="mainImage" src="<?php echo $src2 ?>" />
I have a page that displays the display of an item with a specific id and I want to display other items that are related based on title, subject, or author. I am having issues with displaying related items. Where I put my query together and execute it, the related results are just the same title of the item whose page I am already on (https://brawlins.com/soarOpen/itemRecord.php?id=65437). How do I displays related results that are not the same as the item whose page I currently am on?
<?php
include("config.php");
if(!isset($_GET["id"])) {
echo "No id passed into the page";
exit();
}
$id = $_GET['id'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php
$item_query = $conn->prepare("SELECT * FROM oer_search WHERE id = :id");
$item_query->bindParam(":id", $id);
$item_query->execute();
while($row = $item_query->fetch(PDO::FETCH_ASSOC)){
$type = $row['type'];
$link = $row['link'];
$title = $row['title'];
$description = $row['description'];
$subject = $row['subject'];
$pub_date = $row["publication_date"];
$source = $row['source'];
$isbn = $row["isbn_number"];
$e_isbn = $row["e_isbn_number"];
$license = $row['license'];
$license_url = $row['license_url'];
$base_url = $row['base_url'];
$author = $row['author'];
$review = $row['review'];
$image = $row["image_url"];
$loc_collection = $row["loc_collection"];
$publisher = $row['publisher'];
$pub_url = $row['publisher_url'];
?>
<title><?php echo $title ?></title>
<div class="container content-container">
<div class="card card-content">
<div class="card-body">
<?php
echo "<strong><a class='itemRecordLink' rel='external' href='$link'><h3>$title</h3></a></strong><br/>";
}
$like_query = $conn->prepare("SELECT title FROM oer_search WHERE title LIKE :title OR subject=:subject OR author=:author LIMIT 4");
$titleLike = $searchTerm = "%". $title . "%";
$like_query->bindParam(":title", $titleLike);
$like_query->bindParam(":subject", $subject);
$like_query->bindParam(":author", $author);
$like_query->execute();
echo "<div class='row'>";
while($row = $like_query->fetch(PDO::FETCH_ASSOC)){
echo "<div class='col-md-3 text-center'>";
if($image != "") {
echo "<img src='$image' class='img-fluid img-thumbnail itemRecordImage' alt='cover image' /><br>";
}
else {
echo "<img src='images/cover-image.png' class='img-fluid itemRecordImage' alt='cover image' /><br>";
}
echo "<span>$title</span>";
echo "</div>";
}
echo "</div>";
?>
</div><!--end of card body-->
</div><!--end of card-->
</div><!--end of container-->
<?php include 'footer.php'?>
</body>
</html>
Below is my code for display information of an image including its file, but this is not displaying, I don't know why.
<?php
include('config/dbconnect.php');
if(isset($_GET['id']))
{
$id = $_GET['id'];
$sql = mysqli_query($con, "SELECT * FROM collage WHERE id = '$id'");
while($row = mysqli_fetch_array($sql)) {
$name = $row['name'];
$content = $row['content'];
$size = $row['size'];
$type = $row['type'];
$date_upload = $row['date_upload'];
$file = $row['file'];
}
}
?>
<div class='body-content'>
<div class='img-name-cont'><h3><?php echo $name; ?></h3></div>
<div class='img-detail-cont'>upload: <?php echo $date_upload; ?><br>type: <?php echo $type; ?><br>size: <?php echo $size; ?>KB </div>
<div class='img-file-cont'>
<img src="img/collage/<?php echo $row['file'] ?>" style="width:130px; height:100%"></div>
<div class='img-content-cont'><?php echo $content; ?></div>
</div>
First of all use inspect to check whether the image is actually pointed to by the code. that is the actual loation might not be pointed at. what is your folder structure.
" style="width:130px; height:100%">
your code is ok. as long as php echo $row['file'] returns a file
The problem here is "Your all the variables are local to the while loop". so you can't access it outside of the while loop. Try this code.
<?php
include('config/dbconnect.php');
if(isset($_GET['id']))
{
$id = $_GET['id'];
$sql = mysqli_query($con, "SELECT * FROM collage WHERE id = '$id'");
while($row = mysqli_fetch_array($sql)) {
?>
<div class='body-content'>
<div class='img-name-cont'><h3><?php echo $row['name']; ?></h3></div>
<div class='img-detail-cont'>upload: <?php echo $row['date_upload']; ?><br>type:
<?php echo $row['type']; ?><br>size: <?php echo $row['size']; ?>KB </div>
<div class='img-file-cont'>
<img src="img/collage/<?php echo $row['file']; ?>" style="width:130px; height:100%"></div>
<div class='img-content-cont'><?php echo $row['content']; ?></div>
</div>
<?php
}
}
?>
Be sure to check echo $row['file'] if it returns the correct string of your image path with the correct file extension .
try this one :
first of all you have to check if your image path ok or not using this php function:
<?php
$img=getimagesize($imagewithpath);
if($img=="")
{ echo "there is no image ";}
else{ ?> <img src="<?php echo $imagewithpath; ?>" style="width:130px; height:100%"> <?php } ?>
As answered by Dulaj Sanjaya all your variables are local so you can't use that outside of while.
Second if you want to use your code then you have taken all data of $row in different variables then used outside of file then why don't you used same $file for image why you have used $row['file']?
Simply replace this line as
<div class='img-file-cont'>
<img src="img/collage/<?php echo $file; ?>" style="width:130px; height:100%"></div>
I have a database with a few images already set, I would like to have the url display an ID from each query as the user hits next. The user should be able to share the URL and paste it into their browser, the url should pull that unique ID from the query. The issue i am having is every time i paste a url, I get a random image and not the image that is in the ID. I'm at a loss here and im not sure what to do :( here's the code I have so far.
<?php
if (isset($_GET['id'])) {
include("PHP/db.php");
echo $where = $_GET["id"];
echo $query = "SELECT * FROM images WHERE ID =" . $where;
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
$ID = $row['ID'];
$title = $row['name'];
$image = "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}
if($_GET['next4']) {
echo 'HELLO THIS IS THE NEXT IF METHOD';
include("PHP/db.php");
$query = "SELECT * FROM images ORDER BY RAND() LIMIT 1";
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
$ID = $row['ID'];
$title = $row['name'];
$image = "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}
?>
<body>
</div>
<div id="title"> <?php echo $title ?> </div>
<div id="mainpic">
<?php echo $image ?>
</div>
<div id="prevnext">
<div id="next">
<a href="?id=<?php echo $ID ?>" name="name4" >Next</a>
</div>
<div id="prev">
Previous
</div>
</div>
Try this:
<?php
include("PHP/db.php");
$query2 = "SELECT * FROM images ORDER BY RAND() LIMIT 1";
$result2 = mysqli_query($dbc, $query2);
$rand_row = mysqli_fetch_array($result2);
$rand_id = $rand_row ['ID'];
if (!isset($_GET['id'])) {
$_GET['id'] = $rand_id;
}
if (isset($_GET['id'])) {
echo $where = $_GET["id"];
echo $query = "SELECT * FROM images WHERE ID =" . $where;
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
$ID = $row['ID'];
$title = $row['name'];
$image = "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}
?>
<body>
</div>
<div id="title"> <?php echo $title ?> </div>
<div id="mainpic">
<?php echo $image ?>
</div>
<div id="prevnext">
<div id="next">
<a href="?id=<?php echo $rand_id; ?>" name="name4" >Next</a>
</div>
<div id="prev">
Previous
</div>
</div>
I changed:
-link to next is now id=rand
-changed your code to give me a "rand ID" and its already defined on the href of the page you load
It will go inside condition (next == true).
Make sure that your variables are initialized before use.