Jump to content

Loop and If Question


Custer

Recommended Posts

Alright, on one of my pages, I'm displaying the production stats of my users, such as they are making 100 Lumber an hour, etc..,etc...and I've coded this just fine and it's working great, through a loop. But I want to add an if statement that checks the user if he has enough resources in order for the production to occur (ie, he turns 25 logs into 100 lumber an hour, but my user has 0 logs, so no production occurs), and if he doesn't, I don't want that part of the production to display.

 

So let's say:

 

User1

0 logs

100 stone

 

1 sawmill (turns 50 logs into 100 lumber)

 

-50 logs/hour

-25 stone/hour

+100 lumber/hour

 

Because he doesn't have enough logs, I don't want the -50 logs/hour or the +100 lumber/hour to show.

 

Here's the code for the that production table:

 

//Begin Production Table
$ubuildingsb = ("SELECT * FROM user_buildings WHERE user_id = $userid");
$resultd = mysql_query( $ubuildingsb ) or die(mysql_error());
$infod = mysql_fetch_assoc( $resultd );
$buildingidb = $infod['building_id'];

$input = ("SELECT * FROM buildings WHERE building_id = $buildingid AND cost_type = '1'");
$resultin = mysql_query( $input ) or die(mysql_error());
$infoe = mysql_fetch_assoc( $resultin );
$restype = $infoe['cost_restype'];
$inputamount = $infoe['cost_amount'];

$resources = ("SELECT name FROM resources WHERE id = $restype");
$resultre = mysql_query( $resources ) or die(mysql_error());

$output = ("SELECT * FROM buildings WHERE building_id = $buildingid AND cost_type = '2'");
$resultout = mysql_query( $output ) or die(mysql_error());
$outinfo = mysql_fetch_assoc( $resultout );
$restypeout = $outinfo['cost_restype'];
$outputamount = $outinfo['cost_amount'];

$resourcesb = ("SELECT name FROM resources WHERE id = $restypeout");
$resultout = mysql_query( $resourcesb ) or die(mysql_error());

echo ("<center><h1 style='font-size:150%'>Production</h1></center>"); 
echo ("<center><table>");
echo "<tr><th>Input:</th>";
while($infof = mysql_fetch_assoc( $resultre ))

{ 
echo "<tr><td> - ".$inputamount ." ".$infof['name']." ,</td></tr> "; 
}
echo "<tr><th>Output:</th>";
while($outinfob = mysql_fetch_assoc( $resultout ))
{
echo "<tr><td> + ".$outputamount ." ".$outinfob['name']." ,</td></tr> "; 
} 
echo ("</table></center>");

 

Anybody willing to help me out?

 

Link to comment
https://forums.phpfreaks.com/topic/61444-loop-and-if-question/
Share on other sites

See, it's definitely not that simple. Because above the code, I'm laying out all of the users resources and buildings (working fine!), and I need to check off of his actual amount, not the buildings output amount. And there are like 50 buildings in the game, so I'm going to have to call off of that building's id, and I could probably come up with a good if statement, but it'd take me all night or longer. :s

Link to comment
https://forums.phpfreaks.com/topic/61444-loop-and-if-question/#findComment-305838
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.