-2

How can i count upload images per album based on user id..i have done this but is not ok..some help is appreciated. I'm new learner!

Here is what i have:

     Album db: |albumid|name|image|uid| <- user id 
     Gallery db: |galleryid|albumid|name|images|uid|

Code

 $sql = "
    SELECT COUNT(userid) 
      FROM tbl_gallery 
     where userid = 1
    ";
    $rs_result = mysql_query($sql,$con);
    $row = mysql_fetch_row($rs_result);
    $total_records = $row[0];
    echo $total_records;

    $sql = "SELECT * FROM tbl_album where userid = 1 ORDER BY albumid";
    $rs_result = mysql_query($sql,$con);


    ####### Fetch Results From Table ########

    while ($row = mysql_fetch_assoc($rs_result)) 
    {
        $aimage=$row['image'];
        $aid=$row['albumid'];
        $aname=$row['name'];

        <div class="pic">
    <?php 
        echo "<a href='includes/galeriefoto/gallery.php?id=1'> <img src='includes/galeriefoto/admin/acatch/$aimage' class='thumbimg' alt='Pic' alt='$aname'>"; 
    ?>
        <span class='pic-caption'>

            <center><p style='color:#000;font-size:24px'><?php echo $aname;?> (<?php echo $total_records;?>)</p></center>

        </span></a>
        </div>
   <?php } ?>
Fido
  • 53
  • 5
  • 3
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly May 10 '17 at 21:44
  • thanks for the reply and guidance..can u help me with this? – Fido May 10 '17 at 21:48
  • no explanation of what's not working, but its probably misuse of mysql_ because the sql query looks ok enough – developerwjk May 10 '17 at 21:49
  • 1
    Your SQL query doesn't seem to correspond to the database _schemas_ you've outlined. What's `uid` and where's `userid`? Are these the same field? What's `images`? Is this a single image or a JSON object of images? Please clarify your question and the information you're providing further and consider providing sample data for each table. – fubar May 10 '17 at 21:59
  • uid = user id that upload the image or creating the album and images is the path of the image (single image) not json – Fido May 10 '17 at 22:02
  • You still need to let us know _what_ it is that doesn't work. _"but is not ok"_ is _not_ enough information. What's the expected result and what result are you currently getting? – M. Eriksson May 10 '17 at 22:06
  • What dsnt work is the counting of pictures from albums and what i want is this: Album 1 (32 Pictures) - album 2 (3 pictures) - and so on... – Fido May 10 '17 at 22:15
  • The code from the top is returning me this: http://i.imgur.com/MLgnInH.jpg – Fido May 10 '17 at 22:20

1 Answers1

0

If it is just about counting the number of images in an album, the following would suffice for your query:

SELECT uid, albumid, COUNT(galleryid) as num_upload_images
FROM tbl_gallery
GROUP BY uid, albumid

This results in the following possible output:

[0] => [
    uid => 1,
    albumid => 1,
    num_upload_images => 15
],
[1] => [
    uid => 1,
    albumid => 2,
    num_upload_images => 12
]
Rico Humme
  • 456
  • 5
  • 12
  • thanks for the reply, is showing me () , noting ,empty – Fido May 10 '17 at 23:32
  • Does it return empty as well when executing this query on the database as well? – Rico Humme May 10 '17 at 23:36
  • In the database i see is return ok – Fido May 10 '17 at 23:48
  • So this means that the query is good but the implementation of the code is wrong. Can you provide the PHP code with the updated query? – Rico Humme May 10 '17 at 23:53
  • $sql = "SELECT uid, albumid, COUNT(galleryid) as num_upload_images FROM tbl_gallery WHERE uid = 1 GROUP BY uid, albumid"; $rs_result = mysql_query($sql,$con); $row = mysql_fetch_row($rs_result); $total_records = $row[0]; echo $total_records; – Fido May 11 '17 at 00:00
  • I think you are echoing an array with `$total_records`. could you do: `echo '
    '; print_r($row); print_r($row[0]);`
    And output that result
    – Rico Humme May 11 '17 at 08:36