Jump to content

Recommended Posts

Hi, I have been working on this one page for many many hours trying to get it to work, and I have decided to turn here for some help.

 

I am attaching an image of my database table, I want to know how many times a student appears in the database with "y" in a week column. The code is supposed to show an admin how many lessons a student has had so far in the Term by adding up how many "y"s belong to that student.

 

I can get it to work fine with a single student, but not if I want all students on one page.

 

I thought if I could get it to echo out the student name for just week 1 only, with how many lessons they had, I could repeat the code for weeks 2-10.

 

I can only get a result for the 1st student: "Donald Duck: 1".  I don't know why it doesn't loop back through the other student names. If you see the database table, you'll see there are 3 other names that say "y".

 

Here is the current code I have come up with:

 

 

<?php include('includes/admin_session.php'); ?>
<?php require_once("includes/connection.php");

// Set variables from search query

$tutor_name = $_POST['tutor_name'];
$school_name = $_POST['school'];
$term = $_POST['term'];
$year = $_POST['year'];

// Query database with the above variables to get all stored data belonging to the tutor, at X school during X term and X year

$result = mysqli_query($connection,"SELECT * FROM student_roll WHERE tutor_name='$tutor_name' AND school='$school_name' AND term='$term' AND year='$year' ORDER BY last_name ASC")
or die(mysqli_error($connection));

while($row = mysqli_fetch_array($result)) {

    //get first and last name of all students belonging to tutor at X school, term and year

         $student_first_name= $row['first_name'];
         $student_last_name= $row['last_name'];

    // Use student names to loop through table the names of all students who had a lesson on week 1 and display 1
    // as the number of lessons they had.

$result = mysqli_query($connection,"SELECT * FROM student_roll WHERE first_name='$student_first_name' AND last_name='$student_last_name' AND week1='y'")
or die(mysqli_error($connection));

while($row = mysqli_fetch_array($result)) {


$num_rows = mysqli_num_rows($result);

echo $student_first_name . " " . $student_last_name . ": ";
echo $num_rows;
echo "<br />";
echo "<br />";
}
}
?>

 

I have also tried something like this where I test the value of week1, week2 etc and assign a value of 1 if it is 'y' and adding the total number up as a sum. I did this on the single student page, like this:

 


<?php include('includes/session.php'); ?>
<?php

require_once("includes/connection.php");

// Get student's first name from the database
$username = $_SESSION['username'];

$result = mysqli_query($connection,"SELECT * FROM students WHERE username='$username'")
or die(mysqli_error($connection));

while($row = mysqli_fetch_array($result)) {

$first_name = $row['first_name'];
$last_name = $row['last_name'];

}

// Get current roll from the database

$term = "1"; // get from db not hardcoded - set by admin

$current_week = "4"; // get from db not hardcoded - set by admin

$combined_check = mysqli_fetch_assoc(mysqli_query($connection,"select count(*) as Count from student_roll WHERE first_name='$first_name' AND last_name='$last_name' AND term='$term' AND week1='y' LIMIT $current_week "));
if ($combined_check["Count"]>0) {
$lesson1 = "1";
}

$combined_check = mysqli_fetch_assoc(mysqli_query($connection,"select count(*) as Count from student_roll WHERE first_name='$first_name' AND last_name='$last_name' AND term='$term' AND week2='y' LIMIT $current_week "));
if ($combined_check["Count"]>0) {
$lesson2 = "1";
}

$combined_check = mysqli_fetch_assoc(mysqli_query($connection,"select count(*) as Count from student_roll WHERE first_name='$first_name' AND last_name='$last_name' AND term='$term' AND week3='y' LIMIT $current_week "));
if ($combined_check["Count"]>0) {
$lesson3 = "1";
}

$combined_check = mysqli_fetch_assoc(mysqli_query($connection,"select count(*) as Count from student_roll WHERE first_name='$first_name' AND last_name='$last_name' AND term='$term' AND week4='y' LIMIT $current_week "));
if ($combined_check["Count"]>0) {
$lesson4 = "1";
}

