AdRock Posted December 3, 2006 Share Posted December 3, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/29300-how-to-count-number-of-records-in-database-by-category-resolved/ Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 How are you displaying the results? Is it just one image per page? Quote Link to comment https://forums.phpfreaks.com/topic/29300-how-to-count-number-of-records-in-database-by-category-resolved/#findComment-134293 Share on other sites More sharing options...
AdRock Posted December 3, 2006 Author Share Posted December 3, 2006 Yes. One image per page.I want to count the number of images in the category, and say there are 15 images, I want to start the counting from 1 to 15, not by the ID number which it is currently started by Quote Link to comment https://forums.phpfreaks.com/topic/29300-how-to-count-number-of-records-in-database-by-category-resolved/#findComment-134298 Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 There may be a simpler way but you could try putting each result into an array and then use the position in the array + 1 to get number to display? Quote Link to comment https://forums.phpfreaks.com/topic/29300-how-to-count-number-of-records-in-database-by-category-resolved/#findComment-134304 Share on other sites More sharing options...
AdRock Posted December 3, 2006 Author Share Posted December 3, 2006 How would I do that as I have never done arrays with php before? Quote Link to comment https://forums.phpfreaks.com/topic/29300-how-to-count-number-of-records-in-database-by-category-resolved/#findComment-134404 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.