Jump to content

count function quick question


burge124

Recommended Posts

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 />";
}

Link to comment
Share on other sites

$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'?

 

 

Link to comment
Share on other sites

$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'];

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.