$combined_check = mysqli_fetch_assoc(mysqli_query($connection,"select count(*) as Count from student_roll  WHERE first_name='$first_name' AND last_name='$last_name' AND term='$term' AND week5='y' LIMIT $current_week "));
if ($combined_check["Count"]>0) {
$lesson5 = "1";
}

$combined_check = mysqli_fetch_assoc(mysqli_query($connection,"select count(*) as Count from student_roll  WHERE first_name='$first_name' AND last_name='$last_name' AND term='$term' AND week6='y' LIMIT $current_week "));
if ($combined_check["Count"]>0) {
$lesson6 = "1";
}

$combined_check = mysqli_fetch_assoc(mysqli_query($connection,"select count(*) as Count from student_roll  WHERE first_name='$first_name' AND last_name='$last_name' AND term='$term' AND week7='y' LIMIT $current_week "));
if ($combined_check["Count"]>0) {
$lesson7 = "1";
}

$combined_check = mysqli_fetch_assoc(mysqli_query($connection,"select count(*) as Count from student_roll  WHERE first_name='$first_name' AND last_name='$last_name' AND term='$term' AND week8='y' LIMIT $current_week "));
if ($combined_check["Count"]>0) {
$lesson8 = "1";
}

$combined_check = mysqli_fetch_assoc(mysqli_query($connection,"select count(*) as Count from student_roll WHERE first_name='$first_name' AND last_name='$last_name' AND term='$term' AND week9='y' LIMIT $current_week "));
if ($combined_check["Count"]>0) {
$lesson9 = "1";
}

$combined_check = mysqli_fetch_assoc(mysqli_query($connection,"select count(*) as Count from student_roll WHERE first_name='$first_name' AND last_name='$last_name' AND term='$term' AND week10='y' LIMIT $current_week "));
if ($combined_check["Count"]>0) {
$lesson10 = "1";
}
$total = $lesson1+$lesson2+$lesson3+$lesson4+$lesson5+$lesson6+$lesson7+$lesson8+$lesson9+$lesson10;

echo "You have had " . $total . " lessons";
echo "<p> </p>";

?>

 

I'm certain that advanced php gurus know of a far more efficient way to code than that, but at least it works.

 

Any helpful advice on how to get all of the students to show with a total number of lessons will be greatly appreciated.

 

Thanks in advance.

 

[attachment deleted by admin]

Are you looking for something like this?  This represents how many times each student shows up in week1.  I didn't see a username column in the table.  This query would be a problem if you have two students with the same exact username and last name.

 

Table will show [first_name][last_name][count]

SELECT first_name last_name, count(*) FROM students
GROUP BY CONCAT(first_name, last_name)
WHERE week1 = 'y'

Thanks, I'll give it a try. There won't be a circumstance where two students will have the same first and last name entered in the database. This is checked automatically when a new student account is established.

Oh, by the way, re the username, I think you were going by the 2nd code I posted which I said worked for a single student. My table image belongs to the first code I posted, which I was trying to get multiple students displayed on the page with their lesson count. The 2nd code has a different table.

 

[edit: Your code gives me a syntax error when using it in the first code]

Try something like this:  Un-tested of course.

<?php include('includes/admin_session.php'); ?>
<?php require_once("includes/connection.php");

// Set variables from search query

$tutor_name = $_POST['tutor_name'];
$school_name = $_POST['school'];
$term = $_POST['term'];
$year = $_POST['year'];

// Query database with the above variables to get all stored data belonging to the tutor, at X school during X term and X year

$result = mysqli_query($connection,"SELECT * FROM student_roll WHERE tutor_name='$tutor_name' AND school='$school_name' AND term='$term' AND year='$year' ORDER BY last_name ASC")
or die(mysqli_error($connection));

