jesushax Posted July 11, 2008 Share Posted July 11, 2008 hi below is my code what im trying to do is count how many records there are per VacCat and display them next to the category name unfortunatley my effory doesnt work any help? <?php session_start(); include($_SERVER['DOCUMENT_ROOT'] . '/new/includes/header.php'); include($_SERVER['DOCUMENT_ROOT'] . '/new/includes/access.inc'); echo '<h2 class="titles">Edit Vacancies</h2>'."\n"; echo '<p>Click a vacancy category below to edit archive and delete vacancies.</p>'."\n"; $SQL = mysql_query("SELECT VacCat FROM tblVac WHERE VacArchive='0' GROUP BY VacCat") or die (mysql_error()); $COUNTSQL = mysql_query("SELECT VacCat, COUNT(VacTitle) FROM tblVac GROUP BY VacCat") or die (mysql_error()); if (mysql_num_rows($SQL) <0) { echo "<p>Sorry there are no current vacancies.</p>"; } else { while ($count = mysql_num_rows($COUNTSQL)) { while ($row = mysql_fetch_array($SQL)) { echo '<div style="padding:5px 0; clear:both;">'."\n"; echo '<img src="/new/images/layout/close.gif" alt="arrow" /> '."\n"; echo '<a href="/new/admin/vacancies/edit_vac_list.php?CAT='.$row["VacCat"].' ">'.$row["VacCat"].' Vacancies</a> '.$count["VacTitle"]."\n"; echo "</div>"."\n"; } } } include($_SERVER['DOCUMENT_ROOT'] . '/new/includes/footer.php'); ?> Link to comment https://forums.phpfreaks.com/topic/114257-display-how-many-records-per-category/ Share on other sites More sharing options...
piyushsharmajec Posted July 11, 2008 Share Posted July 11, 2008 i dont sure but change condition mysql_num_rows($sql<0) to mysql_num_rows($sql<1) and check table VacCat in database Link to comment https://forums.phpfreaks.com/topic/114257-display-how-many-records-per-category/#findComment-587502 Share on other sites More sharing options...
jesushax Posted July 11, 2008 Author Share Posted July 11, 2008 no thats not the problem i already have a loop working displaying the records the bit ive been tinkering with to try make work is $COUNTSQL = mysql_query("SELECT VacCat, COUNT(VacTitle) FROM tblVac GROUP BY VacCat") or die (mysql_error()); and while ($count = mysql_num_rows($COUNTSQL)) { .$count["VacTitle"] } thanks for your input though Link to comment https://forums.phpfreaks.com/topic/114257-display-how-many-records-per-category/#findComment-587510 Share on other sites More sharing options...
jesushax Posted July 11, 2008 Author Share Posted July 11, 2008 anyone gimmie a hand here, is the sql even right? Cheers Link to comment https://forums.phpfreaks.com/topic/114257-display-how-many-records-per-category/#findComment-587553 Share on other sites More sharing options...
craygo Posted July 11, 2008 Share Posted July 11, 2008 $COUNTSQL = mysql_query("SELECT VacCat, COUNT(VacTitle) as VacCount FROM tblVac GROUP BY VacCat") or die (mysql_error()); while ($count = mysql_num_rows($COUNTSQL)) { echo "Total ".$count['VacCat']." = ". $count["VacCount"]; } You may have to store the results of the count in an array first then while looping through the rows output the count in the array. You could also use one query to list everything and use php to total up each catagory. Ray Link to comment https://forums.phpfreaks.com/topic/114257-display-how-many-records-per-category/#findComment-587576 Share on other sites More sharing options...
craygo Posted July 11, 2008 Share Posted July 11, 2008 After actually looking at your code, why are you using 2 queries?? Should only need one query to do this <?php session_start(); include($_SERVER['DOCUMENT_ROOT'] . '/new/includes/header.php'); include($_SERVER['DOCUMENT_ROOT'] . '/new/includes/access.inc'); echo '<h2 class="titles">Edit Vacancies</h2>'."\n"; echo '<p>Click a vacancy category below to edit archive and delete vacancies.</p>'."\n"; $SQL = mysql_query("SELECT VacCat, COUNT(VacTitle) AS VacCount FROM tblVac WHERE VacArchive='0' GROUP BY VacCat") or die (mysql_error()); if (mysql_num_rows($SQL) < 1) { echo "<p>Sorry there are no current vacancies.</p>"; } else { while ($row = mysql_fetch_array($SQL)) { echo '<div style="padding:5px 0; clear:both;">'."\n"; echo '<img src="/new/images/layout/close.gif" alt="arrow" /> '."\n"; echo '<a href="/new/admin/vacancies/edit_vac_list.php?CAT='.$row["VacCat"].' ">'.$row["VacCat"].' Vacancies</a> '.$row["VacCount"]."\n"; echo "</div>"."\n"; } } include($_SERVER['DOCUMENT_ROOT'] . '/new/includes/footer.php'); ?> Ray Link to comment https://forums.phpfreaks.com/topic/114257-display-how-many-records-per-category/#findComment-587594 Share on other sites More sharing options...
jesushax Posted July 11, 2008 Author Share Posted July 11, 2008 thanks alot i was presuming id have to do two seperate queires for it for some reason THanks for you help Link to comment https://forums.phpfreaks.com/topic/114257-display-how-many-records-per-category/#findComment-587617 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.