Help with counting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Herb
    Senior Member
    • Apr 2000
    • 106

    Help with counting

    How do all..

    I am trying to finish up an Image Gallery hack and am having some trouble with the admin piece..

    I am trying to first count all the images in my image table.. No problems there.. The Image table entries are linked to the postid that image was uploaded in.. I am trying to count the postid's that match the postid's in the image table..

    Here is what I have so far..

    Code:
      $totalimagesq=$DB_site->query_first("SELECT COUNT(imagegalid) AS imagegalid FROM imagegal");
      $totalimages=$totalimagesq[imagegalid];
      
      $images=$DB_site->query("SELECT imagegalid,postid FROM imagegal");
    
      while ($imagesd=$DB_site->fetch_array($images)) {
       $totalpostsq=$DB_site->query_first("SELECT COUNT(postid) AS postid FROM post WHERE postid='$imagesd[postid]'");
       $totalposts=$totalpostsq[postid];
       }
    
      echo "<p>There are $totalimages images in the image gallery table.
      <BR>There are $totalposts posts that contain images in the posts table.
      <BR><a href=\"imagegal.php?action=doclean\">Delete Dead Images</a></p>\n";
    The problem is that the count for the post table always equals one.. I know for a fact that the count should be 13 since that is what the count of the image table is reporting..

    Any help would greatly appreciated..

    Herb..
    The Cannabis Edge
  • MattR
    Senior Member
    • Jun 2000
    • 1047

    #2
    Herb,

    I have one question, if you are filling your imagegal table with images from posts, then why are you counting the posts that have images? You *know* how many posts have images because each post that has an image also has a corresponding record in the imagegal table.

    You even say this:

    I know for a fact that the count should be 13 since that is what the count of the image table is reporting..
    But, I will assume that you visited the department of redundancy department and want to check twice.

    Well, you need a counter of some sort in the loop to keep counting. Take a look at your code:
    Code:
      while ($imagesd=$DB_site->fetch_array($images)) {
       $totalpostsq=$DB_site->query_first("SELECT COUNT(postid) AS postid FROM post WHERE postid='$imagesd[postid]'");
       $totalposts=$totalpostsq[postid];
       }
    $totalposts=$totalpostsq[postid];

    Make any sense yet? Or to explain a bit further, you're checking to see how many postids there are when you are looking for a particular one...

    So you're saying "count the number of posts with the postid = 1". Obviously you're going to find one every time.

    So, instead of

    $totalposts=$totalpostsq[postid];

    replace it with:

    $totalposts +=$totalpostsq[postid];

    And that should add up all of the postid's that match...

    If this snippet of code you have provided is in another loop, make sure to reinitialize the counter to zero ($totalposts = 0.

    If you change that one line I think you'll accomplish what you're looking for.

    However, I don't see the need for the second loop.

    Matt
    Sybase DBA / PHP fanatic
    Sybase v. MySQL v. Oracle | Why I don't like MySQL | Download Sybase TODAY! | Visit DBForums.com!

    Comment

    • Herb
      Senior Member
      • Apr 2000
      • 106

      #3
      Originally posted by mrogish
      I have one question, if you are filling your imagegal table with images from posts, then why are you counting the posts that have images
      Well when a user submits a new post that includes an image to upload into the uploads folder. I wanted a table to track those uploads using the postid, since it is unique as well as the image name..

      When a moderator/admin deletes a post that was part of an image upload, instead of modifying all of vB's delete rountines to delete the image. I will be able to go into the admin area once in awhile and see a count of images from the image table and compare it with posts that still exist or not.. Hence the routine I asked about..

      Then I have a routine to go out and remove the image records from the image table and images from my upload directory, so I don't store hundres of images that have no purpose and clean up my image table..

      Anyways, your suggestion worked perfectly..

      Thank You..
      The Cannabis Edge

      Comment

      widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
      Working...