while($row = mysqli_fetch_array($result)) {

    //get first and last name of all students belonging to tutor at X school, term and year

         $student_first_name= $row['first_name'];
         $student_last_name= $row['last_name'];

    // Use student names to loop through table the names of all students who had a lesson on week 1 and display 1
    // as the number of lessons they had.

$result = mysqli_query($connection,"SELECT * FROM student_roll WHERE first_name='$student_first_name' AND last_name='$student_last_name'")
or die(mysqli_error($connection));

if(mysqli_num_rows($result)) {
	while($row = mysqli_fetch_array($result)) {
	//set students in an array, so we increment a count to keep total rows.
		$students[$student_first_name][$student_last_name] = 0;
		if($row['week1'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week2'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week3'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week4'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week5'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week6'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week7'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week8'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week9'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week10'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
	}
}
}

if(is_array($students)) {
foreach($students as $fname => $dem2) {
	foreach($dem2 as $lname => $count) {
		echo $fname . " " . $lname . ": ";
		echo $count;
		echo "<br />";
		echo "<br />";
	}
}
}
else {
echo ucwords(strtolower($tutor_name)) . ' does not have any students to display!';
}
?>

SELECT first_name, last_name

 

There won't be a circumstance where two students will have the same first and last name

 

How do you know for sure? Just deny the student from college/university?

 

Hi ignace, I always like reading your posts. :)

 

I know for sure because I am only doing testing, and I am typing my own fake names. :)

 

Eventually this will be a site for music tuition at a few schools. There will be more checks done other than first name and last name, that is the least of my problems right now. I just want a count of lessons for each student to come from the database where the week#="y". Once that works, I can do the rest of the code, including taking away hardcoded variables and other things I know how to do. I don't want to spend time on those extra things now if my script (the bits that I don't know how to do, such as my original question) is never going to work.

 

Thanks for reading my post. :)

Try something like this:  Un-tested of course.

<?php include('includes/admin_session.php'); ?>
<?php require_once("includes/connection.php");

// Set variables from search query

$tutor_name = $_POST['tutor_name'];
$school_name = $_POST['school'];
$term = $_POST['term'];
$year = $_POST['year'];

// Query database with the above variables to get all stored data belonging to the tutor, at X school during X term and X year

$result = mysqli_query($connection,"SELECT * FROM student_roll WHERE tutor_name='$tutor_name' AND school='$school_name' AND term='$term' AND year='$year' ORDER BY last_name ASC")
or die(mysqli_error($connection));

while($row = mysqli_fetch_array($result)) {

    //get first and last name of all students belonging to tutor at X school, term and year

         $student_first_name= $row['first_name'];
         $student_last_name= $row['last_name'];

    // Use student names to loop through table the names of all students who had a lesson on week 1 and display 1
    // as the number of lessons they had.

$result = mysqli_query($connection,"SELECT * FROM student_roll WHERE first_name='$student_first_name' AND last_name='$student_last_name'")
or die(mysqli_error($connection));

if(mysqli_num_rows($result)) {
	while($row = mysqli_fetch_array($result)) {
	//set students in an array, so we increment a count to keep total rows.
		$students[$student_first_name][$student_last_name] = 0;
		if($row['week1'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week2'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week3'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week4'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week5'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week6'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week7'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week8'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week9'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
		if($row['week10'] == 'y') {	$students[$student_first_name][$student_last_name] += 1; }
	}
}
}

if(is_array($students)) {
foreach($students as $fname => $dem2) {
	foreach($dem2 as $lname => $count) {
		echo $fname . " " . $lname . ": ";
		echo $count;
		echo "<br />";
		echo "<br />";
	}
}
}
else {
echo ucwords(strtolower($tutor_name)) . ' does not have any students to display!';
}
?>

 

Hi jcbones, your script works without any errors for returning the first user in my database, but doesn't loop through the rest. Any ideas of what is stopping the script from looping through the other students? My initial attempt wouldn't loop through the students either.

Hi ignace, I always like reading your posts. :)

 

Why is that? For real I mean I am really interested in hearing your opinions (you can even PM me if you don't want to humiliate me in public :D)

 

Haha, well there's certainly no need to humiliate you. I often learn a lot from the help you give others, so I respect your replies, and am always interested to read your suggestions to other people's PHP problems.

 

I have to say though that your suggestion.. that "SELECT first_name,last_name" just produced the first user in the table, like my try attempt did, and jcbones' attempt did.  :D:shrug: Oh well. I might have to try a different method soon. :)

Haha, well there's certainly no need to humiliate you. I often learn a lot from the help you give others, so I respect your replies, and am always interested to read your suggestions to other people's PHP problems.

 

Well 1) I'm always really interested in hearing your opinions on the help I provide, so thank you :D 2) I said that because I sometimes have a dark sense of humor that not everyone can appreciate (or is even funny at times) but I never mean to offend anyone and I am always willing to apologize to anyone who feels offended.

I am away from home for Easter, so I had to set up a different database to try out this code.

 

This is what I have discovered:

 

With my initial attempt, and jcbones attempt, it only shows the first user in the database.

 

If all of the values are in one row, the correct number of lessons is calculated.

 

If the usernames are inserted into the database at different times, the script is reading the first user, once only and the count of "y" in the week column is therefore always "1".

 

I have decided that I my database design must be bad. It works fine for counting the number of lessons for one student though. :)

