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
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
Share on other sites

Sorry for the double post. I got to thinking, and you're either going to have to have a really big loop containing the if, and then the two smaller loops, just because then, you'll want to echo the corresponding data together, so that the 50+ and -50 go together...

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.