Feed Horse Script

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!

Moderator: General Moderators

Post Reply
carleihar
Forum Commoner
Posts: 36
Joined: Fri Nov 13, 2009 5:59 pm

Feed Horse Script

Post by carleihar »

I've been working on this little script for a couple days and it is driving me CRAZY!
I got it working, and then I wanted to change it around a bit and now it's a complete mess. The script allows the logged in user to "feed" the horses based on a database. I would like each horse to have it's individual "feed number" (0-10 based on how hungry it is). So I would like a link that feeds all the horses at once and individual links that feed each horse seperatly. The problem is, I would like the hunger to decrease as days go on, so if you don't feed your horse for 3 days, it will get hungrier. Any help?

The function to change the hunger based on the day:

Code: Select all

 
function getHunger($lastfed, $horseid, $hung, $to_day, $datechanged) {
 
    
    $date1=$lastfed;
    $date2=$to_day;
 
    $difference = dateDiff("-", $date2, $date1);
    
    if ($datechanged != $to_day) {
        
        $hung = $hung - $difference;
    
        if ($hung > 10) {
            $hung = 10;
        }
    
        if ($hung < 0) {
            $hung = 0;
        }
        
            
        $t = "UPDATE hunger SET hunger='$hung', date_changed='$to_day' WHERE user_name='{$_COOKIE['username']}' AND horse_id='$horseid'";
        $p = @mysqli_query($GLOBALS['dbc'], $t);
        
    }
 
 
}//end function
 

Main code:

Code: Select all

<?php
 
 
include('includes/header01.html');
 
 
$q = "SELECT horse_name, horse_id, date_fed, hunger, date_changed FROM hunger WHERE user_name='{$_COOKIE['username']}'";           
$r = @mysqli_query ($dbc, $q); // Run the query.
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    $date_fed=$row['date_fed'];
    $horse_id=$row['horse_id'];
    $horse_name=$row['horse_name'];
    $hunger=$row['hunger'];
    $date_changed=$row['date_changed'];
    
} //end while
 
 
/**********************Find Hay Price**********************/
$today = date("Y-m-d"); //today's date
 
$d = "SELECT date, hayprice FROM worldvariables WHERE date='$today'"; //get the hay price for today
$o = @mysqli_query ($dbc, $d); // Run the query.
 
if (mysqli_num_rows($o) == 1) { //if it runs okay
 
    while ($row = mysqli_fetch_array($o, MYSQLI_ASSOC)) {
 
        $hayprice = $row['hayprice'];
 
    } //end while
 
} else {
 
    echo 'Sorry, there was an error finding today\'s date!';
 
}
 
echo 'Today\'s Hay Price is: ';
 
echo $hayprice;
 
echo '<br /><br /><br />';
/**********************Find Hay Price**********************/
 
/**********************Change Hunger**********************/
 
$q = "SELECT horse_name, horse_id, date_fed, hunger, date_changed FROM hunger WHERE user_name='{$_COOKIE['username']}'";           
$r = @mysqli_query ($dbc, $q); // Run the query.
 
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
 
 
    $date_fed=$row['date_fed'];
    $horse_id=$row['horse_id'];
    $horse_name=$row['horse_name'];
    $hunger=$row['hunger'];
    $date_changed=$row['date_changed'];
    
    getHunger($date_fed, $horse_id, $hunger, $today, $date_changed);
 
}
 
/**********************Change Hunger**********************/
 
 
/**********************If Button Was Clicked**********************/
               
if(isset($_GET['submit'])){
       
    $q = "SELECT horse_name, horse_id, date_fed, hunger, date_changed FROM hunger WHERE user_name='{$_COOKIE['username']}'";           
$r = @mysqli_query ($dbc, $q); // Run the query.
 
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
 
 
    $date_fed=$row['date_fed'];
    $horse_id=$row['horse_id'];
    $horse_name=$row['horse_name'];
    $hunger=$row['hunger'];
    $date_changed=$row['date_changed'];
    $hunger = $hunger + 1;
    
    if ($hunger > 10) {
        $hunger = 10;
    }
    
    if ($hunger < 0) {
        $hunger = 0;
    }
    
    $t = "UPDATE hunger SET date_fed='$today', hunger='$hunger' WHERE user_name='{$_COOKIE['username']}' AND horse_id='$horse_id'";
    $p = @mysqli_query($dbc, $t);
       
    } //end while loop
    
} //end submit get
 
