Pi_Mastuh Posted September 17, 2006 Share Posted September 17, 2006 I'm making a script that when it's been over 24 hours since the pet was fed it increases it's hunger and displays it on the screen. I have the part that increases it working, but it's displaying the hunger for a different pet then the one they clicked on. here's the code:[code]<?session_start();$session=session_id( );include ("secure/config.php");include ("secure/petDetails2.php");?><? $monopetID = $_POST['monopetID'];$query = "SELECT * FROM chibifriendspets WHERE monopetID = '$monopetID'"; $result = mysql_query($SQL,$connection); $query_data = mysql_fetch_array($result); $lastDatefed = $query_data['lastDatefed']; $hunger = $query_data['hunger'];$timespan = strtotime("$today - $lastDatefed");$hours = strtotime("1 day");if ($datelastfed < date("Y-m-d", strtotime("-1 day"))) {; if($hunger < 9) { $hunger++; // or whatever you want to do mysql_query("UPDATE chibifriendspets SET hunger = '$hunger' WHERE monopetID = '$monopetID'"); } }?><html><head><title>My Pet, <? print $monopetName; ?></title></head><body><BR><table width="751" border="0" cellspacing="0" cellpadding="0"> <tr> <td colspan="2" height="347" valign="top"> <table width="200" border="1" cellspacing="0" cellpadding="0" bgcolor="blue" bordercolor="#000066"> <tr> <td align=center> <table width="200" border="0" cellspacing="0" cellpadding="0"> <tr align=center bgcolor="blue"> <td align=center bgcolor="blue"> <div align="center"><font face=Arial, Helvetica, sans-serif size=3 color="#FFFFFF"><b> <? echo $monopetName; ?> </b></font></div> </td> </tr> <tr bgcolor="#FFFFFF" align=center> <? print "<td width=35 align=center><div align='center'><img src=$monopetSpecies.jpg align=center></div></td>"; ?> </tr> </table> </td> </tr> </table><BR> <table width="205" border="1" cellspacing="0" cellpadding="0" bordercolor="#000066"> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%"> <tr bgcolor="blue"> <td height="10" colspan="2"><div align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#FFFFFF"><b>Pet Vitals</b></font></div></td> </tr> <tr> <td width="99" height="10"><font face="Arial, Helvetica, sans-serif" size="1"> Health:</font></td> <td width="101" height="10"><font face="Arial, Helvetica, sans-serif" size="1"> 100% </font></td> </tr> <tr> <td height="19"><font face="Arial, Helvetica, sans-serif" size="1"> Pet Status:</font></td> <td height="19"><font face="Arial, Helvetica, sans-serif" size="1">Happy</font></td> </tr> <tr> <td width="99" height="19"><font face="Arial, Helvetica, sans-serif" size="1">Hunger: </font></td> <td width="101" height="19"><font face="Arial, Helvetica, sans-serif" size="1"> <? if ($hunger == "0") { print "Stuffed"; } elseif ($hunger == "1") { print "Full"; } elseif ($hunger == "2") { print "Satiated"; } elseif ($hunger == "3") { print "Not Hungry"; } elseif ($hunger == "4") { print "Hungry"; } elseif ($hunger == "5") { print "Very Hungry"; } elseif ($hunger == "6") { print "Famished"; } elseif ($hunger == "7") { print "Starving"; } elseif ($hunger == "8") { print "Ravenous"; } else { print "Dying"; } ?> </font></td> </tr> </table></td> </tr> </table> <BR> <div align="center"></div> </td> <td width="10" height="347"> </td> <td width="505" height="347" valign="top" align="center"> <table width="500" border="1" cellspacing="0" cellpadding="0" bordercolor="#000066"> <tr> <td height="111"> <table width="500" border="0" cellspacing="0" cellpadding="0" bordercolor="#000066"> <tr> <td colspan="2" bgcolor="blue"> <div align="center"><font face="Arial, Helvetica, sans-serif" size="3" color="#FFFFFF"><b>Pet Details</b></font></div></td> </tr> <tr> <td width="150"><font face="Arial, Helvetica, sans-serif" size="2"> Name:</font></td> <td width="302"><font face="Arial, Helvetica, sans-serif" size="2"> <? echo $monopetName;?> </font></td> </tr> <tr> <td width="150" bgcolor="ebebeb"><font face="Arial, Helvetica, sans-serif" size="2"> Species:</font></td> <td width="302" bgcolor="ebebeb"><font face="Arial, Helvetica, sans-serif" size="2"> <? echo $monopetSpecies;?> </font></td> </tr> <tr> <td width="150"><font face="Arial, Helvetica, sans-serif" size="2"> Owned Since:</font></td> <td width="302"><font face="Arial, Helvetica, sans-serif" size="2"> <? echo $monopetDOB;?> </font></td> </tr> <tr bgcolor="#ebebeb"> <td><font size="2" face="Arial, Helvetica, sans-serif"> Pet Level:</font></td> <td><font face="Arial, Helvetica, sans-serif" size="2"> <? echo $monopetLevel;?> </font></td> </tr> <tr> <td><font size="2" face="Arial, Helvetica, sans-serif"> Pet Points:</font></td> <td><font face="Arial, Helvetica, sans-serif" size="2"> <? echo $monopetPoints;?> </font></td> </tr> </table> </td> </tr> </table> <br> <br> </td> </tr></table><table width="700" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <div align="center"> </div> </td> </tr> <tr> <td> <div align="center"><a href="javascript:window.close();">Close This Window</a> </div> </td> </tr></table><p> </p></html>[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 17, 2006 Share Posted September 17, 2006 you need to add the id of the pets id on the link ok.?petid=$petid Quote Link to comment Share on other sites More sharing options...
shocker-z Posted September 17, 2006 Share Posted September 17, 2006 also somthing i noticed is that$query = "SELECT * FROM chibifriendspets WHERE monopetID = '$monopetID'"; $result = mysql_query($SQL,$connection);$SQL doesn't exist and $query isn't used in your code anywhere, should it be$query = "SELECT * FROM chibifriendspets WHERE monopetID = '$monopetID'"; $result = mysql_query($query ,$connection);??RegardsLiam Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 17, 2006 Share Posted September 17, 2006 Why are you starting ANOTHER new thread about the same problem?With a new thread, NOBODY understands all the work that's gone before. It seems that you're still forgetting to pass or acquire variables needed for your queries. Add error trapping. Add disply of querystrings. Check your own logic first. Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted September 17, 2006 Author Share Posted September 17, 2006 [quote author=shocker-z link=topic=108390.msg436026#msg436026 date=1158503623]also somthing i noticed is that$query = "SELECT * FROM chibifriendspets WHERE monopetID = '$monopetID'"; $result = mysql_query($SQL,$connection);$SQL doesn't exist and $query isn't used in your code anywhere, should it be$query = "SELECT * FROM chibifriendspets WHERE monopetID = '$monopetID'"; $result = mysql_query($query ,$connection);??RegardsLiam[/quote]Thanks, now the only thing that's not working is the part where it increases the hunger if you havn't fed them for 24 hours. The lastDatefed for the pet I'm testing is today, but it's still going up when it shouldn't be. I'm trying to make it add the ammount of days the pet hasn't been fed to the hunger, unless you fed it today. So if you havn't fed it for a week it adds 7 to the hunger. However all it's doing is changing the hunger to 9.Does this look right? I don't see what's wrong.[code]$query = "SELECT * FROM chibifriendspets WHERE monopetID = '$monopetID'"; $result = mysql_query($query ,$connection); $query_data = mysql_fetch_array($result); $lastDatefed = $query_data['lastDatefed']; $hunger = $query_data['hunger']; $today = strtotime("today");$timespan =((($today - $lastDatefed)/60)/60)/24;$hunger2 = $timespan; if ($lastDatefed = today) { $hunger2 = $hunger; } elseif ($hunger2 > 9) { $hunger2 = 9; } else { $hunger2 = $timespan; } $query = "UPDATE chibifriendspets SET hunger = '$hunger2' WHERE monopetID = '$monopetID'"; $result = mysql_query($query ,$connection); [/code] Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted September 18, 2006 Author Share Posted September 18, 2006 Does anyone know? I just have no idea. Now it's making it always 0 and not changing it. Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 18, 2006 Share Posted September 18, 2006 [quote author=Pi_Mastuh link=topic=108390.msg436298#msg436298 date=1158541781]Does anyone know? I just have no idea. Now it's making it always 0 and not changing it.[/quote]So the code that you're running now is different from the code any unsuspecting reader of this thread will see? How is anyone supposed to help you straighten it out if you keep changing the code and reporting different performance without anyone knowing what the code is?By the way, code like if ($lastDatefed = today) should surely be if ($lastDatefed = [b]$[/b]today)Have you tried echoing the value of your variable named $timestamp to see if it's what you're expecting? Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted September 18, 2006 Author Share Posted September 18, 2006 I updated the code above so it's the most recent. I'll fix the today variable and echo it and post what happens. Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted September 18, 2006 Author Share Posted September 18, 2006 It still is set to 0 and won't change, i echoed $timespan and got this:13409.08568287 Which is very wrong. I'm trying to get the ammount of days between today and the lastdatefed, how do I do that? Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 18, 2006 Share Posted September 18, 2006 Just noticed. That change - since it's a comparison - [b]must[/b] be if ($lastDatefed [b]==[/b] $today)And now you KNOW that the calculation of $timespan is wrong, surely that suggests what sort of changes you need to make. Check the php manual to see how to get dates/times in the same format and so on.What is the exact format of $lastdatefed? Isn't it yyyy-mm-dd? Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted September 18, 2006 Author Share Posted September 18, 2006 Y-m-d H:i:mso yyyy-mm-dd hh:mm:ssWhat's the link to the PHP manual again? Quote Link to comment Share on other sites More sharing options...
Wintergreen Posted September 18, 2006 Share Posted September 18, 2006 www.php.netUseful functions would be strtotime(), date() and mktime() Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted September 18, 2006 Author Share Posted September 18, 2006 Which would i use to compare 2 dates? Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 18, 2006 Share Posted September 18, 2006 Here's a little script that should show you how to work with times/dates the way you need to:[code]<?php$datelastfed = "2006-09-12 02:00:00"; // for example$then = strtotime($datelastfed); // in seconds since the start of time$right_now = time(); // in seconds since the start of time$timesincefed = $right_now - $then;$dayssincefed = number_format($timesincefed/86400,2);echo "Date last fed ". $datelastfed. "<br/>Date right now ". date("Y-m-d H:i:s"). "<br/>And the difference is ". $dayssincefed. " days";?>[/code] Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted September 18, 2006 Author Share Posted September 18, 2006 I added a round function and added the variable to get the last date fed from the database and it's just putting it to 0 and not showing the lastDatefed.[code]$datelastfed = $lastDatefed; // for example$then = strtotime($datelastfed); // in seconds since the start of time$right_now = time(); // in seconds since the start of time$timesincefed = $right_now - $then;$dayssincefed = number_format($timesincefed/86400,2);$dayssincefed2 = round($dayssincefed);echo "Date last fed ". $datelastfed. "<br/>Date right now ". date("Y-m-d H:i:s"). "<br/>And the difference is ". $dayssincefed2. " days";[/code] Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 18, 2006 Share Posted September 18, 2006 My example shows the correct number of days between a yyyy-mm-dd h:i:s formatted date and today's date. What does your example show? Does the show the number of days you expect? If so, we both know that piece of code is working properly, which makes me think that some of the logic you're using in the code beyond isn't right. Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted September 18, 2006 Author Share Posted September 18, 2006 Here's my entire code. I dont think theres anything wrong with it beyond the time piece of it, because it worked until I changed your example time to my variable $lastDatefed, which is the same format.It says Date last fed -1Date right now 2006-09-18 16:52:21And the difference is 13 days and changes their hunger to 13[code]<?session_start();$session=session_id( );include ("secure/config.php");include ("secure/dbinfo.php");include ("secure/petDetails2.php");?><? $monopetID = $_POST['monopetID'];$query = "SELECT * FROM chibifriendspets WHERE monopetID = '$monopetID'"; $result = mysql_query($query ,$connection); $query_data = mysql_fetch_array($result); $lastDatefed = $query_data['lastDatefed']; $hunger = $query_data['hunger'];$datelastfed = strtotime($lastDatefed); // for example$then = strtotime($datelastfed); // in seconds since the start of time$right_now = time(); // in seconds since the start of time$timesincefed = $right_now - $then;$dayssincefed = number_format($timesincefed/86400,2);$dayssincefed2 = round($dayssincefed);echo "Date last fed ". $then. "<br/>Date right now ". date("Y-m-d H:i:s"). "<br/>And the difference is ". $dayssincefed2. " days"; if ($lastDatefed == $right_now) { $hunger2 = $hunger; } elseif ($hunger2 > 9) { $hunger2 = 9; } else { $hunger2 = $dayssincefed2; } $query = "UPDATE chibifriendspets SET hunger = '$hunger2' WHERE monopetID = '$monopetID'"; $result = mysql_query($query ,$connection); ?><html><head><title>My Pet, <? print $monopetName; ?></title></head><body style="text-align: center"><BR><table width="751" border="0" cellspacing="0" cellpadding="0"> <tr> <td colspan="2" height="347" valign="top"> <table width="200" border="1" cellspacing="0" cellpadding="0" bgcolor="blue" bordercolor="#000066"> <tr> <td align=center> <table width="200" border="0" cellspacing="0" cellpadding="0"> <tr align=center bgcolor="blue"> <td align=center bgcolor="blue"> <div align="center"><font face=Arial, Helvetica, sans-serif size=3 color="#FFFFFF"><b> <? echo $monopetName; ?> </b></font></div> </td> </tr> <tr bgcolor="#FFFFFF" align=center> <? print "<td width=35 align=center><div align='center'><img src=$monopetSpecies.jpg align=center></div></td>"; ?> </tr> </table> </td> </tr> </table> <p align="center"><BR> </p> <table width="205" border="1" cellspacing="0" cellpadding="0" bordercolor="#000066"> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%"> <tr bgcolor="blue"> <td height="10" colspan="2"><div align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#FFFFFF"><b>Pet Vitals</b></font></div></td> </tr> <tr> <td width="99" height="10"> <p align="center"><font face="Arial, Helvetica, sans-serif" size="1"> Health:</font></td> <td width="101" height="10"> <p align="center"><font face="Arial, Helvetica, sans-serif" size="1"> 100% </font></td> </tr> <tr> <td height="19"> <p align="center"><font face="Arial, Helvetica, sans-serif" size="1"> Pet Status:</font></td> <td height="19"> <p align="center"><font face="Arial, Helvetica, sans-serif" size="1"> Happy</font></td> </tr> <tr> <td width="99" height="19"> <p align="center"><font face="Arial, Helvetica, sans-serif" size="1"> Hunger: </font></td> <td width="101" height="19"> <p align="center"><font face="Arial, Helvetica, sans-serif" size="1"> <? if ($hunger2 == "0") { echo "Stuffed"; } elseif ($hunger2 == "1") { echo "Full"; } elseif ($hunger2 == "2") { echo "Satiated"; } elseif ($hunger2 == "3") { echo "Not Hungry"; } elseif ($hunger2 == "4") { echo "<font color=red><b>Hungry</b></font>"; } elseif ($hunger2 == "5") { echo "<font color=red><b>Very Hungry</b></font>"; } elseif ($hunger2 == "6") { echo "<font color=red><b>Famished</b></font>"; } elseif ($hunger2 == "7") { echo "<font color=red><b>Starving</b></font>"; } elseif ($hunger2 == "8") { echo "<font color=red><b>Ravenous</b></font>"; } else { echo "<font color=red><b>Dying</b></font>"; } ?> </font></td> </tr> </table></td> </tr> </table> <p align="center"> <BR> </p> <div align="center"></div> </td> <td width="10" height="347"> <p align="center"></td> <td width="505" height="347" valign="top" align="center"> <table width="500" border="1" cellspacing="0" cellpadding="0" bordercolor="#000066"> <tr> <td height="111"> <table width="500" border="0" cellspacing="0" cellpadding="0" bordercolor="#000066" height="108"> <tr> <td colspan="2" bgcolor="blue" height="28"> <div align="center"><font face="Arial, Helvetica, sans-serif" size="3" color="#FFFFFF"><b>Pet Details</b></font></div></td> </tr> <tr> <td width="150" height="16"> <p align="center"><font face="Arial, Helvetica, sans-serif" size="2"> Name:</font></td> <td width="302" height="16"><font face="Arial, Helvetica, sans-serif" size="2"> <? echo $monopetName;?> </font></td> </tr> <tr> <td width="150" bgcolor="ebebeb" height="16"> <p align="center"><font face="Arial, Helvetica, sans-serif" size="2"> Species:</font></td> <td width="302" bgcolor="ebebeb" height="16"><font face="Arial, Helvetica, sans-serif" size="2"> <? echo $monopetSpecies;?> </font></td> </tr> <tr> <td width="150" height="16"> <p align="center"><font face="Arial, Helvetica, sans-serif" size="2"> Owned Since:</font></td> <td width="302" height="16"><font face="Arial, Helvetica, sans-serif" size="2"> <? echo $monopetDOB;?> </font></td> </tr> <tr bgcolor="#ebebeb"> <td height="16"> <p align="center"><font size="2" face="Arial, Helvetica, sans-serif"> Pet Level:</font></td> <td height="16"><font face="Arial, Helvetica, sans-serif" size="2"> <? echo $monopetLevel;?> </font></td> </tr> <tr> <td height="16"> <p align="center"><font size="2" face="Arial, Helvetica, sans-serif"> Intelligence:</font></td> <td height="16"><font face="Arial, Helvetica, sans-serif" size="2"> <? echo $monopetPoints;?> </font></td> </tr> </table> </td> </tr> </table> <br> <br> </td> </tr></table><table width="700" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <div align="center"> </div> </td> </tr> <tr> <td> <div align="center"><a href="javascript:window.close();">Close This Window</a> </div> </td> </tr></table><p> </p></html>[/code] Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted September 18, 2006 Author Share Posted September 18, 2006 I've gone over it again and again and i dont see what's wrong. ??? Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 18, 2006 Share Posted September 18, 2006 [quote author=Pi_Mastuh link=topic=108390.msg436777#msg436777 date=1158611993].. until I changed your example time to my variable $lastDatefed, which is the same format.It says Date last fed -1Date right now 2006-09-18 16:52:21And the difference is 13 days [/quote]Now how could Date last fed equal -1? You've said the dates are in yyyy-mm-dd etc form. Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted September 18, 2006 Author Share Posted September 18, 2006 They are, that's why I'm confused. The pet i'm testing it on has the lastDatefed stored as:2006-09-17 20:44:03in the MySQL database Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted September 18, 2006 Author Share Posted September 18, 2006 Does anyone know why it's being so weird? Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 19, 2006 Share Posted September 19, 2006 Copy only the code below, no editing, no extra stuff at all. Save it as test.php. Upload it to your server and run it. Note the results you see.[code]<?php$lastDatefed = "2006-09-17 20:44:03"; // your database value$then = strtotime($lastDatefed); // in seconds since the start of time = 1158540243echo "Last Date Fed is ". $then. " seconds<br/>"; // outputs $right_now = time(); // in seconds since the start of time varies depending on when you testecho "Right now is ". $right_now. " seconds<br/>"; // outputs $timesincefed = $right_now - $then;echo "# seconds since fed is ". $timesincefed. " seconds<br/>";$dayssincefed = number_format($timesincefed/86400,2);echo "Date last fed ". $lastDatefed. "<br/>Date right now ". date("Y-m-d H:i:s"). "<br/>And the difference is ". $dayssincefed. " days";?>[/code]Exactly the same code is running at http://www.halfadot.com/temp/test.phpRun that. Tell me if you get different results on your server (the times will be [i]slightly different[/i] because we're counting seconds). Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted September 19, 2006 Author Share Posted September 19, 2006 Last Date Fed is 1158540243 secondsRight now is 1158625156 seconds# seconds since fed is 84913 secondsDate last fed 2006-09-17 20:44:03Date right now 2006-09-18 20:19:16And the difference is 0.98 days Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 19, 2006 Share Posted September 19, 2006 OK, there's the proof that my code works on your server. If you can't duplicate the results with a similar calculation applied in your script, it means either that you have mis-named variables, mis-placed logic, or what you think is in the database isn't what's really in it. As always, echo out variables, echo out what comes from the database, echo out anything and everything to help you track down your problem. Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted September 19, 2006 Author Share Posted September 19, 2006 I've looked over it again and again and it looks right, but it's not working :-\ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.