PDA

View Full Version : Is PHP the way to do this?


dots and spots Jeff
12th September 2009, 21:13
I'm after a little advice, a point in the right direction maybe, or even someone to say don't go down that route - it won't work!

What I want is for a visitor to a website to be able to enter a number into a form and click submit. Behind the scenes, I then want this number added to a variable and another variable increased by one, so basically I keep adding up all the numbers that the visitors enter and I also know how many visitors have submitted an entry.

I know how I could do this with each entry being emailed to me, (using a form to email script) but then I'd have to do the maths and add up all the entries - I just wonder if I could automate the whole process.

Its only a bit of fun for a little project I'm doing - I'm happy to try & teach myself the necessary skills to do this if anyone could point me in the right direction. I suspect that PHP might be the answer - but alas I'm not proficient in this language, although I have a good book and am trying to learn!

Thanks

Jeff

edmondscommerce
12th September 2009, 22:40
ok no idea why you would want this:


<?php
if(!empty($_POST)){
$inc=file_get_contents('inc.txt');
$var=file_get_contents('var.txt');
$num = (int)$_POST['num'];
$inc++;
$var+=$num;
file_put_contents('inc.txt',$inc);
file_put_contents('var.txt', $var);
}
?>
<form method="post">
<input name="num">
<input type="submit">
</form>

dots and spots Jeff
13th September 2009, 07:38
Joseph

Many thanks for this - I'll have a play with it.

I watched Derren Brown's 'How he predicted the lottery' on Friday night - he claimed he used the 'wisdom of the crowd' to predict the results. This wouldn't work (for predicting the lottery) and I think he used some TV trickery, but it could work some other things.

For example, one of the earliest recorded examples was when a showman ran a competition to guess the weight of a cow. Whilst the guesses varied wildly, when he added up all the entries and averaged them out they came to the exact weight of the cow.

I was wondering if I could sort of recreate that experiment: Have a webpage with an image on it made up from, say, loads of dots and have the visitor guess the amount of dots in the image. Then I'd see how accurate the wisdom of the crowd was.

No real purpose to it, other than it piques my interest and also its a useful vehicle to help me learn more about PHP. I suppose 30 years ago I'd have spent my leisure time in the shed building model steam engines, mucking about with computers is the modern equivalent! (Much warmer, too!)

Thanks again for your help - if I do get the project up and running I'll find somewhere on the page to give you credit for your help.

Jeff

EmployEasily HR Services
13th September 2009, 07:53
Sounds interesting! Be sure to post an update

edmondscommerce
13th September 2009, 20:36
happy to help

yes a link back would be nice thanks :)

ozbon
14th September 2009, 11:57
You could, of course, just use a database to do this. It's possibly overkill, but you'd then be able to also do maths stuff on the totals over time as well.

So your basic thing would be to add a row in the database for each guess/number, something, and then be able to churn out records etc. in the database.

Each line in the db table would be an id number (using an auto_increment column) and then an int column for the information. Getting fancy, you could also add in a date/time, if you eventually wanted to break down the results by when they were input.

I know I could write something like this in about five minutes, if you're interested...

dots and spots Jeff
14th September 2009, 15:52
Thanks Ozbon, I hadn't thought of using a database - possibly because I don't know alot about them, something else for me to read up about! - but yes it probably would work.

Its only a little hobby project, so I couldn't pay you for anything ...!

Thanks for your help - as with Joseph, if I do get the project up & running, and regardless of what method I use, I'll find somewhere to give you credit for your help.

Thanks

Jeff

ozbon
14th September 2009, 19:56
No worries re payment - that wasn't why I suggested it!

Basically- and I assume you're using a MySQL database alongside the PHP, you're doing something like...
CREATE TABLE `number_suggestions` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`suggestion` INT NOT NULL ,
`suggestion_date` DATETIME NOT NULL
) ENGINE = MYISAM


Your PHP for the suggestion would look something like...

<?php
$action = @$_POST["action"];
if ($action=="input") {
$suggestion = $_POST["suggestion"];
$db = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$add_record = "INSERT INTO number_suggestions SET id=NULL, suggestion=$suggestion, suggestion_date='" . gmdate("Y-m-d H:i:s") . "'";
$xyz = mysql_query($add_record,$db);
}
?>
<html>
<body>
<form name='add_suggestion' method='post' action='<?= $_SERVER["PHP_SELF"] ?>'>
<input type='hidden' name='action' value='input'/>
<input type='text' name='suggestion' size='10'/>
<input type='submit' name='submit' value='Add Suggestion'/>
</form>
</body>
</html>



And that should add the records in to the database we set up in the first bit.

Hope that helps - but feel free to drop me a line if it doesn't, or you've got any questions about it.

dots and spots Jeff
14th September 2009, 20:38
No worries re payment - that wasn't why I suggested it!



I didn't think you did - but I thought I'd better check, hope I've not caused offence!

I'll have a play with this - as I said in an earlier post, its a much about the means as the ends: I've now got a little project to play around with and hopefully learn something along the way.

Thanks again,

Jeff

ozbon
15th September 2009, 07:54
Nah, you'd have to do a lot more than that before I took offence, don't worry. :-)

ben_smith
15th September 2009, 08:56
PHP code snipped


I wouldn't use that. If you enter '1, suggestion_date = NOW(); DROP TABLE number_suggestions; --' into the input box, your database will now be empty.

You should validate that the suggestion is a number before trying to insert it into the database.

Unfortunately, I can't see to post PHP code because I have less than 15 posts on this forum.

ozbon
15th September 2009, 09:22
Agreed - but as explained, that was a basic proof-of-concept and example for a micro-project.

In the real-world - and admittedly, I forgot to add the normal 'don't do this, it's an example' caveat I normally shove in on these things - you should always make sure you validate input. I simply omitted it for simplicity. My bad, and apologies.