-1

I have a code that will display data in a table form. there is a field called value that will store some values. I need to have an if statement that will force a window to popup whenever the value exceeds 30. I have an if statement that is not working. Can anyone help?

This is the code I used:

<?php

$link = mysql_connect("localhost", "root", "");
$select= mysql_select_db("");
mysql_select_db("form");


$query = mysql_query("SELECT * FROM demo ORDER BY parameter ASC");
$record = mysql_fetch_array($query);
echo "<table border=1
<tr>
<th>Record ID</th>
<th>Parameter</th>
<th>Value</th>
<th>Time</th>

</tr>";
$con = mysql_connect("localhost", "root", "");
//$query = "select * from demo";

$query1 = mysql_query("Select * from demo where parameter = \"conductivity\"");
    while($row = mysql_fetch_array($query1)){

        echo " <tr><td>" . $row["recordID"] . "</td><td>" . $row["parameter"] . "</td><td>" . $row["value"] . "</td><td>" . $row["time"]  . "</td><tr>";

    }

if ("value">"30"){
echo "<script>alert('Alert');</script>";
}



echo "</table>";



mysql_close($link);



?>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
engineer
  • 3
  • 2
  • 1
    Possible duplicate of [How to call a JavaScript function from PHP?](http://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php) – Ocaso Protal Apr 29 '16 at 07:31
  • 1
    you need to bin the mysql driver, its gone in PHP7 and horrible before that – DevDonkey Apr 29 '16 at 07:36

3 Answers3

3

Surrely it needs to be like this within the while loop presuming by value you mean the value from the SELECT statement. Which means you need $row["value"] and not just "value". Javascript in php just work like any other string would so echo it out like you've done inside the if.

while($row = mysql_fetch_array($query1)){

    echo " <tr><td>" . $row["recordID"] . "</td><td>" . $row["parameter"] . "</td><td>" . $row["value"] . "</td><td>" . $row["time"]  . "</td><tr>";

    if ($row["value"] > 30){
        echo "<script>alert('Alert');</script>";
    }
}
Ben Rhys-Lewis
  • 3,118
  • 8
  • 34
  • 45
1

Change the condition that you're using to this:

if ($row["value"] > 30) {
    echo "<script>alert('Alert');</script>";
}

The condition was checking the string with a value of "value" against "30". Checking the field value should be better.

Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
1

I think that the if ("value">"30").

the value should be a variable loop from the database.

declare a variable called $value before the loop.

And in the loop

$query1 = mysql_query("Select * from demo where parameter = \"conductivity\""); $value; while($row = mysql_fetch_array($query1)){

$value = $value + ~$row['value'];

//Assuming that value is the name of the column in your table

}

Reyan Tropia
  • 140
  • 1
  • 10