Page 1 of 1

On Page Load Shouldn't [Resolved]

Posted: Sat Oct 19, 2013 3:04 pm
by Epiales
Okay, the problem I'm running into now is that upon going to the page, if you don't have enough gold to get the bus, or so, then it tells you the error message that you don't have enough gold. That is supposed to show WHEN they CLICK on the bus, or the taxi, but not WHEN the page loads... Why? And when I do have enough gold, it automatically starts the countdown timer for the bus?

Code:

Code: Select all

<?php /////////////////////////////////////////PURCHASING A BUS/////////////////////////////////////////////////

$sql = "UPDATE bususerbdlg SET times1='$currenttime'";
$query = mysqli_query($db_conx, $sql);

?>

<div id="bus">
<table>
<form method='post' action='cabs.php'>
<td class="tooltip"><input type='submit' value='Take Bus'><span>To Travel In A Bus From City To City:<br>Gold: 1K<br>Food: 300<br>Build Time: 45 Minutes</span></td>
<input type='hidden' name='bus' value='1'></form>
</table>
<?php
if(isset($_POST['bus'])) {

$sql = "SELECT * from bususerbdlg";
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);  

} 

if ($stats_gold < 1000) {

echo "<span id='errormess'><big><left><font color='red'>You do not have enough Gold</center></span></big></font>";

}

elseif ($stats_food < 300) {

echo "<span id='errormess'><big><left><font color='red'>You do not have enough Food</center></span></big></font>"; 

}

elseif ($owned = 1) {

echo "<span id='errormess'><big><left><font color='red'>You Already Own This</center></span></big></font>";


}else if ($stats_gold >= 1000 || $stats_food >= 300){

        $gold1 = ($stats_gold - 1000);
        $food1 = ($stats_food - 300);

$update_gold = "UPDATE stats SET gold='$gold1',food='$food1' WHERE $id = '$_SESSION[userid]'";
        $query = mysqli_query($db_conx, $update_gold);

$time = time();
$finish = time() + strtotime($row['times']) + (15 * 1);
$randid = rand(999,9999999);
$bdlgid = 1;

        $sql = "UPDATE bususerbdlg SET times1='$currenttime'";
        $query = mysqli_query($db_conx, $sql);

        $sql = "INSERT INTO bususerbdlg (bdlgid, playerid, times1, finish, randid) VALUES ('$bdlgid','$_SESSION[userid]','$time', '$finish', '$randid')";
        $query = mysqli_query($db_conx, $sql);
        
}

?>
<?php

$sql = "SELECT * from bususerbdlg";
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);        
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {

if ($row['times1'] >= $row['finish']) {

?> <?php
$bus="<img src='images/bus.png'>";
echo " $bus";

$sql = "UPDATE bususerbdlg SET owned='1' WHERE playerid = '$_SESSION[userid]'";
$query = mysqli_query($db_conx, $sql);

}else{

?>  <?php
echo buildtimes($row['finish'] - $row['times1']);

}      
}
/////////////////////////////////////////END OF PURCHASING A BUS/////////////////////////////////////////////////
?>

Re: On Page Load Shouldn't

Posted: Sat Oct 19, 2013 5:13 pm
by Epiales
Okay, I rewrote the error messaging, but it still does the same thing upon page load :( Grrrrr

Code:

Code: Select all

if(isset($_POST['bus'])) {

$sql = "SELECT * from bususerbdlg";
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);  
$error_mess = "";
if ($owned == 1) {
    $error_mess = "<span id='errormess'><big><left><font color='red'>You Already Have This!</center></span></big></font>";
}if ($stats_gold < 1000) {
    $error_mess = "<span id='errormess'><big><left><font color='red'>You Do Not Have Enough Gold!</center></span></big></font>";
}if ($stats_food < 300) {
    $error_mess = "<span id='errormess'><big><left><font color='red'>You Do Not Have Enough Food!</center></span></big></font>";

}
else 
{
        $gold1 = ($stats_gold - 1000);
        $food1 = ($stats_food - 300);

$update_gold = "UPDATE stats SET gold='$gold1',food='$food1' WHERE $id = '$_SESSION[userid]'";
        $query = mysqli_query($db_conx, $update_gold);
}
 
I need it to check to see if they own it, and then if they do, say they already own it, and that's it. Then if they don't own it, it goes to the other error messages until they meet the requirements and it purchases it.

Re: On Page Load Shouldn't

Posted: Sat Oct 19, 2013 7:07 pm
by Epiales
Okay, have solved it loading upon page load, but now it doesn't want to show the "You already own This" as well as it keeps charging the person food/gold if they hit the button over and over. Thus why I have another thread about hiding the button when they click it. But anyway, I'm closer, but again, it doesn't show that you already own it and it keeps taking gold/food out when it shouldn't.

Code:

Code: Select all

<?php
if(isset($_POST['bus'])) {

$sql = "SELECT * from bususerbdlg";
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);

$owned = $row['owned'];
$times = $row['times1'];  
$error_mess = "";


if ($owned == 1) {
    $error_mess = "<span id='errormess'><big><left><font color='red'>You Already Have This!</center></span></big></font>";
}elseif ($stats_gold < 1000) {
    $error_mess = "<span id='errormess'><big><left><font color='red'>You Do Not Have Enough Gold!</center></span></big></font>";
}elseif ($stats_food < 300) {
    $error_mess = "<span id='errormess'><big><left><font color='red'>You Do Not Have Enough Food!</center></span></big></font>";
}elseif ($owned != 1) {

        $gold1 = ($stats_gold - 1000);
        $food1 = ($stats_food - 300);

$update_gold = "UPDATE stats SET gold='$gold1',food='$food1' WHERE $id = '$_SESSION[userid]'";
        $query = mysqli_query($db_conx, $update_gold);

$time = time();
$finish = time() + strtotime($row['times1']) + (15 * 1);
$randid = rand(999,9999999);
$bdlgid = 1;

        $sql2 = "INSERT INTO bususerbdlg (bdlgid, playerid, times1, finish, randid) VALUES ('$bdlgid','$_SESSION[userid]','$time', '$finish', '$randid')";
        $query = mysqli_query($db_conx, $sql2);
 
 
        $sql = "UPDATE bususerbdlg SET times1='$currenttime'";
        $query = mysqli_query($db_conx, $sql);
}
}
?>

