Page 1 of 2

which day is typed into text box

Posted: Wed Mar 31, 2004 11:12 pm
by furanku
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 :cry:

Posted: Thu Apr 01, 2004 1:45 am
by twigletmac
Have you made a start with some code we can see?

Mac

reply to Mac

Posted: Thu Apr 01, 2004 5:02 am
by furanku
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

Posted: Thu Apr 01, 2004 6:11 am
by Illusionist
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.

furanku

Posted: Thu Apr 01, 2004 4:03 pm
by furanku
Just to let you know, I know about 8% about php coding. Hints don't help me at all. Thanks for answering thou.[/php_man]

Posted: Thu Apr 01, 2004 4:14 pm
by josh
You should learn a php / mysql tutorial... heres an excellent one http://www.vtwebwizard.com/tutorials/mysql/index.php start from step one

update with code

Posted: Thu Apr 01, 2004 5:47 pm
by furanku
Can you see any problems with this and how can I improve it.

<?
$dbh=pg_connect("dbname=tempdb port=5432 user=man password=man123")

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);
?>

Posted: Thu Apr 01, 2004 5:52 pm
by tim
your if statements are incorrect

Code: Select all

<?php
if ($sunday) 
echo ($dbh, "Select avg(sun) from temp); 
if ($monday) 
// etc

change

if ($_POST['sunday']) {
echo "$dbh, "Select avg(sun) from temp"; 
} elsef ($_POST['monday']) {
echo "its monday";
}
?>
so forth - you must begin an if statement with { and terminate it with a }, and elseif is a better suite for the task set-up you have.

edit - also, your SQL connections are incorrect, you cannot echo that, well u can, but...

Code: Select all

<?php
if ($_POST['sunday']) {
$sql = mysql_query("SELECT avg(sun) FROM temp", $dbh);
}
?>

Posted: Thu Apr 01, 2004 6:34 pm
by Illusionist
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!

Posted: Thu Apr 01, 2004 6:36 pm
by Illusionist
i dont think he is using mysql though... He is using, by the way he is connecting, PostgreSQL.

Posted: Thu Apr 01, 2004 6:38 pm
by tim
Illusionist wrote:i dont think he is using mysql though... He is using, by the way he is connecting, PostgreSQL.

oooooh, I didnt even notice. Good eye ill, and I didnt know about the { } not needed.

Even if they arent, i find it much cleaner/more easy to read with them present. Like u said as well :)

Posted: Thu Apr 01, 2004 6:45 pm
by Illusionist
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.

furanku

Posted: Thu Apr 01, 2004 7:40 pm
by furanku
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. :?:

Posted: Thu Apr 01, 2004 8:00 pm
by tim
if ($_POST['sunday']) {
$sql = mysql_query("SELECT avg(sun) FROM temp", $dbh);
}


?????

echo $sql;

and translate to porte, if thats what your using :wink:

Posted: Thu Apr 01, 2004 9:23 pm
by Illusionist
tim that wouldn't work either, You'd end up with something like Resource ID #2 or something. I would do it like this:

Code: Select all

//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:

Code: Select all

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