Codeigniter 2.1 - join one to many - php

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();
}

Related

Codeigniter - Return members associated with a specific location

I am building software where a Choir Leader can run multiple Choirs.
I am trying to return all members associated with a specific Choir Group. So when the Choir leader selects a specific Choir Location, they see all members asssociated with that location.
I have a aauth_users table and a choir_locations_to_users table in the MySQL Database.
the aauth_users table holds all the data regarding each choir member:
the choir_locations_to_users table is a seperate table that lists the user ID and the ID of the choir location.
I am performing a query to grab the necessary data I need to display all the members. such as member name, avatar etc to display on the Choir Location page.
The $id parameter in the function is the Choir Location ID.
public function get_choirlocation_members($id) {
// $query = 'SELECT aauth_users.full_name, aauth_users.avatar, aauth_users.last_login, choir_location_to_users.location_id FROM aauth_users, choir_location_to_users WHERE aauth_users.id = choir_location_to_users.user_id ';
$this->db->select('aauth_users.full_name, aauth_users.avatar, aauth_users.last_login, aauth_users.id, choir_location_to_users.location_id');
$this->db->from('aauth_users, choir_location_to_users');
$this->db->where('aauth_users.id = choir_location_to_users.user_id');
$this->db->order_by('aauth_users.last_login', 'DESC');
$members = $this->db->get();
foreach ($members->result() as $row) {
if($row->location_id == $id){
return $members;
}
}
}
The query above returns the following results:
The problem is in the function after the query it seems to be returning all of the members and not just the members related to this location. as when I do another foreach in my View File with the result I still see all members.
foreach ($members->result() as $row): ?>
<li>
<img class="profile-user-img img-responsive img-circle" src="<?php echo BASE_URL.'uploads/user/'.$row->avatar ?>" alt="User Image">
<a class="users-list-name" href="#"><?php echo $row->full_name ?></a>
<span class="users-list-date">Last Login: <?php echo date ( "d.m.Y " , strtotime ($row->last_login )); ?></span>
</li>
<?php endforeach; ?>
I would really appreciate any help to point out where I may be going wrong in trying to filter out the query results to show only members associated with a specific Choir location based on the location id.
UPDATE Anwser from #Dinesh Ali not working here is what I have:
I tested the new Join Query you suggested in phpMyAdmin and this is the result:
As you see it is returning the correct data in a result table, which is good, so I added the function you suggested like so:
public function get_choirlocation_members($id) {
$this->db->select('aauth_users.full_name, aauth_users.avatar, aauth_users.last_login, aauth_users.id, choir_location_to_users.location_id');
$this->db->from('aauth_users');
$this->db->join('choir_location_to_users', 'aauth_users.id = choir_location_to_users.user_id');
$this->db->where('aauth_users.id = choir_location_to_users.user_id');
$this->db->order_by('aauth_users.last_login', 'DESC');
$members = $this->db->get()->result();
if(isset($members) && count($members) > 0){
return $members;
}else{
return false;
}
}
Then in my view File I am using the following to show each user as a LI list item:
foreach ($members as $row): ?>
<li>
<img class="profile-user-img img-responsive img-circle" src="<?php echo BASE_URL.'uploads/user/'.$row->avatar ?>" alt="User Image">
<a class="users-list-name" href="<?php echo BASE_URL.'administrator/user/view/'.$row->id ?>"><?php echo $row->full_name ?></a>
<span class="users-list-date">Last Login: <?php echo date ( "d.m.Y " , strtotime ($row->last_login )); ?></span>
</li>
<?php endforeach; ?>
But every user is still showing I have 3 users and I only have one user associated with each location, so I expect to see just one result in the view?
You can't get data from two table in way that you are trying to do.
You need to use join() for this read more
public function get_choirlocation_members($id) {
$this->db->select('aauth_users.full_name, aauth_users.avatar, aauth_users.last_login, aauth_users.id, choir_location_to_users.location_id');
$this->db->from('aauth_users');
$this->db->join('choir_location_to_users', 'aauth_users.id = choir_location_to_users.user_id');
$this->db->where('aauth_users.id = choir_location_to_users.user_id');
$this->db->order_by('aauth_users.last_login', 'DESC');
$members = $this->db->get()->result();
When you are using join() no need to check location with loop and condition because join() is doing this job for you
Now first check $members is empty or not
if(isset($members) && count($members) > 0){
return $members;
}else{
return false;
}
}
Try This,
public function get_choirlocation_members($id) {
// $query = 'SELECT aauth_users.full_name, aauth_users.avatar, aauth_users.last_login, choir_location_to_users.location_id FROM aauth_users, choir_location_to_users WHERE aauth_users.id = choir_location_to_users.user_id ';
$this->db->select('aauth_users.full_name, aauth_users.avatar, aauth_users.last_login, aauth_users.id, choir_location_to_users.location_id');
$this->db->from('aauth_users');
$this->db->join('choir_location_to_users','aauth_users.id = choir_location_to_users.user_id','inner');
//$this->db->where('aauth_users.id = choir_location_to_users.user_id');
$this->db->order_by('aauth_users.last_login', 'DESC');
$members = $this->db->get();
foreach ($members->result() as $row) {
if ($row->location_id == $id) {
return $members;
}
}
}

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.

