tobimichigan Posted April 20, 2010 Share Posted April 20, 2010 Fatal error: Call to undefined function int() in C:\xampp\htdocs\details.php on line 135 <?php $grade = array("A","B","C","D","E","F"); $adno = $_GET['adno']; //$grade[0]=A; //$grade[1]=B; //$grade[2]=C; //$grade[3]=D; //$grade[4]=E; //$grade[5]=F; $adno=$_GET['adno']; $select = mysql_query("select * from acadinfo where adno='$adno'") or die('<p>Error Retrieving<br/>'.'Error: ' .mysql_error() . '</p>'); echo "<table width='548' border='1'> <tr> <th width='26' scope='col'>ADMISSION NO.</th> <th width='26' scope='col'>MATHEMATICS</th> <th width='26' scope='col'>ENGLISH</th> <th width='26' scope='col'>PHYSICS</th> <th width='26' scope='col'>CHEMISTRY</th> <th width='365' scope='col'>BIOLOGY</th> </tr> <tr><th scope='row'>$adno</th>"; $datalist = mysql_fetch_array($select); //$datalist = htmlspecialchars($datalist); foreach($datalist as $data){ if (int($data) >= 80) { echo "<td>$grade[0]</td>"; } else { if (int($data) >= 70 && int($data) < 80) { echo "<td>$grade[1]</td>"; } else { if (int($data) >= 60 && int($data) < 70) { echo "<td>$grade[2]</td>"; }else { if (int($data) >= 50 && int($data) < 60) { echo "<td>$grade[3]</td>"; } else { if (int($data) >= 40 && int($data) < 50) { echo "<td>$grade[4]</td>"; } else { if (int($data) <= 39 && int($data) < 40) { echo "<td>$grade[5]</td>"; } } } } } } } echo "</tr></table>"; ?> Please can I stand corrected? I m working on student grades... Line 135-if (int($data) >= 80) { Quote Link to comment Share on other sites More sharing options...
Gekk0 Posted April 20, 2010 Share Posted April 20, 2010 try intval() instead of int() Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 20, 2010 Share Posted April 20, 2010 Correct usage (int) $someVar; Example: $foo = 1.23; $bar = (int) $foo; echo $bar; //Output 1 Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted April 20, 2010 Author Share Posted April 20, 2010 Hello Gekko, please could you tell what exactly is making the code output F,F,F,FF, instead of the real grades? Here's my code: <?php $grade = array("A","B","C","D","E","F"); $adno = $_GET['adno']; $grade[0]=A; $grade[1]=B; $grade[2]=C; $grade[3]=D; $grade[4]=E; $grade[5]=F; $adno=$_GET['adno']; $select = mysql_query("select * from acadinfo where adno='$adno'") or die('<p>Error Retrieving<br/>'.'Error: ' .mysql_error() . '</p>'); echo "<table width='548' border='1'> <tr> <th width='26' scope='col'>ADMISSION NO.</th> <th width='26' scope='col'>MATHEMATICS</th> <th width='26' scope='col'>ENGLISH</th> <th width='26' scope='col'>PHYSICS</th> <th width='26' scope='col'>CHEMISTRY</th> <th width='365' scope='col'>BIOLOGY</th> </tr> <tr><th scope='row'>$adno</th>"; $datalist = mysql_fetch_array($select); $num=mysql_num_rows($select); //$datalist = htmlspecialchars($datalist); foreach($datalist as $data){ if (intval($data) >= 80) { echo "<td>$grade[0]</td>"; } else { if (intval($data) >= 70 && intval($data) < 80) { echo "<td>$grade[1]</td>"; } else { if (intval($data) >= 60 && intval($data) < 70) { echo "<td>$grade[2]</td>"; }else { if (intval($data) >= 50 && intval($data) < 60) { echo "<td>$grade[3]</td>"; } else { if (intval($data) >= 40 && intval($data) < 50) { echo "<td>$grade[4]</td>"; } else { if (intval($data) <= 39 && intval($data) < 40) { echo "<td>$grade[5]</td>"; } } } } } } } echo "</tr></table>"; ?> By the way the int problem was solved by implementing your correction...thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 20, 2010 Share Posted April 20, 2010 $datalist = mysql_fetch_array($select); $num=mysql_num_rows($select); //$datalist = htmlspecialchars($datalist); foreach($datalist as $data){ if (intval($data) >= 80) { echo "<td>$grade[0]</td>"; $data is an ARRAY. You can't get the integer of an arryay. Although there is a lot more wrong with that code. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 20, 2010 Share Posted April 20, 2010 What does your DB table look like? Quote Link to comment Share on other sites More sharing options...
Gekk0 Posted April 20, 2010 Share Posted April 20, 2010 Okidoki, a few things.... $grade = array("A","B","C","D","E","F"); $adno = $_GET['adno']; $grade[0]=A; $grade[1]=B; $grade[2]=C; $grade[3]=D; $grade[4]=E; $grade[5]=F; this is kind of redundant isn't it? you can remove all of this: $grade[0]=A; $grade[1]=B; $grade[2]=C; $grade[3]=D; $grade[4]=E; $grade[5]=F; you've already defined your array of possible grades, there's no need to do it again! for this section of code: foreach($datalist as $data){ if (intval($data) >= 80) { echo "<td>$grade[0]</td>"; } else { if (intval($data) >= 70 && intval($data) < 80) { echo "<td>$grade[1]</td>"; } else { if (intval($data) >= 60 && intval($data) < 70) { echo "<td>$grade[2]</td>"; }else { if (intval($data) >= 50 && intval($data) < 60) { echo "<td>$grade[3]</td>"; } else { if (intval($data) >= 40 && intval($data) < 50) { echo "<td>$grade[4]</td>"; } else { if (intval($data) <= 39 && intval($data) < 40) { echo "<td>$grade[5]</td>"; } } } } } } } I'd look at investing in some elseif() statements, seriously so something like this: foreach($datalist as $data) { if(intval($data) >= 80) { echo "<td>$grade[0]</td>"; } elseif(intval($data) >= 70 && intval($data) < 80) { echo "<td>$grade[1]</td>"; } elseif(intval($data) >= 60 && intval($data) < 70) { echo "<td>$grade[2]</td>"; } elseif(intval($data) >= 50 && intval($data) < 60) { echo "<td>$grade[3]</td>"; } elseif(intval($data) >= 40 && intval($data) < 50) { echo "<td>$grade[4]</td>"; } elseif(intval($data) <= 39 && intval($data) < 40) { echo "<td>$grade[5]</td>"; } } thats what the elseif is for! but, check that $data variable, i'm guessing that what you actually want there is something like $data['mark'] instead just $data, because your looking at the element of that array $data jus editing this again .. yeah so basically you downloaded your database into that variable $datalist (with entries of each row you've retrieved from the database), which is an array, and each element in that array will be another array - this will be an array for example $dataList[0]['name'], $dataList[0]['mark'], $dataList[0]['birthday'] - or whatever, just depends on how your tables are structured so when you launch into that foreach statment your going through each $dataList element and giving it the name $data, but $data is still an array - because each element of $dataList was an array.. am I making sense? Quote Link to comment Share on other sites More sharing options...
Gekk0 Posted April 20, 2010 Share Posted April 20, 2010 I dunno if this is necessary, maybe some other forum members could correct me, but this is the way i'd get your $datalist array from the database $query = mysql_query($select); //Build the array $i = 0; while ($row = mysql_fetch_assoc($query)) { $keys = array_keys($row); foreach($keys as $key) { $datalist[$i] = $row[$key]; } $i++; } instead of $datalist = mysql_fetch_array($select); $num=mysql_num_rows($select); Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 20, 2010 Share Posted April 20, 2010 Nah, if you want to go that route, I would set the index of the array as the grade percentage and the value as the letter grade. Then you can get the letter grade by a simple foreach loop $grades = array(80=>'A', 70=>'B', 60=>'C', 50=>'D', 40=>'E', 0=>'F'); $studentPercent = 75; //Some number from the DB query foreach($grades as $gradePercent => $letterGrade) { if($studentPercent>$gradePercent) { break; } } echo $letterGrade However, I would take a whole different approach. After looking over your code some more, it seems that you should be getting multiple grades from the DB query (one for each column header). I assume the grades for each subject are in a different field name in the database. Here is a rewrite of what you had. Replace the field names as appropriate: <?php function getLetterGrade($percent) { if(!is_numeric($percent)) { return '--'; } if($percent >= 80) { return 'A'; } if($percent >= 70) { return 'B'; } if($percent >= 60) { return 'C'; } if($percent >= 50) { return 'D'; } if($percent >= 40) { return 'E'; } return 'F'; } $adno = (int) $_GET['adno']; $query = "SELECT * FROM acadinfo WHERE adno='{$adno}'"; $result = mysql_query($query) or die('<p>Error Retrieving<br/>'.'Error: ' .mysql_error() . '</p>'); if(!mysql_num_rows($result)) { echo "No records for admission number {$adno}"; } else { $studentGrades = mysql_fetch_assocay($result); echo "<table width=\"548\" border=\"1\">\n"; echo " <tr>\n"; echo " <th width=\"26\" scope=\"col\">ADMISSION NO.</th>\n"; echo " <th width=\"26\" scope=\"col\">MATHEMATICS</th>\n"; echo " <th width=\"26\" scope=\"col\">ENGLISH</th>\n"; echo " <th width=\"26\" scope=\"col\">PHYSICS</th>\n"; echo " <th width=\"26\" scope=\"col\">CHEMISTRY</th>\n"; echo " <th width=\"365\" scope=\"col\">BIOLOGY</th>\n"; echo " </tr>\n"; echo " <tr>\n"; echo " <td>{$adno}</td>\n"; echo " <td>" . getLetterGrade($studentGrades['MATHEMATICS']) . "</td>\n"; echo " <td>" . getLetterGrade($studentGrades['ENGLISH']) . "</td>\n"; echo " <td>" . getLetterGrade($studentGrades['PHYSICS']) . "</td>\n"; echo " <td>" . getLetterGrade($studentGrades['CHEMISTRY']) . "</td>\n"; echo " <td>" . getLetterGrade($studentGrades['BIOLOGY']) . "</td>\n"; echo " </tr>\n"; echo "</table>\n"; } ?> Quote Link to comment Share on other sites More sharing options...
jcbones Posted April 20, 2010 Share Posted April 20, 2010 My take on this script; <?php function get_grade((int)$i) { if($i > 79) return 'A'; elseif($i > 69 && $i < 80) return 'B'; elseif($i > 59 && $i < 70) return 'C'; elseif($i > 49 && $i < 60) return 'D'; elseif($i > 39 && $i < 50) return 'E'; else return 'F'; } $adno = $_GET['adno']; $select = mysql_query("select * from acadinfo where adno='$adno'") or die('<p>Error Retrieving<br/>'.'Error: ' .mysql_error() . '</p>'); echo "<table width='548' border='1'> <tr> <th width='26' scope='col'>ADMISSION NO.</th> <th width='26' scope='col'>MATHEMATICS</th> <th width='26' scope='col'>ENGLISH</th> <th width='26' scope='col'>PHYSICS</th> <th width='26' scope='col'>CHEMISTRY</th> <th width='365' scope='col'>BIOLOGY</th> </tr> <tr><td scope='row'>$adno</td>"; $datalist = mysql_fetch_assoc($select); foreach($datalist as $data){ echo '<td>' . get_grade($data) . '</td>'; } echo '</tr></table>'; ?> Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted April 21, 2010 Author Share Posted April 21, 2010 Wow, way 2 go guz..I'm gonna implement ur corrections and get back 2 u soon...thanks a mil.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.