PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hi
When someone types the day of the week in a text box on a html doc called "day". The html referse to a php doc that looks up a table called "temprature" in a database called "tempdb". The php doc knows somehow which day was typed and finds the average temprature for that day and echo it to the screen.
The table has colums called sun, mon, tue etc.... in it. Can someone help with the php script needed to do this?
Thanks
Hi Mac
I've only made the db and table so far. When the html is made it will have a box to type the day into. The php I havn't got any Idea how to do. So to answer your question. no. Sorry
well just to get you started:
to get the day passed to the PHP script by POST method from the HTML form, you would use $_POST['day']
Now see if you can put something together.
if ($sunday)
echo ($dbh, "Select avg(sun) from temp);
if ($monday)
echo ($dbh, "Select avg(mon) from temp);
if ($tuesday)
echo ($dbh, "Select avg(tue) from temp);
if ($wednesday)
echo ($dbh, "Select avg(wed) from temp);
if ($thursday)
echo ($dbh, "Select avg(thur) from temp);
if ($friday)
echo ($dbh, "Select avg(fri) from temp);
if ($saturday)
echo ($dbh, "Select avg(sat) from temp);
?>
tim wrote:so forth - you must begin an if statement with { and terminate it with a }
Not all the time. If there is just one line under the if you dont need opening and closing brackets. ATleast i dont think you do, but i agree to your approache using the brackets and elseif's. But i always put brackets even if it is one line, because laster if i wanted to go and add something in the if, i could add it easily, where as if i didn't have teh brackets, i might just add it under that line and forget to add the brackets, and it would always get processed because its not in the if statment, if that made any sense!
yeah, i like using the brackets as well. Much more cleaner and easir to read.
The way he was connecting jsut caught my eye because it was different!
And my suggestions to furanku, do as jshpro2 said. Read over some basic php tutorials, and get pretty comfortable with PHP. Then look into some PostgreSQL + PHP tutorials. That is if you are even using postgre.
thanks for the help but the mesage that has to be echo is the avg temp, not the day of week. When the chech box is clicked and submited on the html, it lookes at the table, finds the colum for that day and echo the avg temp.
//for mysql, no clue about postgre....
if ($_POST['sunday']){
$res = mysql_query("SELECT AVG(sun) as avgSun FROM temp");
$row = mysql_fetch_array($res);
echo $row['avgSun'];
}
But what might be the best way, is like have check boxes all the same name, but different values. And the values be like the field in your database, then you would only have to do one if:
if (isset($_POST['day'])){
$day = $_POST['day'];
echo $day ."<br>"; //for testing purposes to make sure you have right day
$sql = "SELECT AVG($day) as avgDay FROM temp";
echo $sql ."<br>"; // for testing to make sure your query is correct
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
echo $row['avgDay']; //should be the AVG($day)
}
Of course all this will only work with mysql, so for Postgre you'll probably have to change only the function names