Fetch image from folder and image data from database in php is not working

Table Name : image;
Field Name in database table : image_name;
Foldar Name : user_images
<?php
$query= "select * from image where image_user_id ='".$_SESSION['userid']."'";
echo $query;
if($query->num_rows > 0){
while($row = $query->fetch_assoc()){
$imageURL = 'user_images/'.$row["image_name"];
echo $imageURL;
?>
<a href="<?php echo $imageURL; ?>" data-fancybox="group" ></a>
<?php }
} ?>
Where are you running the query on your database?
$query= "select * from image where image_user_id ='".$_SESSION['userid']."'"; //Here you are creating a string
echo $query; //Here you are printing the string to the view
I can not see anywhere that you are querying the database or calling a function to do it. That seems to be the main problem. Take a look at this link to see an example of setting up a connection and running a query on a DB http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

How to show html images witg dynamic id

I'm kind of newbie on this and I need some more expert help. The thing is, I'm trying to use this pure css easy slider but the example seems to be used with static images.
In my case I'm using php to dynamically show the images so I need to use some kind of loop (I supose) to automatically generate the id's for these tags
What I want to do is the same thing at the example but with individual ids:
<img id="img-1" src="img.jpg" alt>
<img id="img-2" src="img.jpg" alt>
<img id="img-3" src="img.jpg" alt>
<img id="img-4" src="img.jpg" alt>
<img id="img-5" src="img.jpg" alt>
So I'm trying with this code:
<?php
$categories = $conexion->query("SELECT COUNT(id) AS total,id,icon,name,image FROM categories ORDER BY id LIMIT 8");
while($row = $categories->fetch_array()) {
$id = $row['id'];
$image = $row['image'];
$name = $row['name'];
$total = $row['total'];
echo '<div id="slider">';
for($i = 0; $i <= $total; $i++) {
echo '<figure>
<img class="ns-img" id="img-'.$i.'" src="'.$image.'">
</figure>';
}
echo '</div>';
}
$categories->close();
?>
But seems that it doesn't work properly because it just show me the same first image over and over again, and the css structure just broke when these images are dynamically showed :C What could I do?
Sine you have a while loop, there is no need for the for loop
Also as in the sample you shared in your question, you only need 1 <div id="slider"> and <figure>. So you need to move them out of your loop
Your final code should look something like this:
<?php
$categories = $conexion->query("SELECT COUNT(id) AS total,id,icon,name,image FROM categories ORDER BY id LIMIT 8");
$i = 1;
echo '<div id="slider"><figure>';
while($row = $categories->fetch_array()) {
$id = $row['id'];
$image = $row['image'];
$name = $row['name'];
$total = $row['total'];
echo '<img class="ns-img" id="img-'.$i.'" src="'.$image.'">';
$i++;
}
echo '</figure></div>';
$categories->close();
?>

php mysql while loop inside while loop querying same table

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

Categories