php mysql while loop inside while loop querying same table - php

This is my code
<?php
$query= "SELECT album_name FROM gallery group by album_name order by MIN(date_time)";
$stmt = $connection->prepare($query);
$stmt->execute();
$result=$stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_BOTH)) { //First while loop
$album_names =htmlspecialchars($row['album_name']);`
?>
<ul>
<li data-tags="<?php echo $album_names ?>">
Till this point everything works fine , It shows me the names of all the albums that are there in my db .
What I want to do next is I want it to show all the pics which belong to $album_names so I wrote this code after above code
// Code continues
<?php
$query2= "SELECT imgage_path,resized_path FROM gallery WHERE album=?";
$stmt = $connection->prepare($query2);
$stmt->bind_param("s",$album_names);
$stmt->execute();
$result2=$stmt->get_result();
while ($row2 = $result2->fetch_array(MYSQLI_BOTH)) { //Second while loop
$thumbnail_path = =htmlspecialchars($row2['resized_path']);
$image_path =htmlspecialchars($row2['image_path']);
?>
<a href="<?php echo $image_path ?>">
<img src="<?php echo $thumbnail_path ?>" alt="Illustration" />
</a>
</li>
</ul>
<?php
} //Second While loop ends
} //First While loop ends
?>
Problem is that it only shows me the first result of $image_path and $thumbnail_path but I want it to show all the image paths belonging to that particular album.
Hope I have cleared my question .

This looks like pseudo-code to me (as it is not having proper php tags). Anyway, Problem is as follows.. Your html tags li arrangements are wrong. That is why you are not seeing the other results. It has to be fixed as below
<?php
$query= "SELECT album_name FROM gallery group by album_name order by MIN(date_time)";
$stmt = $connection->prepare($query);
$stmt->execute();
$result=$stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_BOTH)) { //First while loop
$album_names =htmlspecialchars($row['album_name']);`
?>
<ul>
<li data-tags="<?php echo $album_names ?>">
<?php
$query2= "SELECT imgage_path,resized_path FROM gallery WHERE album=?";
$stmt = $connection->prepare($query2);
$stmt->bind_param("s",$album_names);
$stmt->execute();
$result2=$stmt->get_result();
while ($row2 = $result2->fetch_array(MYSQLI_BOTH)) { //Second while loop
$thumbnail_path = =htmlspecialchars($row2['resized_path']);
$image_path =htmlspecialchars($row2['image_path']);
?>
<a href="<?php echo $image_path ?>">
<img src="<?php echo $thumbnail_path ?>" alt="Illustration" />
</a>
<?php
} //Second While loop ends
?>
</li>
</ul>
<?php
} //First While loop ends
?>

Simplifying to a single query. This assumes that image_path and resized path always both exist for each image (if they don't it is easily fixed but this shows you the basics).
<?php
// Query gets one row per album. Row has 3 fields, album name, then all the image_path fields concatentated together, the all the resized_path fields joined together
$query= "SELECT album_name
GROUP_CONCAT(image_path ORDER BY image_path) AS image_path_concat,
GROUP_CONCAT(resized_path ORDER BY image_path) AS resized_path_concat
FROM gallery
GROUP BY album_name
ORDER BY MIN(date_time)";
$stmt = $connection->prepare($query);
$stmt->execute();
$result=$stmt->get_result();
// Start of unordered list of album names
echo "<ul>";
while ($row = $result->fetch_array(MYSQLI_BOTH))
{
$album_names = htmlspecialchars($row['album_name']);
// List item of album name
echo "<li data-tags='$album_names'>";
// Explode out the image paths into an array
$image_path = explode(',', $row['image_path_concat']);
// Explode out the image resized paths into an array
$resized_path = explode(',', $row['resized_path_concat']);
// Start of unordered list of image paths
echo "<ul>";
// Loop around the array of image paths
foreach($image_path AS $key=>$value)
{
// Assuming that the list of image paths always match the array of resized paths then use the key of the current
// image_path for both the image path and also the resized / thumbnail path
$image_path = htmlspecialchars($image_path[$key]);
$thumbnail_path = htmlspecialchars($resized_path[$key]);
echo "<li><a href='".$image_path."'><img src='".$thumbnail_path."' alt='Illustration' /></a></li>";
}
// End of unordered list of image paths
echo "</ul>";
// End of list item of album name
echo "</li>";
} //First While loop ends
// End of unordered list of album names
echo "</ul>";
?>

I do not know if my answer will help you.
But i think there is wrong in your loop for the html tag.
<?php
$query2= "SELECT imgage_path,resized_path FROM gallery WHERE album=?";
$stmt = $connection->prepare($query2);
$stmt->bind_param("s",$album_names);
$stmt->execute();
$result2=$stmt->get_result();
while ($row2 = $result2->fetch_array(MYSQLI_BOTH)) { //Second while loop
$thumbnail_path = =htmlspecialchars($row2['resized_path']);
$image_path =htmlspecialchars($row2['image_path']);
?>
<a href="<?php echo $image_path ?>">
<img src="<?php echo $thumbnail_path ?>" alt="Illustration" />
</a>
</li> <- you loop the close <li> tag
</ul> <- you loop the close <ul> tag
<?php
} //Second While loop ends
} //First While loop ends
?>
You loop the close tags (li and ul) in the second loop but you do not put open tag for the li and ul.
I hope this help. thankyou

