Jump to content

[SOLVED] Very quick one


coolphpdude

Recommended Posts

Hey there,

 

This will probs be a nice easy question to all you php boffins out there.

 

I have got a students table with around 100 students in it. Each student has a preferred subject out of a choice of 30 subjects (maths, english, french, etc...) what i want to do is create a list of how many students prefer each subject. So for example

 

maths (70)

english (77)

french (40)

etc (33)

...

 

what i originally started doing was a select statement for each subject, i.e...

 

select * FROM students WHERE preferred_subject = 'maths'

select * FROM students WHERE preferred_subject = 'english'

select * FROM students WHERE preferred_subject = 'french'

 

Then doing a $maths_rows=@mysql_num_rows($maths_result); to count the number of rows returned.

 

This can be quite a tedious job when you have quite alot of subjects so what i want to know is, is there a quicker way of coming up with this info? could i do an overall statement such as 'select * FROM students' then do something where i could extract how any maths was in that query?

 

Cheers

 

Link to comment
Share on other sites

CV meant use the GROUP BY statement in your query, eg

$sql = 'SELECT preferred_subject, count(preferred_subject) as subject_total FROM students GROUP BY preferred_subject';
$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result))
{
    echo '<pre>' . print_r($row, true) . '</pre>';
}

Link to comment
Share on other sites

that works a treat. Got 1 or 2 questions about her to modify it to my needs.

 

my output is as follows...

 

Array
(
    [prefered_subject] => Maths
    [subject_total] => 1
)
Array
(
    [prefered_subject] => English
    [subject_total] => 3
)
Array
(
    [prefered_subject] => French
    [subject_total] => 1
)

 

How do i get it to display it in this format...

 

maths (1)

english (3)

french (1)

 

Also i was toying with the idea of listing all subjects and then putting a 0 if no students preferred them such as Physics (0) but im guessing that would not be possible as no students would have physics in the preferred subject field in order to display that info, is that right??

 

Thanks for your help!!

Link to comment
Share on other sites

How do i get it to display it in this format...

Just change code within the while loop, eg

$sql = 'SELECT preferred_subject, count(preferred_subject) as subject_total FROM students GROUP BY preferred_subject';
$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result))
{
    echo $row['preferred_subject'] . '(' . $row['subject_total'] . ')<br />';
}

 

Also i was toying with the idea of listing all subjects and then putting a 0 if no students preferred them such as Physics (0) but im guessing that would not be possible as no students would have physics in the preferred subject field in order to display that info, is that right??

 

That would be possible if you stored your subjects in a separate table, then you'd use a JOIN to query the two tables at once. Example table structure

 

[pre] subjects

----------               students

id -------------+     ----------

subject            |     ... your current fields ...

                      +--  preferred_subject[/pre]

 

Now if your tables where setup like that, you'd store the id of subject in the preferred_subject field (instead of the actual subject name). Then using a simple JOIN:

SELECT s.subject as pref_subject, COUNT(s.subject) as total_subject FROM subjects s, students st WHERE s.id = st.prefered_subject GROUP BY s.subject

 

Should produce a list like

 

maths (1)

english (3)

french (1)

physics (0)

... etc ...

 

Link to comment
Share on other sites

  • 2 weeks later...

Hey,

 

I previously marked this topic as solved but after trying it today i'm a bit stuck so i'm back for a bit more guidance!

 

I now have my tables set up so that i have a table of subjects (ID, subject) and i have a students table with a field called preferred_subject. preferred_subject contains the ID relevant to a particular subject from the subjects table. *** As the post above ***

 

Im using the query off the previous post...

 


$sql = 'SELECT s.subject as preferred_subject, COUNT(s.subject) as total_subject FROM subjects s, students st WHERE s.id = st.prefered_subject GROUP BY s.subject';
$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result))
{
   echo $row['preferred_subject'] . '(' . $row['subject_total'] . ')<br />';
}

 

but i think i must be writing the select query wrong because it is only counting the the subjects the students have selected. I need it to list all subjects from the subjects table and include the number of students that have chosen that subject as their preferred choice, i.e...

 

maths (3)

english (1)

french (0)

physics (0)

history (0)

PE (9)

 

Can any1 tell me what is wrong with my select statement and advise me what i can do so that ALL subjects are listed regardless of whether a student has chosen it as their preferred subject?

 

Thanks

Link to comment
Share on other sites

Close... you'll need to use LEFT JOIN to pull in non-matching rows:

 

SELECT 
s.subject as preferred_subject
,COUNT(s.subject) as total_subject 
FROM subjects AS s
LEFT JOIN students AS st ON ( s.id = st.prefered_subject ) 
GROUP BY s.subject

Link to comment
Share on other sites

It's getting there... really appreciate this guys!! would never have figured this out on my own. Theres only 1 last problem with this, where there is a subject that no students have selected as their preferred subject it is displaying (1) next to it instead of (0)... any ideas?

 

It should be outputting the following

 

maths (3)

english (1)

french (0)

physics (0)

history (0)

PE (9)

 

but its actually displaying

 

maths (3)

english (1)

french (1)

physics (1)

history (1)

PE (9)

 

Thanks

 

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.