What are you exactly trying to achieve (based on the roll_table.jpg)? Can you give a short table preview of the desired result, something like:

 

field1, field2, field3

-----------------------

bla, bla, bla

old, mcdonald, had a farm

and on his, farm, he had a cow,

..

 

I can create a query based on this result.

 

There won't be a circumstance where two students will have the same first and last name

 

It seems like Walt Disney has already been successful :)

From roll_table.jpg I should get the total lessons for each user.

 

Donald Duck: 6

 

Rolf Harris: 5

 

Mickey Mouse: 5

 

And so on. The idea was to calculate how many times each first name, last name and "y" appeared together in the database. I thought it would be simple, but it isn't

 

 

 

 

There won't be a circumstance where two students will have the same first and last name

 

It seems like Walt Disney has already been successful :)

 

lol! I'ts the same student each time!  ;D This isn't the "students" table.

 

Btw the database table is as you see it in roll_table.jpg

lol! I'ts the same student each time!  ;D This isn't the "students" table.

 

Then why does it have student information stored? Send me an SQL dump (create table only) so that I can review your database and maybe optimize it.

 

For your query try and tell if that comes close to what you want:

 

SELECT first_name, last_name, count(*) as lessons

FROM roll_table

GROUP BY first_name, last_name

HAVING week1 = 'y' AND week2 = 'y' ..

"students" is a table with all of the students contact info.

 

"students_roll" is a table which logs whether the student has had a lesson or not.

 

This script I am having trouble with is the result of an admin searching for students of a tutor during the period of X term in X year.

 

It uses the search words "tutor name" "school" "term" and "year" to get the names of all students belonging to that tutor, at X school at whatever term, in whichever year.

 

After getting the student names, it looks in the "students_roll" table to see how many times the student's first names and last names appear in the table with "y" in the week column.

 

My database structure is this:

 

-- phpMyAdmin SQL Dump
-- version 3.2.4
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Apr 03, 2010 at 08:39 PM
-- Server version: 5.1.41
-- PHP Version: 5.3.1

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `krism_db`
--

-- --------------------------------------------------------

--
-- Table structure for table `admin`
--

CREATE TABLE IF NOT EXISTS `admin` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

-- --------------------------------------------------------

--
-- Table structure for table `log`
--

