Ordering-Sorting on runtime in PHP - php

My DB schema is like Course_Table {course_name,marks,std_ID}
I am getting only 1 subject marks for a particluar student,Therefore my flow of Logic and presnetation is
$std_id=Select std_id From student_Table
foreach(Iterate all students)
select marks from course_table where std_ID=student_ID
{
Print Student marks //here i need sorted List,,,
}
My problem is not actually the Code,I am looking for logic
I want to Print the student marks in ascending Order.
What I thought is to use Order By clause,but it is of no use as I am getting only 1 record per student .
may Be I need to create a sepreate class,Fed all the data inside this class and then Sort it.. (a very hectic approach would be my last option)
I am looking for an alternative approach or logic that could solve my query.

I'd write one query to give me all the data needed and then loop through...
something like...
Select student_ID, marks from course_Table order by student_ID, marks //this way the select is already orderd by student and marks. all we have to do is display it. Since you're not using name or anything getting records just from course_Table is fine.
for each record in resultset
if oldstudent=resultset.student then
isnewStudent=false
displaymark(resultset.mark, isNewStudent)
else
oldstudent = resultset.student
isNewStudent=True
displaymark(resultset.mark, isNewstudent)
end if
next record

Related

Why is my Query within a loop is extremely very slow

I don't have a enough experience in mysql with php, but am trying to develop a school report card, am trying to query the students, with their exams, subjects and marks. In this case I have three loops. the First loop is for the students I use it to get the student's ID, the second loop is for the exam, i use it to get the exam ID and the last is for subject I use it to get the subject ID, i need all this IDs in order to get the marks for each student per exam per subject. Bellow are the loops;
foreach ($students as $student_row){ //get each student's ID
$studentID = $student_row['studentID'];
foreach ($allExams as $allExam) { //get each exam's ID
$examID = $allExam['examID'];
foreach ($subjects as $subject){ //get each subject's ID
$subjectID = $subject['subjectID'];
$obtained_mark_query = $this->db->query("select * from mark
where mark.studentID = '$studentID' && mark.examID = '$examID' &&
mark.subjectID = '$subjectID'")->row()->mark; //to get mark for each student per exam per subject
}
}
}
I don't know what's really the problem but this loop takes really long, it takes 26 second to return all the students in a specific class, there are only 23 students in a class, four exams, and five subjects, so am wondering what it'll really be when there are more than those students. Please any idea why it's behaving like this, any idea i can use other than loops.
There are 2 main issues in your code's logic:
You are selecting every field with SELECT * FROM which is highly inefficient, try selecting only the fields you are going to use
You are querying in your 3rd nested loop, so instead of querying it once, you are doing 1 query per student, per exam, per subject (like 23 * 4 * 5 = 460 queries)
The solution is to refactor your code, your db, and fetch that information in a simple query, then iterate through the returned records and handle the information server side.
Communicating with the database is very slow. Imagine you're cooking a meal, and your database queries are trips to the grocery store. You wouldn't make a trip for every single ingredient - you'd make a list of all the ingredients you'll need so you can just make a single trip.
So you want to run a single query to fetch all the records you'll need on this page. To make this easier you can turn your "WHERE" clause into the "ORDER BY" of a single query:
SELECT * FROM mark ORDER BY studentID, subjectID, examID;
Then you can handle the output or any processing of each student's marks one after the other in a single loop over that result set.

Understanding DB Design

I wanted to make a database to store examination marks. I was wondering if it is better to have
a table for each subject which will make a single record for a roll number
or
have a marks table which will have multiple records for a roll number for every subject
Since the number of subjects is not fixed, which approach would be better.
For the first method, As the tables would be created using php dynamically, so the table names will become a variable, so that will mean extra sanitisation work,
For the second method, the roll number will have many duplicates
Since I am new to DBMS, so any insight is appreciated.
Here's one good reason to choose the second method over the first. Let's say you want to get all marks for a specific roll number. (Using 1 for example.) With the second method, it's just:
SELECT * FROM marks WHERE roll_number = 1;
With the first method (unless someone else knows a better way to do it), it's:
SELECT * FROM subject_a WHERE roll_number = 1
UNION
SELECT * FROM subject_b WHERE roll_number = 1
UNION
SELECT * FROM subject_c WHERE roll_number = 1
...
and so on, for each one of your unknown number of subject tables.

php sql check value print one line for each positive record

I have been working on my "administrative" webpage for a while now, almost finished. Just need a little help with one last thing;
I want to get a value (ID) from a table posted as Yes if the ID is found, but no if it isn't.
Have managed to get it printet, but it prints all the records in the table on every person that displays on the website (http://gyazo.com/13f271bbb8c4e83ff9ecd9908545c854 where it says "Betalt" it should just be ONE correct for each one).
The code for that part is here:
if ($row[9] == $row[1]) {
$betalt = "Ja";
}
$row[9] is the value from table SI_PAYMENTS. And $row[1] is the value from table BUSS1.
These two values should be the same if the record exists in SI_PAYMENTS, and if it exists, I want the webpage to display "Yes". But if it don't find it, I want the webpage to display "No".
EDIT
Here's my SQL Query:
SELECT buss1.navn, buss1.plassnummer, buss1.telefon, buss1.epost, buss1.fodselsdato, buss1.pastigningssted, buss1.bilettype, buss1.ankommet, buss1.merknader, si_payment.ac_inv_id FROM buss1, si_payment ORDER BY buss1.plassnummer ASC
I think you need to JOIN the two tables. At the moment, you will get every row from table buss1 next to every row from si_payment. So if there are 6 rows in buss1 and 4 in si_payment, you'll end up with 24 rows. I suspect there is a column in the two tables that links them, so you need a JOIN: something like this
SELECT buss1.navn, buss1.plassnummer, buss1.telefon, buss1.epost, buss1.fodselsdato, buss1.pastigningssted, buss1.bilettype, buss1.ankommet, buss1.merknader, si_payment.ac_inv_id
FROM buss1 INNER JOIN si_payment WHERE buss1.some_column = si_payment.another_column
ORDER BY buss1.plassnummer ASC
I can't tell what some_column and another_column should be, I'm afraid. But if you change those, that will give you only the matching rows from the two tables. If that's not quite right, you may need a LEFT JOIN, which will give you all rows from the first table and the matching rows from the same table.
If I've misunderstood, I apologise.

PHP print from two tables where attribute from table A matches id in table B

I am building a simple adress book website where you can enter information about a person and then enter information about adresses. You can then select where people lives using a drop down menu to update the building id attribute in the person to match the automaticly generated id in the building. What I can't figure out how to do print all info about all persons(this I can do) and then print the info about the building that matches each persons building id(this I have no idea how to do. Right now I am using a while loop to print each person. But I can't get the matching building to print.
Edit: also, thanks for all info I have gotten from this site in my learning of webbprograming
You could use joins in SQL to combine several tables in one query. Also you could use GROUP BY.
Just search for that and I think you will get a good tutorial in your language.
Here are some good links:
SQL JOIN, SQL GROUP BY
Could use some code and table names to help define this example a little better, but regardless, you need to change your mysql query. In your database you need at least 1 matching row between your persons table and your buildings table. This way you can JOIN them together using the common row.
I will be using id as a placeholder for the common row in this example. Somethings like this should work:
SELECT * FROM persons LEFT JOIN buildings ON(persons.id=buildings.id) WHERE persons.building_id = buildings.building_id
To get all the results from persons and buildings you will need a separate query. But, the query above should give you the results of the person's info where the building_id matches the building_id from buildings.

Echoing a pseudo column value after a COUNT

Please don't beat me if this is elementary. I searched and found disjointed stuff relating to pseudo columns. Nothing spot on about what I need.
Anyway... I have a table with some rows. Each record has a unique ID, an ID that relates to another entity and finally a comment that relates to that last entity.
So, I want to COUNT these rows to basically find what entity has the most comments.
Instead of me explaining the query, I'll print it
SELECT entity_id, COUNT(*) AS amount FROM comments GROUP BY entity_id ORDER BY amount DESC
The query does just what I want, but I want to echo the values from that pseudo column, 'amount'
Can it be done, or should I use another method like mysql_num_rows?
Thank you!!!
It's just the same as with the other column – you use the mysql_fetch_* family.
Note that moving to the Mysqli extension is encouraged. See here why.
Once you have the row in, say, $row, you can simply use the value of $row['amount'].

Categories