Jump to content

Recommended Posts

Hi! I'm trying to get working a photo list (library) that I made for my website. I need to query db for somethings and I have to join them all but I'm not very familiarize with this tecquine, neither COUNT/SUM.

 

First I need to get (all) the images from user_uploads table (imgID, user_id, filename, description, up_time, etc), second get the user info from members table (id, name and usr_img) where id = user_uploads.user_id, third and last make 2 counts to know if the logged in user already liked the img ($_SERVER['user_id']) and get the total likes the current "user_uploads.img_id" have.

 

I'm already tried many queries, joining, subquery and finally I'm going to try again joining (or inner join). This is what I have:

SELECT user_uploads.* AS uu, members.*, COUNT(img_id, user_id) AS usr_liked, COUNT(img_id) AS total_likes
FROM user_uploads
INNER JOIN members AS m ON m.id = uu.user_id -- get img owner info
INNER JOIN img_likes AS il ON il.img_id = uu.imgID AND il.user_id = ? -- not merge members.id (img owner) with members.id (connected userid) / check if already liked
INNER JOIN img_likes AS ilt ON ilt.img_id = uu.imgID -- get img total likes
-- maybe group by, but i've not idea 
ORDER BY up_time DESC -- for the user_uploads.* ???

And this is the php code:

if (login_check($mysqli) == true) {
    $user_id = $_SESSION['user_id'];
}
else {
    $user_id = ip2long(get_ip_address());
}

if ($stmt = $mysqli->prepare(" SELECT user_uploads.* AS uu, members.*, COUNT(img_id, user_id) AS usr_liked, COUNT(img_id) AS total_likes
                               FROM user_uploads
                               INNER JOIN members AS m ON m.id = uu.user_id
                               INNER JOIN img_likes AS il ON il.img_id = uu.imgID AND il.user_id = ?
                               INNER JOIN img_likes AS ilt ON ilt.img_id = uu.imgID
                               GROUP BY img_id, user_id
                               ORDER BY up_time DESC")) {
    $stmt->bind_param('i', $user_id);
    $stmt->execute(); // get photos

    //$Items->store_result();
    //$Items->bind_result();

    $Items = $stmt->get_result();
    
    foreach ($Items as $ItemInfo) {
    
        $liked = $ItemInfo['usr_liked'];      // will this work???
        $total_likes = $ItemInfo['total_likes'];
        
        $imgID = $aItemInfo['imgID']; // user_uploads
        
        if ($liked == 0) {
            $like = 'Like';
        }
        else if ($liked == 1) {
            $like = 'Unlike';
        }
        
        $photo_list .= '
        // img
        // button
        <span class="total_likes" id="">' . $totallikes . '</span>
        <a id="' . $total_likes . '" class="likes">' . $like . '</a>
        ';
    }
}

I'm not sure how to do this and I'd appreciate any help.

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/292035-join-queries-and-get-counts/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.