CREATE TABLE IF NOT EXISTS `log` (
  `id` mediumint(11) NOT NULL AUTO_INCREMENT,
  `ip` varchar(255) NOT NULL,
  `username` varchar(255) NOT NULL,
  `page` varchar(255) NOT NULL,
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=127 ;

-- --------------------------------------------------------

--
-- Table structure for table `schools`
--

CREATE TABLE IF NOT EXISTS `schools` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `school_name` varchar(255) NOT NULL,
  `type` varchar(255) NOT NULL,
  `address` varchar(255) NOT NULL,
  `state` varchar(255) NOT NULL,
  `postcode` varchar(255) NOT NULL,
  `school_phone` varchar(255) NOT NULL,
  `school_email` varchar(255) NOT NULL,
  `contact_name` varchar(255) NOT NULL,
  `position` varchar(255) NOT NULL,
  `contact_phone` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Table structure for table `students`
--

CREATE TABLE IF NOT EXISTS `students` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(255) NOT NULL,
  `last_name` varchar(255) NOT NULL,
  `contact` varchar(255) NOT NULL,
  `phone_h` varchar(255) NOT NULL,
  `phone_m` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `address` varchar(255) NOT NULL,
  `state` varchar(255) NOT NULL,
  `postcode` varchar(255) NOT NULL,
  `instrument_1` varchar(255) NOT NULL,
  `tutor_1` varchar(255) NOT NULL,
  `instrument_2` varchar(255) NOT NULL,
  `tutor_2` varchar(255) NOT NULL,
  `school` varchar(255) NOT NULL,
  `school_year` varchar(255) NOT NULL,
  `class` varchar(255) NOT NULL,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `profile` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

-- --------------------------------------------------------

--
-- Table structure for table `student_roll`
--

CREATE TABLE IF NOT EXISTS `student_roll` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `term` varchar(1) NOT NULL,
  `year` varchar(4) NOT NULL,
  `tutor_name` varchar(50) NOT NULL,
  `school` varchar(255) NOT NULL,
  `first_name` varchar(50) NOT NULL,
  `last_name` varchar(50) NOT NULL,
  `week1` varchar(3) NOT NULL,
  `week2` varchar(3) NOT NULL,
  `week3` varchar(3) NOT NULL,
  `week4` varchar(3) NOT NULL,
  `week5` varchar(3) NOT NULL,
  `week6` varchar(3) NOT NULL,
  `week7` varchar(3) NOT NULL,
  `week8` varchar(3) NOT NULL,
  `week9` varchar(3) NOT NULL,
  `week10` varchar(3) NOT NULL,
  `notes` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

-- --------------------------------------------------------

--
-- Table structure for table `tutors`
--

CREATE TABLE IF NOT EXISTS `tutors` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(255) NOT NULL,
  `last_name` varchar(255) NOT NULL,
  `phone_h` varchar(255) NOT NULL,
  `phone_m` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `instrument` varchar(255) NOT NULL,
  `school` varchar(255) NOT NULL,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `profile` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

 

I'll try your query now :)

Using this query:

 

$result = mysqli_query($connection,"SELECT first_name, last_name, count(*) as lessons
FROM student_roll GROUP BY first_name, last_name HAVING week1 = 'y' AND week2 = 'y' AND week3 = 'y' AND week4 = 'y' AND week5 = 'y' AND week6 = 'y' AND week7 = 'y' AND week8 = 'y' AND week9 = 'y' AND week10 = 'y'")
or die(mysqli_error($connection));

 

I get:

 

Unknown column 'week1' in 'having clause'

 

I think it needs glasses. :)

 

Maybe it's time to stop trying to write PHP code in the hotel bathtub. :)

Do you mean this?

 

$result = mysqli_query($connection,"SELECT first_name, last_name, count(*) as lessons FROM student_roll
WHERE week1 = 'y' AND week2 = 'y' AND week3 = 'y' AND week4 = 'y' AND week5 = 'y' AND week6 = 'y' AND week7 = 'y' AND week8 = 'y' AND week9 = 'y' AND week10 = 'y' GROUP BY first_name, last_name ")
or die(mysqli_error($connection));

 

I get ony 1 user again, and one of their entries: "Bugs Bunny: 1"

 

My table is "student_roll". If I typed "students_roll" I know I'll get a worse error (though easier to fix) :)

No, I am working with a different database table (just for student_roll) because I am not at home, and didn't have that table on my laptop so I created a new one.

 

I can't do a print screen and post because I don't have anything like photoshop installed, but my new database does have two students who have had 10 lessons, and the only one that has had 10 lessons with "y" is Fred Flintstone. I could do a table dump of that one table if you want, but since you have the table without data, you could experiment if you wanted to.

 

I got bugs bunny:1 because it found my first student which was bugs bunny who had 4 lessons.

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.