Hejsa,
Jeg ønsker at brugeren kun kan "plante" sin plante 10 gange om dagen. Hvordan gør jeg dette. Har sat timestamp på hver enkel. Men hvordan tjekker jeg om han har "Plantet" 10 gange i dag?
Vil det være bedre at lave en dato på, også tjekker nuværende dato og tælle rows?
Har følgende der definere hvilket stadie og antal gange om dagen:
- $this->states[1] = 0;
- $this->states[2] = 10;
- $this->states[3] = 25;
- $this->states[4] = 50;
- $this->states[5] = 100;
Her er min kode:
- <?php
- include("databasehandler.php");
- include("log.php");
-
- class Plants {
- public $daySeconds = 86400;
- public $states = array();
- public $user = 0;
-
- public function __construct() {
- $this->states[1] = 0;
- $this->states[2] = 10;
- $this->states[3] = 25;
- $this->states[4] = 50;
- $this->states[5] = 100;
-
- $this->user = 1;
- }
-
- public function DeleteAndUpgrade() {}
- public function giveWater() {
- $o = DatabaseHandler::get('id, state', 'plants', array("owner" => $this->user), 'q');
- $id = mysql_fetch_assoc($o); $pid = $id['id']; $state=$id['state'];
-
- $this->plantId = $pid;
- $this->currentState = $state;
-
- if ( $this->currentState >= 5) :
- echo "Du har ikke opgradere til mere end stadie 5!"; exit;
- endif;
-
- $o = mysql_query("select tid, owner, plantid FROM watered WHERE owner = '".$this->user."' AND plantid = '".$this->plantId."' AND state = '".$this->currentState."' ORDER BY id DESC LIMIT 0,1");
- $l=mysql_fetch_assoc($o);
-
-
- mysql_query("insert into watered (owner, plantid, tid, state) values ('".$this->user."','".$this->plantId."','".time()."','".$this->currentState."')");
-
- $antalGangeVandet = mysql_query("select id FROM watered where owner = '".$this->user."' AND plantid = '".$this->plantId."' AND state = '".$this->currentState."'");
- $antalGangeVandet = mysql_num_rows($antalGangeVandet);
-
- if ( $antalGangeVandet == $this->states[$this->currentState+1]):
- mysql_query("update watered SET state = state+1 WHERE owner = '".$this->user."' AND plantid = '".$this->plantId."'") or die ( mysql_error());
- mysql_query("update plants SET state = state+1 WHERE owner = '".$this->user."' AND id = '".$this->plantId."'") or die ( mysql_error());
-
- echo "Opgraderde fra ".$this->currentState." til ".intval($this->currentState+1);
-
- $this->currentState = intval($this->currentState+1);
- $this->DeleteAndUpgrade();
- endif;
- }
- public function makePlant() {
- $o = DatabaseHandler::get('id', 'plants', array("owner" => $this->user), '');
- if ( mysql_num_rows($o) == 0 ) :
- DatabaseHandler::insert(array("owner" => $this->user, "state" => 1, "type" => mt_rand(1,3)), 'plants');
- endif;
- }
- }
-
- $C = new DatabaseHandler();
- $Plant = new Plants;
-
- if ( isset ( $_POST['o'] )) :
- $Plant->makePlant();
- $Plant->giveWater();
- endif;
-
-
- ?>
-
- <form action="" method="post">
- <input type="submit" value="Opret plante" name="o" />
Indlæg senest redigeret d. 03.10.2012 17:53 af Bruger #9820