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 trial

PHP

Trying to validate data if it is an integer but it's not working

I'm trying to make sure the attendance is between 0 and 100 and the data is an integer.If those conditions are true then the program carries on. I keep on getting this error " Parse error: syntax error, unexpected T_STRING, expecting '(' in /home/u717042829/public_html/predicter.php on line 93"

This is line 93:

elseif bool is_int (mixed $attendance);

Here is the rest of my code :

<?php 
error_reporting(E_ALL); ini_set('display_errors', 1);
require("includes/config.php");

// Require: use your own path
require_once 'morris.php';
require_once 'morris-charts.php';

// Optional: include chart line
require_once 'morris-line-charts.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">


    <script src="js/bootstrap.min.js" type="text/javascript"></script>
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
     <script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>

</head>

<body>




<?php 

    //posted form data
    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'];

          // <--------------Validation for form Post----------------------->




        $error = false;

        //validation make sure  2 of the same subjects haven't been chosen 
        if ($subject1 == $subject2 or $subject1 ==  $subject3 or  $subject1 == $subject4) {
           $error = false;
          } 
        elseif ($subject2 == $subject1 or $subject2 == $subject3 or $subject2 == $subject4) {
          $error = false;

          }
        elseif ($subject3 == $subject1 or $subject3 == $subject2 or $subject3 == $subject4) {
             $error = false;
            }
            // checks if the data entered is an integer 
        elseif bool is_int (mixed $attendance);

        if ($error === true) AND (bool = true) {
            header("location:error.php");
        }

    else {  


      //selecting from the database

       $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->fetch(PDO::FETCH_ASSOC);
    // fetch() instead of fetchAll just gets the first result

    // Output the predicted grade (or any item from the array) like this

    }
    }?>

<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="jumbotron">

                <h1>

                  <?php 
                       if (empty($result['final_maths'])) {
                        header('location:error.html');
                       }
                       else {
                           echo "<h1>The predicted grade for " . $name . " is a " . $result['final_maths'] . "</h1>";
                                $grade = $result['final_maths'];

                            }

                   ?>
                <p>
                    Please remember that these predictions does not mean the student will get this grade
                    <br>
                    <i> "It always seems impossible until it is done" </i>
                </p>

                <a class="btn btn-primary btn-lg btn-block" href="memberpage.php" role="button">Home  &#x2302;</a>

            </div> 
        </div>
    </div>
</div>

                     <!--- GENERATING THE GRAPH --> 

              <div id="myfirstchart" style="height: 250px;"></div>
              <script>
                    new Morris.Line({
  // ID of the element in which to draw the chart.
  element: 'myfirstchart',
  // Chart data records -- each entry in this array corresponds to a point on
  // the chart.
  data: [
    { year: '2008', value: 20 },
    { year: '2009', value: 10 },
    { year: '2010', value: 5 },
    { year: '2011', value: 5 },
    { year: '2012', value: 20 }
  ],
  // The name of the data record attribute that contains x-values.
  xkey: 'year',
  // A list of names of data record attributes that contain y-values.
  ykeys: ['value'],
  // Labels for the ykeys -- will be displayed when you hover over the
  // chart.
  labels: ['Value']
}); 




        /** new Morris.Bar({

            element: 'prediction',
            data:[{
                term:'Autumn',
                grade:'D'
            },{
                term:'Christmas',
                grade:'C',
            },{
                term:'Spring',
                grade:'C'
            },{
                term:'Summer',
                grade:'B'
            }],
            xkey:'term',
            ykeys:['grade'],
            labels:['Grade'],
            barRatio:0.4
            xLabelAngle: 35,
            hideHover: 'auto',
            resize: true



}): **/


                </script>









</body>
</html>

1 Answer

Paul Yabsley
Paul Yabsley
46,713 Points

Hi Furquan

It looks like that line has been copied from some docs because the syntax used is incorrect. If, ifelse, else statements are constructed like:

if ( $foo > 0 ) {
  // do something
}
elseif ( $foo <= 100) {
  // do something else
}
else {
  // do this
}

On top of that is_int is a function that returns true if the value is an integer and false if not. So you need to pass $attendance into the is_int function i.e. is_int($attendance). You can then use that inside your elseif expression. In addition, you also need to check $attendance is between 0 and 100. Replace your line 93 with the following.

elseif ( is_int( $attendance ) && $attendance >= 0 && $attendance <= 100 ) {
  $error = false; // If the above condition is met, $error is set to false
}