burge124 Posted February 28, 2008 Share Posted February 28, 2008 how can i count $chap $chap = $_POST['select']; $question = mysql_query("SELECT * FROM question WHERE ChapterID='$chap'"); $chapter = mysql_query("SELECT * FROM chapter WHERE ChapterID='$chap'"); $query = "SELECT ChapterID, COUNT(*****) FROM question GROUP BY ChapterID"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo "There are ". $row['COUNT(*****)']; echo "<br />"; } Quote Link to comment Share on other sites More sharing options...
teng84 Posted February 28, 2008 Share Posted February 28, 2008 what do you mean count? can you explain it better? any way if this are array count($chop); if you want to view the total rows.. mysql_num_rows($result) is that what you mean? Quote Link to comment Share on other sites More sharing options...
burge124 Posted February 28, 2008 Author Share Posted February 28, 2008 $chap is the users chapter selection from previous page... want a query to find out how many questions in this chapter there are. (bare in mind all questions are stored in the same place and can only be idenfified via chapterid) just want it to do the count but dont know how to put it into brackets is it '$chap'? Quote Link to comment Share on other sites More sharing options...
teng84 Posted February 28, 2008 Share Posted February 28, 2008 $question = mysql_query("SELECT * FROM question WHERE ChapterID='$chap'"); echo mysql_num_rows($question); or $question = mysql_query("SELECT count(putthetableidhere) as ttl FROM question WHERE ChapterID='$chap'"); $query =mysql_fetch_array($question ); echo $query['ttl']; Quote Link to comment Share on other sites More sharing options...
burge124 Posted February 28, 2008 Author Share Posted February 28, 2008 i already know the structure of the query and tested it. the user on the other page will pick say chapter "1" i want the query to go off into the question table and find all of the questions with the foreign key (chapterID) of "1" how do i get the count simply to return to me a count of all the questions belonging to their selection (in this case "1") ive been playing around with it and dont know if its ['COUNT("$chap")']; ['COUNT($chap)']; 'COUNT($chap)'; etc... (its the same problem for the second location "COUNT" is needed Quote Link to comment Share on other sites More sharing options...
teng84 Posted February 28, 2008 Share Posted February 28, 2008 sorry im lost.. can you just give as an example output and maybe your table structure ! Quote Link to comment Share on other sites More sharing options...
keeB Posted February 28, 2008 Share Posted February 28, 2008 Burge. Please look up how to ask questions so other people can understand them. The best I can guess you want to learn how to join and count the amount of results from your join. Can you please post the structure of your database or these 2 tables? Again, my guess from the sample provided is you want to do this: <?php $q = "SELECT count(*) from chapter c inner join question q on q.chapterid = c.chapterid where c.chapterid = 1 " $result = mysql_query($q); $row = mysql_fetch_array($result); print "there are " . $row[0] . "questions for chapter 1"; Quote Link to comment Share on other sites More sharing options...
burge124 Posted February 28, 2008 Author Share Posted February 28, 2008 an example output that im hoping to create is.... there are "4" questions in this chapter. but as the user changes which chapter they are navigated the query is given new information from the $chap variable in response to KeeB, i think that its kind of a inner join, but when you but "c.chapterid = 1" doesnt that mean its limited to just counting chapter 1's contents. i need it to do a count everytime a user changes chapters... similar to this... http://www.tizag.com/mysqlTutorial/mysqlcount.php Quote Link to comment Share on other sites More sharing options...
burge124 Posted February 28, 2008 Author Share Posted February 28, 2008 with this code... $chap = $_POST['select']; $question = mysql_query("SELECT * FROM question WHERE ChapterID='$chap'"); $chapter = mysql_query("SELECT * FROM chapter WHERE ChapterID='$chap'"); $query = "SELECT ChapterID, COUNT([color=red][color=black]QuestionID[/color][/color]) FROM question GROUP BY ChapterID"; // query from total questions in chapter - amount of questions with true & false workout question number! $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo "There are ". $row['COUNT([color=red]QuestionID[/color])']; echo "<br />"; } i acheive an output of There are 3 (questions belonging to chapter 1) There are 1 (questions belonging to chapter 2) but i would like to change the count from QuestionID to $chap, but i cant without errors appearing Notice: Undefined index: COUNT($chap) in C:\Program Files\EasyPHP 2.0b1\www\Untitled-2.php on line 18 Notice: Undefined index: COUNT($chap) in C:\Program Files\EasyPHP 2.0b1\www\Untitled-2.php on line 18 Quote Link to comment Share on other sites More sharing options...
keeB Posted February 28, 2008 Share Posted February 28, 2008 so, do the join and count every time the user changes chapters? Quote Link to comment Share on other sites More sharing options...
burge124 Posted February 28, 2008 Author Share Posted February 28, 2008 thanks for your help, ill have a go with the join tomorrow Quote Link to comment 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.