Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialFurquan Ahmad
5,148 PointsMy output is always array for student predicted grade.
For my project i'm trying to predict a paststudent's A level maths grade by using past student's data in my database. So paststudent1 got an A in maths A level. In year one exams they got an A in Computing, B in Physics, A in Chemistry, Male and 97% attendance. If the current student in Year 1 got the same grades, same subjects, male and same attendance i want to say they will get the same grade.
My error is that, every time i click submit and enter details the prediction always says array.
I have a lot of commented out code as i was trying different things to work, but clearly nothing is working for me and i'm a noob at programming at the age of 18 im screwed.
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
require("includes/config.php");
//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: login.php'); }
//define page title
$title = "Predict a Student's Grade";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Past Student</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<?php
if (isset($_POST['name'])) {
$name = $_POST['name'];
$subject1 = $_POST['subject1'];
$grade1 = $_POST['grade1'];
$subject2 = $_POST['subject2'];
$grade2 = $_POST['grade2'];
$subject3 = $_POST['subject3'];
$grade3 = $_POST['grade3'];
$subject4 = $_POST['subject4'];
$grade4 = $_POST['grade4'];
$attendance = $_POST['attendance'];
$gender = $_POST['gender'];
/**$query = $db->prepare("SELECT * FROM paststudent WHERE subject1 = :subject1 AND grade1 = :grade1");
$query->bindValue(':subject1', $subject1, PDO::PARAM_STR);
$query->bindValue(':grade1', $grade1), PDO::PARAM_STR);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC); **/
$query = $db->prepare( " SELECT * FROM paststudent "
. " WHERE subject1 = :subject1 AND grade1 = :grade1");
$query->execute(array(':subject1' => $subject1, ':grade1' => $grade1)) ;
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
/*$stmt = $db->prepare("SELECT * FROM paststudent WHERE subject1 = :subject1 AND grade1 = :grade1 AND subject2 = :subject2 AND grade2 =:grade2 AND subject3 = :subject3 AND grade3 =:grade3 ");
$stmt->execute(array(':subject1' => $subject1, ':grade1' => $grade1));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC); */
}
?>
<h1> Your predicted grade for maths is <?php echo $rows?> </h1>
<h3> <a href="memberpage.php">Home</h3>
</body>
</html>
2 Answers
Henrik Hansen
23,176 PointsMake it $rows[0]['final_maths'] instead.
PDO::fetchAll will return each row in an array. The array would look something like rows[0][fieldname] => value
shezazr
8,275 Pointsbecause ROWS returns an array! this is the result of your query..
Furquan Ahmad
5,148 Pointsso how do i output the final_grade from the array ?
<?php print_r($rows); ?>
<h1> <?php echo "Your predicted grade is {".$rows['final_maths']."}";?> </h1>
<h3> <a href="memberpage.php">Home</h3>