Jump to content

How to count number of records in database by category [RESOLVED]


Recommended Posts

I have an image gallery split down into 3 categories and in my database I have fields for id, image, thumbnail and category.

What I am trying to do is display only the images that belong to that category but what I am having trouble with is counting the images.

I would like to count all the images belonging to a category and display something like

[b]Showing image 1 of 10[/b] where 10 is the maximum number of images belonging to that category.

I have tried this but is is using the id number so it displays something like [b]Showing image 25 of 23 [/b].  I have mananged to count the max number but how do I start the numbering from 1 so the image number would be starting from 1 - 23?

Here is my code:

[code]include_once("includes/connection.php");

//**EDIT TO YOUR TABLE NAME, ECT.

$t = mysql_query("SELECT * FROM images");
  if(!$t) die(mysql_error());
   
$a                = mysql_fetch_object($t);
$total_items      = mysql_num_rows($t);
$limit            = $_GET['limit'];
$type             = $_GET['type'];
$page             = $_GET['id'];
$cat   = $_GET['cat'];

//set default if: $limit is empty, non numerical, less than 1, greater than 50
if((!$limit)  || (is_numeric($limit) == false) || ($limit < 2) || ($limit > 50)) {
     $limit = 1; //default
}
//set default if: $page is empty, non numerical, less than zero, greater than total available
if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $total_items)) {
      $page = 1; //default
}

//calcuate total pages
$total_pages     = ceil($total_items / $limit);
$set_limit          = $page * $limit - ($limit);
$count = mysql_query("SELECT COUNT(*) FROM images WHERE cat='$cat'");

//query: **EDIT TO YOUR TABLE NAME, ECT.

$q = mysql_query("SELECT * FROM images WHERE id = '$page'");
  if(!$q) die(mysql_error());
     $err = mysql_num_rows($q);
       if($err == 0) die("No matches met your criteria.");
$numofrows = mysql_num_rows($q);

//show data matching query:
while($code = mysql_fetch_array($q)) {
    echo "<p><b>".$page."</b> of <b>".$count1 = mysql_result($count,0,0)."</b></p>";
    echo "<img src='/images/gallery/large/".$code['image']."'><br/><br/>";
}[/code]
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.