Related

how i can diplay GROUP_CONCAT values in php

sqlQueryImage ThisWhatIgetinPHP
i have tried this but it keeps displaying me just the first image of every article i don't know how display multiple image for one article
$result = $db->prepare("SELECT article.titreArticle, article.textArticle,
article.idArticle, article.dateArticle, GROUP_CONCAT(images.images) AS IMG
FROM article LEFT JOIN images
ON article.idArticle = images.idArticle
GROUP BY article.idArticle");
for ($i = 0;
$row = $result->fetch();
$i++){
?>
<h1 style="text-align: center"><?php echo $row['idArticle']; ?></h1>
<hr>
<img style="width: 300px" src="images/<?php if($row ['IMG'] !=null ) {
$value = $row['IMG'];
}
$values = explode(",", $value);
echo $values[0];
?>">
The aim of GROUP_CONCAT is to return each value within each GROUP, concatenated in the same field.
You expect to have several images for certain groups, however you only display one image, the first one encountered : echo $values[0];
If you want to display all images of a group, you have to make a second loop on your array of images, to print each element.
So you have your main loop on row, and a second loop on images.
Try this:
<?php
$result = $db->prepare("SELECT article.titreArticle, article.textArticle,
article.idArticle, article.dateArticle, GROUP_CONCAT(images.images) AS IMG
FROM article LEFT JOIN images
ON article.idArticle = images.idArticle
GROUP BY article.idArticle");
// FIRST LOOP ON ROWS
for ($i = 0; $row = $result->fetch(); $i++){
?>
<h1 style="text-align: center"><?php echo $row['idArticle']; ?></h1>
<hr>
<?php
$images = ""; // we will store all our images for this row here (if any)
if($row ['IMG'] !=null ) {
$value = $row['IMG'];
$values = explode(",", $value);
// SECOND LOOP ON IMAGES
foreach($values, $imgsrc){
$images .= '<img style="width: 300px" src="images/'.$imgsrc.'">';
}
echo $images; // output all images
}
?>

how to display a photo from database base on its id to another page

I have a huge problem and been having a hard time figuring it out.I want to pass down the image id i click on onto the 2nd page.The 2nd page then uses the id and echos out an image,image_text based on what i gave in the database.
1st page
while ($row = mysqli_fetch_assoc($result)) {
echo "<div class='img-single'>
<a href='images/fpage.php?image_id={$row['image_id']}'><img class='i'
src='images/".$row['image']."' ></a>
<figcaption class='figcaption'>".$row['image_text']."</figcaption>
</div>";
}
?>
2nd page
<?php
$image_id = $_GET['image_id'];
?>
So after passing the image id in a a href='somepage.php&image_id=$image' tag. On the 2nd page use: if (isset($_GET['image_id'])) { $pic_id = $_GET['image_id'] Then make a select from database using pic_idmysqli_query($dbc, "SELECT picture, name from table_name WHERE picture_id = '$pic_id' LIMIT 1;"); echo the picture in image tags and your done.
<img src="<?php echo $picture; ?>" alt="<?php echo $name; ?>">} // Close if(issset()) I hope this helps.

Display album using PHP and SQL

I have a question which is very similar to this one: Display Album with Photos (PHP), however I need a bit of help in applying the code to my situation.
I have two tables, which are related by the column AlbumID:
Album: AlbumID, name, date(optional)
Image: ImageID, name, imageURL, AlbumID
My PHP code so far:
function select_galleryimage($sql,$a_id, $img_id) {
include 'connect.php';
$result = $conn->query($sql);
if ($result->num_rows > 0);
while ($row = $result->fetch_assoc()) {
echo "<div id='".$row['AlbumID']."'>\n
<a class='$a_id' href=".$row["imageURL"]." target='_blank'>
<img src=".$row["imageURL"]." alt='".$row['name']."'>
</a>\n
</div>\n";
}
}
where $sql = SELECT * FROM album JOIN image ON album.AlbumID=image.AlbumID'
$a_id = 'lightbox' and $img_id = ''
The issue that I am having is how to create a loop which creates the divs into which the loop with the images is inserted, so as the end result is something like this:
<div id="album1" style="display:none">
<h1>Gallery</h1>
<a class="lightboxX35" href="media/Photos/_MG_7732.jpg"><img src="media/Photos/_MG_7732.jpg"></a>
<a class="lightboxX35" href="media/Photos/_MG_7508.jpg"><img src="media/Photos/_MG_7508.jpg"></a>
</div>
Gallery
Thank you all in advance for any help provided.
Somehing like this?
if ($result->num_rows > 0) {
$current_album = null;
while ($row = $result->fetch_assoc()){
if ($current_album <> $row['AlbumID']) {
if (!empty($current_album))
echo "</div>\n";
echo "<div id='".$row['AlbumID']."'>\n";
echo "<h1>Gallery</h1>\n";
$current_album := $row['AlbumID'];
}
echo "<a class='$a_id' href='".$row["imageURL"]."' target='_blank'><img src=".$row["imageURL"]." alt='".$row['name']."'></a>\n";
}
echo "</div>\n";
}
I haven't tried it on a server so may not be the correct synthax...
With more than a single album you have to sort the SQL by album using ORDER BY then echo the DIV fo every new album. To achieve this assign a variable $current_album = null; outside the while loop then check for changes inside.

i want to print first image record form Database if its found and stop the loop

i have two tables in DB, one table have the Detail of person like name, place, country, city etc and 2nd table have multiple images records. in some records of images "images" filed is empty, so i want to print only one image. if the image filed is empty leave that and when the image is found? print it and stop the loop.
Here is my code:
$sno= $resultvenue['Sno'];
$searchphotolinks = mysql_query("SELECT `Image` FROM `Photos` Where `PersonID`='".$sno."' ORDER BY `Sno` ASC");
while($linkphoto = mysql_fetch_array($searchphotolinks))
{
if($linkphoto['PhotosLink']!="")
{
?>
<div class="imageplace">
<a href="http://www.bigvenue.net/singlerecord/?id=<?php echo $sno;
?>"><img src="<?php echo $finalfile = $linkphoto['Image']; ?>" /></a>
</div>
<?php
}
}
Try this:
$finalfile = null;
while($linkphoto = mysql_fetch_array($searchphotolinks) && $finalfile != null){
if($linkphoto['PhotosLink']!=""){
$finalfile = $linkphoto['Image'];
echo '<div class="imageplace">';
echo "<a href=\"http://www.bigvenue.net/singlerecord/?id=\"$sno\">";
echo "<img src=\"$finalfile\" /></a></div";
}
}
This will stop the loop when you've set the $finalfile variable
break; would also work at the end of your if, and would be preferable, unless you plan on using $finalfile for something later

Codeigniter 2.1 - join one to many

I have to tables:
gallery -> id_gallery, name, desc, data
and:
images -> gallery_id, name
One gallery can have many images. I need to select all galleries and all images and on view page present all galleries and all images which contains that gallery. How can do this?
I have code like this for now:
$this->db->select('*')->from('image')->join('gallery','id_gallery = gallery_id');
$q = $this->db->get();
return $q = $q->result_array();
EDIT:
<?php foreach ($gallery as $gal): ?>
<figure>
<a href="<?php echo IMG ?>galerija/<?php echo $gal['name'] ?>/<?php echo $gal['path'] ?>" rel="galerija[slike]" class="figure_img">
<img src="<?php echo IMG ?>galerija/<?php echo $gal['naziv'] ?>/thumbs/<?php echo $gal['path'] ?>" >
<figcaption><?php echo $gal['name']; ?></figcaption>
</a>
</figure>
<?php endforeach; ?>
This foreach loop is producing 5 div tags instead of one (if gallery have 5 images for example).
If you want to select all the images from images table for all galleries..........
$this->db->select('*');
$this->db->from('images');
$this->db->join('gallery', 'gallery.id_gallery = images.gallery_id');
$query = $this->db->get();
return $q = $query->result_array();
or in the other way. Write a model function and in that
public function get_images()
{
$res=$this->db->query("select * from images and gallery where gallery.id_gallery = images.gallery_id");
if($res->num_rows()>0){
return $res->result("array");
}
return array();
}

Categories