/**********************If Button Was Clicked**********************/
 
 
/**********************If Link Was Clicked**********************/
 
if(isset($_GET['id'])){
 
    $feedid = $_GET['id'];
    
    $q = "SELECT horse_name, horse_id, date_fed, hunger, date_changed FROM hunger WHERE user_name='{$_COOKIE['username']}'";           
$r = @mysqli_query ($dbc, $q); // Run the query.
 
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
 
 
    $date_fed=$row['date_fed'];
    $horse_id=$row['horse_id'];
    $horse_name=$row['horse_name'];
    $hunger=$row['hunger'];
    $date_changed=$row['date_changed'];             
    $hunger = $row['hunger'];
           
    $hunger = $hunger + 1;
    
    if ($hunger > 10) {
        $hunger = 10;
    }
    
    if ($hunger < 0) {
        $hunger = 0;
    }
    
    $t = "UPDATE hunger SET date_fed='$today', hunger='$hunger' WHERE user_name='{$_COOKIE['username']}' AND horse_id='$feedid'";
    $p = @mysqli_query($dbc, $t);
       
    } //end while loop
    
} //end submit get
 
/**********************If Link Was Clicked**********************/
 
 
/*****************************Page***************************/
echo'<table width="400" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>Horse Name</td>
    <td>Hunger</td>
    <td>Date Last Fed</td>
  </tr>';
 
$q = "SELECT horse_name, horse_id, date_fed, hunger, date_changed FROM hunger WHERE user_name='{$_COOKIE['username']}'";           
$r = @mysqli_query ($dbc, $q); // Run the query.
 
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
 
 
    $date_fed=$row['date_fed'];
    $horse_id=$row['horse_id'];
    $horse_name=$row['horse_name'];
    $hunger=$row['hunger'];
    $date_changed=$row['date_changed'];
            
    echo '<tr>
        <td>' . $horse_name . '</td>
        <td>' . $hunger . '</td>';
        echo "<td>'<a href=\"feedhorses.php?id=" . $horse_id . "\">Feed</a>'
      </tr>";
 
}   
 
 
echo'</table>';        
               
echo '<form action="" method="GET">';  
echo '<input name="submit" type="submit" value="Feed Horses"><br />';  
echo '</form>';  
/*****************************Page***************************/
 
 
include('includes/footer01.html');
 
?>

If you could shed some light on me, that would be great! Thanks!
limitdesigns
Forum Commoner
Posts: 25
Joined: Sat Feb 06, 2010 9:05 pm

Re: Feed Horse Script

Post by limitdesigns »

I think you can entirely avoid that function to change the hunger of your horses. All you have to do is add a timestamp to the horse's row in the database that says when someone last fed him. Then, you can dynamically get how hungry the horse is just by doing getting the date difference between the current time and the timestamp in the database. Something like:

Code: Select all

 
$horse = {the id of the horse you want to check on};
$array = mysql_fetch_array(mysql_query("SELECT `last_fed` FROM `hunger` WHERE `horse_id` = '$horse'"));
$last_fed = $array['last_fed'];
$diff = time() - strtotime($last_fed); // This would be the difference in seconds, so let's convert to days
$day_diff = $diff / (60*60*24);
 
...and then you can do different things based on the horse's hunger. I feel like that's a lot easier than what you were trying to do, because your method requires updating the database every so often to change how hungry the horse is.
carleihar
Forum Commoner
Posts: 36
Joined: Fri Nov 13, 2009 5:59 pm

Re: Feed Horse Script

Post by carleihar »

Well thats sort of what I'm doing. The problem is, people can "feed" the horse and then the hunger number changes.
limitdesigns
Forum Commoner
Posts: 25
Joined: Sat Feb 06, 2010 9:05 pm

Re: Feed Horse Script

Post by limitdesigns »

Maybe I read your code wrong, but it looks to me like you have a function that goes in and changes the value of "hunger" in the database based on what day it is. It would be a lot easier if you avoid having to do that by getting the actual hunger value outside of the database, and just store the time that the horse was last fed within the database. When someone feeds the horse, just update the timestamp.
Post Reply