Jump to content

Why isn't it working?


Pi_Mastuh

Recommended Posts

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">&nbsp;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">&nbsp;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">&nbsp;</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">&nbsp;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">&nbsp;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">&nbsp;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">&nbsp;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">&nbsp;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>&nbsp;</p>

</html>
[/code]
Link to comment
Share on other sites

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

??

Regards
Liam
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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

??

Regards
Liam
[/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]
Link to comment
Share on other sites

[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?
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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 -1
Date right now 2006-09-18 16:52:21
And 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">&nbsp;Health:</font></td>
              <td width="101" height="10">
              <p align="center"><font face="Arial, Helvetica, sans-serif" size="1"> &nbsp;100% </font></td>
            </tr>
            <tr>
              <td height="19">
              <p align="center"><font face="Arial, Helvetica, sans-serif" size="1">&nbsp;Pet
                Status:</font></td>
              <td height="19">
              <p align="center"><font face="Arial, Helvetica, sans-serif" size="1">&nbsp;Happy</font></td>
              </tr>
            <tr>
              <td width="99" height="19">
              <p align="center"><font face="Arial, Helvetica, sans-serif" size="1">&nbsp;Hunger: </font></td>
              <td width="101" height="19">
              <p align="center"><font face="Arial, Helvetica, sans-serif" size="1">
 
&nbsp;<?
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">&nbsp;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">&nbsp;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">&nbsp;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">&nbsp;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">&nbsp;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>&nbsp;</p>

</html>

[/code]
Link to comment
Share on other sites

[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 -1
Date right now 2006-09-18 16:52:21
And 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.
Link to comment
Share on other sites

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 = 1158540243
echo "Last Date Fed is ". $then. " seconds<br/>"; // outputs

$right_now = time(); // in seconds since the start of time varies depending on when you test
echo "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.php

Run that. Tell me if you get different results on your server (the times will be [i]slightly different[/i] because we're counting seconds).
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.