View Full Version : grades 2 students
Shoe
Tue 29th May '01, 12:36am
I'm wanting to give grades to my students. I have created an empty database called grades. I want to create a table with 3 items: section, id(ss#), finalgrade.
How do I write php code to retreive the grade based on id?
BTW I have the data as an ascii text: section, SS#, finalgrade format one record per line.
How do I create the table and load it with the data in the ascii file?
Can I do this with php?
Theiggsta
Wed 30th May '01, 1:16am
the main mysql query you shoudl be using is:
SELECT * FROM grades WHERE id = '$studentid'
So when submitting a form, use the ID your lookign for as the variable, and it will pull up the entire Row record for all matching ID #'s
krs-one
Sat 16th Jun '01, 12:14am
If you have your mysql database set up like so:
Table: grades
[CODE]
+---------------+----------------+
| id | 1 |
| ssnumber | 123456 |
| grade | 98 |
+----------------+---------------+
[CODE}
Then you can select data from it like so:
$sql = "SELECT * FROM grades WHERE id=$id"; // id is a value from a field
$result = mysql("database",$sql) or die("failed to issue query");
$numrows = mysql_num_rows($result); // not always necessary, only if retreiving more than one row, in this case, we are not
for($i=0; $i<$numrows; $i++) {
$id = mysql_result($result,$i,"id"); // grab the id
$ss = mysql_result($result,$i,"ssnumber"); // grab the ss number
$grade = mysql_result($result,$i,"grade"); // grab the grade
print "$id - $ss - $grade<br>\n";
}
The for() loop is only necessary if you have more than one row, and in this case you don't. In this case, you could have also taken out the $i and replaced it with a 0 for the mysql_result() calls.
Hope this helps.
-Vic
Mark Hensler
Sun 17th Jun '01, 4:06am
fine..... I'll take the harder part
This is quicky code.... meaning it's untested.
Someone plz proof.
[fake file]
section 1,123-45-6789,a
section 2,928-54-7669,c
section 3,715-48-7521,b
[/fake file]
// create DB connection
// somewhere up here
// update path to your file
$data_file = "/path/to/fake_file.txt";
// open and read file into an array
$file= file($data_file);
// loop through the lines
for ($i=0; $i<=count($file); $i++) {
// get the data from the line
list($section,$id,$grade) = split(",",$file[$i]);
// shove the data into the DB
$query = "INSERT INTO grades (section, id, finalgrade) VALUES('$section','$id','$grade')";
$result = mysql_query($query);
// did it get in the DB successfully?
if (!$result) die("\n<BR>\nQuery Error: $query\n<BR>\n");
}
vBulletin® v3.8.0 Beta 4, Copyright ©2000-2008, Jelsoft Enterprises Ltd.