Re: On Page Load Shouldn't

Posted: Sat Oct 19, 2013 7:17 pm
by Epiales
Okay, the problem was with this:

Code: Select all

if ($owned == 1) {
Changed to:

Code: Select all

if ($owned = 1) {
But how do you know when to use two =='s? or even three? Why did that make the difference? grrr

But anyway it's all working except the intial buying of it. When I click the button it says I already have it, when I don't. :evil: :evil: :evil:

OMG, how messed up. If I keep owned = 1, then I can't buy it... if I switch it to owned == 1, then I can......

AND...

After it is bought, when I click on it again, it does NOTHING but take u're gold/food, unless I switch it BACK TO owned = 1, then it gives the error and won't charge anything for it.

Re: On Page Load Shouldn't

Posted: Sat Oct 19, 2013 7:36 pm
by OldRod
1 = sets a variable to some value: $var = 1

2 == allows you to check if two variables are equal

3 === checks if two variables are equal and of the same type (int, double, etc.)

http://www.w3schools.com/php/php_operators.asp

Re: On Page Load Shouldn't

Posted: Sat Oct 19, 2013 7:58 pm
by Epiales
OldRod wrote:1 = sets a variable to some value: $var = 1

2 == allows you to check if two variables are equal

3 === checks if two variables are equal and of the same type (int, double, etc.)

http://www.w3schools.com/php/php_operators.asp
Thank you for the link :)

Okay, updated code, since when asking if the row owned is zero....for one, there would be no row to look for, so I had to change that. Current code below, but still won't let me build anything upon clicking the button. No error messages or anything:

Code:

Code: Select all

<?php
if(isset($_POST['bus'])) {

$sql = "SELECT * from bususerbdlg";
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {

$owned = $row['owned'];
$times = $row['times1'];  
$error_mess = "";


if ($owned == 1) {
    $error_mess = "<span id='errormess'><big><left><font color='red'>You Already Have This!</center></span></big></font>";
}elseif ($stats_gold < 1000) {
    $error_mess = "<span id='errormess'><big><left><font color='red'>You Do Not Have Enough Gold!</center></span></big></font>";
}elseif ($stats_food < 300) {
    $error_mess = "<span id='errormess'><big><left><font color='red'>You Do Not Have Enough Food!</center></span></big></font>";
}else

        $gold1 = ($stats_gold - 1000);
        $food1 = ($stats_food - 300);

$update_gold = "UPDATE stats SET gold='$gold1',food='$food1' WHERE $id = '$_SESSION[userid]'";
        $query = mysqli_query($db_conx, $update_gold);

$time = time();
$finish = time() + strtotime($row['times1']) + (15 * 1);
$randid = rand(999,9999999);
$bdlgid = 1;

        $sql2 = "INSERT INTO bususerbdlg (bdlgid, playerid, times1, finish, randid) VALUES ('$bdlgid','$_SESSION[userid]','$time', '$finish', '$randid')";
        $query = mysqli_query($db_conx, $sql2);
 
 
        $sql = "UPDATE bususerbdlg SET times1='$currenttime'";
        $query = mysqli_query($db_conx, $sql);
}}
?>
There is no row to look for until they build it, so my code is all messed up LMAO! Glad I saw that :) Now to see if I can't fix it somehow :oops: :cry: :cry:

OMG I'm making something so easy into something hard LMAO. That's me though! GRR

Re: On Page Load Shouldn't

Posted: Sat Oct 19, 2013 8:40 pm
by Epiales
Okay, it's all fixed! :roll: :roll: lol...YAY!

I basically fixed it by checking to see if there was a row in there with their id on it, and if not, it charged them the gold/food and put it in there.

Thanks for those who read and replied. ;)